Giter VIP home page Giter VIP logo

simplenotification's Introduction

SimpleNotification

SimpleNotification is a library to display simple yet customizable notifications. You can stylize text with a simple syntax, add buttons to make the notifications interactable and add callbacks for some events during the life of a notification.

SimpleNotification weighs 11.12 Kb (3.57 Kb gzipped), with no dependencies.

You can find a live demo here: https://notification.nikurasu.org/

Installation

You simply need to include simpleNotification.css, simpleNotification.js (or the minified versions) and you're ready to go!

How to use

SimpleNotification has static methods to display notifications with 5 default templates. You can call each of the templates by their name directly on SimpleNotification without instantiating it, like so: SimpleNotification.success(...).

The five templates are success, error, info, warning and message.

SimpleNotification.success({
    title: 'Title', // The title of the notification
    image: 'url', // Optional image displayed inside the notification
    text: 'Content', // Content of the notification
    // Optional list of buttons to interact with the notification
    buttons: [{
        value: 'Confirm', // The text inside the button
        type: 'success', // The type of the button, same as for the notifications
        onClick: (notification) => {
            // The onClick function receive the notification from which the button has been clicked
            // You can call notification.remove(), notification.close() or notification.closeFadeout()
            // if you wish to remove the notification by clicking on  the buttons
        }
    }]
}, options);

All keys in the first parameter are optional, but at least one is required.

You can still update the content and state of any notification after it's been created with these functions:

setType(type), setTitle(title), setImage(image), setText(text), addButton(button), removeButtons(), disableButtons()

You can use custom classes and make your own design by using SimpleNotification.custom(classes, content, options) where classes is an array of CSS classes that will be added to the body of each notification.

You can jump lines inside the notification content by using any linebreak character (\r, \n or \r\n).

Options

There are a few options that you can set by using SimpleNotification.options(object) or more specifically for a single notification on the third parameter.

Name Description Default
duration The time (in ms) that the notification is displayed. 4000
position Valid positions: top-left, top-center, top-right, bottom-left, bottom-center and bottom-right. "top-right"
sticky If true, the notification will not disappear until the user clicks it or its close button. false
closeButton If true, a close button will be added either on the title or the content. true
closeOnClick If true, clicking anywhere in the notification will close it. true
removeAllOnDisplay If true, all notifications will be cleared before the new one is added to the screen. false
maxNotifications If >0, notifications (starting with oldest) will clear out until the number displayed is less than or equal to the specified option. 0
events Object with events functions, see Events.
insertAnimation Object with CSS class name and duration, see Animations. { name: 'default-insert', duration: 250 }
removeAnimation Object with CSS class name and duration, see Animations. { name: 'fadeout', duration: 400 }
display Display the notification when creating it. true

If a notification is sticky and closeOnClick is disabled, closeButton is set to true to always have a way to close a notification.

Events

There are four events during the process of displaying every notification:

  • onCreate(notification) called when the notification node is created but empty.
  • onDisplay(notification) called when the notification node is appended to its wrapper.
  • onDeath(notification) called when the duration timer has expired.
    • If you set the onDeath function you need to call notification.close(), notification.remove() or notification.closeFadeout() or else the notification won't disappear.
  • onDisplay(notification) after the notification has been closed.

Animations

You can customize the insert and remove animations of a notification by giving a value to the insertAnimation (or removeAnimation) option. The option take an object like the following:

{
    name: "default-insert", // See the list below
    duration: 250 // In ms
}

The insert animations are: insert-[left|top|right|bottom], fadein, scalein and rotatein.

By default the animation is default-insert which is a special value that automatically choose the corresponding insert animation after the notification position.

The remove animations are: fadeout, scaleout and rotateout.

You can add your own animations by adding them in your own CSS files and setting the name of the animation as the parameter.

Markdown-like tags

You can insert links, images and stylize text by using tags that resemble Markdown. Most of these tags can be nested to combine their effects.

