Here is a demo of HTML, CSS, and JavaScript codes for a chat user interface with a Material You dark theme. The DMs (Direct Messages) are on the left, and the chat window is on the right. The first DM is opened by default.
We'll use standard HTML, CSS, and JavaScript to create this interface, applying Material You styling principles manually to achieve the desired look.
HTML (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Material You Chat UI Demo</title>
<!-- Include Google Fonts for Material Design typography -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
<!-- Include the CSS file -->
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="chat-container">
<div class="dm-list">
<!-- List of DMs -->
<ul id="dm-list">
<li class="dm-item active" data-chat-id="chat1">
<div class="user-avatar">A</div>
<div class="user-info">
<span class="user-name">Alice</span>
<span class="last-message">Hey there!</span>
</div>
</li>
<li class="dm-item" data-chat-id="chat2">
<div class="user-avatar">B</div>
<div class="user-info">
<span class="user-name">Bob</span>
<span class="last-message">Let's catch up.</span>
</div>
</li>
<li class="dm-item" data-chat-id="chat3">
<div class="user-avatar">C</div>
<div class="user-info">
<span class="user-name">Charlie</span>
<span class="last-message">Meeting at 5 PM.</span>
</div>
</li>
<!-- Add more DMs as needed -->
</ul>
</div>
<div class="chat-window">
<!-- Chat content -->
<div id="chat1" class="chat-content active">
<!-- Messages for Alice -->
<div class="message received">Hey there!</div>
<div class="message sent">Hi Alice, how are you?</div>
</div>
<div id="chat2" class="chat-content">
<!-- Messages for Bob -->
<div class="message received">Let's catch up soon.</div>
<div class="message sent">Sure, when are you free?</div>
</div>
<div id="chat3" class="chat-content">
<!-- Messages for Charlie -->
<div class="message received">Don't forget our meeting at 5 PM.</div>
<div class="message sent">Got it, see you then!</div>
</div>
</div>
</div>
<!-- Include the JavaScript file -->
<script src="script.js"></script>
</body>
</html>
CSS (styles.css)
/* Material You Dark Theme Styles */
body {
margin: 0;
font-family: 'Roboto', sans-serif;
background-color: #121212; /* Dark background */
color: #FFFFFF; /* Light text */
}
.chat-container {
display: flex;
height: 100vh;
}
.dm-list {
width: 25%;
background-color: #1E1E1E; /* Slightly lighter than the main background */
overflow-y: auto;
border-right: 1px solid #333333;
}
#dm-list {
list-style-type: none;
padding: 0;
margin: 0;
}
.dm-item {
display: flex;
align-items: center;
padding: 15px;
cursor: pointer;
transition: background-color 0.2s;
}
.dm-item:hover, .dm-item.active {
background-color: #2A2A2A;
}
.user-avatar {
background-color: #2E7D32; /* Example accent color */
color: #FFFFFF;
width: 40px;
height: 40px;
border-radius: 20px;
display: flex;
align-items: center;
justify-content: center;
margin-right: 15px;
font-weight: bold;
}
.user-info {
display: flex;
flex-direction: column;
}
.user-name {
font-weight: 500;
}
.last-message {
font-size: 0.9em;
color: #B0B0B0;
}
.chat-window {
width: 75%;
display: flex;
flex-direction: column;
background-color: #121212;
}
.chat-content {
flex-grow: 1;
padding: 20px;
display: none;
overflow-y: auto;
}
.chat-content.active {
display: block;
}
.message {
margin: 10px 0;
padding: 12px 18px;
border-radius: 18px;
max-width: 70%;
line-height: 1.4;
font-size: 1em;
}
.message.received {
background-color: #2A2A2A;
align-self: flex-start;
}
.message.sent {
background-color: #2979FF; /* Blue accent color */
align-self: flex-end;
color: #FFFFFF;
}
.message.sent::after {
content: '';
display: block;
clear: both;
}
@media screen and (max-width: 768px) {
.dm-list {
width: 100%;
height: 50vh;
}
.chat-window {
width: 100%;
height: 50vh;
}
}
JavaScript (script.js)
document.addEventListener('DOMContentLoaded', function() {
const dmItems = document.querySelectorAll('.dm-item');
const chatContents = document.querySelectorAll('.chat-content');
dmItems.forEach(item => {
item.addEventListener('click', () => {
// Remove 'active' class from all DM items
dmItems.forEach(i => i.classList.remove('active'));
// Add 'active' class to clicked item
item.classList.add('active');
// Hide all chat contents
chatContents.forEach(content => content.classList.remove('active'));
// Show the selected chat content
const chatId = item.getAttribute('data-chat-id');
document.getElementById(chatId).classList.add('active');
});
});
});
Explanation:
How to Use:
-
Set Up the Files:
- Create three files in the same directory:
index.html, styles.css, and script.js.
- Copy the relevant code snippets above into each file.
-
Run the Demo:
- Open
index.html in a web browser to see the chat interface.
- Click on different users in the DM list to switch between chats.
-
Customizations:
- To add more DMs, add more
li elements in the dm-list with corresponding chat-content divs.
- Customize user names, avatars, and messages as desired.
- Modify CSS styles to change colors, fonts, and layout according to your preferences.
Notes:
-
Material You Styling:
- Material You emphasizes personalization and dynamic color theming. This demo uses static colors resembling a dark theme. For dynamic theming, additional JavaScript and access to device settings would be required.
- Since web support for full Material You features may be limited, this demo manually applies Material Design principles.
-
Responsive Design:
- A basic media query is included to adjust the layout on smaller screens (e.g., stacking the DM list and chat window vertically).
-
Font and Icons:
- The Roboto font is included via Google Fonts for a Material Design look.
- You can replace the avatar letters with images or icons if desired.
-
Enhancements:
- For a more advanced implementation, consider using a UI framework or library that supports Material Design components, such as Material Design Lite, Material Components for the Web, or a React library like MUI.
- Implementing actual message sending functionality would require a backend setup and real-time communication protocols (e.g., WebSockets).
Feel free to ask if you have any questions or need further assistance with this demo!