Installation

  1. Set up the project

    Once per project: the packages, Tailwind CSS, the theme tokens and the cn helper. Follow Installation.

  2. Copy the component into your project

    The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.

    src/ui/toast.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { ToastItem, ToastProvider } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    /** What the `view` closure of the kit's `ToastProvider` receives. */
    export type ToastProviderParts = Component.Headless.Parts<typeof ToastProvider>
    
    const positions = {
      "top-left": "inset-[1rem_auto_auto_1rem] [--toast-direction:1]",
      "top-center": "inset-[1rem_0_auto_0] mx-auto [--toast-direction:1]",
      "top-right": "inset-[1rem_1rem_auto_auto] [--toast-direction:1]",
      "bottom-left": "inset-[auto_auto_1rem_1rem] [--toast-direction:-1]",
      "bottom-center": "inset-[auto_0_1rem_0] mx-auto [--toast-direction:-1]",
      "bottom-right": "inset-[auto_1rem_1rem_auto] [--toast-direction:-1]"
    } as const
    
    export type ToastPosition = keyof typeof positions
    
    type ClassOption = { readonly class?: string | undefined }
    
    /**
     * The region that holds the stack. The kit makes it a popover in the top
     * layer, so these classes clear the browser's popover box and pin it to a
     * corner. With `topLayer: false` on the provider it is an ordinary element:
     * pass `class: "absolute"` to place it inside a `relative` ancestor.
     */
    export const ToastViewport = <Msg>(
      h: Html.H<Msg>,
      toastProvider: ToastProviderParts,
      options: ClassOption & { readonly position?: ToastPosition | undefined } = {}
    ) => {
      const position = options.position ?? "bottom-right"
      return toastProvider.viewport(
        h.div
          .class(cn(
            "group/toasts fixed m-0 h-[var(--toast-frontmost-height,0px)] w-[356px] max-w-[calc(100vw-2rem)] overflow-visible",
            "border-0 bg-transparent p-0 text-foreground outline-none [--toast-gap:10px] [--toast-peek:12px]",
            positions[position],
            options.class
          ))
          .attr("data-side", position.startsWith("top") ? "top" : "bottom")
      )
    }
    
    /**
     * One toast. Collapsed, the toasts sit behind the front one and peek out;
     * while the pointer or keyboard focus is on the stack they fan out, each
     * moved by the heights the kit measured.
     */
    export const Toast = <Msg>(
      h: Html.H<Msg>,
      toastProvider: ToastProviderParts,
      toast: ToastItem,
      options: ClassOption = {}
    ) =>
      toastProvider.root({
        element: h.div.class(cn(
          "absolute inset-x-0 bottom-0 z-[calc(100_-_var(--toast-index,0))] box-border origin-bottom rounded-md border",
          "h-[var(--toast-frontmost-height,auto)] bg-popover text-[13px] leading-snug text-foreground shadow-float",
          "group-data-[side=top]/toasts:top-0 group-data-[side=top]/toasts:bottom-auto group-data-[side=top]/toasts:origin-top",
          "translate-y-[calc(var(--toast-index,0)_*_var(--toast-peek)_*_var(--toast-direction))]",
          "scale-[calc(1_-_var(--toast-index,0)_*_0.05)]",
          "transition-[translate,scale,opacity,height] duration-200 ease-out",
          // Fills the gap between two toasts, so the pointer stays on the stack.
          "after:absolute after:inset-x-0 after:-inset-y-[var(--toast-gap)] after:-z-10 after:content-['']",
          "data-[expanded]:h-[var(--toast-height,auto)] data-[expanded]:scale-100",
          "data-[expanded]:translate-y-[calc((var(--toast-offset-y,0px)_+_var(--toast-index,0)_*_var(--toast-gap))_*_var(--toast-direction))]",
          "data-[starting-style]:translate-y-[calc(var(--toast-direction)_*_-24px)] data-[starting-style]:opacity-0",
          "data-[ending-style]:scale-95 data-[ending-style]:opacity-0",
          "data-[limited]:pointer-events-none data-[limited]:opacity-0",
          options.class
        )),
        toast
      })
    
    /** The visible content of a toast, hidden while the toast sits behind the front one. */
    export const ToastContent = <Msg>(
      h: Html.H<Msg>,
      toastProvider: ToastProviderParts,
      toast: ToastItem,
      options: ClassOption = {}
    ) =>
      toastProvider.content({
        element: h.div.class(cn(
          "flex h-full items-start gap-2.5 overflow-hidden rounded-[inherit] bg-popover px-3.5 py-3",
          "transition-opacity duration-200 data-[behind]:not-data-[expanded]:opacity-0",
          options.class
        )),
        toast
      })
    
    /** The column that holds the title and the description. */
    export const ToastText = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) =>
      h.div.class(cn("grid min-w-0 flex-1 gap-0.5", options.class))
    
    export const ToastTitle = <Msg>(
      h: Html.H<Msg>,
      toastProvider: ToastProviderParts,
      toast: ToastItem,
      options: ClassOption = {}
    ) =>
      toastProvider.title({ element: h.div.class(cn("font-medium", options.class)), toast }).children(toast.title)
    
    export const ToastDescription = <Msg>(
      h: Html.H<Msg>,
      toastProvider: ToastProviderParts,
      toast: ToastItem,
      options: ClassOption = {}
    ) =>
      toastProvider
        .description({ element: h.div.class(cn("text-muted-foreground", options.class)), toast })
        .children(toast.description)
    
    /** A button for the consumer's own action. Chain `.on("click", message)` and the label. */
    export const ToastAction = <Msg>(
      h: Html.H<Msg>,
      toastProvider: ToastProviderParts,
      toast: ToastItem,
      options: ClassOption = {}
    ) =>
      toastProvider.action({
        element: h.button.class(cn(
          "inline-flex h-6.5 shrink-0 items-center self-center rounded-sm bg-primary px-2.5 text-xs font-medium",
          "text-primary-foreground transition-opacity duration-150 hover:opacity-85",
          options.class
        )),
        toast
      })
    
    export const ToastClose = <Msg>(
      h: Html.H<Msg>,
      toastProvider: ToastProviderParts,
      toast: ToastItem,
      options: ClassOption = {}
    ) =>
      toastProvider
        .close({
          element: h.button
            .class(cn(
              "-mt-0.5 -mr-1 inline-flex size-5.5 shrink-0 items-center justify-center rounded-sm text-muted-foreground",
              "transition-colors duration-150 hover:bg-accent hover:text-foreground",
              options.class
            ))
            .attr("aria-label", "Close"),
          toast
        })
        .children(
          h.el("svg")
            .attrs({
              class: "size-3.5",
              viewBox: "0 0 24 24",
              fill: "none",
              stroke: "currentColor",
              "stroke-width": "2",
              "stroke-linecap": "round",
              "aria-hidden": "true"
            })
            .children(h.el("path").attr("d", "M6 6l12 12M18 6 6 18"))
        )
    
    const icons: Record<string, { readonly class: string; readonly path: string }> = {
      success: { class: "text-success", path: "M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20ZM8.5 12.5 11 15l4.5-5.5" },
      error: { class: "text-destructive", path: "M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20ZM15 9l-6 6M9 9l6 6" },
      warning: {
        class: "text-warning",
        path: "M10.3 3.9 2.4 17.5a2 2 0 0 0 1.7 3h15.8a2 2 0 0 0 1.7-3L13.7 3.9a2 2 0 0 0-3.4 0ZM12 9v4M12 16.5h.01"
      },
      info: { class: "text-muted-foreground", path: "M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20ZM12 16v-4.5M12 8h.01" }
    }
    
    /** An icon for the toast's `type`: success, error, warning, info, or a spinner for loading. Nothing for any other. */
    export const ToastIcon = <Msg>(h: Html.H<Msg>, toast: ToastItem, options: ClassOption = {}) => {
      if (toast.type === "loading") {
        return h.span
          .class(cn(
            "mt-px size-4 shrink-0 animate-spin rounded-full border-2 border-muted-foreground border-r-transparent",
            options.class
          ))
          .attr("aria-hidden", "true")
      }
      const icon = toast.type === undefined ? undefined : icons[toast.type]
      return icon === undefined ? undefined : h.el("svg")
        .attrs({
          class: cn("mt-px size-4 shrink-0", icon.class, options.class),
          viewBox: "0 0 24 24",
          fill: "none",
          stroke: "currentColor",
          "stroke-width": "2",
          "stroke-linecap": "round",
          "stroke-linejoin": "round",
          "aria-hidden": "true"
        })
        .children(h.el("path").attr("d", icon.path))
    }
    
    /**
     * The whole stack with the usual toast: icon, title, description and a close
     * button. Draw it once inside the provider's `view` closure, next to the app:
     * `ToastProvider(h, { view: (toastProvider) => h.div(App(h, {}), Toaster(h, toastProvider)) })`.
     * For a toast with other content, compose the parts above yourself.
     */
    export const Toaster = <Msg>(
      h: Html.H<Msg>,
      toastProvider: ToastProviderParts,
      options: ClassOption & { readonly position?: ToastPosition | undefined } = {}
    ) =>
      ToastViewport(h, toastProvider, options).children(
        toastProvider.toasts.map((toast) =>
          Toast(h, toastProvider, toast).children(
            ToastContent(h, toastProvider, toast).children(
              ToastIcon(h, toast),
              ToastText(h).children(
                ToastTitle(h, toastProvider, toast),
                toast.description !== undefined && ToastDescription(h, toastProvider, toast)
              ),
              toast.type !== "loading" && ToastClose(h, toastProvider, toast)
            )
          )
        )
      )
    
  3. Import it

    import { ToastViewport, Toast, ToastContent, ToastText, ToastTitle, ToastDescription, ToastAction, ToastClose, ToastIcon, Toaster } from "#ui/toast"
    

