Accessible Toast Popup With JavaScript And CSS

Category: Image | December 18, 2020
Author:Ryan Trimble
Views Total:315 views
Official Page:Go to website
Last Update:December 18, 2020
License:MIT

Preview:

Accessible Toast Popup With JavaScript And CSS

Description:

A simple JavaScript & CSS solution to help you create accessible, pretty cool, toast-like notification popups on the page.

How to use it:

1. Insert predefined toast notifications to the toast container.

<section class="toast__container">
  <div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
    <div class="toast__icon">
      <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="info-circle" class="svg-inline--fa fa-info-circle fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
        <path fill="currentColor" d="M256 8C119.043 8 8 119.083 8 256c0 136.997 111.043 248 248 248s248-111.003 248-248C504 119.083 392.957 8 256 8zm0 110c23.196 0 42 18.804 42 42s-18.804 42-42 42-42-18.804-42-42 18.804-42 42-42zm56 254c0 6.627-5.373 12-12 12h-88c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h12v-64h-12c-6.627 0-12-5.373-12-12v-24c0-6.627 5.373-12 12-12h64c6.627 0 12 5.373 12 12v100h12c6.627 0 12 5.373 12 12v24z"></path>
      </svg>
    </div>
    <div class="toast__content">
      <h2>Information</h2>
      <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Labore cumque nesciunt quod, eius error officiis aut excepturi repellat asperiores cum, neque eveniet voluptatum. Quae necessitatibus velit dolore quidem facere repellat!</p>
    </div>
  </div>
</section>

2. The necessary styles for the toast popup.

.toast {
  cursor: pointer;
  box-sizing: border-box;
  display: none;
  width: 100%;
  max-width: 640px;
  font-size: 0.825em;
  border-top-right-radius: 5px;
  border-top-left-radius: 5px;
  background: #ffffff;
  box-shadow: 0 2.8px 2.2px rgba(0, 0, 0, 0.02), 0 6.7px 5.3px rgba(0, 0, 0, 0.028), 0 12.5px 10px rgba(0, 0, 0, 0.035), 0 22.3px 17.9px rgba(0, 0, 0, 0.042), 0 41.8px 33.4px rgba(0, 0, 0, 0.05), 0 100px 80px rgba(0, 0, 0, 0.07);
  -webkit-transition: 0.2s ease-in;
  transition: 0.2s ease-in;
}
@media (min-width: 640px) {
  .toast {
    border-radius: 5px;
    margin-bottom: 0.5em;
  }
}
.toast--active {
  display: -webkit-box;
  display: flex;
  -webkit-animation: slidein--bottom 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55) forwards;
          animation: slidein--bottom 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55) forwards;
}
.toast__container {
  box-sizing: border-box;
  padding: 0em 1em;
  position: fixed;
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  display: -webkit-box;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
          flex-flow: column;
  bottom: 0;
  left: 0;
  right: 0;
  -webkit-box-align: center;
          align-items: center;
  -webkit-box-pack: center;
          justify-content: center;
}
@media (min-width: 640px) {
  .toast__container {
    padding: 0 1em;
  }
}
@media (min-width: 1024px) {
  .toast__container {
    left: initial;
    right: 0;
  }
}
.toast__icon {
  height: 60px;
  width: 60px;
  box-sizing: border-box;
  padding: 1em;
  display: none;
  -webkit-box-align: center;
          align-items: center;
  -webkit-box-pack: center;
          justify-content: center;
}
.toast__icon svg {
  height: 100%;
}
@media (min-width: 640px) {
  .toast__icon {
    display: -webkit-box;
    display: flex;
  }
}
.toast__icon ~ .toast__content {
  padding: 1em;
}
@media (min-width: 640px) {
  .toast__icon ~ .toast__content {
    padding: 1em 1em 1em 0;
  }
}
.toast__content {
  box-sizing: border-box;
  padding: 1em;
}
.toast__content h2 {
  margin: 0 0 0.25em 0;
  padding: 0;
  font-size: 1.2em;
}
.toast__content p {
  margin: 0;
  padding: 0;
  font-size: 1em;
}
@-webkit-keyframes slidein--bottom {
  0% {
    opacity: 0;
    -webkit-transform: translateY(100%);
            transform: translateY(100%);
  }
  100% {
    opacity: 1;
    -webkit-transform: translateY(0);
            transform: translateY(0);
  }
}
@keyframes slidein--bottom {
  0% {
    opacity: 0;
    -webkit-transform: translateY(100%);
            transform: translateY(100%);
  }
  100% {
    opacity: 1;
    -webkit-transform: translateY(0);
            transform: translateY(0);
  }
}

