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_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. |
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. |
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. |
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", 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_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. |
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).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(enterAnimation, exitAnimation)
Sets the enter and exit animations for the toast.
enterAnimation
: The animation for toast entry (e.g.,
Toast.SLIDE_IN_TOP
).
exitAnimation
: The animation for toast exit (e.g.,
Toast.SLIDE_OUT_TOP
).
The Toast
instance for method chaining.
toast.setAnimation(
Toast.LIGHT_SPEED_IN_RIGHT,
Toast.LIGHT_SPEED_OUT_RIGHT
).show();
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" });
setIconShape(shape)
Sets the shape of the icon (circle, square, or squircle).
shape
: One of the shape constants (ICON_SHAPE_CIRCLE
,
ICON_SHAPE_SQUARE
, ICON_SHAPE_SQUIRCLE
).
The Toast
instance for method chaining.
toast.setIconShape(Toast.ICON_SHAPE_SQUIRCLE).show();
setDismissible(dismissible, closeButtonColor)
Makes the toast dismissible with an optional close button color.
dismissible
(optional): A boolean indicating if the toast is dismissible. Defaults to true
.closeButtonColor
(optional): A string representing the color of the close button. Defaults to "#fff"
.The Toast
instance for method chaining.
toast.setDismissible(true).show();
// Or You Can Use
toast.setDismissible(true, "#000000").show();
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, "#fff").show();
// Specifying the color remains optional
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,
"Custom Animations!",
Toast.LENGTH_SHORT
);
animatedToast.setAnimation(
Toast.WAVE_IN,
Toast.WOBBLE_OUT
).show();
const iconToast = Toast.makeText(
document.body,
"Toast with Icon!",
Toast.LENGTH_SHORT
);
iconToast.setIcon(
"/path/to/icon.svg",
Toast.ICON_SIZE.SMALL
).show();
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();
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();
You can customize the toast by:
setStyle
.setIcon
.setAnimation
.setIconShape
.