Skip to content
Viewership Inquire
UI Components

Toast.

Small notifications that stack in a corner, close on their own and pause on hover. Trigger from script or a data attribute.

FeedbackComponentNo dependencies Original, MIT

.md

    Using Claude Code? Give it the agent prompt and skip these steps.

    1. Import the foundation once

      foundation.css holds the tokens every component reads.

      Layout.astro
      ---
      // src/layouts/Layout.astro
      import "../styles/foundation.css";
      ---
    2. Copy the 2 files

      Paste into src/components/viewership/toast/

    3. Use it

      Page.astro
      ---
      import Toaster from "../components/viewership/toast/Toaster.astro";
      ---
      
      <Toaster />

    Props 4

    PropTypeDefaultDescription
    position "bottom-right" | "bottom-center" | "top-right" "bottom-right" Corner the toasts stack in.
    max number 3 Most toasts shown at once. The oldest closes first.
    toast(opts) function · From toast.ts. Options: { title, body?, tone?: "neutral" | "success" | "error", duration? }.
    data-vs-toast attribute · On any clickable element: the same options as JSON. Shows a toast on click.

    Events

    EventDetailWhen
    vs-toast CustomEvent<ToastOptions> Dispatch on window to show a toast without importing anything.

    Source 2 files

    src/components/viewership/toast/toast.ts
    /**
     * Show a toast from any script on a page that renders <Toaster />.
     *
     *   import { toast } from "./toast";
     *   toast({ title: "Report sent", body: "Shared with 4 people.", tone: "success" });
     */
    export interface ToastOptions {
      title: string;
      body?: string;
      tone?: "neutral" | "success" | "error";
      /** Milliseconds before it closes. 0 keeps it open until dismissed. Default 4500. */
      duration?: number;
    }
    
    export function toast(opts: ToastOptions) {
      window.dispatchEvent(new CustomEvent<ToastOptions>("vs-toast", { detail: opts }));
    }

    Original, MIT.