1.8.0 • Published 9 months ago

@knighttower/toast v1.8.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

x-toast.js Library Documentation

Introduction

x-toast.js is a lightweight toast notification library for JavaScript that allows you to display customizable toast messages in various styles (info, success, warning, error). The library supports custom configuration, animations, and callbacks.


CDN Integration

To use x-toast.js, include the following links in your project:

JS and CSS via Bundle:

<script src="https://cdn.jsdelivr.net/npm/knighttower@latest/packages/toast/dist/browser/bundle.js"></script>

Individual Links:

<script src="https://cdn.jsdelivr.net/npm/knighttower@latest/packages/toast/dist/browser/toast.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/knighttower@latest/packages/toast/dist/browser/style.css">

Via npm

npm i @knighttower/toast

or from the monorepo

npm i knighttower

Basic Usage

// Example toast messages:
$toast.info('This is an info message.');
$toast.success('This is a success message.');
$toast.warning('This is a warning message.');
$toast.error('This is an error message.');

Methods

1. $toast.info(message, duration)

  • Displays an info toast.
  • message (String): The message text.
  • duration (Integer, optional): Display time in milliseconds.

2. $toast.success(message, duration)

  • Displays a success toast.

3. $toast.warning(message, duration)

  • Displays a warning toast.

4. $toast.error(message, duration)

  • Displays an error toast.

5. $toast.config(options)

  • Set global configuration options.

Options:

$toast.config({
  classname: 'custom-class',
  transition: 'fade',
  position: 'top-left',
  autoClose: true,
  duration: 5000,
  progressBar: true,
  insertBefore: false,
  onShow: function(type) { console.log(`${type} toast shown`); },
  onHide: function(type) { console.log(`${type} toast hidden`); },
});

Configurations

OptionTypeDescriptionDefault Value
classnameStringAdditional class name for the toast container.'x'
transitionStringCSS transition name (fade, slide-left-right, slide-up-down, etc.).'slide-left-right'
positionStringToast position (top-right, top-left, bottom-right, bottom-left).'top-right'
durationIntegerDuration in milliseconds. 0 calculates duration based on message length.3000 (3 sec)
autoCloseBooleanEnables/disables automatic closing of the toast message.true
progressBarBooleanShows/hides the progress bar. Enabled only if autoClose is true.true
insertBeforeBooleanAdds the new toast before/after existing toasts.true
onShowFunctionCallback function executed when the toast appears.function() {}
onHideFunctionCallback function executed when the toast disappears.function() {}

Transitions

Transition NameDescription
fadeFades in/out the toast.
slide-left-rightSlides from the right.
slide-right-leftSlides from the left.
slide-up-downSlides down from the top.
slide-down-upSlides up from the bottom.

Positions

Position NameLocation in Viewport
top-rightTop-right corner
top-leftTop-left corner
bottom-rightBottom-right corner
bottom-leftBottom-left corner

Example

1. Simple Notification

$toast.success('Data saved successfully!', 4000);

2. Custom Configuration

$toast.config({
  position: 'bottom-right',
  transition: 'slide-up-down',
  duration: 5000,
  progressBar: false,
});
$toast.info('This is a custom info message.');

3. Callback Example

$toast.config({
  onShow: (type) => console.log(`Toast type: ${type} displayed`),
  onHide: (type) => console.log(`Toast type: ${type} hidden`)
});
$toast.warning('Warning with callbacks!');

CSS Customization

To customize transitions, follow the naming convention:

  • {transition-name}--init
  • {transition-name}--show
  • {transition-name}--hide

Example:

/* Custom fade transition */
.x-toast.fade--init { opacity: 0; }
.x-toast.fade--show { opacity: 1; transition: opacity 0.5s ease-in; }
.x-toast.fade--hide { opacity: 0; transition: opacity 0.5s ease-out; }

1.8.0

9 months ago

1.7.0

9 months ago

1.6.0

9 months ago

1.5.0

10 months ago

1.4.0

10 months ago

1.3.0

10 months ago

1.2.0

10 months ago

1.1.0

10 months ago