JSFiddle - React, Tailwind, and code Playground

by alexpts

HTML

<a href='#' id='add'>add</a>

CSS

#pts_stikers {
  width: 220px;
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 900;
  font-family: Times New Roman, sans-serif;
  line-height: 22px;
}
#pts_stikers .stiker {
  border: 1px solid rgba(0, 0, 0, 0.3);
  background-color: #f9f9f9;
  border-radius: 6px;
  opacity: 0.01;
  margin-top: 30px;
  z-index: 905;
  position: relative;
  padding: 1px;
  font-size: 14px;
  white-space: normal;
  transition: 700ms ease-out;
  transition-property: opacity, margin;
  box-shadow: 2px 5px 7px rgba(0, 0, 0, 0.1);
}
#pts_stikers .stiker.removeAll {
  opacity: 0.1;
  margin-top: -60px;
  margin-bottom: 22px;
}
#pts_stikers .stiker.removeAll.show {
  margin-top: 0;
}
#pts_stikers .stiker.show {
  opacity: 0.7;
  margin-top: 10px;
  margin-bottom: 0px;
}
#pts_stikers .stiker.remove {
  opacity: 0.01 !important;
  z-index: -1;
}
#pts_stikers .stiker:hover {
  opacity: 1;
}
#pts_stikers .stiker .stiker_title {
  background-color: #004499;
  border-radius: 5px;
  font-size: 14px;
  font-weight: 400;
  line-height: 18px;
  margin: 0;
  padding: 5px 10px;
  color: #fff;
  cursor: pointer;
  text-shadow: 1px 1px 0 #000;
}
#pts_stikers .stiker .msg {
  padding: 8px 10px;
  margin: 0;
}
#pts_stikers .stiker_success .stiker_title {
  background-color: #446622;
}
#pts_stikers .stiker_error .stiker_title {
  background-color: #751100;
}

JavaScript

(function($) {
    'use strict';

    
    var NotificationCollection = function(){
        var count = 0;
        var $view = $('<div id="pts_stikers"><div class="stiker removeAll"><div class="stiker_title">Remove all</div></div></div>');

        var addWrapper = function(){
            $('body').append($view);
        };

        var removeWrapper = function(){
            $view.remove();
        };


        /**
         * @param {Notification} notification
         */
        var add = function(notification){
            if (!count++) {
                addWrapper();
            }
            if (count > 1) {
                $view.find('.removeAll').addClass('show');
            }
            $view.append(notification.createView());
            setTimeout(notification.show, 0); // add to end event lopp => not atomary with append

            if (notification.delay) {
                notification.timerId = setTimeout(remove, notification.delay * 1000, notification);
            }
        };

        /**
         * @param {Notification} notification
         */
        var remove = function(notification){
            hideAndRemove(notification);
            if (!--count) {
                removeWrapper();
            }
            if (count < 2) {
                $view.find('.removeAll').removeClass('show');
            }
        };

        var hideAndRemove = function(notification){
            notification.$view.removeClass('show');
            var h = notification.$view.height();
            notification.$view.css('margin-top', -h);

            setTimeout(_removeFromDom, 1000, notification);
        };

        var _removeFromDom = function(notification){
            notification.$view.remove();
        };

        return {
            add: add
        }
    }();

    var Notification = function(title, msg, type, delay){
        this.title = title;
        this.msg = msg;
        this.type = type;
        this.delay = delay;
    };

   ...