Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <section id="chatApp" class="chatApp">
	<div class="chatApp__loaderWrapper">
		<div class="chatApp__loaderText">Loading...</div>
		<div class="chatApp__loader"></div>
	</div>
</section>
              
            
!

CSS

              
                .clearfix::after {
	content: "";
	display: block;
	clear: both;
}
html, body {
	margin: 0;
	padding: 0;
	background: #181b21 url(https://www.toptal.com/designers/subtlepatterns/patterns/nami.png);
	font-family: 'Quicksand', sans-serif;
	letter-spacing: -0.23px;
}
.chatApp__loaderWrapper {
	margin: 80px auto;
	text-align: center;
}
.chatApp__loaderText {
	color: #4870df;
	font-weight: bold;
}
.chatApp__loader,
.chatApp__loader::before,
.chatApp__loader::after {
	border-radius: 1em;
	width: 1em;
	height: 1em;
	animation-fill-mode: both;
	animation: loading 1.8s infinite ease-in-out;
}
.chatApp__loader {
	margin: auto;
	color: #4870df;
	font-size: 12px;
	position: relative;
	animation-delay: -0.16s;
}
.chatApp__loader::before,
.chatApp__loader::after {
	content: '';
	position: absolute;
	top: 0;
}
.chatApp__loader::before {
	left: -1.5em;
	animation-delay: -0.32s;
}
.chatApp__loader::after {
	left: 1.5em;
}
.chatApp__room {
	display: flex;
	align-content: flex-start;
	justify-content: center;
	padding: 5rem 20rem;
	animation: fadeChatApp 0.6s ease-in-out;
	animation-fill-mode: forwards;
}
.chatApp__conv {
	flex-grow: 1;
	flex-shrink: 1;
	flex-basis: 0;
	float: left;
	margin: 0 2.5rem;
	box-shadow: 0 35px 20px -30px rgba(0,0,0,0.1);
}
.chatApp__convTitle {
	display: inline-block;
	margin: 0 0 2rem 0;
	font-size: 18px;
	font-weight: bold;
	color: #f0f0f0;
	text-transform: uppercase;
	position: relative;
}
.chatApp__convTitle::after {
	content: '';
	position: absolute;
	background-color: #4870df;
	width: 100%;
	height: 2px;
	border-radius: 30px;
	left: 0;
	right: 0;
	bottom: -16px;
}
.chatApp__convTimeline {
	display: flex;
	height: 400px;
	padding: 1rem;
	border-top-left-radius: 8px;
	border-top-right-radius: 8px;
	background-color: #fff;
	flex-direction: column-reverse;
	overflow-y: auto;
}
.chatApp__convMessageItem {
	padding: 1rem 0 0 0;
	opacity: 0;
	transition: all 0.15s ease-in-out;
	animation: fadeNewMessage 0.5s;
	animation-fill-mode: forwards;
}
.chatApp__convMessageAvatar {
	width: 40px;
	height: 40px;
	border-radius: 40px;
	image-rendering: -webkit-optimize-contrast;
	-webkit-touch-callout: none;
	-webkit-user-select: none;
	-khtml-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
}
.chatApp__convMessageValue {
	position: relative;
	max-width: 50%;
	min-height: 40px;
	padding: 0.75rem;
	border-radius: 4px;
	font-size: 14px;
	line-height: 18px;
	font-weight: bold;
	box-shadow: 0px 10px 10px -8px rgba(0,0,0,0.08);
	box-sizing: border-box;
	overflow: hidden;
	text-overflow: ellipsis;
}
.chatApp__convMessageItem--left .chatApp__convMessageAvatar {
	float: left;
}
.chatApp__convMessageItem--left .chatApp__convMessageValue {
	float: left;
	text-align: left;
	margin: 0 0 0 1rem;
	color: #fff;
	background-color: #4870df;
}
.chatApp__convMessageItem--left .chatApp__convMessageValue::before {
	content: '';
	position: absolute;
	top: 14px;
	left: -8px;
	width: 0;
	height: 0;
	border-style: solid;
	border-width: 6px 10px 6px 0;
	border-color: transparent #4870df transparent transparent;
}
.chatApp__convMessageItem--right .chatApp__convMessageAvatar {
	float: right;
}
.chatApp__convMessageItem--right .chatApp__convMessageValue {
	float: right;
	text-align: right;
	margin: 0 1rem 0 0;
	background-color: #fafafa;
}
.chatApp__convMessageItem--right .chatApp__convMessageValue::after {
	content: '';
	position: absolute;
	top: 14px;
	right: -8px;
	width: 0;
	height: 0;
	border-style: solid;
	border-width: 6px 0 6px 10px;
	border-color: transparent transparent transparent #fafafa;
}
.chatApp__convMessageItem--left .chatApp__convMessageValue a {
	color: #91ffb1;
}
.chatApp__convMessageItem--left .chatApp__convMessageValue a:hover,
.chatApp__convMessageItem--left .chatApp__convMessageValue a:focus {
	color: #75ff9e;
}
.chatApp__convMessageItem--right .chatApp__convMessageValue a {
	color: #09f;
}
.chatApp__convMessageItem--right .chatApp__convMessageValue a:hover,
.chatApp__convMessageItem--right .chatApp__convMessageValue a:focus {
	color: #007dd1;
}
.chatApp__convSendMessage {
	position: relative;
	border-bottom-left-radius: 8px;
	border-bottom-right-radius: 8px;
	background-color: #fff;
}
.chatApp__convSendMessage {
	padding: 1rem;
}
.chatApp__convInput {
	float: left;
	height: 40px;
	border: none;
	padding: 0.5rem 1rem;
	border-radius: 40px;
	margin: 0 0.5rem 0 0;
	width: calc(100% - 52px);
	background-color: #dedfed;
	box-shadow: inset 0 0 0 2px #dedfed;
	font-size: 14px;
	font-family: 'Quicksand', sans-serif;
	font-weight: bold;
	transition: 0.15s all ease-in-out;
	box-sizing: border-box;
}
.chatApp__convInput::placeholder { /* Chrome, Firefox, Opera, Safari 10.1+ */
	color: #b1b1b1;
	opacity: 1;
}
.chatApp__convInput:-ms-input-placeholder { /* Internet Explorer 10-11 */
	color: #b1b1b1;
}
.chatApp__convInput::-ms-input-placeholder { /* Microsoft Edge */
	color: #b1b1b1;
}
.chatApp__convInput:focus, .chatApp__convInput:active {
	box-shadow: inset 0 0 0 2px #7690d6;
}
.chatApp__convInput:focus {
	outline: none;
}
.chatApp__convButton {
	float: right;
	position: relative;
	width: 40px;
	height: 40px;
	cursor: pointer;
	padding: 8px;
	border-radius: 30px;
	color: #fff;
	background-color: #4870df;
	text-align: center;
	box-shadow: 0px 14px 10px -8px rgba(0,0,0,0.2);
	transition: 0.15s all ease-in-out;
	box-sizing: border-box;
	-webkit-user-select: none;
	-khtml-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
}
.chatApp__convButton:hover {
	background-color: #3c559c;
}
.chatApp__convButton i {
	position: relative;
	top: 50%;
	transform: translateY(-50%);
	padding: 0 0 0 1px;
	font-size: 22px;
}
.chatApp__convButton--loading {
	cursor: wait;
	background-color: #4870df;
}
.chatApp__convButton--loading:hover {
	background-color: #4870df;
}
.chatApp__convButton--loading::before, .chatApp__convButton--loading::after {
	content: '';
	position: absolute;
	z-index: 1;
	display: block;
	left: 0;
	right: 0;
	top: 50%;
	transform: translateY(-50%);
	width: 8px;
	height: 8px;
	margin: auto;
	border-radius: 40px;
	background-color: #fff;
}
.chatApp__convButton--loading::after {
	animation: loadSendMessage 1.5s;
	animation-fill-mode: forwards;
	animation-iteration-count: infinite;
}
.chatApp__convButton--loading i {
	display: none;
}
.chatApp__convSendMessage--typing .chatApp__convTyping {
	display: block;
}
.chatApp__convTyping {
	position: absolute;
	top: 0;
	font-size: 10px;
	font-weight: bold;
}
.chatApp__convTypingDot {
	position: relative;
	left: 4px;
	display: inline-block;
	width: 2px;
	height: 2px;
	background-color: #606060;
	animation: typingMessage 1.5s;
	animation-fill-mode: forwards;
	animation-iteration-count: infinite;
	animation-timing-function: ease-in-out;
}
.chatApp__convTypingDot::before, .chatApp__convTypingDot::after {
	content: '';
	position: absolute;
	display: inline-block;
	width: 2px;
	height: 2px;
	background-color: #606060;
	animation: typingMessage 1.5s;
	animation-fill-mode: forwards;
	animation-iteration-count: infinite;
	animation-timing-function: ease-in-out;
}
.chatApp__convTypingDot::before {
	left: 4px;
	animation-delay: 0.5s;
}
.chatApp__convTypingDot::after {
	left: 8px;
	animation-delay: 1s;
}
@media screen and (max-width:768px) {
	.chatApp__room { flex-wrap: wrap; padding: 2rem 1rem; }
	.chatApp__conv { flex-basis: 100%; min-width: auto; width: 100%; margin: 0; }
	.chatApp__conv:first-of-type { margin: 0 0 4rem 0; }
}
@keyframes loading {
	0%, 80%, 100% { box-shadow: 0 1em 0 -1.3em; }
	40% { box-shadow: 0 1em 0 0; }
}
@keyframes fadeChatApp {
	0% { opacity: 0; transform: translateY(1rem); }
	100% { opacity: 1; transform: translateY(0px); }
}
@keyframes fadeNewMessage {
	0% { opacity: 0; transform: translateY(1rem); }
	100% { opacity: 1; transform: translateY(0px); }
}
@keyframes loadSendMessage {
	0% { opacity: 1; width: 4px; height: 4px; }
	100% { opacity: 0; width: 100%; height: 100%; }
}
@keyframes typingMessage {
	0% { background-color: #606060; }
	50% { background-color: #fff; }
	60% { background-color: #606060; }
}
              
            
!

JS

              
                /* detect url in a message and add a link tag */
function detectURL(message) {
	var urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
	return message.replace(urlRegex, function(urlMatch) {
		return '<a href="' + urlMatch + '">' + urlMatch + '</a>';
	})
}

/* ========== */
/* Title component */
class Title extends React.Component {
	constructor(props, context) {
		super(props, context);
	}
	render() {
		return (
			<div className={"chatApp__convTitle"}>{this.props.owner}'s display</div>
		);
	}
}
/* end Title component */
/* ========== */

/* ========== */
/* InputMessage component - used to type the message */
class InputMessage extends React.Component {
	constructor(props, context) {
		super(props, context);
		this.handleSendMessage = this.handleSendMessage.bind(this);
		this.handleTyping = this.handleTyping.bind(this);
	}
	handleSendMessage(event) {
		event.preventDefault();
		/* Disable sendMessage if the message is empty */
		if( this.messageInput.value.length > 0 ) {
			this.props.sendMessageLoading(this.ownerInput.value, this.ownerAvatarInput.value, this.messageInput.value);
			/* Reset input after send*/
			this.messageInput.value = '';
		}
	}
	handleTyping(event) {
		/* Tell users when another user has at least started to write */
		if( this.messageInput.value.length > 0 ) {
			this.props.typing(this.ownerInput.value);
		}
		else {
			/* When there is no more character, the user no longer writes */
			this.props.resetTyping(this.ownerInput.value);
		}
	}
	render() {
		/* If the chatbox state is loading, loading class for display */
		var loadingClass = this.props.isLoading ? 'chatApp__convButton--loading' : '';
		let sendButtonIcon = <i className={"material-icons"}>send</i>;
		return (
			<form onSubmit={this.handleSendMessage}>
				<input
					type="hidden"
					ref={owner => (this.ownerInput = owner)}
					value={this.props.owner}
				/>
				<input
					type="hidden"
					ref={ownerAvatar => (this.ownerAvatarInput = ownerAvatar)}
					value={this.props.ownerAvatar}
				/>
				<input
					type="text"
					ref={message => (this.messageInput = message)}
					className={"chatApp__convInput"}
					placeholder="Text message"
					onKeyDown={this.handleTyping}
					onKeyUp={this.handleTyping}
					tabIndex="0"
				/>
				<div className={'chatApp__convButton ' + loadingClass} onClick={this.handleSendMessage}>
				{sendButtonIcon}
				</div>
			</form>
		);
	}
}
/* end InputMessage component */
/* ========== */

/* ========== */
/* TypingIndicator component */
class TypingIndicator extends React.Component {
	constructor(props, context) {
		super(props, context);
	}
	render() {
		let typersDisplay = '';
		let countTypers = 0;
		/* for each user writing messages in chatroom */
		for ( var key in this.props.isTyping ) {
			/* retrieve the name if it isn't the owner of the chatbox */
			if( key != this.props.owner && this.props.isTyping[key] ) {
				typersDisplay += ', ' + key;
				countTypers++;
			}
		}
		/* formatting text */
		typersDisplay = typersDisplay.substr(1);
		typersDisplay += (( countTypers > 1 ) ? ' are ' : ' is ');
		/* if at least one other person writes */
		if ( countTypers > 0 ) {
			return (
				<div className={"chatApp__convTyping"}>{typersDisplay} writing
				<span className={"chatApp__convTypingDot"}></span>
				</div>
			);
		}
		return (
			<div className={"chatApp__convTyping"}></div>
		);
	}
}
/* end TypingIndicator component */
/* ========== */

/* ========== */
/* MessageList component - contains all messages */
class MessageList extends React.Component {
	constructor(props, context) {
		super(props, context);
	}
	render() {
		return (
			<div className={"chatApp__convTimeline"}>
			{this.props.messages.slice(0).reverse().map(
				messageItem => (
					<MessageItem
						key={messageItem.id}
						owner={this.props.owner}
						sender={messageItem.sender}
						senderAvatar={messageItem.senderAvatar}
						message={messageItem.message}
					/>
				)
			)}
			</div>
		);
	}
}
/* end MessageList component */
/* ========== */

/* ========== */
/* MessageItem component - composed of a message and the sender's avatar */
class MessageItem extends React.Component {
	render() {
		/* message position formatting - right if I'm the author */
		let messagePosition = (( this.props.owner == this.props.sender ) ? 'chatApp__convMessageItem--right' : 'chatApp__convMessageItem--left');
		return (
			<div className={"chatApp__convMessageItem " + messagePosition + " clearfix"}>
				<img src={this.props.senderAvatar} alt={this.props.sender} className="chatApp__convMessageAvatar" />
				<div className="chatApp__convMessageValue" dangerouslySetInnerHTML={{__html: this.props.message}}></div>
			</div>
		);
	}
}
/* end MessageItem component */
/* ========== */

/* ========== */
/* ChatBox component - composed of Title, MessageList, TypingIndicator, InputMessage */
class ChatBox extends React.Component {
	constructor(props, context) {
		super(props, context);
		this.state = {
			isLoading: false
		};
		this.sendMessageLoading = this.sendMessageLoading.bind(this);
		var timeout = null;
	}
	/* catch the sendMessage signal and update the loading state then continues the sending instruction */
	sendMessageLoading(sender, senderAvatar, message) {
		this.setState({ isLoading: true });
		this.props.sendMessage(sender, senderAvatar, message);
		setTimeout(() => {
			this.setState({ isLoading: false });
		}, 400);
	}
	render() {
		return (
			<div className={"chatApp__conv"}>
				<Title
					owner={this.props.owner}
				/>
				<MessageList
					owner={this.props.owner}
					messages={this.props.messages}
				/>
				<div className={"chatApp__convSendMessage clearfix"}>
					<TypingIndicator
						owner={this.props.owner}
						isTyping={this.props.isTyping}
					/>
					<InputMessage
						isLoading={this.state.isLoading}
						owner={this.props.owner}
						ownerAvatar={this.props.ownerAvatar}
						sendMessage={this.props.sendMessage}
						sendMessageLoading={this.sendMessageLoading}
						typing={this.props.typing}
						resetTyping={this.props.resetTyping}
					/>
				</div>
			</div>
		);
	}
}
/* end ChatBox component */
/* ========== */

/* ========== */
/* ChatRoom component - composed of multiple ChatBoxes */
class ChatRoom extends React.Component {
	constructor(props, context) {
		super(props, context);
		this.state = {
			messages: [{
				id: 1,
				sender: 'Shun',
				senderAvatar: 'https://i.pravatar.cc/150?img=32',
				message: 'Hello 👋'
			},
			{
				id: 2,
				sender: 'Gabe',
				senderAvatar: 'https://i.pravatar.cc/150?img=56',
				message: 'Hey!'
			},
			{
				id: 3,
				sender: 'Gabe',
				senderAvatar: 'https://i.pravatar.cc/150?img=56',
				message: 'How are you?'
			},
			{
				id: 4,
				sender: 'Shun',
				senderAvatar: 'https://i.pravatar.cc/150?img=32',
				message: 'Great! It\'s been a while... 🙃'
			},
			{
				id: 5,
				sender: 'Gabe',
				senderAvatar: 'https://i.pravatar.cc/150?img=56',
				message: 'Indeed.... We\'re gonna have to fix that. 🌮🍻'
			}
			],
			isTyping: [],
		};
		this.sendMessage = this.sendMessage.bind(this);
		this.typing = this.typing.bind(this);
		this.resetTyping = this.resetTyping.bind(this);
	}
	/* adds a new message to the chatroom */
	sendMessage(sender, senderAvatar, message) {
		setTimeout(() => {
			let messageFormat = detectURL(message);
			let newMessageItem = {
				id: this.state.messages.length + 1,
				sender: sender,
				senderAvatar: senderAvatar,
				message: messageFormat
			};
			this.setState({ messages: [...this.state.messages, newMessageItem] });
			this.resetTyping(sender);
		}, 400);
	}
	/* updates the writing indicator if not already displayed */
	typing(writer) {
		if( !this.state.isTyping[writer] ) {
			let stateTyping = this.state.isTyping;
			stateTyping[writer] = true;
			this.setState({ isTyping: stateTyping });
		}
	}
	/* hide the writing indicator */
	resetTyping(writer) {
		let stateTyping = this.state.isTyping;
		stateTyping[writer] = false;
		this.setState({ isTyping: stateTyping });
	}
	render() {
		let users = {};
		let chatBoxes = [];
		let messages = this.state.messages;
		let isTyping = this.state.isTyping;
		let sendMessage = this.sendMessage;
		let typing = this.typing;
		let resetTyping = this.resetTyping;

		/* user details - can add as many users as desired */
		users[0] = { name: 'Shun', avatar: 'https://i.pravatar.cc/150?img=32' };
		users[1] = { name: 'Gabe', avatar: 'https://i.pravatar.cc/150?img=56' };
		/* test with two other users :)
		users[2] = { name: 'Kate', avatar: 'https://i.pravatar.cc/150?img=47' };
		users[3] = { name: 'Patrick', avatar: 'https://i.pravatar.cc/150?img=14' };
		*/
		
		/* creation of a chatbox for each user present in the chatroom */
		Object.keys(users).map(function(key) {
			var user = users[key];
			chatBoxes.push(
				<ChatBox
					key={key}
					owner={user.name}
					ownerAvatar={user.avatar}
					sendMessage={sendMessage}
					typing={typing}
					resetTyping={resetTyping}
					messages={messages}
					isTyping={isTyping}
				/>
			);
		});
		return (
			<div className={"chatApp__room"}>
				{chatBoxes}
			</div>
		);
	}
}
/* end ChatRoom component */
/* ========== */

/* render the chatroom */
setTimeout(() => {
	ReactDOM.render(<ChatRoom />, document.getElementById("chatApp"));
}, 400);
              
            
!
999px

Console