Toast JS V2.1.0-beta Documentation

A lightweight and customizable library for displaying Toast Notifications.

Developed by Soufiano Dev

Table of Contents

Class Overview

The Toast class encapsulates all the functionality required to create and manage toast notifications. It supports:

Static Properties

Duration Constants

Constant Value Description
LENGTH_SHORT 3000 Toast duration of 3 seconds.
LENGTH_LONG 6500 Toast duration of 6.5 seconds.

Position Constants

Constant Value Description
POSITION_TOP_LEFT "top-left" Position at the top-left.
POSITION_TOP_CENTER "top-center" Position at the top-center.
POSITION_TOP_RIGHT "top-right" Position at the top-right.
POSITION_BOTTOM_LEFT "bottom-left" Position at the bottom-left.
POSITION_BOTTOM_CENTER "bottom-center" Position at the bottom-center.
POSITION_BOTTOM_RIGHT "bottom-right" Position at the bottom-right.

Icon Position Constants

Constant Value Description
ICON_POSITION_START "start" Icon appears before the text.
ICON_POSITION_END "end" Icon appears after the text.

Animation Constants

Constant Value Description
FADE "fade" Fade-in and fade-out animation.
SLIDE_IN_TOP "slide-in-top" Slide-in from the top.
SLIDE_OUT_TOP "slide-out-top" Slide-out to the top.
SLIDE_IN_BOTTOM "slide-in-bottom" Slide-in from the bottom.
SLIDE_OUT_BOTTOM "slide-out-bottom" Slide-out to the bottom.
SLIDE_IN_TOP_CENTER "slide-in-top-center" Slide-in from the top center.
SLIDE_OUT_TOP_CENTER "slide-out-top-center" Slide-out to the top center.
SLIDE_IN_BOTTOM_CENTER "slide-in-bottom-center" Slide-in from the bottom center.
SLIDE_OUT_BOTTOM_CENTER "slide-out-bottom-center" Slide-out to the bottom center.
SLIDE_IN_RIGHT "slide-in-right" Slide-in from the right.
SLIDE_OUT_RIGHT "slide-out-right" Slide-out to the right.
SLIDE_IN_LEFT "slide-in-left" Slide-in from the left.
SLIDE_OUT_LEFT "slide-out-left" Slide-out to the left.
LIGHT_SPEED_IN_RIGHT "light-speed-in-right" Light-speed animation from the right.
LIGHT_SPEED_OUT_RIGHT "light-speed-out-right" Light-speed animation to the right.
LIGHT_SPEED_IN_LEFT "light-speed-in-left" Light-speed animation from the left.
LIGHT_SPEED_OUT_LEFT "light-speed-out-left" Light-speed animation to the left.
WAVE_IN "wave-in" Wave animation on entry.
WAVE_OUT "wave-out" Wave animation on exit.
WOBBLE_IN "wobble-in" Wobble animation on entry.
WOBBLE_OUT "wobble-out" Wobble animation on exit.

Text Overflow Constants

Constant Value Description
TextOverflow.ELLIPSIS "ellipsis" Text is truncated with an ellipsis (...) when it overflows.
TextOverflow.WRAP "wrap" Text wraps to the next line when it overflows.
TextOverflow.CLIP "clip" Text is clipped when it overflows.

Icon Size Constants

Constant Value Description
ICON_SIZE.SMALL { width: "20px", height: "20px" } Small icon size.
ICON_SIZE.MEDIUM { width: "24px", height: "24px" } Medium icon size.
ICON_SIZE.LARGE { width: "32px", height: "32px" } Large icon size.
ICON_SIZE.EXTRA_LARGE { width: "48px", height: "48px" } Extra large icon size.

Predefined Style Constants

