Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <el-toast></el-toast>

<template id="elToastItem">
 <div class="el-toast__icon"></div>
<div class="el-toast__close">
  <button>
    <i class="fa fa-fw fa-times"></i>
  </button>
</div>
<div class="el-toast__description"></div>
<div class="el-toast__action"></div>
<div class="el-toast__progress">
  <div class="el-toast__bar" style="width: 100%;"></div>
</div>
</template>

<template id="elToast">
 <ul class="el-toast"></ul>
</template>


<!-- Example -->

<!--- ul class="el-toast el-toast--example">
 <li class="el-toast__item el-toast__item--info">
  <div class="el-toast__icon"><i class="fa fa-exclamation-circle"></i></div>
  <div class="el-toast__close">
   <button><i class="fa fa-fw fa-times"></i></button>
  </div>
  <div class="el-toast__description">What is going to happen?</div>
  <div class="el-toast__action">
   <button>OK</button>
   <button>Something wonderful.</button>
  </div>
  <div class="el-toast__progress">
   <div class="el-toast__bar" style="width: 80%;"></div>
  </div>
 </li>
</ul -->
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700");

body,
html {
 font-family: 'Open Sans', sans-serif;
 font-size: 10px;
 background: linear-gradient(90deg, #e3ffe7 0%, #d9e7ff 100%);
 height: 100vh;
}

/*
** Component
*/

$root: ".el-toast";
$transition-default: cubic-bezier(0.5, 0, 0.1, 1);
$transition-in: cubic-bezier(0, 0, 0.25, 1);
$transition-out: cubic-bezier(0.25, 0, 1, 1);

#{$root} {
 align-items: center;
 bottom: 0;
 display: flex;
 flex-direction: column;
 list-style: none;
 margin: 16px;
 opacity: 1;
 padding: 0 0 10px;
 position: fixed;
 right: 0;
 transition: opacity 150ms $transition-default;
 width: 350px;
 
 &--paused {
  opacity: 0.9;
 }

 button {
  &,
  &:focus,
  &:hover {
   background-color: transparent;
   border: 0;
   cursor: pointer;
   outline: none;
  }
 }

 &__item:not(:first-child) {
  margin-top: 8px;
 }

 &__item:nth-child(n + 4) {
  bottom: 0;
  max-height: 40px;
  position: absolute;

  #{$root}__icon,
  #{$root}__progress,
  #{$root}__description,
  #{$root}__action {
   opacity: 0;
  }
 }

 &__item:nth-child(n + 6) {
  opacity: 0;
  transform: translateY(0) scale(0.85);
  z-index: 1970;
 }

 &__item:nth-child(4) {
  background-color: #595959;
  transform: translateY(-4px) scale(0.95);
  z-index: 1990;
 }

 &__item:nth-child(5) {
  background-color: #767676;
  transform: translateY(0) scale(0.9);
  z-index: 1980;
 }

 &__item {
  animation: append-in 300ms $transition-in forwards;
  background-color: #222222;
  border-radius: 2px;
  color: #fff;
  display: grid;
  grid-template: [header] "icon description close"auto [body] "icon action close"auto [footer] "progress progress progress"auto / 38px 1fr 38px;
  max-height: 400px;
  overflow: hidden;
  position: relative;
  transform-origin: center bottom;
  transition: transform 300ms $transition-in, background 300ms $transition-in,
   opacity 300ms $transition-in, max-height 300ms $transition-in;
  user-select: none;
  width: 100%;
  z-index: 2000;

  @keyframes append-in {
   from {
    max-height: 0;
    opacity: 0;
    transform: scale(0.9);
   }
  }

  @keyframes append-out {
   to {
    max-height: 0;
    opacity: 0;
    transform: scale(0.9);
   }
  }

  &--danger {
   #{$root} {
    &__action,
    &__description,
    &__icon {
     button,
     a, i {
      color: 	#DF6962;
     }
    }

    &__bar {
     background-color: #D64038;
    }
   }
  }

  &--alert {
   #{$root} {
    &__action,
    &__description,
    &__icon {
     button,
     a, i {
      color: #F3CC6B;
     }
    }

    &__bar {
     background-color: #EFBC3C;
    }
   }
  }

  &--success {
   #{$root} {
    &__action,
    &__description,
    &__icon {
     button,
     a, i {
      color: #54A668;
     }
    }

    &__bar {
     background-color: #54A668;
    }
   }
  }

  &--info {
   #{$root} {
    &__action,
    &__description,
    &__icon {
     button,
     a, i {
      color: #71C3DD;
     }
    }

    &__bar {
     background-color: #48B1D3;
    }
   }
  }

  &--removing {
   animation: append-out 300ms $transition-out forwards;
  }
 }

 &__icon {
  display: flex;
  font-size: 1.4rem;
  grid-area: icon;
  justify-content: center;
  padding: 12px 8px 12px 12px;
 }

 &__description {
  font-size: 1.3rem;
  grid-area: description;
  line-height: 1.5;
  margin: 10px 0;
  max-height: 200px;
  overflow-x: auto;
  padding: 0 8px;

  button {
   &:hover,
   &:focus {
    text-decoration: underline;
   }
  }
 }

 &__close {
  display: flex;
  font-size: 1.4rem;
  grid-area: close;
  justify-content: center;

  button {
   color: #fff;
   display: block;
   height: 38px;
   padding: 10px 8px 8px;
   text-align: center;
   width: 100%;
  }
 }

 &__action {
  grid-column: action;

  button,
  a {
   display: inline-block;
   font-size: 1.3rem;
   padding: 0 8px 10px;
   text-transform: uppercase;

   &:hover,
   &:focus {
    text-decoration: underline;
   }
  }
 }

 &__progress {
  background-color: transparentize(#E1E3E6, 0.85);
  grid-column: progress;
  height: 4px;
  width: 100%;
  z-index: 20;

  &--hide {
   display: none;
  }
 }

 &__bar {
  bottom: 0;
  display: block;
  height: 4px;
  position: absolute;
  transition: width 100ms $transition-default;
  width: 100%;
  z-index: 10;
 }

 // Ignore
 
 &--example {
  left: calc(50% - 175px);
  right: 0;
  top: calc(50% - 33px);
 }
}

@media only screen and (max-width: 720px) {
 #{$root} {
  margin: 16px 0;
  max-height: 400px;
  padding: 10px 0px;
  width: 100%;
  &__item {
   width: calc(100% - 30px);
  }
  &__item:not(:first-child) {
   margin-top: 8px;
  }

  &__item:nth-child(n + 2) {
   bottom: 0;
   max-height: 40px;
   position: absolute;

   #{$root}__icon,
   #{$root}__progress,
   #{$root}__description,
   #{$root}__action {
    opacity: 0;
   }
  }

  &__item:nth-child(n + 4) {
   opacity: 0;
   transform: translateY(0) scale(0.9);
   z-index: 1970;
  }

  &__item:nth-child(2) {
   background-color: #595959;
   transform: translateY(-4px) scale(0.95);
   width: calc(100% - 60px);
   z-index: 1990;
  }

  &__item:nth-child(3) {
   background-color: #767676;
   transform: translateY(0) scale(0.9);
   width: calc(100% - 60px);
   z-index: 1980;
  }

 // Ignore
 
 &--example {
  left: 0;
  top: calc(50% - 33px);
  }
 }
}

              
            
!

JS

              
                const NAME_ITEM = "el-toast",
 ICONS = {
  SUCCESS: {
   CLASSES: ["fa", "fa-check-circle"]
  },
  DANGER: {
   CLASSES: ["fa", "fa-exclamation-triangle"]
  },
  INFO: {
   CLASSES: ["fa", "fa-exclamation-circle"]
  },
  ALERT: {
   CLASSES: ["fa", "fa-exclamation-triangle"]
  }
 },
 MOBILE_RESOLUTION = 720;

let $templateItem = document.querySelector("#elToastItem");
$templateItem = $templateItem.cloneNode(true).content;

class ToastItem extends HTMLLIElement {
 constructor() {
  super();

  this._eventOnRemove =
   new CustomEvent("TOAST_REMOVED") || document.createEvent("TOAST_REMOVED");

  this.isVisible = false;
 }

 connectedCallback() {
  let type = this.getAttribute("type").toUpperCase(),
   description = this.getAttribute("description"),
   timeout = Number(this.getAttribute("timeout")),
   hideClose = Boolean(JSON.parse(this.getAttribute("hide-close")));

  if (!ICONS.hasOwnProperty(type)) {
   throw new Error(`[${NAME_ITEM}] Tipo inválido de toast`);
  }

  let $item = $templateItem.cloneNode(true),
   $itemDescription = $item.querySelector(`.${NAME_ITEM}__description`),
   $itemIcon = $item.querySelector(`.${NAME_ITEM}__icon`),
   $icon = document.createElement("i"),
   $itemProgress = $item.querySelector(`.${NAME_ITEM}__progress`),
   $itemProgressBar = $item.querySelector(`.${NAME_ITEM}__bar`),
   $itemClose = $item.querySelector(`.${NAME_ITEM}__close`),
   $itemCloseAction = $item.querySelector(`.${NAME_ITEM}__close button`),
   $container = this.parentElement.parentElement;

  this.addEventListener("TOAST_REMOVED", this.checkVisible, false);

  if (hideClose) {
   $itemClose.remove();
  } else {
   $itemCloseAction.addEventListener("click", event =>
    $container.remove(this.id)
   );
  }

  $icon.classList.add(...ICONS[type].CLASSES);

  $itemIcon.appendChild($icon);

  this.classList.add(`${NAME_ITEM}__item`);
  this.classList.add(`${NAME_ITEM}__item--${type.toLowerCase()}`);

  if (description.length > 100) {
   setShortDescription();
  } else {
   setFullDescription();
  }

  function setFullDescription(event) {
   $itemDescription.innerText = description;

   if (!event) return;

   let viewLess = document.createElement("button");
   viewLess.innerText = "View less";
   viewLess.addEventListener("click", setShortDescription);

   $itemDescription.appendChild(viewLess);
  }

  function setShortDescription() {
   let viewMore = document.createElement("button");
   viewMore.innerText = "View more";
   viewMore.addEventListener("click", setFullDescription);

   $itemDescription.innerText = description.slice(0, 99) + "...";
   $itemDescription.appendChild(viewMore);
  }

  this.appendChild($item);

  this.checkVisible();

  if (timeout === 0) {
   $itemProgress.classList.add(`${NAME_ITEM}__progress--hide`);
   return;
  }

  let countdownValue = 100,
   size = timeout - countdownValue;

  let checking = setInterval(() => {
   if (this.isVisible && !$container.isMouseOver()) {
    $itemProgressBar.style.width =
     size * 100 / (timeout - countdownValue) + "%";

    size -= countdownValue;
   }

   if (size < -countdownValue) {
    $container.remove(this.id);

    clearInterval(checking);
   }
  }, countdownValue);
 }

 checkVisible() {
  if (this.parentNode) {
   let numCards = document.body.clientWidth > MOBILE_RESOLUTION ? 3 : 1;
   this.isVisible =
    Array.from(this.parentNode.children).indexOf(this) < numCards;
  }
 }

 remove() {
  let parent = this.parentElement;
  this.classList.add(`${NAME_ITEM}__item--removing`);
  if (
   !parent ||
   parent.nodeType !== Node.ELEMENT_NODE ||
   !this ||
   this.nodeType !== Node.ELEMENT_NODE
  )
   return false;

  setTimeout(() => {
   if (parent.hasChildNodes() && parent.contains(this)) {
    parent.removeChild(this);

    parent.parentElement.dispatchEvent(this._eventOnRemove);

    return true;
   }
   return false;
  }, 300);
 }

 setOnClose(action) {
  let $itemCloseAction = this.querySelector(`.${NAME_ITEM}__close button`);

  $itemCloseAction.addEventListener("click", action);
 }

 setActions(actions) {
  if (!actions.length) {
   return;
  }

  let $itemActions = this.querySelector(`.${NAME_ITEM}__action`);

  actions.map(action => {
   if (typeof action !== "object") {
    throw new Error(`[${NAME_ITEM}] Ação inválida`);
    return;
   }

   let $action = document.createElement("button");

   if (typeof action.title !== "string") {
    throw new Error(`[${NAME_ITEM}] Informe um título válido para a ação`);
   }

   $action.innerText = action.title;

   if (typeof action.onClick !== "function") {
    throw new Error(`[${NAME_ITEM}] Informe um função válida para a ação`);
   }

   $action.addEventListener("click", action.onClick);

   $itemActions.appendChild($action);
  });
 }

 static is() {
  return `${NAME_ITEM}-item`;
 }
}

customElements.define(ToastItem.is(), ToastItem, { extends: "li" });

const NAME = "el-toast",
 TIMEOUT_REMOVE = 8000,
 DOM_LIMIT = 5;

let $template = document.querySelector("#elToast");
$template = $template.cloneNode(true).content.querySelector(`.${NAME}`);

class Toast extends HTMLElement {
 constructor() {
  super();

  this._eventOnRemove =
   new CustomEvent("TOAST_REMOVED") || document.createEvent("TOAST_REMOVED");

  this._mouseOver = false;

  this._list = {};

  this._listCache = {};
 }

 isMouseOver() {
  return this._mouseOver;
 }

 setMouseOver(value) {
  this._mouseOver = Boolean(value);
 }

