Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                <div class="toast-fixed">
  <div id="js-toast-container" class=""></div>
</div>

<script id="js-api-toast-template" type="text/template">
        <div class="toast <%= type ? 'toast--' + type : '' %> <%= dismissable ? 'toast--dismiss' : '' %>" title="<%= dismissable ? 'Click to dismiss' : '' %>">
            <p class="toast__text"><%= text %></p>
            <% _.each(buttons, function(button, i) { %>
                <a class="toast__btn js-toast-btn" href="#" data-index="<%= i %>"><%= button.text %></a>
            <% }); %>
    </div>
</script>

<button class="btn js-add-notification">Add Success</button><br/>
<button class="btn js-add-alert">Add Alert</button><br/>
<button class="btn js-add-error">Add Error</button><br/>
<button class="btn js-add-btn">Add Toast w/ Button</button>
              
            
!

CSS

              
                @color-default: #424242;
@color-alert: #ff9;
@color-success: #2FB456;
@color-error: #de1c25;
@color-text-dark: #333;
@color-text-light: #fff;

html {
    font-size: 62.5%;
}

body {
    background: #283048; /* fallback for old browsers */
    background: -webkit-linear-gradient(to left, #283048 , #859398); /* Chrome 10-25, Safari 5.1-6 */
    background: linear-gradient(to left, #283048 , #859398); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
     
    font-size: 15px;
    font-size: 1.5rem;
    line-height: 180%;
    padding: 1.5rem;
}

@keyframes fadeInExpand {
    0% {
        opacity: 0;
    }

    35% {
        opacity: 1;
        transform: scale(1.25);
    }

    100% {
        opacity: 1;
        transform: scale(1);
    }
}

.btn {
    background: #333;
    border: 1px solid #fff;
    color: #fff;
    display: inline-block;
    font-size: 1.5rem;
    line-height: normal;
    margin-bottom: 1.5rem;
    padding: 1.5rem 3rem;
    transition: all 200ms;
    text-transform: uppercase;

    &:hover,
    &:focus,
    &:active {
        background: #000;
        color: #fff;
    }
}

.toast-fixed {
    max-width: 30rem;
    position: fixed;
    right: 1.5rem;
    top: 1.5rem;
    z-index: 120;
    width: 100%;
}

.toast {
    background: fade(@color-default, 95%);
    animation: fadeInExpand 150ms;
    box-shadow: 0 0 2px 1px rgba(0, 0, 0, 0.3);
    margin-bottom: 0.5rem;
    padding: 0.8rem;
}

.toast--alert {
    background: fade(@color-alert, 95%);
}

.toast--error {
    background: fade(@color-error, 95%);
}

.toast--success {
    background: fade(@color-success, 95%);
}

.toast--dismiss {
    cursor: pointer;
    position: relative;

    &:before,
    &:after {
        background: @color-text-light;
        content: '';
        height: 1rem;
        position: absolute;
        opacity: 0.8;
        right: 0.8rem;
        top: 0.5rem;
        width: 0.2rem;
    }

    &:before {
        transform: rotate(45deg);
    }

    &:after {
        transform: rotate(-45deg);
    }
}

.toast--alert.toast--dismiss {
    &:before,
    &:after {
        background: @color-text-dark;
    }
}

.toast__text {
    color: @color-text-light;
    line-height: 140%;
    margin: 0;
    padding-left: 0.8rem;
    padding-right: 0.8rem;
    text-align: left;
}

.toast--alert .toast__text {
    color: @color-text-dark;
}

.toast__btn {
    background: #fff;
    border: 1px solid @color-default;
    border-radius: 3px;
    color: @color-default;
    display: inline-block;
    font-size: 1.4rem;
    line-height: 1;
    margin: 0.8rem 0 0.4rem 0.8rem;
    padding: 0.6rem 1.2rem;
    text-decoration: none;
    text-transform: uppercase;

    &:hover {
        opacity: 0.8;
    }
}
              
            
!

JS

              
                // Requires underscore for templating

// Make toast available everywhere
window.globalToast = {

    $container: $('#js-toast-container'),

    add: function (model) {
        var toast = new ToastView(model);

        this.$container.append(toast.render().$el);

        return toast;
    }

};

// define the types of toast available
window.ToastTypes = {
    ERROR: 'error',
    ALERT: 'alert',
    SUCCESS: 'success'
};

// Toast class logic
function ToastView(m) {

    // default settings
    this.model = $.extend({
        type: '',
        text: '',
        timeout: 10000,
        dismissable: true,
        buttons: [],
        close: function () { }
    }, m);

    this.$el = $('<div/>');

    // render template with model settings
    this.render = function () {
        var self = this;

        this.$el.html(this.template(this.model));

        // if timeout, set a timer to close toast automatically
        if (this.model.timeout) {
            this.timeout = setTimeout(function () {
                self.close();
            }, this.model.timeout);
        }
        else this.model.dismissable = true;

        // close toast on click
        if (this.model.dismissable) {
            this.$el.one('click', function () {
                self.close();
            });
        }

        // if buttons are available
        // add listeners
        if (this.model.buttons.length) {
            this.$el.on('click', '.js-toast-btn', function (e) {
                e.preventDefault();
                e.stopPropagation(); // stop toast from closing

                var index = +this.getAttribute('data-index');

                if (self.model.buttons[index].callback) self.model.buttons[index].callback.apply(self);
            });
        }

        return this;
    };
    
    this.close = function () {
        var self = this;

        this.model.close();
        window.clearTimeout(this.timeout);
        this.$el.slideUp(function () {
            self.destroy();
        });
    };

    // clear listeners
    this.destroy = function () {
        this.$el.off('click').remove();
    }

}

// compile the template only once
ToastView.prototype.template = _.template($('#js-api-toast-template').html());

$(function() {
  globalToast.add({
      text: 'Click the buttons on the left to create another toast notification. This is what a default notification looks like.',
      timeout: 0
    });
  
  $('.js-add-notification').on('click', function() {
    globalToast.add({
        type: ToastTypes.SUCCESS,
      text: 'This is a success notification. Click to dismiss. Closes itself after 10 sec.'
    });
  });
    
  $('.js-add-alert').on('click', function() {
    globalToast.add({
        type: ToastTypes.ALERT,
      text: 'This is an alert. Click to dismiss. Closes itself after 10 sec.'
    });
  });
  
  $('.js-add-error').on('click', function() {
    globalToast.add({
        type: ToastTypes.ERROR,
      text: 'This is an error. Click to dismiss. Closes itself after 10 sec.'
    });
  });
    
  $('.js-add-btn').on('click', function() {
    globalToast.add({
        type: ToastTypes.SUCCESS,
      text: 'This is a notification with a button. Click to dismiss. Closes itself after 10 sec.',
        buttons: [
            {
                text: 'Click me',
                callback: function() {
                    alert('you clicked me');
                }
            }
        ]
    });
  });
});
              
            
!
999px

Console