Toastify JS

repository·master·Indexed 25 days ago

https://github.com/apvarun/toastify-js

A lightweight, vanilla JavaScript library for creating customizable toast notifications that do not block the execution thread. Version 1.12.0 supports custom HTML nodes, flexible positioning via gravity and offset, and various configuration options for duration, styling, and interactivity.

Tokens
2.4K
Snippets
6
Records
15
Agent score
79%

What's inside toastify-js

  1. Import Toastify into your module

    master

    After installing, import the library and its default CSS into your JavaScript module. You can use the default CSS or override it with your own styles.

    import Toastify from 'toastify-js'
    import "toastify-js/src/toastify.css"
  2. Add Toastify to an HTML page using CDN

    master

    If you are not using a module bundler, you can include Toastify via jsDelivr by adding the CSS link in your <head> and the script tag at the bottom of your <body>.

    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css">
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/toastify-js"></script>
  3. Use custom HTML or DOM nodes in toasts

    master

    Instead of using the text property, you can pass a DOM node via the node property. When using node, the text and escapeMarkup properties are ignored.

    Alternatively, you can pass an HTML string to text and set escapeMarkup: false to render custom HTML content.

  4. Handle toast click events

    master

    You can make toasts interactive in two ways:

    1. Navigation: Provide a destination URL. If newWindow is true, it opens in a new tab; otherwise, it navigates the current window.
    2. Callback: Provide an onClick function. This is used if destination is not provided.

    Note: event.stopPropagation() is used internally to prevent click events from bubbling up to parent elements.

  5. Display a basic toast notification

    master

    To display a toast, call the Toastify() function with a configuration object and chain the .showToast() method.

    Toastify({
      text: "This is a toast",
      duration: 3000,
      destination: "https://github.com/apvarun/toastify-js",
      newWindow: true,
      close: true,
      gravity: "top", // `top` or `bottom`
      position: "left", // `left`, `center` or `right`
      stopOnFocus: true, // Prevents dismissing of toast on hover
      style: {
        background: "linear-gradient(to right, #00b09b, #96c93d)",
      },
      onClick: function(){}
    }).showToast();

    Note: Toast messages will be centered on devices with screen width less than 360px.

  6. Add an offset to the toast position

    master

    Use the offset object to push the toast away from its gravity/position anchor. The x and y values can be numbers (pixels) or strings (e.g., '2em').

    • If position is left, x pushes from the left.
    • If gravity is bottom, y pushes from the bottom.
    Toastify({
      text: "This is a toast with offset",
      offset: {
        x: 50,
        y: 10
      },
    }).showToast();
    Toastify({
      text: "This is a toast with offset",
      offset: {
        x: 50, // horizontal axis - can be a number or a string indicating unity. eg: '2em'
        y: 10 // vertical axis - can be a number or a string indicating unity. eg: '2em'
      },
    }).showToast();
  7. Add custom CSS classes to a toast

    master

    Use the className option to apply custom CSS classes (e.g., for 'info' or 'warning' styles). You can provide multiple classes as a space-separated string.

    Toastify({
      text: "This is a toast",
      className: "info",
      style: {
        background: "linear-gradient(to right, #00b09b, #96c93d)",
      }
    }).showToast();
  8. Configure Toastify options

    master

    The Toastify constructor accepts an options object to customize the notification. Below are the available configuration keys:

    KeyTypeDefaultDescription
    textstring'Toastify is awesome!'The message text to display.
    nodeHTMLElementundefinedA DOM node to display instead of text.
    durationnumber3000Duration in ms. Set to 0 to make it persistent.
    selectorstring or HTMLElementundefinedThe parent element/ID where toasts will be appended. Defaults to document.body.
    callbackfunctionfunction(){}Function called after the toast is removed from the DOM.
    destinationstringundefinedURL to navigate to on click.
    newWindowbooleanfalseIf true, opens destination in a new window.
    closebooleanfalseIf true, shows a close icon.
    gravitystring'toastify-top'Position: 'toastify-top' or 'toastify-bottom'.
    positionstring''Position: 'left', 'center', or 'right'. (Note: positionLeft is deprecated).
    backgroundColorstring''Background color (Deprecated: use style.background instead).
    avatarstring''URL or path to an image to display as an avatar.
    classNamestring''Additional CSS classes to add to the toast.
    stopOnFocusbooleantrueIf true, the timer pauses when the mouse is over the toast.
    onClickfunctionfunction(){}Callback function triggered when the toast is clicked (only if destination is not set).
    offset{x: number, y: number}{x: 0, y: 0}Custom offset for the toast position.
    escapeMarkupbooleantrueIf true, treats text as plain text. If false, treats it as HTML.
    ariaLivestring'polite'ARIA live region setting for screen readers.
    styleobject{background: ''}An object of CSS properties to apply directly to the toast element.
  9. Reference: Toastify configuration options

    master

    The following options can be passed to the Toastify() constructor:

    Option KeytypeUsageDefaults
    textstringMessage to be displayed in the toast"Hi there!"
    nodeELEMENT_NODEProvide a node to be mounted inside the toast. node takes higher precedence over text
    durationnumberDuration for which the toast should be displayed.<br>-1 for permanent toast3000
    selectorstring | ELEMENT_NODEShadowRootCSS Selector or Element Node on which the toast should be added
    destinationURL stringURL to which the browser should be navigated on click of the toast
    newWindowbooleanDecides whether the destination should be opened in a new window or notfalse
    closebooleanTo show the close icon or notfalse
    gravity"top" or "bottom"To show the toast from top or bottom"top"
    position"left" or "right"To show the toast on left or right"right"
    backgroundColorCSS background valueTo be deprecated, use style.background option instead. Sets the background color of the toast
    avatarURL stringImage/icon to be shown before text
    classNamestringAbility to provide custom class name for further customization
    stopOnFocusbooleanTo stop timer when hovered over the toast (Only if duration is set)true
    callbackFunctionInvoked when the toast is dismissed
    onClickFunctionInvoked when the toast is clicked
    offsetObjectAbility to add some offset to axis
    escapeMarkupbooleanToggle the default behavior of escaping HTML markuptrue
    styleobjectUse the HTML DOM Style properties to add any style directly to toast
    ariaLivestringAnnounce the toast to screen readers"polite"
    oldestFirstbooleanSet the order in which toasts are stacked in pagetrue

    Note: backgroundColor is deprecated. Use style.background instead.