 connectedCallback() {
  this.appendChild($template);

  this.addEventListener("TOAST_REMOVED", broadcastToastRemoved, false);
  this.addEventListener("mouseenter", handleMouseOver);
  this.addEventListener("touchstart", handleMouseOver);
  this.addEventListener("touchend", handleMouseOut);
  document.body.addEventListener("touchend", handleMouseOut);
  this.addEventListener("mouseleave", handleMouseOut);

  function handleMouseOver() {
   $template.classList.add(`${NAME}--paused`);
   this.setMouseOver(true);
  }

  function handleMouseOut() {
   $template.classList.remove(`${NAME}--paused`);
   this.setMouseOver(false);
  }

  function broadcastToastRemoved() {
   Object.values(this._list)
    .slice(0, 5)
    .map(toast => {
     toast.dispatchEvent(this._eventOnRemove);
    });
  }
 }

 publish(config) {
  let type = String(config.type || "info"),
   description = String(config.description || ""),
   timeout =
    config.timeout < 0 || typeof config.timeout !== "number"
     ? TIMEOUT_REMOVE
     : config.timeout,
   onClose =
    typeof config.onClose === "function" ? config.onClose : function() {},
   hideClose = Boolean(config.hideClose),
   actions = typeof config.actions === "object" ? config.actions : [],
   $toast = document.createElement("li", { is: `${NAME}-item` }),
   id = "toast_" + generateId();

  if (!description) {
   throw new Error(`[${NAME}] É necessário informar uma descrição`);
  }

  $toast.id = id;
  $toast.setAttribute("type", type);
  $toast.setAttribute("description", description);
  $toast.setAttribute("timeout", timeout);
  $toast.setAttribute("hide-close", hideClose);

  $toast = $toast.cloneNode(true);

  if (Object.values(this._list).length > DOM_LIMIT) {
   this._listCache[id] = {
    el: $toast,
    hideClose: hideClose,
    onClose: onClose,
    actions: actions
   };
  } else {
   $toast = $template.appendChild($toast);

   if (!$toast) {
    return;
   }
   if (!hideClose) {
    $toast.setOnClose(onClose);
   }

   if (actions.length) {
    $toast.setActions(actions);
   }

   this._list[id] = $toast;
  }

  function generateId() {
   return Math.random()
    .toString(36)
    .substr(2, 9);
  }

  return id;
 }
 remove(id) {
  if (this._list.hasOwnProperty(id)) {
   this._list[id].remove();

   delete this._list[id];
  } else if (this._listCache.hasOwnProperty(id)) {
   delete this._listCache[id];
  } else {
   return;
  }

  if (Object.keys(this._listCache).length) {
   let keyCached = Object.keys(this._listCache)[0],
    valueCached = Object.values(this._listCache)[0];

   valueCached.el = $template.appendChild(valueCached.el);

   if (!valueCached.hideClose) {
    valueCached.el.setOnClose(valueCached.onClose);
   }
   
   if (valueCached.actions.length) {
    valueCached.el.setActions(valueCached.actions);
   }

   this._list[keyCached] = valueCached.el;

   delete this._listCache[keyCached];
  }
 }
 static is() {
  return NAME;
 }
}

customElements.define(Toast.is(), Toast);

let toast = document.querySelector("el-toast");

for (let i = 0; i < 100; i++) {
 toast.publish({
  type: "success",
  description:
   i +
   " Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
  timeout: 8000
 });
 toast.publish({
  type: "danger",
  description:
   i +
   " Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
  timeout: 8000
 });
 toast.publish({
  type: "info",
  description: i + " Lorem Ipsum is simply.",
  timeout: 0,
  actions: [
   {
    title: "OK",
    onClick: function() {
     alert("OK");
    }
   }
  ]
 });
 let idToast = undefined;
 idToast = toast.publish({
  type: "info",
  description:
   i +
   " Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
  timeout: 0,
  hideClose: true,
  actions: [
   {
    title: "OK",
    onClick: function() {
     toast.remove(idToast);
    }
   }
  ]
 });
 toast.publish({
  type: "info",
  description: i + " Lorem Ipsum is simply.",
  timeout: 20000
 });
 toast.publish({
  type: "info",
  description: i + " Lorem Ipsum is simply.",
  timeout: 20000,
  actions: [
   {
    title: "Try again",
    onClick: function() {
     alert("Try again");
    }
   },
   {
    title: "Ignore",
    onClick: function() {
     alert("Ignore");
    }
   },
   {
    title: "Custom",
    onClick: function() {
     alert("Custom Button");
    }
   }
  ]
 });
 toast.publish({
  type: "alert",
  description:
   i +
   " Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
 });
 toast.publish({
  type: "alert",
  description:
   i +
   " Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book"
 });
}

              
            
!
999px

Console