Name Description
Inline code ``code``
Header (h1) # Header 1\n
Header (h2) ## Header 2\n
Link {{title|http://www.example.org/}} or {{http://www.example.org/}} without title.
Image ![title|http://www.example.org/image.jpg] or ![http://www.example.org/image.jpg] without title.
Bold **http://www.example.org/**
Italic *http://www.example.org/*
Separator \n---\n
Float right >*>Text<

Tags work by looking for an open token, an optional separator if there is a title, and the close token.
If the tag can have a title you need to use | as the separator with the content.

You can add custom tags easily by adding them to SimpleNotification.tags or by using SimpleNotification.addTag(name, object).
A tag object can have the following properties:

{
    type: 'span', // The node type, e.g <span>
    class: ['class1', 'class2'], // Optional class list as an array or string to use
    attributes: {
        name: value
    }, // Optional attributes to set
    textContent: "$content", // textContent of the created node, see below for variables
                             // If textContent is defined and not false the content cannot have childs (nested other tags)
    title: false, // See "Title" below
    open: '{{', // The opening token - any length
    close: '}}' // The closing token - can be linebreak by using \n - can also be empty
}

Variables

There are two usable variables inside attribute values textContent and title:

  • $content: the content found between the open and close token, without the title if there is one.
  • $title: the title found, if there is none it is replaced by the same value as $content.

simplenotification's People

Contributors

ajmeese7 avatar glagan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

simplenotification's Issues

Issue with new notification style

Before the notifications would get pretty wide to show my message in a clean manner, like this:

old

But now they only get so wide then stop, leaving the messages extremely unattractive:

new

I don't know what changed and I have been unable to pinpoint the issue myself, so I'm going to stick with using my unsupported hacky method until this is resolved. I can upload my entire old CSS if that will help you, but I'm leaving tomorrow so I won't be able to be much help fixing it.

More Mask

Hi, @Glagan Notification is very nice and light

There is no Mask
Add Mask option

  • Add a background Mask : option

  • text: 'Content', is it possible to add comprehensive Html? For example: Content.html or Div.

Feature request: opening and closing animation options

It would be neat if there could be things like sliding in from the right, left, top, and bottom for opening then sliding out for closing. Basically something like this but with options instead of the one preset method.

It could be implemented with an openingAnimation and closingAnimation option, with things like slideInLeft and possibly an option for timings, making it an object parameter like openingAnimation= { animation: "slideInLeft", time: 400 } with a default amount like 400. The default could be fadeIn for openingAnimation and fadeOut for closingAnimation.

I have already implemented a small version of this in my project, where I replaced the code of @keyframes insert-right with the following to make it actually insert from the bottom:

from {
    transform: rotateX(-70deg);
    transform-origin: bottom;
}
to {
    transform: rotateX(0deg);
    transform-origin: bottom;
}

If there was official support for this, it would make customization much easier. This actually ties in with my next issue I will create. Don't hesitate to clarify any confusions, I know this is a lot in one issue.

Feature request: detect if a notification already exists

It would be a really useful feature if when a new notification is opened there was an option to detect if one is already present and to decide what action to take if there is. For example, you could choose to still show the new notification (default) or to close the old one as as it closes bring in the new notification.

Currently in my project if a user clicks buttons too quickly they will have more than one notification show at once and it is not attractive with how they are styled in my app. If you do not plan on working on this I will see if I can brew up my own solution in a few months when I have time.

Feature request: more notification location options

Right now I have my notifications displayed in the center of the bottom of my page, which wasn't that hard to customize myself, but it would be cool if there was a way to specify location: "bottomCenter" as an option or something similar. This is my current hacked way of getting it to display how I want:

.gn-wrapper {
    position: absolute;
    bottom: 0;
    width: 100%;
    overflow: hidden;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    z-index: 1080;
}

Depending on the layout of the page, displaying in the center from the top or the bottom might be very visually attractive. This is what a notification looks like in my application:

Coming from the bottom and being centered makes it best for my use case.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.