--- title: "toast" description: "Toast queue: dispatch a toast window event or call toast() for stacked notifications with variants, actions and in-place updates." category: "Feedback" --- # toast Toast queue: dispatch a toast window event or call toast() for stacked notifications with variants, actions and in-place updates. ## Installation ```bash php artisan ui:add toast ``` ## Dependencies - composer: `gehrisandro/tailwind-merge-laravel` ## Usage ```blade {{-- One region per page, near . position: bottom-right (default), bottom-left, bottom-center, top-right, top-left or top-center. max: how many show at once (default 3); the rest wait in a queue. --}} {{-- Show one by dispatching the event. Every field but title is optional. --}} Save {{-- From a controller: flash it, then dispatch it from the layout. return back()->with('toast', ['title' => 'Saved', 'variant' => 'success']); --}} @if (session('toast')) @endif ``` ## Example The demo rendered on the docs page. ```blade
Default Success Error Info Warning (sticky) {{-- Loading, then the same id turns it into a success. --}} Promise {{-- The action fires a window event named 'undo-archive'. --}} Undo action
``` ## Source Copy this file to the path shown above it. `resources/views/components/ui/toast.blade.php` ```blade @props([ 'position' => 'bottom-right', // How many toasts show at once; the rest wait their turn. 'max' => 3, 'label' => 'Notifications', 'closeLabel' => 'Close', ]) @php $anchor = match ($position) { 'top-right' => 'top-4 right-4', 'top-left' => 'top-4 left-4', 'top-center' => 'top-4 inset-x-0 mx-auto', 'bottom-left' => 'bottom-4 left-4', 'bottom-center' => 'bottom-4 inset-x-0 mx-auto', default => 'bottom-4 right-4', }; $side = str_starts_with($position, 'top') ? 'top' : 'bottom'; $regionClasses = \TailwindMerge\Laravel\Facades\TailwindMerge::merge( 'group/toasts fixed z-[100] w-[calc(100%-2rem)] max-w-sm transition-[height] duration-300', $anchor, $attributes->get('class'), ); @endphp
except('class') }}> {{-- Cloned once per toast; edit the markup here to restyle them. --}}
@once @endonce ``` ## Theme tokens `--background`, `--border`, `--foreground`, `--muted-foreground`, `--primary`, `--primary-foreground`, `--destructive`, `--ring`, `--radius` Place one region near ``; it ships its own inline script. Show a toast with `window.dispatchEvent(new CustomEvent('toast', { detail: { title, description, variant, duration, id, action } }))` or the `toast.success(title, options)` helpers, which return the id. Reusing an `id` updates that toast (loading to success), `duration: 0` keeps it open, and `action: { label, event }` fires a window event of that name. Props: `position`, `max`, `label`, `close-label`.