Skip to content
MarkasUI
Type to search

    Guide

    MarkasUI

    Form

    Feedback

    Overlay

    Navigation

    Data

    Sections

    Animations

    Backgrounds

    Buttons

    Templates

    Pages

    Toast

    Short notification in the bottom right corner that dismisses itself, triggered with the toast() function or an event.

    <div class="flex flex-wrap items-center gap-3" x-data>
        <x-markas.button x-on:click="$dispatch('markas-toast', { title: 'Changes saved', description: 'Your profile has been updated', variant: 'success', duration: 0 })">Show toast</x-markas.button>
        <x-markas.button variant="secondary" x-on:click="$dispatch('markas-toast', { title: 'Draft saved automatically' })">Temporary toast</x-markas.button>
    </div>
    <x-markas.toast />
    import { Button } from "@/components/markas/Button";
    import { Toaster, toast } from "@/components/markas/Toast";
    
    export default function ToastExample() {
      return (
        <>
          <div className="flex flex-wrap items-center gap-3">
            <Button
              onClick={() =>
                toast({
                  title: "Changes saved",
                  description: "Your profile has been updated",
                  variant: "success",
                  duration: 0,
                })
              }
            >
              Show toast
            </Button>
            <Button
              variant="secondary"
              onClick={() => toast({ title: "Draft saved automatically" })}
            >
              Temporary toast
            </Button>
          </div>
          <Toaster />
        </>
      );
    }
    <script setup lang="ts">
    import Button from "@/components/markas/Button.vue";
    import Toaster, { toast } from "@/components/markas/Toast.vue";
    </script>
    
    <template>
      <div class="flex flex-wrap items-center gap-3">
        <Button
          @click="
            toast({
              title: 'Changes saved',
              description: 'Your profile has been updated',
              variant: 'success',
              duration: 0,
            })
          "
        >
          Show toast
        </Button>
        <Button
          variant="secondary"
          @click="toast({ title: 'Draft saved automatically' })"
        >
          Temporary toast
        </Button>
      </div>
      <Toaster />
    </template>
    <script lang="ts">
      import Button from "$lib/components/markas/Button.svelte";
      import Toaster, { toast } from "$lib/components/markas/Toast.svelte";
    </script>
    
    <div class="flex flex-wrap items-center gap-3">
      <Button
        onclick={() =>
          toast({
            title: "Changes saved",
            description: "Your profile has been updated",
            variant: "success",
            duration: 0,
          })}>Show toast</Button
      >
      <Button
        variant="secondary"
        onclick={() => toast({ title: "Draft saved automatically" })}
        >Temporary toast</Button
      >
    </div>
    <Toaster />
    ---
    import Button from "@/components/markas/Button.astro";
    import Toaster from "@/components/markas/Toast.astro";
    ---
    
    <div class="flex flex-wrap items-center gap-3">
      <Button data-toast={JSON.stringify({ title: "Changes saved", description: "Your profile has been updated", variant: "success", duration: 0 })}>Show toast</Button>
      <Button variant="secondary" data-toast={JSON.stringify({ title: "Draft saved automatically" })}>Temporary toast</Button>
    </div>
    <Toaster />
    
    <script>
      // Toaster listens for the markas-toast event on window.
      document.addEventListener("click", (event) => {
        const button = (event.target as Element).closest<HTMLElement>("[data-toast]");
        if (!button) return;
        window.dispatchEvent(
          new CustomEvent("markas-toast", { detail: JSON.parse(button.dataset.toast!) }),
        );
      });
    </script>
    import { Component } from "@angular/core";
    import { Button } from "./markas/button.component";
    import { Toaster, toast } from "./markas/toast.component";
    
    @Component({
      selector: "markas-example",
      imports: [Button, Toaster],
      template: `
        <div class="flex flex-wrap items-center gap-3">
          <button
            markas-button
            (click)="
              toast({
                title: 'Changes saved',
                description: 'Your profile has been updated',
                variant: 'success',
                duration: 0,
              })
            "
          >
            Show toast
          </button>
          <button
            markas-button
            variant="secondary"
            (click)="toast({ title: 'Draft saved automatically' })"
          >
            Temporary toast
          </button>
        </div>
        <markas-toaster />
      `,
    })
    export default class ToastExample {
      protected readonly toast = toast;
    }

    Examples

    Variants

    With description

    Duration

    Installation

    npx markasui@latest add toast

    Usage

    Mount the region once in your layout, for example before </body>:

    <x-markas.toast :initial="session('toast') ? [session('toast')] : []" />

    Trigger a toast from Alpine or plain JavaScript:

    <x-markas.button x-data x-on:click="$dispatch('markas-toast', { title: 'Changes saved', variant: 'success' })">Save</x-markas.button>
    window.dispatchEvent(
      new CustomEvent("markas-toast", {
        detail: { title: "Could not send", variant: "danger", duration: 0 },
      }),
    );

    Needs Alpine.js. Livewire 3 and 4 already ship it.

    Livewire

    $this->dispatch('markas-toast', title: 'Saved', variant: 'success');

    Livewire 3 and 4 dispatch browser events on window, so a toast can come straight from a component action. Place <x-markas.toast /> once in the layout.

    import { Toaster, toast } from "@/components/markas/Toast";
    
    // In the layout
    <Toaster />;
    
    // Anywhere
    toast({
      title: "Changes saved",
      description: "Your profile has been updated",
      variant: "success",
    });
    <script setup lang="ts">
    import Toaster, { toast } from "@/components/markas/Toast.vue";
    </script>
    
    <template>
      <Button @click="toast({ title: 'Changes saved', variant: 'success' })">
        Save
      </Button>
      <Toaster />
    </template>
    <script lang="ts">
      import Button from "$lib/components/markas/Button.svelte";
      import Toaster, { toast } from "$lib/components/markas/Toast.svelte";
    </script>
    
    <Button onclick={() => toast({ title: "Changes saved", variant: "success" })}>
      Save
    </Button>
    <Toaster />
    ---
    import Toaster from "@/components/markas/Toast.astro";
    ---
    
    <!-- In the layout -->
    <Toaster />
    
    <script>
      // Anywhere in the browser
      window.dispatchEvent(
        new CustomEvent("markas-toast", {
          detail: { title: "Changes saved", variant: "success" },
        }),
      );
    </script>

    An Astro component cannot export functions to the page, so the Astro Toaster listens for the same markas-toast event as Blade. The initial prop shows toasts right away.

    import { Component } from "@angular/core";
    import { Button } from "./markas/button.component";
    import { Toaster, toast } from "./markas/toast.component";
    
    @Component({
      selector: "app-root",
      imports: [Button, Toaster],
      template: `
        <button markas-button (click)="save()">Save</button>
        <markas-toaster />
      `,
    })
    export class App {
      save() {
        toast({ title: "Changes saved", variant: "success" });
      }
    }
    • Toast details: title, description, variant (default, success, danger), and duration in milliseconds (default 5000, 0 keeps it until closed).
    • The timer pauses while a toast is hovered or holds focus.
    • toast() returns an id. In React and Vue, call dismissToast(id) to close it from code.
    • The toast queue lives at module level, so one Toaster per app is enough.
    • Call toast() only in the browser, for example in an event handler, useEffect (React), or onMounted (Vue). During server rendering (SSR) toast() does nothing and returns 0.
    • The region is labelled “Notifications” and each close button “Close”. Translate them with regionLabel and closeLabel (Blade: region-label, close-label).

    Props

    NameTypeDefaultDescription
    titlestring-Toast title. Option for toast() or the markas-toast event detail (Blade, Astro).
    descriptionstring-Supporting text below the title.
    variant'default' | 'success' | 'danger'defaultBorder and icon color.
    durationnumber5000Milliseconds before the toast dismisses itself. 0 keeps it until closed.
    initialarray[]Blade and Astro only: toasts shown right away, for example from a session flash.
    closeLabelstringCloseAccessible label of each toast close button.
    regionLabelstringNotificationsAccessible label of the live region.

    Accessibility

    • The region uses role status and aria-live polite, so new toasts are announced without moving focus.
    • The timer pauses while a toast is hovered or holds focus, and resumes once both are gone.
    • The close button is labelled by closeLabel and reachable with Tab.
    • The entry animation is turned off under prefers-reduced-motion.

    Related components

    More feedback components with the same classes in every framework.