Toast JS Documentation V1.0.0 (Beta)

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_TOP "slide-top" Slide-in from the top.
SLIDE_BOTTOM "slide-bottom" Slide-in from the bottom.
SLIDE_LEFT "slide-left" Slide-in from the left.
SLIDE_RIGHT "slide-right" Slide-in from the right.

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", padding: "10px 20px", borderRadius: "5px", boxShadow: "0 2px 4px rgba(0, 0, 0, 0.3)", fontSize: "14px", fontFamily: "system-ui, -apple-system, sans-serif" } Default style with dark background.
STYLE_DEFAULT_2 { backgroundColor: "rgba(0, 0, 0, 0.8)", color: "#fff", padding: "10px 20px", borderRadius: "5px", boxShadow: "0 2px 4px rgba(0, 0, 0, 0.3)", fontSize: "14px", fontFamily: "system-ui, -apple-system, sans-serif" } Default style with semi-transparent background.
STYLE_SUCCESS { backgroundColor: "#4CAF50", color: "#fff", padding: "10px 20px", borderRadius: "5px", boxShadow: "0 2px 4px rgba(0, 0, 0, 0.3)", fontSize: "14px", fontFamily: "system-ui, -apple-system, sans-serif" } Success style with green background.
STYLE_ERROR { backgroundColor: "#F44336", color: "#fff", padding: "10px 20px", borderRadius: "5px", boxShadow: "0 2px 4px rgba(0, 0, 0, 0.3)", fontSize: "14px", fontFamily: "system-ui, -apple-system, sans-serif" } Error style with red background.
STYLE_WARNING { backgroundColor: "#FF9800", color: "#fff", padding: "10px 20px", borderRadius: "5px", boxShadow: "0 2px 4px rgba(0, 0, 0, 0.3)", fontSize: "14px", fontFamily: "system-ui, -apple-system, sans-serif" } Warning style with orange background.
STYLE_INFO { backgroundColor: "#2196F3", color: "#fff", padding: "10px 20px", borderRadius: "5px", boxShadow: "0 2px 4px rgba(0, 0, 0, 0.3)", fontSize: "14px", fontFamily: "system-ui, -apple-system, sans-serif" } Info style with blue 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
);
toast.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(animationClass)

Sets the animation effect for the toast.

Parameters

Returns

The Toast instance for method chaining.

Example

toast.setAnimation(Toast.SLIDE_TOP);

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" });

setDismissible(dismissible)

Makes the toast dismissible by adding a close button.

Parameters

Returns

The Toast instance for method chaining.

Example

toast.setDismissible(true).show();

addCallback(callback)

Adds a callback function to be executed when the toast is shown.

Parameters

Returns

The Toast instance for method chaining.

Example

toast.addCallback(() => console.log("Toast dismissed!")).show();

show()

Displays the toast.

Example

toast.show();

hide()

Hides the toast.

Example

toast.hide();

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).show();

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 animation:

const animatedToast = Toast.makeText(
  document.body,
  "Slide-in animation!",
  Toast.LENGTH_SHORT
);
animatedToast.setAnimation(Toast.SLIDE_LEFT).show();

8. Create a toast with custom text direction (RTL):

const rtlToast = Toast.makeText(
  document.body,
  "مرحبا بالعالم",
  Toast.LENGTH_SHORT
);
rtlToast.setStyle("info")
  .setTextDirection("rtl")
  .show();

Customization

You can customize the toast by: