Toast
A succinct message that is displayed temporarily. Often used for feedback after an action.
When To Use
- To provide feedback such as success, warning, error etc.
- A notification with complex content.
- A message that is displayed at top and center and will be dismissed automatically, as a non-interrupting light-weighted prompt.
Example Usage
Include the <Toast /> component once in your root layout. Then, use window.Toast.show() anywhere in your client-side JavaScript to render toasts.
Example.astro
---
import Toast from '@/components/feedback/Toast.astro';
---
<!-- Add this once in your layout -->
<Toast />
<!-- Use the JS API to show toasts -->
<script>
window.Toast.show({
title: 'Toast',
description: 'Friday, February 10, 2023 at 5:57 PM'
});
</script> Variants
Toasts include semantic variants (`success`, `error`, `info`, `warning`) that automatically render an appropriate icon and coloring.
script.js
window.Toast.show({
title: 'Toast',
description: 'Your changes have been saved.',
variant: 'success' // 'default' | 'success' | 'error' | 'info' | 'warning'
}); Positions
Toasts can be rendered in different positions on the screen. The default is bottom-right.
Actions & Options
You can add an action button to the toast, adjust the auto-close duration, or remove the close button entirely.
script.js
window.Toast.show({
title: 'Toast',
action: {
label: 'Try again',
onClick: () => console.log('Retrying...')
},
duration: 10000, // 10 seconds. Set to 0 to disable auto-close.
closable: false // Removes the 'x' close button
}); API Reference
| Property | Description | Type | Default |
|---|---|---|---|
| title | The main title text for the toast. | string | - |
| description | Secondary description text below the title. | string | - |
| variant | Semantic variant determining icon and color (success, error, info, warning). | 'default' | 'success' | 'error' | 'info' | 'warning' | 'default' |
| position | Screen position where the toast appears (e.g., top-right, bottom-center). | 'top-left' | 'top-right' | 'top-center' | 'bottom-left' | 'bottom-right' | 'bottom-center' | 'bottom-right' |
| duration | Time in ms before auto-closing (set to 0 to disable). | number | 5000 |
| closable | Whether to show the close button. | boolean | true |
| action | Configuration for an inline action button. | ToastAction | - |