Skip to content

Commit

Permalink
completed toast notification
Browse files Browse the repository at this point in the history
  • Loading branch information
tsr-kairi committed Jul 7, 2021
1 parent eef6394 commit 7c46431
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Day 27 - Toast Notification/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const button = document.getElementById("button");
const toasts = document.getElementById("toasts");

const message = [
"Message One",
"Message Two",
"Message Three",
"Message Four",
"Message Five",
"Message Six",
];

const types = ["info", "success", "error"];

button.addEventListener("click", () => createNotification());

function createNotification(message = null, type = null) {
const notif = document.createElement("div");

notif.classList.add("toast");
notif.classList.add(type ? type : getRandomType());

notif.innerText = message ? message : getRandomMessage();

toasts.appendChild(notif);

setTimeout(() => {
notif.remove();
}, 3000);
}

function getRandomMessage() {
return message[Math.floor(Math.random() * message.length)];
}

function getRandomType() {
return types[Math.floor(Math.random() * types.length)];
}
17 changes: 17 additions & 0 deletions Day 27 - Toast Notification/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./style.css" />
<title>Toast | Notification</title>
</head>
<body>
<div id="toasts"></div>

<button class="btn" id="button">Show Notification</button>

<script src="./app.js"></script>
</body>
</html>
65 changes: 65 additions & 0 deletions Day 27 - Toast Notification/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
*::after,
*::before {
box-sizing: border-box;
margin: 0;
padding: 0;
}

body {
background-color: rebeccapurple;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
overflow: hidden;
height: 100vh;
margin: 0;
}

.btn {
background-color: #ffffff;
color: rebeccapurple;
font-family: inherit;
font-weight: bold;
padding: 1rem;
border-radius: 5px;
border: none;
cursor: pointer;
}

.btn:focus {
outline: none;
}

.btn:active {
transform: scale(0.98);
}

#toasts {
position: fixed;
bottom: 10px;
right: 10px;
display: flex;
flex-direction: column;
align-items: flex-end;
justify-content: center;
}

.toast {
background-color: #fff;
border-radius: 5px;
padding: 1rem 2rem;
margin: 0.5rem;
}

.toast.info {
color: rebeccapurple;
}

.toast.success {
color: green;
}

.toast.error {
color: red;
}

0 comments on commit 7c46431

Please sign in to comment.