Back

Designing Interactive and Customizable Snackbars

A step-by-step guide to implementing snackbars that are interactive, engaging, and highly customizable.

HTML

To start with designing a customizable snackbar, you'll need a simple HTML structure that includes the snackbar itself and a trigger button to display it. The basic HTML element for the snackbar is a <div> with an ID attribute set to "snackbar". This <div> will serve as the container for the notification message. Also, a <button> is needed to trigger the snackbar's display.

<div id="snackbar" class="snackbar">Sample Snackbar Notification</div>
<button onclick="showSnackbar()" class="snackbar-button">Show Snackbar</button>

The <div> element holds the text for your notification and the button initiates the function that shows the snackbar. You can customize the text within the snackbar to fit your application's needs.

JavaScript

The JavaScript function showSnackbar() handles the display logic for our snackbar.

function showSnackbar() {
  const snackbar = document.getElementById('snackbar');
  snackbar.classList.add('show');
  setTimeout(() => { snackbar.classList.remove('show'); }, 3000);
}

In this script, the snackbar is displayed by adding the show class to it, which is predefined in the CSS to make the snackbar visible. The snackbar is automatically hidden after 3000 milliseconds (or 3 seconds) using setTimeout(), which removes the show class.

CSS

The CSS styles are crucial for the snackbar's appearance and behavior. Below is a CSS snippet that provides both the design and animation for revealing and hiding the snackbar.

.snackbar {
  visibility: hidden;
  min-width: 250px;
  margin-left: -125px;
  background-color: #333;
  color: #fff;
  text-align: center;
  border-radius: 2px;
  padding: 16px;
  position: fixed;
  left: 50%;
  bottom: 30px;
  font-size: 17px;
  transition: opacity 0.5s, visibility 0.5s;
}

.snackbar.show {
  visibility: visible;
  opacity: 0.9;
}

Initially, the snackbar is invisible because the visibility property is set to hidden. Once the show class is added, visibility changes to visible and opacity is set to create a fade-in effect.

Navigating further, the button to trigger the snackbar is designed to be simple and effective:

.snackbar-button {
  padding: 10px 20px;
  font-size: 16px;
  background-color: #333;
  color: #fff;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.snackbar-button:hover {
  background-color: #444;
}

The button's style enhances the user's experience with a smooth color transition effect on hover, complementing the snackbar's modern aesthetic.

Your current setup features a sleek and simple snackbar design that can be further developed to fit the style and functionality of your web application.

Advanced Customizations

To enhance the basic snackbar you've just implemented, there are numerous ways in which customization can provide additional utility and flair:

  • Theming and Styles: Customize the color scheme of your snackbar to align with your brand’s identity. You can alter the background and text colors, as well as add shadow effects for a more pronounced appearance.
  • Duration Configuration: Make the display duration of the snackbar configurable. Introduce additional parameters in the JavaScript function allowing the developer to specify the time this alert should remain visible.
  • Positioning: Adjust the position of your snackbar. While it defaults to the bottom center, it can be placed at the top, bottom-left, or bottom-right to suit different UI layouts.
  • Accessibility Enhancements: Implement ARIA attributes to improve accessibility. Ensure that screen readers correctly announce the snackbar's content as it appears, offering support for users with disabilities.
  • Animation Complexity: Introduce advanced animations such as slide from below or rotate on entry to add more visual excitement.
  • Content Customization: Allow developers to insert HTML content within the snackbar’s <div> so that links or formatted text can be displayed.
  • Multiple Snackbar Support: Modify the current logic to support multiple snackbar instances appearing simultaneously, each possibly with its independent controls and lifecycle.

By applying these customization options, you can maximize the versatility and impact of snackbars in your web applications, ensuring both aesthetics and functionality are aligned with user expectations and business goals.

Whole code

<div id="snackbar" class="snackbar">Sample Snackbar Notification</div><button onclick="showSnackbar()" class="snackbar-button">Show Snackbar</button>

<script>
function showSnackbar() {
  const snackbar = document.getElementById('snackbar');
  snackbar.classList.add('show');
  setTimeout(() => { snackbar.classList.remove('show'); }, 3000);
}
</script>

<style>
.snackbar {
  visibility: hidden;
  min-width: 250px;
  margin-left: -125px;
  background-color: #333;
  color: #fff;
  text-align: center;
  border-radius: 2px;
  padding: 16px;
  position: fixed;
  z-index: 1;
  left: 50%;
  bottom: 30px;
  font-size: 17px;
  transition: opacity 0.5s, visibility 0.5s;
}
.snackbar.show {
  visibility: visible;
  opacity: 0.9;
}
.snackbar-button {
  padding: 10px 20px;
  font-size: 16px;
  background-color: #333;
  color: #fff;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s;
}
.snackbar-button:hover {
  background-color: #444;
}
</style>
      
Thank you for reading this article.

Comments

More snackbars

Similar

See also