The theme for week #16 of the Weekly Coding Challenge is:
Notifications
The notification component is used in a web/mobile app when you want to notify the user that something happened based on his action - āsuccessfully completed registrationā, āerror on an input fieldā, āconfirmation of deletion of an itemā, etc.
In this article weāre going to build these Notifications Boxes:
https://codepen.io/FlorinPop17/pen/QXgVNa/
The HTML
In our example, all the notifications appear in the top-right corner (feel free to change it if you want) and for that we need to have a container which will hold all these notifications and itāll be positioned in that spot.
Also, weāll have 4 types of notifications: info
, success
, warning
, danger
. Weāll set the notification color based on its type later in the CSS, and for that weāll use different classes in the HTML:
<div class="notification-container" id="notification-container">
<div class="notification notification-info">
<strong>Info:</strong> Lorem ipsum dolor sit amet.
</div>
<div class="notification notification-success">
<strong>Success:</strong> Lorem ipsum dolor sit amet consectetur
adipisicing elit.
</div>
<div class="notification notification-warning">
<strong>Warning:</strong> Lorem ipsum dolor sit amet, consectetur
adipisicing.
</div>
<div class="notification notification-danger">
<strong>Danger:</strong> Lorem ipsum dolor sit amet consectetur.
</div>
</div>
We added an id
on the .notification-container
because weāll target it later with JavaScript and weāll create some dynamic notifications. š
The CSS
As mentioned above, the .notification-container
will be position: fixed
as we want to be able to place it in the top-right corner.
Weāll also have a little bit of animation on the .notification
box when itāll appear and disappear, and for this weāll create two keyframes - grow and shrink - and weāll manipulate the opacity
and then the size
of the element using transform: scale
.
Last, but not least, weāll have different background-colors
for each type of notification.
.notification-container {
position: fixed;
top: 15px;
right: 15px;
width: 500px;
max-width: calc(100% - 30px);
}
.notification {
background-color: #fff;
border-radius: 5px;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
color: #fff;
font-size: 16px;
padding: 15px 20px;
line-height: 20px;
margin-bottom: 15px;
animation: grow 0.5s ease-in forwards;
}
@keyframes grow {
from {
opacity: 0;
transform: scale(0.8);
}
to {
opacity: 1;
transform: scale(1);
}
}
.notification.hide {
animation: shrink 0.3s ease-out forwards;
}
@keyframes shrink {
to {
opacity: 0;
transform: scale(0.8);
}
}
.notification strong {
font-size: 12px;
line-height: 20px;
letter-spacing: 0.5px;
text-transform: uppercase;
}
.notification-info {
background-color: #00cae3;
}
.notification-success {
background-color: #55b559;
}
.notification-warning {
background-color: #ff9e0f;
}
.notification-danger {
background-color: #f55145;
}
As you can see we used the .hide
class on the notification to add the shrink
animation. Weāll add this class dynamically in JavaScript before we want to remove the notification from the DOM in order to have a nice disappear animation.
The JavaScript
One more phase to go. š
First, letās target the notification-container
and also letās create a NOTIFICATION_TYPES
variable which will hold all the notification types we want to have in our app:
const notificationContainer = document.getElementById('notification-container');
const NOTIFICATION_TYPES = {
INFO: 'info',
SUCCESS: 'success',
WARNING: 'warning',
DANGER: 'danger'
};
Next, letās write a function which will create a new notification when called - the newNotification
function. This function will require two parameters:
- the type of the notification
- the text
Itāll also return the newly created DOM element as weāll use that in the removeNotification
function to know which notification will be remove.
function addNotification(type, text) {
// create the DIV and add the required classes
const newNotification = document.createElement('div');
newNotification.classList.add('notification', `notification-${type}`);
const innerNotification = `
<strong>${type}:</strong> ${text}
`;
// insert the inner elements
newNotification.innerHTML = innerNotification;
// add the newNotification to the container
notificationContainer.appendChild(newNotification);
return newNotification;
}
You can see that the structure of the component matches what we had in the HTML and what we styled in the CSS.
Finally, letās write the removeNotification
function. This will happen in two steps:
- Add the
.hide
class to the notification - this will trigger theshrink
animation in the CSS - remove the DOM element from itās parent after 0.5 seconds, allowing the required time for the animation to take place
function removeNotification(notification) {
notification.classList.add('hide');
// remove notification from the DOM after 0.5 seconds
setTimeout(() => {
notificationContainer.removeChild(notification);
}, 500);
}
And finally, letās see an example of how we can use these two functions:
const info = addNotification(NOTIFICATION_TYPES.INFO, 'Info text added');
setTimeout(() => {
removeNotification(info);
}, 5000);
I added another setTimeout
for the removal and in this case the notification will be visible for 5 seconds before itāll disappear, but you could just as easily add a button that will trigger the removeNotification
function if you want. š
Conclusion
This is a simple demonstration of a notification feature. You can go even further and you could add:
- different positioning for the
.notification-container
(eg. top-left, bottom-left, bottom-right) - a small
x
/close button inside the notification which would trigger theremoveNotification
function when clicked - icons inside the notification
and many more!
Canāt wait to see what youāll build and as always, make sure you share it with me!
Happy Coding! š