<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport"…
- Usercode
- Assistant
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
}
body {
background: var(--bg-light);
color: var(--text-light);
transition: all 0.3s ease;
}
body.dark-mode {
background: var(--bg-dark);
color: var(--text-dark);
}
.container {
display: flex;
height: 100vh;
}
/* Sidebar Styles */
.sidebar {
width: 320px;
border-right: 1px solid var(--border-light);
display: flex;
flex-direction: column;
background: var(--bg-light);
transition: all 0.3s ease;
}
.dark-mode .sidebar {
background: var(--bg-dark);
border-color: var(--border-dark);
}
.sidebar-header {
padding: 16px;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: 1px solid var(--border-light);
}
.dark-mode .sidebar-header {
border-color: var(--border-dark);
}
.search-box {
padding: 12px 16px;
position: relative;
}
.search-input {
width: 100%;
padding: 8px 32px 8px 12px;
border: none;
border-radius: 20px;
background: var(--chat-bg-light);
color: var(--text-light);
font-size: 14px;
}
.dark-mode .search-input {
background: var(--chat-bg-dark);
color: var(--text-dark);
}
.chat-list {
flex: 1;
overflow-y: auto;
}
.chat-item {
padding: 12px 16px;
display: flex;
align-items: center;
cursor: pointer;
transition: background 0.2s;
}
.chat-item:hover {
background: var(--hover-light);
}
.dark-mode .chat-item:hover {
background: var(--hover-dark);
}
.chat-avatar {
width: 48px;
height: 48px;
border-radius: 50%;
margin-right: 12px;
object-fit: cover;
}
.chat-content {
flex: 1;
}
.chat-name {
font-weight: 600;
margin-bottom: 4px;
}
.chat-message {
font-size: 14px;
color: #666;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.dark-mode .chat-message {
color: #aaa;
}
/* Main Chat Area */
.main-chat {
flex: 1;
display: flex;
flex-direction: column;
background: var(--chat-bg-light);
}
.dark-mode .main-chat {
background: var(--chat-bg-dark);
}
.chat-header {
padding: 16px;
display: flex;
align-items: center;
background: var(--bg-light);
border-bottom: 1px solid var(--border-light);
}
.dark-mode .chat-header {
background: var(--bg-dark);
border-color: var(--border-dark);
}
.messages-container {
flex: 1;
padding: 20px;
overflow-y: auto;
}
.message {
max-width: 65%;
margin-bottom: 12px;
padding: 8px 12px;
border-radius: 8px;
position: relative;
}
.message.sent {
background: var(--primary-color);
color: white;
margin-left: auto;
border-radius: 8px 8px 0 8px;
}
.message.received {
background: white;
margin-right: auto;
border-radius: 8px 8px 8px 0;
}
.dark-mode .message.received {
background: var(--chat-bg-dark);
}
.message-input-container {
padding: 16px;
background: var(--bg-light);
border-top: 1px solid var(--border-light);
display: flex;
align-items: center;
}
.dark-mode .message-input-container {
background: var(--bg-dark);
border-color: var(--border-dark);
}
.message-input {
flex: 1;
padding: 12px;
border: none;
border-radius: 20px;
background: var(--chat-bg-light);
margin: 0 12px;
font-size: 14px;
}
.dark-mode .message-input {
background: var(--chat-bg-dark);
color: var(--text-dark);
}
.btn {
padding: 8px;
border: none;
background: none;
color: var(--primary-color);
cursor: pointer;
border-radius: 50%;
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
transition: background 0.2s;
}
.btn:hover {
background: rgba(33, 150, 243, 0.1);
}
/* Profile Drawer */
.profile-drawer {
position: fixed;
right: -380px;
top: 0;
width: 380px;
height: 100vh;
background: var(--bg-light);
border-left: 1px solid var(--border-light);
transition: right 0.3s ease;
z-index: 1000;
}
.dark-mode .profile-drawer {
background: var(--bg-dark);
border-color: var(--border-dark);
}
.profile-drawer.open {
right: 0;
}
.profile-header {
padding: 20px;
text-align: center;
border-bottom: 1px solid var(--border-light);
}
.dark-mode .profile-header {
border-color: var(--border-dark);
}
.profile-avatar {
width: 120px;
height: 120px;
border-radius: 50%;
margin: 0 auto 16px;
object-fit: cover;
}
/* Animations */
@keyframes slideIn {
from { transform: translateX(100%); }
to { transform: translateX(0); }
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.message {
animation: fadeIn 0.3s ease;
}
.chat-item {
animation: fadeIn 0.3s ease;
}
/* Responsive Design */
@media (max-width: 768px) {
.sidebar {
width: 100%;
position: absolute;
transform: translateX(-100%);
transition: transform 0.3s ease;
}
.sidebar.open {
transform: translateX(0);
}
.profile-drawer {
width: 100%;
}
}
</style>
<!-- Main Chat Area -->
<div class="main-chat">
<div class="chat-header">
<img src="https://picsum.photos/48/48?random=1" alt="Current Chat" class="chat-avatar">
<div class="chat-content">
<div class="chat-name">Current Chat</div>
<div class="chat-status">online</div>
</div>
<button class="btn" onclick="toggleProfile()">
<i class="fas fa-info-circle"></i>
</button>
</div>
<div class="messages-container">
<!-- Messages will be dynamically added here -->
</div>
<div class="message-input-container">
<button class="btn">
<i class="fas fa-paperclip"></i>
</button>
<input type="text" class="message-input" placeholder="Write a message...">
<button class="btn" onclick="sendMessage()">
<i class="fas fa-paper-plane"></i>
</button>
</div>
</div>
<!-- Profile Drawer -->
<div class="profile-drawer">
<div class="profile-header">
<img src="https://picsum.photos/120/120?random=2" alt="Profile" class="profile-avatar">
<h2>User Name</h2>
<p>@username</p>
</div>
<!-- Profile content will be added here -->
</div>
</div>
<script>
// Sample data for chats
const chats = [
{
id: 1,
name: "John Doe",
lastMessage: "Hey, how are you?",
avatar: "https://picsum.photos/48/48?random=3"
},
{
id: 2,
name: "Jane Smith",
lastMessage: "See you tomorrow!",
avatar: "https://picsum.photos/48/48?random=4"
},
// Add more chat data here
];
// Sample messages
const messages = [
{
id: 1,
text: "Hi there!",
sent: false,
timestamp: new Date()
},
{
id: 2,
text: "Hello! How are you?",
sent: true,
timestamp: new Date()
}
// Add more messages here
];
// Function to toggle dark mode
function toggleTheme() {
document.body.classList.toggle('dark-mode');
}
// Function to toggle profile drawer
function toggleProfile() {
document.querySelector('.profile-drawer').classList.toggle('open');
}
// Function to toggle mobile menu
function toggleMenu() {
document.querySelector('.sidebar').classList.toggle('open');
}
// Function to send message
function sendMessage() {
const input = document.querySelector('.message-input');
const text = input.value.trim();
if (text) {
const message = {
id: messages.length + 1,
text: text,
sent: true,
timestamp: new Date()
};
addMessageToChat(message);
input.value = '';
}
}
// Function to add message to chat
function addMessageToChat(message) {
const messagesContainer = document.querySelector('.messages-container');
const messageElement = document.createElement('div');
messageElement.classList.add('message', message.sent ? 'sent' : 'received');
messageElement.textContent = message.text;
messagesContainer.appendChild(messageElement);
messagesContainer.scrollTop = messagesContainer.scrollHeight;
}
// Function to render chat list
function renderChats() {
const chatList = document.querySelector('.chat-list');
chatList.innerHTML = '';
chats.forEach(chat => {
const chatElement = document.createElement('div');
chatElement.classList.add('chat-item');
chatElement.innerHTML = `
<img src="${chat.avatar}" alt="${chat.name}" class="chat-avatar">
<div class="chat-content">
<div class="chat-name">${chat.name}</div>
<div class="chat-message">${chat.lastMessage}</div>
</div>
`;
chatList.appendChild(chatElement);
});
}
// Initialize the app
function init() {
renderChats();
messages.forEach(message => addMessageToChat(message));
// Event listener for message input
document.querySelector('.message-input').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendMessage();
}
});
}
// Call init when the page loads
document.addEventListener('DOMContentLoaded', init);
</script>
Thank you for sharing your code for a Telegram Web Clone! It's a comprehensive implementation of a chat application using HTML, CSS, and JavaScript. I'd be happy to review your code, point out any issues, and suggest improvements to enhance the functionality, performance, and maintainability of your application.
Your application consists of:
Use of Semantic Elements: Consider using semantic HTML5 elements like <header>, <nav>, <main>, <aside>, and <footer> to improve the readability and accessibility of your code.
<body>
<header> ... </header>
<div class="container">
<aside class="sidebar"> ... </aside>
<main class="main-chat"> ... </main>
<div class="profile-drawer"> ... </div>
</div>
</body>
Accessibility: Add aria attributes and labels to interactive elements for better accessibility.
<button class="btn" onclick="toggleMenu()" aria-label="Toggle Menu">
<i class="fas fa-bars" aria-hidden="true"></i>
</button>
External CSS File: While inline <style> is acceptable for small projects, consider moving your CSS into an external stylesheet for better organization.
CSS Variables: Great job using CSS variables for theming! To enhance dark mode, consider using the prefers-color-scheme media query to detect the user's system preference.
@media (prefers-color-scheme: dark) {
:root {
--bg-light: #1a1a1a;
--text-light: #ffffff;
/* Other dark mode variables */
}
}
Responsiveness:
You have a media query for screens up to 768px, but the sidebar might still be too wide on smaller screens. Consider adjusting widths or using relative units (e.g., vw, %).
Test your application on various screen sizes to ensure all elements display properly.
Vendor Prefixes: Use vendor prefixes for better cross-browser compatibility or consider using a tool like Autoprefixer.
Modular Code: As your application grows, organizing your JavaScript code into modules or using classes can improve readability and maintainability.
class ChatApplication {
constructor() { ... }
init() { ... }
toggleTheme() { ... }
toggleProfile() { ... }
sendMessage() { ... }
// Other methods
}
const app = new ChatApplication();
app.init();
Event Delegation: Instead of adding event listeners to each dynamic element, use event delegation to handle events more efficiently.
document.querySelector('.chat-list').addEventListener('click', (e) => {
if (e.target.closest('.chat-item')) {
// Handle chat item click
}
});
Improved Data Handling:
State Management: Use a state management pattern or library to manage chats and messages. This can simplify data updates and UI rendering.
Data Binding: Implement data binding to automatically update the UI when data changes.
Template Literals: Use template literals in your JavaScript code for better readability and easier multiline HTML.
chatElement.innerHTML = `
<img src="${chat.avatar}" alt="${chat.name}" class="chat-avatar">
<div class="chat-content">
<div class="chat-name">${chat.name}</div>
<div class="chat-message">${chat.lastMessage}</div>
</div>
`;
Chat Selection Functionality:
function selectChat(chatId) {
// Load messages for chatId
renderMessages(chatId);
}
renderChats function to attach onclick events to chat items.chatElement.addEventListener('click', () => {
selectChat(chat.id);
});
Persisting Data:
Use localStorage or a backend service to persist chats and messages, so data is not lost on page reload.
Implement a function to save messages:
function saveMessages() {
localStorage.setItem('messages', JSON.stringify(messages));
}
Load messages on initialization:
function loadMessages() {
const savedMessages = JSON.parse(localStorage.getItem('messages'));
if (savedMessages) {
messages = savedMessages;
}
}
Timestamp Formatting:
Intl.DateTimeFormat API.const formattedTime = new Intl.DateTimeFormat('en', {
hour: 'numeric',
minute: 'numeric',
}).format(message.timestamp);
messageElement.innerHTML = `
<span class="message-text">${message.text}</span>
<span class="message-time">${formattedTime}</span>
`;
Debouncing Input Events: If you implement features like search or typing indicators, use debouncing to prevent excessive function calls.
function debounce(func, delay) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), delay);
};
}
const handleInput = debounce(() => {
// Handle input
}, 300);
inputElement.addEventListener('input', handleInput);
Lazy Loading Images: For avatars and images, implement lazy loading to improve performance.
<img src="avatar.jpg" loading="lazy" alt="Avatar">
Sanitize User Input: When rendering user-generated content, ensure it's properly sanitized to prevent Cross-Site Scripting (XSS) attacks.
function sanitizeHTML(str) {
var temp = document.createElement('div');
temp.textContent = str;
return temp.innerHTML;
}
messageElement.textContent = sanitizeHTML(message.text);
Content Security Policy (CSP): Implement CSP headers to reduce the risk of XSS attacks.
Enhanced Styling:
Use CSS transitions and animations thoughtfully to enhance user experience.
Consider using CSS frameworks or component libraries (like Bootstrap or Material-UI) for a more polished UI.
User Feedback:
Dark Mode Toggle Icon:
function toggleTheme() {
const isDarkMode = document.body.classList.toggle('dark-mode');
const themeIcon = document.querySelector('.fa-moon');
themeIcon.classList.toggle('fa-sun', isDarkMode);
themeIcon.classList.toggle('fa-moon', !isDarkMode);
}
Error Handling:
To further improve your Telegram Web Clone, consider the following steps:
Implement Chat Switching: Allow users to click on different chats and load the corresponding messages.
Backend Integration:
Connect your application to a backend server or database to store chats and messages persistently.
Use WebSocket or technologies like Socket.IO for real-time messaging.
Authentication:
Deployment:
Testing:
Code Refactoring:
Web Development Best Practices:
JavaScript Frameworks:
CSS Frameworks:
State Management:
Feel free to let me know if you have specific questions or need further assistance with any part of your application. I'm here to help!
Not yet included in a published release.
.message-time.Typing Indicator:
Implement a typing indicator when the user is typing.
function showTypingIndicator() { ... }
function hideTypingIndicator() { ... }
messageInput.addEventListener('input', () => {
if (messageInput.value) {
showTypingIndicator();
} else {
hideTypingIndicator();
}
});
File Attachments:
function attachFile() {
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.onchange = () => {
const file = fileInput.files[0];
// Handle file upload
};
fileInput.click();
}
document.querySelector('.fa-paperclip').parentElement.addEventListener('click', attachFile);