Toast
The Toast component provides you with popup notifications with auto-dismiss and positioning.
Live Examples
Using the Hook
Live Example
☀️
Copy
View Code
import { ToastProvider, useToast } from '@jarllyng/nostromo'
function ToastDemo() {
const { addToast } = useToast()
return (
<div className="space-x-2">
<button onClick={() => addToast({
variant: 'success',
title: 'Success!',...
With Action
Live Example
☀️
Copy
View Code
import { ToastProvider, useToast } from '@jarllyng/nostromo'
function ToastWithAction() {
const { addToast } = useToast()
return (
<button onClick={() => addToast({
variant: 'default',
title: 'File deleted',
description: 'The file has been moved to trash.',...
Import
import { Toast, ToastProvider, useToast } from "@jarllyng/nostromo";
// or
import { Toast, ToastProvider, useToast } from "@jarllyng/nostromo/toast";Usage
import { ToastProvider, useToast } from "@jarllyng/nostromo";
function App() {
return (
<ToastProvider>
<MyComponent />
</ToastProvider>
);
}
function MyComponent() {
const { addToast } = useToast();
const handleClick = () => {
addToast({
variant: "success",
title: "Success!",
description: "Your action was completed.",
});
};
return <button onClick={handleClick}>Show Toast</button>;
}Best Practices
Do ✅
- Wrap your app with
ToastProviderat the root level - Use the
useToasthook for easy toast management - Provide clear
titleanddescriptionfor each toast - Use appropriate
variantfor different message types - Set
duration={0}for important toasts that require user action
Don’t ❌
- Don’t show too many toasts at once
- Don’t use toasts for critical errors (use dialogs instead)
- Don’t forget to handle toast actions
- Don’t use toasts for form validation (use inline errors)
Props
Toast
| Prop | Type | Default | Required | Description |
|---|---|---|---|---|
variant | "default" | "success" | "error" | "warning" | "info" | "default" | No | Visual variant of the toast |
position | "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right" | "top-right" | No | Toast position |
title | string | undefined | No | Toast title |
description | string | undefined | No | Toast description |
duration | number | 5000 | No | Auto-dismiss duration in ms (0 = no auto-dismiss) |
onClose | () => void | undefined | No | Callback when toast is closed |
action | { label: string; onClick: () => void } | undefined | No | Action button configuration |
appearDelay | number | 10 | No | Delay before toast appears (ms) |
animationMs | number | 150 | No | Exit animation duration (ms) |
className | string | undefined | No | Additional CSS classes |
children | React.ReactNode | undefined | No | Toast content |
ToastProvider
| Prop | Type | Default | Required | Description |
|---|---|---|---|---|
children | React.ReactNode | - | Yes | App content |
useToast Hook
Returns:
toasts: ToastProps[]- Array of current toastsaddToast: (toast: Omit<ToastProps, 'id'>) => string- Add a new toast (returns toast ID)removeToast: (id: string) => void- Remove a toast by IDclearToasts: () => void- Clear all toasts
Last updated on