3. Create a trigger to display the toast notification.

<button class="toast__trigger">Info Toast</button>

4. Enable the trigger button to toggle the toast notification.

const toasts = document.querySelectorAll(".toast");
const toastTriggers = document.querySelectorAll(".toast__trigger");
toastTriggers.forEach((trigger, index) => {
  let toastTimeout;
  trigger.addEventListener("click", () => {
    toasts[index].classList.add("toast--active");
    toastTimeout = setTimeout(() => {
      toasts[index].classList.remove("toast--active");
    }, 3500);
  });
  toasts[index].addEventListener("click", () => {
    toasts[index].classList.remove("toast--active");
    clearTimeout(toastTimeout);
  });
});

5. Create your own notification types:

<div class="toast toast--warning" role="alert" aria-live="assertive" aria-atomic="true">
  <div class="toast__icon">
    <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="exclamation-circle" class="svg-inline--fa fa-exclamation-circle fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
      <path fill="currentColor" d="M504 256c0 136.997-111.043 248-248 248S8 392.997 8 256C8 119.083 119.043 8 256 8s248 111.083 248 248zm-248 50c-25.405 0-46 20.595-46 46s20.595 46 46 46 46-20.595 46-46-20.595-46-46-46zm-43.673-165.346l7.418 136c.347 6.364 5.609 11.346 11.982 11.346h48.546c6.373 0 11.635-4.982 11.982-11.346l7.418-136c.375-6.874-5.098-12.654-11.982-12.654h-63.383c-6.884 0-12.356 5.78-11.981 12.654z"></path>
    </svg>
  </div>
  <div class="toast__content">
    <h2>Danger!</h2>
    <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Labore cumque nesciunt quod, eius error officiis aut excepturi repellat asperiores cum, neque eveniet voluptatum. Quae necessitatibus velit dolore quidem facere repellat!</p>
  </div>
</div>
<div class="toast toast--success" role="alert" aria-live="assertive" aria-atomic="true">
  <div class="toast__icon">
    <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="check-circle" class="svg-inline--fa fa-check-circle fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
      <path fill="currentColor" d="M504 256c0 136.967-111.033 248-248 248S8 392.967 8 256 119.033 8 256 8s248 111.033 248 248zM227.314 387.314l184-184c6.248-6.248 6.248-16.379 0-22.627l-22.627-22.627c-6.248-6.249-16.379-6.249-22.628 0L216 308.118l-70.059-70.059c-6.248-6.248-16.379-6.248-22.628 0l-22.627 22.627c-6.248 6.248-6.248 16.379 0 22.627l104 104c6.249 6.249 16.379 6.249 22.628.001z"></path>
    </svg>
  </div>
  <div class="toast__content">
    <h2>All Clear</h2>
    <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Labore cumque nesciunt quod, eius error officiis aut excepturi repellat asperiores cum, neque eveniet voluptatum. Quae necessitatibus velit dolore quidem facere repellat!</p>
  </div>
</div>
.toast--warning {
  background: #bf360c;
  color: white;
}
.toast--success {
  background: #43a047;
  color: white;
}

You Might Be Interested In:


Leave a Reply