Usage

Basic.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { ToastProvider } from "@effect/ui-kit"
import { Schema } from "effect"
import { Button } from "#ui/button"
import { Toaster } from "#ui/toast"

const ToastBasicTriggerMessage = UiSchema.TaggedEnum({ PressedAddToCalendar: {} })

// A sender sits below the provider and posts to it from `update`.
const ToastBasicTrigger = Html.component({
  name: "ToastBasicTrigger",
  model: Schema.Struct({ scheduled: Schema.Number }),
  message: ToastBasicTriggerMessage
}).make({
  init: () => ({ scheduled: 0 }),
  update: {
    PressedAddToCalendar: ($) =>
      $.evolve({ scheduled: (scheduled) => scheduled + 1 }).send(ToastProvider, {
        _tag: "RequestedAdd",
        toast: {
          id: `event-${$.model.scheduled}`,
          title: "Event has been created",
          description: "Sunday, December 3 at 9:00 AM"
        }
      })
  },
  view: (h) =>
    Button(h, { variant: "outline", onPress: ToastBasicTriggerMessage.PressedAddToCalendar() }).children(
      "Add to calendar"
    )
})

export const ToastBasic = Html.component({
  name: "ToastBasic",
  model: Schema.Struct({}),
  message: Schema.Never
}).make({
  init: () => ({}),
  update: ($) => $.model,
  view: (h) =>
    ToastProvider(h, {
      view: (toastProvider) => h.div(ToastBasicTrigger(h, {}), Toaster(h, toastProvider))
    })
})

