Skip to main content

Toast

A lightweight toast notification component that shows transient messages. Supports various types, automatic dismissal, positioning, and custom icons.

๐Ÿ“ฆ Importโ€‹

import { Toast } from "@sahilphondekar/react-component-library";

๐Ÿงฑ Propsโ€‹

NameTypeDefaultDescription
messagestring or ReactNodeโ€” (required)Content to display inside the toast.
type"info" | "success" | "error" | "warning""info"The style of the toast. Sets color and default icon.
isOpenbooleanโ€” (required)Controls the visibility of the toast.
onClosefunctionโ€” (required)Callback triggered when the toast is closed (auto or manual).
durationnumber3000Duration in ms before the toast auto-dismisses. Use 0 to disable auto-close.
iconstring (raw SVG) or React.ElementTypeโ€”Custom icon to override the default one based on type.
position"top-left" | "top-right" | "bottom-left" | "bottom-right""bottom-right"Controls where the toast appears on screen.
classNamestring""Additional Tailwind CSS classes for custom styling.

๐Ÿ“ Positioningโ€‹

ValueDescription
top-leftTop-left corner of viewport
top-rightTop-right corner
bottom-leftBottom-left corner
bottom-rightBottom-right corner

๐ŸŽจ Type Stylingโ€‹

TypeBackgroundDefault Icon
infobg-blue-500Info circle
successbg-green-500Checkmark
errorbg-red-500Cross (X)
warningbg-yellow-500Warning exclamation

๐Ÿงช Exampleโ€‹

<Toast
message="This is an info toast!"
type="info"
isOpen={isOpen}
onClose={() => setIsOpen(false)}
duration={4000}
/>

With custom position and icon:

import { AlertTriangle } from "lucide-react";

<Toast
message="Be cautious of the changes"
type="warning"
icon={AlertTriangle}
position="top-right"
isOpen={toastVisible}
onClose={() => setToastVisible(false)}
/>

๐Ÿง  Notesโ€‹

  • The toast will auto-dismiss based on the duration prop.
  • If duration is 0, it must be dismissed manually using the close button.
  • Uses a SvgIcon helper component to handle inline SVG string rendering.
  • When icon is a React component (e.g., from lucide-react), it's rendered via React.createElement.

๐Ÿ’ก Use Casesโ€‹

  • Form submission success/failure
  • System warnings or messages
  • Timed alerts or user feedback