I’m very new to website development and I want to create simple diary that look like a chatgpt in terms of ui. Now I’m stuck for storing each message for each chat-section locally.
Here’s my html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" />
<script src="script.js" defer></script>
<title>Document</title>
</head>
<body>
<!-- navhead -->
<div class="chat-header">
<span class="material-symbols-outlined sidebar-menu" onclick="showSideBar()" >
menu
</span>
<p>MyDiary 1.0</p>
<span class="material-symbols-outlined">
settings
</span>
</div>
<!-- sidemenu -->
<div class="sidebar-wrapper">
<div class="sidebar-new-chat">
<div class="add-chat">
<span class="material-symbols-outlined">
add
</span>
<p>New Chat</p>
</div>
</div>
<!-- chat lists -->
<div class="chat-lists"><!-- it will contain chat section here --></div>
</div>
<!-- chatbox main -->
<div class="chatbox">
<!-- chat content -->
<div class="chatbox-wrapper">
<div class="message-boxWrapper">
</div>
</div>
</div>
<!-- message bar -->
<div class="message-bar">
<div class="bar-wrapper">
<textarea id="text-area" cols="30" rows="1" resize="none" placeholder="Enter your message here..."></textarea>
<button>
<span class="material-symbols-outlined">
send
</span>
</button>
</div>
</div>
</body>
</html>
This is my css :
*{
padding: 0;
margin: 0;
font-family: "Poppins",sans-serif;
box-sizing: border-box;
}
body{
width: 100%;
height: 100vh;
background-color: #303030;
}
::-webkit-scrollbar {
margin-left: 5px;
width: 10px;
}
/* Track */
::-webkit-scrollbar-track {
border-radius: 10px;
}
/* Handle */
::-webkit-scrollbar-thumb {
background: #303030;
border-radius: 10px;
}
.chat-header{
position: fixed;
top: 0;
width: 100%;
height: 4rem;
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px;
color: #ccc;
font-size: 1.5rem;
font-weight: 700;
background-color: #202020;
border-bottom: 3px solid #252525;
z-index: 100;
box-shadow: 0 0 30px #202020;
}
.chat-header > span{
cursor: pointer;
}
.sidebar-wrapper{
position: fixed;
display: fixed;
flex-direction: column;
left: 0;
top: 4rem;
min-width: 100px;
width: 50%;
height: 78vh;
padding: 20px;
background-color: #202020;
border-right: 3px solid #252525;
color: #ccc;
transform: translateX(-100%);
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
transition: all .5s ease;
overflow-x: hidden;
overflow-y: auto;
z-index: 99;
}
.sidebar-wrapper .add-chat{
display: flex;
padding: 10px 0;
margin-bottom: 10px;
width: 100%;
border: 1px solid #303030;
align-items: center;
justify-content: space-around;
border-radius: 10px;
font-size: 0.7rem;
font-weight: 700;
cursor: pointer;
}
.sidebar-wrapper .add-chat:hover{
color: #202020;
background-color: #ccc;
}
.sidebar-wrapper .chat-lists {
display: block;
position: fixed;
padding: 10px 0;
width: 85%;
height: 66.2vh;
overflow-y: auto;
}
.chat-lists .chat-head {
display: flex;
position: relative;
padding: 10px;
margin: 10px 0;
width: 90%;
border: 1px solid #303030;
align-items: center;
justify-content: space-around;
border-radius: 10px;
font-size: 0.8rem;
cursor: pointer;
transition: 0.3s ease-in-out;
}
.chat-lists .chat-head:hover {
border-color: #ccc;
}
.chat-head .chat-name{
width: 80%;
color: #ccc;
border: none;
outline: none;
background: none;
}
.menu-btn {
cursor: pointer;
}
.menu {
display: flex;
flex-direction: column;
position: absolute; /* Position the menu absolutely relative to its parent chat-head */
top: 100%; /* Position the menu below the chat-head */
right: 0; /* Align the menu to the right side of the chat-head */
padding: 2px;
background-color: #303030;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 2px 50px rgba(0, 0, 0, 0.1);
z-index: 1000; /* Ensure it's above other elements */
}
.menu button {
padding: 5px 10px;
color: #ccc;
border: none;
background: none;
cursor: pointer;
}
/* Hide the menu by default */
.hidden {
display: none;
}
.chatbox{
display: flex;
position: fixed;
top:4rem;
right: 0;
width: 100%;
height: 78vh;
overflow-y: auto;
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
transition: all .5s ease;
}
.chatbox-wrapper {
position: relative;
margin-left: auto;
width: 100%;
justify-content: center;
}
.chat {
display: flex;
gap: 20px;
padding: 25px;
margin: 10px;
color: #fff;
font-size: 15px;
font-weight: 300px;
border-radius: 10px;
justify-content: space-between;
}
.chat img {
width: 35px;
height: 35px;
border-radius: 50%;
}
.message-boxWrapper{
padding: 5px 0;
}
.message {
display: flex;
background-color: #202020;
align-items: center;
margin-bottom: 20px;
transition: 0.3s ease-in-out;
border: 1px solid #252525;
}
.message:hover{
border-color: #ccc;
}
.message span{
display: block;
width: 80%;
align-items: center;
}
.message span p{
font-weight: 700;
margin: 5px 0;
}
.message pre{
width: 100%;
white-space: pre-wrap;
overflow-wrap: break-word;
word-wrap: break-word;
word-break: break-word;
}
.message-date{
margin-left: auto;
}
.message-bar{
position: fixed;
bottom: 0;
height: 5rem;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
border-top: 1px solid #252525;
background-color: #202020;
z-index: 100;
}
.message-bar .bar-wrapper{
background-color: #303030;
border-radius: 10px;
width: 60vw;
padding: 10px;
display: flex;
align-items: center;
justify-content: space-between;
}
.bar-wrapper textarea{
width: 100%;
padding: 5px;
border: none;
outline: none;
font-size: 14px;
background: none;
color: #ccc;
word-wrap: break-word;
resize: none;
}
.bar-wrapper textarea::placeholder{
color: #ccc;
}
.message-bar button{
display: flex;
align-items: center;
justify-content: center;
background: none;
border: none;
outline: none;
color: #ccc;
cursor: pointer;
transition: 0.2s ease-in-out;
}
.message-bar button:hover{
color: #fff;
}
And this is javascript I’ve made (Uhh kinda, I’m asking chatgpt also for helping me to understand for creating function)
const messageBar = document.querySelector(".bar-wrapper textarea");
const sendBtn = document.querySelector(".bar-wrapper button");
const messageBox = document.querySelector(".message-boxWrapper");
const chatContainer = document.querySelector(".chatbox");
function scrollToBottom() {
chatContainer.scrollTop = chatContainer.scrollHeight;
}
function getCurrentDate() {
const now = new Date();
const options = { year: "numeric", month: "short", day: "numeric", hour: "numeric", minute: "numeric" };
return now.toLocaleDateString("en-US", options);
}
function showSideBar() {
var sideBar = document.querySelector(".sidebar-wrapper");
// Check if the sidebar is currently hidden or partially visible
if (sideBar.style.transform === 'translateX(-100%)' || sideBar.style.transform === 'translate(-100%)') {
sideBar.style.transform = 'translateX(0%)'; // Show the sidebar by translating it back to 0%
sideBar.style.width = '20%'; // Set the sidebar width when visible
chatContainer.style.opacity = '30%';
} else {
sideBar.style.transform = 'translateX(-100%)'; // Hide the sidebar by translating it off-screen
sideBar.style.width = '0'; // Optionally, set the sidebar width to 0 when hidden
chatContainer.style.opacity = '100%';
}
}
function chatNameMenu() {
const menuBtns = document.querySelectorAll('.menu-btn');
menuBtns.forEach(btn => {
// Remove existing event listeners
btn.removeEventListener('click', toggleMenu);
// Attach new event listener
btn.addEventListener('click', toggleMenu);
});
}
function toggleMenu() {
const menu = this.nextElementSibling;
menu.classList.toggle('hidden');
}
// Add New Chat Section
const addChatBtn = document.querySelector('.sidebar-new-chat');
addChatBtn.addEventListener('click', function () {
const chatListsContainer = document.querySelector('.chat-lists');
const newChatSection = document.createElement('div');
newChatSection.classList.add('chat-head');
newChatSection.innerHTML = `
<input type="text" class="chat-name" value="New Chat" readonly>
<span class="material-symbols-outlined menu-btn">more_vert</span>
<div class="menu hidden">
<button class="edit-btn">Edit</button>
<button class="delete-btn">Delete</button>
</div>
`;
chatListsContainer.insertBefore(newChatSection, chatListsContainer.lastElementChild);
chatNameMenu();
});
// Function to handle edit button click
function handleEdit(event) {
if (event.target.classList.contains('edit-btn')) {
const chatNameInput = event.target.closest('.chat-head').querySelector('.chat-name');
chatNameInput.removeAttribute('readonly');
chatNameInput.focus();
chatNameInput.addEventListener('keydown', function (e) {
if (e.key === 'Enter') {
const newName = chatNameInput.value.trim();
chatNameInput.value = newName;
chatNameInput.setAttribute('readonly', true);
}
});
}
}
// Add event listener to chat lists container for handling edit button click
const chatListsContainer = document.querySelector('.chat-lists');
chatListsContainer.addEventListener('click', handleEdit);
// Function to handle delete button click
function handleDelete(event) {
if (event.target.classList.contains('delete-btn')) {
const chatSection = event.target.closest('.chat-head');
const confirmDelete = window.confirm("Are you sure you want to delete this chat section?");
if (confirmDelete) {
chatSection.remove();
}
}
}
// Add event listener to chat lists container for handling delete button click
chatListsContainer.addEventListener('click', handleDelete);
// Function to handle sending a message
sendBtn.onclick = function(){
if(messageBar.value.length > 0){
let message =
`<div class="chat message">
<img src="img/chatgpt-icon.svg" alt="usericon">
<span>
<p>UserName</p>
<pre>${messageBar.value}</pre>
</span>
<div class="message-date">
${getCurrentDate()}
</div>
</div>`;
const existingChatSections = document.querySelectorAll('.chat-head');
if (existingChatSections.length === 0) {
// If no chat sections exist, create a new chat section
const chatListsContainer = document.querySelector('.chat-lists');
const newChatSection = document.createElement('div');
newChatSection.classList.add('chat-head');
newChatSection.innerHTML = `
<input type="text" class="chat-name" value="New Chat" readonly>
<span class="material-symbols-outlined menu-btn">more_vert</span>
<div class="menu hidden">
<button class="edit-btn">Edit</button>
<button class="delete-btn">Delete</button>
</div>`;
chatListsContainer.insertBefore(newChatSection, chatListsContainer.lastElementChild);
chatNameMenu();
} else {
// Append the message to the last chat section (whether existing or newly created)
// const lastChatSection = document.querySelector('.chat-lists .chat-head:last-child');
// const messagesContainer = lastChatSection.querySelector('.messages');
// messagesContainer.insertAdjacentHTML('beforeend', message);
}
messageBox.insertAdjacentHTML("beforeend",message);
messageBar.value = "";
scrollToBottom();
}
};
window.addEventListener("load", scrollToBottom);