A lightweight and customizable library for displaying Toast Notifications.
Developed by Soufiano Dev
The Toast
class encapsulates all the functionality
required to create and manage toast notifications. It supports:
Constant | Value | Description |
---|---|---|
LENGTH_SHORT |
3000 | Toast duration of 3 seconds. |
LENGTH_LONG |
6500 | Toast duration of 6.5 seconds. |
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. |
Constant | Value | Description |
---|---|---|
ICON_POSITION_START |
"start" | Icon appears before the text. |
ICON_POSITION_END |
"end" | Icon appears after the text. |
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. |
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. |
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. |
makeText(context, text, duration)
Creates a new Toast
instance with predefined text and
duration.
context
: The DOM element where the toast will be
appended.
text
: The text to display in the toast.duration
(optional): The duration of the toast.
Defaults to Toast.LENGTH_SHORT
.
A new Toast
instance.
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.
text
: The text to display.The Toast
instance for method chaining.
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.
duration
: The duration in milliseconds or one of the
duration constants (LENGTH_SHORT
,
LENGTH_LONG
).
The Toast
instance for method chaining.
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.
style
: A string (e.g., "success"
) or an
object containing CSS properties.
The Toast
instance for method chaining.
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.
position
: One of the position constants (e.g.,
POSITION_TOP_CENTER
).
The Toast
instance for method chaining.
toast.setPosition(Toast.POSITION_TOP_CENTER);
setAnimation(animationClass)
Sets the animation effect for the toast.
animationClass
: One of the animation constants (e.g.,
FADE
).
The Toast
instance for method chaining.
toast.setAnimation(Toast.SLIDE_TOP);
setIcon(iconPath, size, position)
Sets an icon for the toast.
iconPath
: The path to the icon (image or video).size
(optional): The size of the icon (e.g.,
ICON_SIZE.MEDIUM
).
position
(optional): The position of the icon relative
to the text (ICON_POSITION_START
or
ICON_POSITION_END
).
The Toast
instance for method chaining.
// 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.
dismissible
(optional): Whether the toast is
dismissible. Defaults to true
.
The Toast
instance for method chaining.
toast.setDismissible(true).show();
addCallback(callback)
Adds a callback function to be executed when the toast is shown.
callback
: A function to execute.The Toast
instance for method chaining.
toast.addCallback(() => console.log("Toast dismissed!")).show();
show()
Displays the toast.
toast.show();
hide()
Hides the toast.
toast.hide();
const toast = Toast.makeText(
document.body,
"Hello Toast!",
Toast.LENGTH_SHORT
);
toast.show();
const successToast = Toast.makeText(
document.body,
"Operation Successful!",
Toast.LENGTH_LONG
);
successToast.setStyle("success").show();
const errorToast = Toast.makeText(
document.body,
"Error occurred!",
Toast.LENGTH_SHORT
);
errorToast.setPosition(Toast.POSITION_TOP_RIGHT)
.setStyle("error")
.show();
const iconToast = Toast.makeText(
document.body,
"Toast with Icon!",
Toast.LENGTH_SHORT
);
iconToast.setIcon(
"https://example.com/icon.png",
Toast.ICON_SIZE.SMALL
).show();
const dismissibleToast = Toast.makeText(
document.body,
"You can close this toast.",
Toast.LENGTH_SHORT
);
dismissibleToast.setDismissible(true).show();
const callbackToast = Toast.makeText(
document.body,
"Callback after hide!",
Toast.LENGTH_SHORT
);
callbackToast.addCallback(() => console.log("Toast dismissed!"))
.show();
const animatedToast = Toast.makeText(
document.body,
"Slide-in animation!",
Toast.LENGTH_SHORT
);
animatedToast.setAnimation(Toast.SLIDE_LEFT).show();
const rtlToast = Toast.makeText(
document.body,
"مرحبا بالعالم",
Toast.LENGTH_SHORT
);
rtlToast.setStyle("info")
.setTextDirection("rtl")
.show();
You can customize the toast by:
setStyle
.setIcon
.setAnimation
.