<div class="flexbox">
<div class="chat-box">
<div class="chat-box-header">
<h3>Some Chat Room<br /><small>Last active: 0 min ago</small></h3>
</div>
<div id="chat_box_body" class="chat-box-body">
<div id="chat_messages">
<div class="profile other-profile">
<img src="https://images.unsplash.com/photo-1537396123722-b93d0acd8848?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ&s=efc6e85c24d3cfdd15cd36cb8a2471ed" width="30" height="30" />
<span>Other profile</span>
</div>
<div class="message other-message">
Hello!
</div>
<div class="profile my-profile">
<span>My profile</span>
<img src="https://images.unsplash.com/photo-1534135954997-e58fbd6dbbfc?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ&s=02d536c38d9cfeb4f35f17fdfaa36619" width="30" height="30" />
</div>
<div class="message my-message">
Hi!
</div>
<div class="message my-message">
How are you!
</div>
</div>
</div>
<div id="typing">
<div><span></span> <span></span> <span></span> <span class="n">Someone</span> is typing...</div>
</div>
<div class="chat-box-footer">
<textarea id="chat_input" placeholder="Enter your message here..."></textarea>
<button id="send">
<svg style="width:24px;height:24px" viewBox="0 0 24 24">
<path fill="#006ae3" d="M2,21L23,12L2,3V10L17,12L2,14V21Z" />
</svg>
</button>
</div>
</div>
</div>
@import url("https://fonts.googleapis.com/css?family=Raleway:400,400i,700");
$body-bg-color: #7dd8ff;
$chat-bg-color: #fff;
$font-size: 16px;
$font-color: #2f2f2f;
$font-color-muted: #afafaf;
$standard-padding: 20px 15px;
$standard-radius: 13px;
html, body, h1, h2, h3, h4, h5, h6, p, ul, li, button {
padding: 0;
margin: 0;
}
html, body, .flexbox {
width: 100%;
height: 100%;
}
body {
color: $font-color;
font-family: Raleway, sans-serif;
font-size: $font-size;
}
.flexbox {
background-color: $body-bg-color;
display: flex;
justify-content: center;
align-items: center;
}
.chat-box {
background-color: $chat-bg-color;
margin: 20px;
width: 100%;
height: 100%;
max-height: calc(100% - 40px);
display: flex;
flex-direction: column;
border-radius: $standard-radius;
box-shadow: 0 5px 8px rgba(0, 0, 0, 0.3);
> .chat-box-header {
width: 100%;
padding: $standard-padding;
border-bottom: solid 1px #cfcfcf;
box-sizing: border-box;
}
> .chat-box-body {
height: 100%;
display: flex;
overflow: auto;
display: flex;
flex-direction: column;
#chat_messages {
width: 100%;
padding: $standard-padding;
margin-top: auto;
display: flex;
flex-direction: column;
box-sizing: border-box;
.message {
width: 80%;
padding: $standard-padding;
margin: 2px 0;
overflow: hidden;
border-radius: $standard-radius;
transition: height 0.3s ease-in-out;
&.hide {
height: 0;
}
}
.my-message {
color: #fff;
background-color: #006ae3;
align-self: flex-end;
}
.other-message {
background-color: #e2e2e2;
}
.profile {
margin: 2px 0;
overflow: hidden;
transition: height 0.3s ease-in-out;
&.hide {
height: 0;
}
img {
display: inline-block;
margin: 0;
padding: 0;
vertical-align: middle;
border-radius: 50%;
}
}
.my-profile {
color: $font-color-muted;
align-self: flex-end;
}
.other-profile {
color: $font-color-muted;
}
}
}
#typing {
color: $font-color-muted;
width: 100%;
height: 0;
padding: 0 15px;
overflow: hidden;
box-sizing: border-box;
opacity: 0;
transition: 0.3s height ease-in-out, 0.3s opacity ease-in-out;
&.active {
height: 80px;
opacity: 1;
}
span:not(.n) {
background-color: $font-color-muted;
width: 10px;
height: 10px;
margin-top: 20px;
display: inline-block;
border-radius: 50%;
&:nth-child(1) {
animation: typing 1.2s infinite;
}
&:nth-child(2) {
animation: typing 1.2s infinite 0.1s;
}
&:nth-child(3) {
animation: typing 1.2s infinite 0.2s;
}
}
}
> .chat-box-footer {
width: 100%;
padding: $standard-padding;
border-top: solid 1px #cfcfcf;
box-sizing: border-box;
display: flex;
> #chat_input {
color: $font-color;
font-family: Raleway, sans-serif;
font-size: $font-size;
background-color: #d2d2d2;
width: 100%;
height: 40px;
max-height: 120px;
border: none;
padding: 10px 15px;
resize: none;
box-sizing: border-box;
border-radius: $standard-radius;
transition: 0.3s background-color;
&:focus {
background-color: #efefef;
}
}
> #send {
background: none;
border: none;
margin-left: 10px;
padding: 5px;
cursor: pointer;
border-radius: 50%;
}
}
}
@media (min-width: 480px) {
.chat-box {
width: 480px;
}
}
@keyframes typing {
0% { transform: translateY(0px); }
33.3333% { transform: translateY(-5px); }
66.6667% { transform: translateY(5px); }
100% { transform: translateY(0px); }
}
button::-moz-focus-inner {
border-style: none;
padding: 0;
}
button {
outline: none;
}
h3 > small {
color: $font-color-muted;
font-weight: normal;
}
View Compiled
'use strict';
var chatInput = document.querySelector('#chat_input');
var typing = document.querySelector('#typing');
var send = document.querySelector('#send');
var chatMessages = document.querySelector('#chat_messages');
var chatBoxBody = document.querySelector('#chat_box_body');
var chatThread = {};
// ----- EVENTS ----- //
chatInput.addEventListener('input', function() {
this.style.height = '0';
this.style.height = this.scrollHeight + 1 + 'px';
});
chatInput.addEventListener('keydown', function(evt) {
if (evt.keyCode == 13 && !evt.shiftKey) {
sendMessage('my', this);
evt.preventDefault();
}
});
send.addEventListener('click', function() {
sendMessage('my', chatInput);
});
// ----- FUNCTIONS -----//
function renderProfile(p) {
return '<div class="profile ' + p + '-profile hide"><img src="' + profile[p].pic + '" alt="' + profile[p].name + '" width="30" height="30" /> <span>' + profile[p].name + '</span></div>';
}
function renderMessage(p, m) {
return '<div class="message ' + p + '-message hide">' + m + '</div>';
}
function appendMessage(r) {
chatMessages.insertAdjacentHTML('beforeend', r);
var elms = document.querySelectorAll('.profile.hide, .message.hide');
for (var i = 0; i < elms.length; i++) {
if (elms[i].classList.contains('profile')) {
elms[i].style.height = elms[i].scrollHeight + 'px';
}
else {
elms[i].style.height = elms[i].scrollHeight - 20 + 'px';
}
elms[i].classList.remove('hide');
}
chatBoxBody.scrollTop = chatBoxBody.scrollHeight;
}
function sendMessage(p, elm) {
var r = '';
if (chatThread[chatThread.length - 1].profile !== p) {
r += renderProfile(p);
}
if (typeof elm === 'string') {
r += renderMessage(p, elm);
chatThread.push({
'profile': p,
'message': elm
});
}
else {
r += renderMessage(p, elm.value);
chatThread.push({
'profile': p,
'message': elm.value
});
elm.value = '';
}
appendMessage(r);
}
chatBoxBody.scrollTop = chatBoxBody.scrollHeight;
// ---------- Example ---------- //
profile = {
'my': {
'name': 'My profile',
'pic': 'https://images.unsplash.com/photo-1534135954997-e58fbd6dbbfc?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ&s=02d536c38d9cfeb4f35f17fdfaa36619'
},
'other': {
'name': 'Other profile',
'pic': 'https://images.unsplash.com/photo-1537396123722-b93d0acd8848?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ&s=efc6e85c24d3cfdd15cd36cb8a2471ed'
}
};
chatThread = [{
'profile': 'other',
'message': 'Hello!'
},{
'profile': 'my',
'message': 'Hi!'
},{
'profile': 'my',
'message': 'How are you?'
}];
(function autoText() {
setTimeout(function() {
typing.classList.add('active');
setTimeout(function() {
var randomMessages = ['Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.', 'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.', 'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'];
sendMessage('other', randomMessages[Math.floor(Math.random() * 4)]);
typing.classList.remove('active');
autoText();
}, 8000);
}, 3000);
})();
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.