Constant Value Description
STYLE_DEFAULT_1 { backgroundColor: "#323232", color: "#fff", display: "flex", boxShadow: "0 2px 4px rgba(0, 0, 0, 0.3)", borderRadius: "10px", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Default style with dark background.
STYLE_DEFAULT_2 { backgroundColor: "rgba(0, 0, 0, 0.8)", color: "#fff", display: "flex", fontFamily: "system-ui, -apple-system, sans-serif", boxShadow: "0 2px 4px rgba(0, 0, 0, 0.3)", fontSize: "14px", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Default style with dark background and system-ui font family.
STYLE_SUCCESS { backgroundColor: "#16A34A", color: "#FFFFFF", display: "flex", boxShadow: "0 6px 10px rgba(16, 185, 129, 0.2)", borderRadius: "10px", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "fit-content", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Success style with green background.
STYLE_WARNING { backgroundColor: "#FF9800", color: "#fff", display: "flex", fontFamily: "system-ui, -apple-system, sans-serif", boxShadow: "0 2px 4px rgba(0, 0, 0, 0.3)", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Warning style with orange background.
STYLE_WARNING1 { background: "linear-gradient(90deg, #f39c12, #e67e22)", color: "#fff", display: "flex", borderRadius: "10px", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Warning style with gradient background.
STYLE_WARNING2 { background: "linear-gradient(90deg, #e67e22, #f39c12)", color: "#fff", boxShadow: "0 4px 10px rgba(230, 126, 34, 0.3)", } Warning style variant 2 with amber gradient.
STYLE_WARNING3 { background: "linear-gradient(90deg, #f39c12, #e67e22)", color: "#000000", } Warning style variant 3 with dark text on amber gradient.
STYLE_ERROR { backgroundColor: "#DC2626", color: "#FFFFFF", display: "flex", boxShadow: "0 6px 10px rgba(220, 38, 38, 0.2)", borderRadius: "10px", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Error style with red background.
STYLE_ERROR1 { background: "#E74C3C", color: "#fff", display: "flex", borderRadius: "10px", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Error style with red background.
STYLE_ERROR2 { background: "linear-gradient(90deg, #f44336, #e57373)", color: "#fff", display: "flex", boxShadow: "0 4px 10px rgba(0, 0, 0, 0.2)", borderRadius: "10px", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Error style with gradient background.
STYLE_INFO { backgroundColor: "#2563EB", color: "#FFFFFF", border: "1px solid rgba(37, 99, 235, 0.3)", boxShadow: "0 6px 10px rgba(37, 99, 235, 0.2)", fontFamily: "system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif", backdropFilter: "blur(6px)", borderRadius: "10px", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Info style with blue background.
STYLE_GRADIENT { background: "linear-gradient(90deg, #ff9a9e, #fad0c4)", color: "#000", display: "flex", boxShadow: "0 5px 15px rgba(255, 154, 158, 0.3)", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Gradient style with pink background.
STYLE_NEON { background: "linear-gradient(90deg, #00d2ff, #3a7bd5)", color: "#fff", display: "flex", boxShadow: "0 0 15px #00d2ff, 0 0 30px #3a7bd5", borderRadius: "10px", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Neon style with gradient background.
STYLE_NEON1 { backgroundColor: "#0ff", color: "#000", boxShadow: "0 0 10px #0ff, 0 0 40px #0ff", fontFamily: "monospace", borderRadius: "10px", display: "flex", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Neon style with blue background.
STYLE_DARK_MODE { background: "#1f2937", color: "#fff", boxShadow: "0 5px 15px rgba(0, 0, 0, 0.3)", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Dark mode style with dark background.
STYLE_LIGHT_MODE { background: "#fff", color: "#000", boxShadow: "0 5px 15px rgba(0, 0, 0, 0.1)", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Light mode style with white background.
STYLE_SHADOW { backgroundColor: "#000000", color: "#fff", display: "flex", boxShadow: "0 8px 16px rgba(0, 0, 0, 0.2)", fontFamily: "Verdana, sans-serif", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Shadow style with white background.
STYLE_RETRO { backgroundColor: "#FCD34D", color: "#000", display: "flex", boxShadow: "0 6px 12px rgba(252, 211, 77, 0.2)", borderRadius: "10px", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Retro style with yellow background.
STYLE_METALLIC { background: "linear-gradient(145deg, #d3d3d3, #a8a8a8)", color: "#222", display: "flex", boxShadow: "inset 3px 3px 6px #999, inset -3px -3px 6px #ddd", fontFamily: "Tahoma, sans-serif", borderRadius: "10px", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Metallic style with gradient background.
STYLE_GLOW { backgroundColor: "#5A67D8", color: "#fff", boxShadow: "0 0 15px rgba(90, 103, 216, 0.8)", display: "flex", borderRadius: "10px", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Glow style with blue background.
STYLE_TRANSPARENT { backgroundColor: "rgba(0, 0, 0, 0.6)", color: "#fff", fontFamily: "system-ui, sans-serif", borderRadius: "10px", fontSize: "clamp(12px, 3vw, 14px)", padding: "8px 16px", width: "fit-content", maxWidth: "400px", height: "auto", justifyContent: "center", alignItems: "center", textAlign: "center", transition: "all 0.3s ease", WebkitTransition: "all 0.3s ease", MozTransition: "all 0.3s ease", msTransition: "all 0.3s ease", "@media (max-width: 480px)": { width: "90%", maxWidth: "none", padding: "20vh 20vh", borderRadius: "8px", }, } Transparent style with dark background.

Methods

makeText(context, text, duration)

Creates a new Toast instance with predefined text and duration.

Parameters

Returns

A new Toast instance.

Example

const toast = Toast.makeText(document.body,"Hello Toast!",Toast.LENGTH_SHORT).show();

setText(text)

Sets the text content of the toast. Used for advanced customizations after creating a toast instance.

Parameters

Returns

The Toast instance for method chaining.

Example

const toast = Toast.makeText(
document.body,
"Hello Toast!",
Toast.LENGTH_SHORT
);
toast.setText("Operation successful!")
.setStyle("success")
.setIcon("checkmark.png", Toast.ICON_SIZE.MEDIUM)
.show();

setDuration(duration)

Sets the duration of the toast.

Parameters

Returns

The Toast instance for method chaining.

Example

const toast = Toast.makeText(
document.body,
"Hello Toast!",
Toast.LENGTH_SHORT
);
toast.setDuration(5000); // 5 seconds
toast.show();

setStyle(style)

Sets the style of the toast. Can be a predefined style name or a custom style object.

Parameters

Returns

The Toast instance for method chaining.

Example

const toast = Toast.makeText(
document.body,
"Hello Toast!",
Toast.LENGTH_SHORT
);

// Apply custom styles
toast.setStyle({
backgroundColor: "#4A5568",
color: "#FFFFFF",
padding: "15px 25px",
borderRadius: "10px",
boxShadow: "0 4px 6px rgba(0, 0, 0, 0.2)",
fontSize: "16px",
fontFamily: "Arial, sans-serif",
});

toast.show();

setPosition(position)

Sets the position of the toast on the screen.

Parameters

Returns

The Toast instance for method chaining.

Example

toast.setPosition(Toast.POSITION_TOP_CENTER);

setAnimation(enterAnimation, exitAnimation)

Sets the enter and exit animations for the toast.

Parameters

Returns

The Toast instance for method chaining.

Example

toast.setAnimation(
  Toast.LIGHT_SPEED_IN_RIGHT,
  Toast.LIGHT_SPEED_OUT_RIGHT
).show();
h3>setIcon(iconPath, size, position)

Sets an icon for the toast.

Parameters

Returns

The Toast instance for method chaining.

Example

// Using a predefined icon size
toast.setIcon("https://example.com/icon.png", Toast.ICON_SIZE.SMALL).show();

// Using a custom icon size
toast.setIcon("https://example.com/icon.png", { width: "30px", height: "30px" });

setIconShape(shape)

Sets the shape of the icon (circle, square, or squircle).

Parameters

Returns

The Toast instance for method chaining.

Example

toast.setIconShape(Toast.ICON_SHAPE_SQUIRCLE).show();

setDismissible(dismissible, closeButtonColor)

Makes the toast dismissible with an optional close button color.

Parameters

Returns

The Toast instance for method chaining.

Example

toast.setDismissible(true).show();    
  // Or You Can Use 
 toast.setDismissible(true, "#000000").show();

setTextOverflow(mode)

Sets how overflowing text is handled in the toast.

Parameters

Returns

The Toast instance for method chaining.

Example

const toast = Toast.makeText(
  document.body,
  "This is a very long message that will wrap to multiple lines!",
  Toast.LENGTH_LONG
);
toast.setTextOverflow(Toast.TextOverflow.WRAP).show();

on(event, callback)

Registers a callback function for a lifecycle event.

Parameters

Returns

The Toast instance for method chaining.

Example

const toast = Toast.makeText(document.body, "Hello!", Toast.LENGTH_SHORT);
toast.on("show", () => console.log("Toast shown!"))
     .on("hide", () => console.log("Toast hidden!"))
     .on("dismiss", () => console.log("Toast dismissed by user!"))
     .show();

off(event, callback)

Removes a previously registered event callback.

Parameters

Returns

The Toast instance for method chaining.

Usage Examples

1. Create a simple toast with default settings:

const toast = Toast.makeText(
document.body,
"Hello Toast!",
Toast.LENGTH_SHORT
);
toast.show();

2. Create a success toast with custom style:

const successToast = Toast.makeText(
document.body,
"Operation Successful!",
Toast.LENGTH_LONG
);
successToast.setStyle("success").show();

3. Create an error toast with custom position:

const errorToast = Toast.makeText(
document.body,
"Error occurred!",
Toast.LENGTH_SHORT
);
errorToast.setPosition(Toast.POSITION_TOP_RIGHT)
.setStyle("error")
.show();

4. Create a toast with an icon:

const iconToast = Toast.makeText(
document.body,
"Toast with Icon!",
Toast.LENGTH_SHORT
);
iconToast.setIcon(
"https://example.com/icon.png",
Toast.ICON_SIZE.SMALL
).show();

5. Create a dismissible toast:

const dismissibleToast = Toast.makeText(
document.body,
"You can close this toast.",
Toast.LENGTH_SHORT
);
dismissibleToast.setDismissible(true, "#fff").show(); // Specifying the color remains optional 

6. Create a toast with a callback after it disappears:

const callbackToast = Toast.makeText(
document.body,
"Callback after hide!",
Toast.LENGTH_SHORT
);
callbackToast.addCallback(() => console.log("Toast dismissed!"))
.show();

7. Create a toast with custom animations:

const animatedToast = Toast.makeText(
  document.body,
  "Custom Animations!",
  Toast.LENGTH_SHORT
);
animatedToast.setAnimation(
  Toast.WAVE_IN,
  Toast.WOBBLE_OUT
).show();

8. Create a toast with an icon:

const iconToast = Toast.makeText(
      document.body,
      "Toast with Icon!",
      Toast.LENGTH_SHORT
        );
        iconToast.setIcon(
      "/path/to/icon.svg",
      Toast.ICON_SIZE.SMALL
        ).show();

9. Create a toast with a custom icon size:

const customIconToast = Toast.makeText(
      document.body,
      "Toast with Custom Icon Size!",
      Toast.LENGTH_SHORT
    );
    customIconToast.setIcon(
      "/path/to/custom-icon.png", 
      { width: "30px", height: "30px" }
    ).show();

10. Create a toast with a custom icon shape:

const shapeIconToast = Toast.makeText(
      document.body,
      "Toast with Custom Icon Shape!",
      Toast.LENGTH_SHORT
    );
    shapeIconToast.setIcon(
      "https://example.com/icon.gif", 
      Toast.ICON_SIZE.SMALL
    ).setIconShape(Toast.ICON_SHAPE_SQUIRCLE).show();

ToastManager

The Toast.Manager (also accessible as ToastManager) is a queue system that prevents multiple toasts from overlapping. It manages toast display by stacking them vertically and limiting the number of visible toasts.

Static Properties

Property Default Description
maxVisible 3 Maximum number of toasts visible at one time.

Methods

Toast.Manager.show(toast)

Shows a toast using the queue system. Use this instead of toast.show() for managed display.

Example

// Using ToastManager for queued display
Toast.Manager.show(
  Toast.makeText(document.body, "First toast", Toast.LENGTH_SHORT)
);
Toast.Manager.show(
  Toast.makeText(document.body, "Second toast", Toast.LENGTH_SHORT)
);

Changelog (v2.1.0-beta)

New Features

Improvements

Customization

You can customize the toast by: