Back
Discover how to design a magical snackbar that hilariously spills its message like a mystical potion spreading across the screen, utilizing gentle animations.
Snackbars are an essential part of modern UI design, providing brief but important notifications to users. Today, we're going to redesign the traditional snackbar and give it a delightful, magical twist by incorporating a dynamic spill effect!
Let's start by taking a look at the HTML structure of our "Magic Spill Snackbar." The HTML is straightforward, using a <div> to house the snackbar and a <button> inside for additional interactivity.
<div class="alchemy-container">
<div class="magic-spill-snackbar" role="alert" aria-live="assertive">
<span class="chant-text">Potion Brewed!</span>
<button class="undo-spell" aria-label="Undo action">Undo</button>
</div>
</div>
The role="alert" and aria-live="assertive" attributes are vital for ensuring accessibility by informing screen readers that this is an important message.
Cascading Style Sheets (CSS) bring our snackbar to life. The .magic-spill-snackbar class drives the main styling, using a whimsical color scheme and shadow effects to mimic an enchanted potion.
.alchemy-container {
position: fixed;
bottom: 20px;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.magic-spill-snackbar {
background: rgba(75, 0, 130, 0.8);
color: white;
border-radius: 20px;
padding: 10px 20px;
font-size: 16px;
display: flex;
align-items: center;
box-shadow: 0 6px 9px rgba(0,0,0,0.12);
transform: translateY(100%);
animation: spill 1s ease-out forwards;
}
Notice the use of position: fixed and centering techniques for maintaining the snackbar's placement at the bottom of the screen. The @keyframes spill animation embodies the spell-casting effect as the snackbar smoothly floats into view.
Interactivity adds another layer of enchantment. A simple event listener on the "Undo" button evokes a playful alert message when clicked:
document.querySelector('.undo-spell').addEventListener('click', () => {
alert('Spell Reversed!');
});
This code snippet captures the button click event and triggers an alert message. The alert function, though basic, enhances the magic by humorously "reversing the spell."
Every interactive component should include accessibility considerations to enhance user experience. We use the aria-label attribute on the button to clearly define its purpose to assistive technologies. To accommodate users with motion sensitivity, we've incorporated a media query:
@media (prefers-reduced-motion: reduce) {
.magic-spill-snackbar {
animation: none;
transform: translateY(0);
}
}
This ensures that users who prefer minimal motion experience the component effortlessly, without the default animation.
In this tutorial, we transformed a simple snackbar notification into an entertaining, magical UI element through subtle animations and thoughtful styling. Our enchanted "Magic Spill Snackbar" demonstrates how creativity and usability can unite to form a captivating user experience.
Continue exploring and experimenting with CSS animations and accessibility features to make your UI creations even more enchanting!
<div class="alchemy-container">
<div class="magic-spill-snackbar" role="alert" aria-live="assertive">
<span class="chant-text">Potion Brewed!</span>
<button class="undo-spell" aria-label="Undo action">Undo</button>
</div>
</div>
<script>
document.querySelector('.undo-spell').addEventListener('click', () => {
alert('Spell Reversed!');
});
</script>
<style>
.alchemy-container {
position: fixed;
bottom: 20px;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.magic-spill-snackbar {
background: rgba(75, 0, 130, 0.8);
color: white;
border-radius: 20px;
padding: 10px 20px;
font-size: 16px;
display: flex;
align-items: center;
box-shadow: 0 6px 9px rgba(0,0,0,0.12);
transform: translateY(100%);
animation: spill 1s ease-out forwards;
}
@keyframes spill {
to {
transform: translateY(0);
}
}
.undo-spell {
margin-left: 20px;
background: rgba(238,130,238,0.95);
border: none;
border-radius: 50px;
padding: 8px 16px;
color: white;
cursor: pointer;
transition: background 0.3s;
}
.undo-spell:hover {
background: rgba(255,192,203,0.95);
}
@media (prefers-reduced-motion: reduce) {
.magic-spill-snackbar {
animation: none;
transform: translateY(0);
}
}
</style>
Thank you for reading this article.
Comments
No comments yet. Be the first to comment!