Examples

Types

The toast's type shows as data-type on every part, here for the icon. The error is a high-priority alert.

With an action

Composed from the parts instead of Toaster: Undo restores the file and closes the toast.

  • invoice.pdf
  • notes.md
  • photo.png

Loading, then done

A loading toast stays until a RequestedUpdate with the same id gives it its result.

Positions

The viewport is placed by its classes, so the position option moves the stack to any corner.

Stacked

Toasts that never time out, four at a time. The stack expands under the pointer or keyboard focus.

0 open, 0 past the limit of 4. Hover the stack to expand it.

In place

With topLayer: false the viewport is an ordinary element, clipped by the card it is drawn in.

Team

Toasts stay inside this card.

Anatomy

The parts the view closure receives. Apply each one to an element of your own.

PartDescription
viewport<div>: the landmark region that holds the toasts. A manual popover in the top layer.
root<div>: one toast. Takes { element, toast }, as every part below, and keys the element by the toast's id.
content<div> inside the root, for the visible content.
titleAny element: the toast's label.
descriptionAny element: the toast's description.
action<button> for the consumer's own action.
close<button> that closes the toast.
toastsThe toasts to draw, newest first, closing and limited ones included.
expandedWhether the pointer is on the viewport or keyboard focus is in it.

API reference

Props

PropTypeDescription
timeoutnumberMilliseconds before a toast without its own timeout closes. 5000 by default, 0 keeps toasts until they are closed.
limitnumberHow many toasts show at once, 3 by default. Older ones stay in the list, limited and inert.
topLayerbooleanfalse draws the viewport in place instead of as a popover in the top layer.
toast.idstringPicked by the sender, so a later RequestedUpdate or RequestedClose can name the toast.
toast.title / descriptionstringThe text the consumer draws, and the toast's accessible name and description.
toast.typestringAny string, shown as data-type. "loading" never times out.
toast.timeoutnumberThis toast's own timeout in milliseconds.
toast.priority"low" | "high""high" announces the toast at once, as an alert.

Events

EventDescription
DismissedThe user closed the toast id with its close part or Escape, or its timeout ran out.
$.send RequestedAdd{ toast }: shows a toast, or replaces the one that shows with the same id.
$.send RequestedUpdate{ id, ...changes }: changes the fields it names on a toast that shows.
$.send RequestedClose{ id }: closes a toast, without a Dismissed.

Data attributes

AttributeDescription
data-expandedOn viewport, root and content while the stack is expanded.
data-typeOn root, title, description, action and close: the toast's type.
data-starting-styleOn root until the toast is first measured.
data-ending-styleOn root while the toast plays its exit.
data-limitedOn root: the toast is past the provider's limit.
data-behindOn content: every toast but the front one.
--toast-indexOn root: the toast's place from the front.
--toast-offset-yOn root: the height of the toasts in front of it.
--toast-heightOn root: its own measured height.
--toast-frontmost-heightOn viewport: the front toast's height.

The components this page's file and examples import.

  • Button: A native button that can stay focusable while disabled.

effectcn is built with Effect UI and @effect/ui-kit. Not affiliated with Effectful Technologies.