Working
This commit is contained in:
parent
abdffe2e7a
commit
75840914cf
5 changed files with 322 additions and 117 deletions
|
@ -2,10 +2,13 @@ import moment from "https://jspm.dev/moment"
|
|||
import { io } from "https://cdn.socket.io/4.3.2/socket.io.esm.min.js"
|
||||
const socket = io(":3000")
|
||||
const messageButton = document.querySelector("#send")
|
||||
const menuButton = document.querySelector("#menu")
|
||||
const nameButton = document.querySelector("#name")
|
||||
const roomContainer = document.querySelector("#room-container")
|
||||
const nameInput = document.querySelector("input")
|
||||
const roomContainer = document.querySelector("#room-container")
|
||||
const messageInput = document.querySelector("textarea")
|
||||
const login = document.querySelector("#login")
|
||||
const nameDisplay = document.querySelector("#name-display")
|
||||
|
||||
setInterval(
|
||||
() =>
|
||||
|
@ -25,7 +28,7 @@ const addMessage = (messageObject) => {
|
|||
.querySelector(".message")
|
||||
|
||||
if (isYours) messageDiv.classList.add("message-right")
|
||||
else if (isSystem) messageDiv.classList.add("system")
|
||||
if (isSystem) messageDiv.classList.add("system")
|
||||
messageDiv.querySelector(".message-text").innerText = message
|
||||
const time = Date.now()
|
||||
messageDiv.dataset.time = time
|
||||
|
@ -35,6 +38,26 @@ const addMessage = (messageObject) => {
|
|||
document.querySelector("#messages").prepend(messageDiv)
|
||||
}
|
||||
|
||||
if (roomName) {
|
||||
socket.emit("new-user", roomName, localStorage.getItem("name"))
|
||||
addMessage({
|
||||
message: "You joined",
|
||||
isYours: true,
|
||||
isSystem: true,
|
||||
})
|
||||
}
|
||||
|
||||
menuButton.addEventListener("click", () =>
|
||||
document.querySelector("#rooms").classList.toggle("opened")
|
||||
)
|
||||
|
||||
if (!localStorage.getItem("name")) login.classList.remove("done")
|
||||
else nameDisplay.innerHTML = localStorage.getItem("name")
|
||||
|
||||
document
|
||||
.querySelector("#change-name")
|
||||
.addEventListener("click", () => login.classList.remove("done"))
|
||||
|
||||
socket.on("room-created", (room) => {
|
||||
const roomItem = document.createElement("li")
|
||||
const roomLink = document.createElement("a")
|
||||
|
@ -42,18 +65,23 @@ socket.on("room-created", (room) => {
|
|||
roomLink.innerText = room
|
||||
roomItem.append(roomLink)
|
||||
roomContainer.append(roomItem)
|
||||
if (document.querySelector("#no-rooms"))
|
||||
document.querySelector("#no-rooms").classList.add("changed")
|
||||
})
|
||||
|
||||
if (nameButton)
|
||||
nameButton.addEventListener("click", () => {
|
||||
if (!nameInput.value) return (nameInput.required = true)
|
||||
document.querySelector("#login").classList.add("done")
|
||||
socket.emit("new-user", roomName, nameInput.value)
|
||||
addMessage({
|
||||
message: "You joined",
|
||||
isYours: true,
|
||||
isSystem: true,
|
||||
})
|
||||
localStorage.setItem("name", nameInput.value)
|
||||
nameDisplay.innerHTML = localStorage.getItem("name")
|
||||
if (roomName) {
|
||||
socket.emit("name-change", roomName, nameInput.value)
|
||||
addMessage({
|
||||
message: `You renamed yourself to ${nameInput.value}`,
|
||||
isSystem: true,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
if (messageButton)
|
||||
|
@ -69,8 +97,14 @@ socket.on("chat-message", (message, user) => {
|
|||
addMessage({ message, user })
|
||||
})
|
||||
|
||||
socket.on("name-changed", (oldName, newName) => {
|
||||
addMessage({
|
||||
message: `${oldName} was renamed to ${newName}.`,
|
||||
isSystem: true,
|
||||
})
|
||||
})
|
||||
|
||||
socket.on("user-connected", (name) => {
|
||||
socket.emit("join-room", ROOM_ID)
|
||||
addMessage({ message: `${name} joined`, isSystem: true })
|
||||
})
|
||||
|
||||
|
|
175
public/style.css
175
public/style.css
|
@ -16,11 +16,92 @@ body {
|
|||
"Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
|
||||
font-size: 1.2em;
|
||||
scroll-behavior: smooth;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
#menu {
|
||||
position: absolute;
|
||||
top: 25px;
|
||||
z-index: 2;
|
||||
left: 0;
|
||||
}
|
||||
|
||||
#content {
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
display: grid;
|
||||
grid-template-rows: 100px 1fr 175px;
|
||||
}
|
||||
|
||||
#rooms {
|
||||
background-color: var(--secondary);
|
||||
width: 0;
|
||||
overflow: hidden;
|
||||
align-items: center;
|
||||
transition: width 2s;
|
||||
display: flex;
|
||||
z-index: 1;
|
||||
color: white;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
#rooms h1 {
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
#rooms a:hover {
|
||||
background-color: #485b70 !important;
|
||||
}
|
||||
|
||||
#rooms a {
|
||||
background-color: #374a5e;
|
||||
}
|
||||
|
||||
#rooms a.active {
|
||||
background-color: var(--primary);
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
#rooms ul {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
gap: 10px;
|
||||
font-size: 1.5em;
|
||||
}
|
||||
|
||||
#rooms :is(li, ul, a) {
|
||||
width: 95%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
#rooms.opened {
|
||||
width: 18vw;
|
||||
}
|
||||
|
||||
#rooms:not(.opened) + div nav {
|
||||
padding-left: 70px;
|
||||
}
|
||||
|
||||
#rooms input {
|
||||
width: 80%;
|
||||
}
|
||||
|
||||
#rooms input::placeholder {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#rooms form {
|
||||
text-align: center;
|
||||
margin: auto 0 20px 0;
|
||||
}
|
||||
|
||||
#rooms form button {
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
nav,
|
||||
footer {
|
||||
background-color: var(--secondary);
|
||||
|
@ -28,6 +109,8 @@ footer {
|
|||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
max-width: 100vw;
|
||||
transition: padding 0.6s;
|
||||
}
|
||||
|
||||
a {
|
||||
|
@ -38,10 +121,6 @@ a {
|
|||
grid-template-columns: 1fr 500px;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
nav:first-child a {
|
||||
font-size: 1.5em !important;
|
||||
}
|
||||
|
@ -61,11 +140,11 @@ input[type="text"] {
|
|||
font-family: inherit;
|
||||
font-size: 2rem;
|
||||
max-width: 100%;
|
||||
max-height: 90%;
|
||||
max-height: 10vh;
|
||||
}
|
||||
|
||||
:is(textarea, input[type="text"])::placeholder {
|
||||
color: white;
|
||||
color: #dbcece;
|
||||
}
|
||||
|
||||
button {
|
||||
|
@ -74,7 +153,6 @@ button {
|
|||
color: #f5f0f0;
|
||||
background-color: transparent;
|
||||
font-size: 1em;
|
||||
cursor: pointer;
|
||||
padding: 0.7rem 1.6rem;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
@ -96,7 +174,11 @@ svg {
|
|||
width: 1.5em;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
button:not(:disabled) {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
button:is(:disabled, :hover) {
|
||||
filter: brightness(0.9);
|
||||
}
|
||||
|
||||
|
@ -106,6 +188,14 @@ button:hover {
|
|||
text-transform: capitalize;
|
||||
}
|
||||
|
||||
#change-name {
|
||||
margin: 0 10px 0 auto;
|
||||
}
|
||||
|
||||
#name-display {
|
||||
font-size: 1.5em;
|
||||
}
|
||||
|
||||
.message-text {
|
||||
padding: 10px;
|
||||
font-size: 17px;
|
||||
|
@ -119,6 +209,7 @@ button:hover {
|
|||
color: white;
|
||||
border-radius: 20px;
|
||||
}
|
||||
|
||||
#main-content {
|
||||
background-color: var(--primary);
|
||||
color: white;
|
||||
|
@ -128,6 +219,7 @@ button:hover {
|
|||
}
|
||||
|
||||
#messages {
|
||||
max-width: 100vw !important;
|
||||
background-color: var(--primary);
|
||||
padding: 30px;
|
||||
overflow-y: auto;
|
||||
|
@ -168,10 +260,6 @@ button:hover {
|
|||
justify-content: right;
|
||||
}
|
||||
|
||||
.message-middle {
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.message-right .message-text {
|
||||
background-color: #e9e932;
|
||||
color: #424242;
|
||||
|
@ -190,21 +278,21 @@ button:hover {
|
|||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100vh;
|
||||
width: 100vw;
|
||||
}
|
||||
|
||||
#login.done {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#login:not(.done) + nav,
|
||||
#login:not(.done) + nav + div,
|
||||
#login:not(.done) + nav + div + footer {
|
||||
#login:not(.done) + div,
|
||||
#login:not(.done) + div + div {
|
||||
display: none !important;
|
||||
}
|
||||
|
||||
.medium-header {
|
||||
font-size: 3rem;
|
||||
font-weight: 250;
|
||||
font-size: 2.5rem;
|
||||
font-weight: 200;
|
||||
line-height: 1.2;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
@ -226,6 +314,59 @@ button:hover {
|
|||
color: #f20000;
|
||||
}
|
||||
|
||||
#no-rooms.changed {
|
||||
display: none;
|
||||
}
|
||||
|
||||
@media (max-width: 1520px) {
|
||||
#rooms-header {
|
||||
visibility: hidden;
|
||||
}
|
||||
#rooms.opened {
|
||||
width: 30vw;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 1000px) {
|
||||
#rooms.opened {
|
||||
width: 100vw;
|
||||
}
|
||||
|
||||
#rooms {
|
||||
position: absolute;
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
#rooms-header {
|
||||
visibility: visible;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 500px) {
|
||||
#login input {
|
||||
max-width: 60vw;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 725px) {
|
||||
#name-display {
|
||||
display: none;
|
||||
}
|
||||
|
||||
body {
|
||||
font-size: 1.1em;
|
||||
}
|
||||
|
||||
footer {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
button:not(#menu, .button-circle) {
|
||||
display: block;
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes shake {
|
||||
0% {
|
||||
margin-left: 0rem;
|
||||
|
|
Reference in a new issue