Skip to content
WebJs
Menu

An opinionated toast component.

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';

export default function SonnerDemo() {
  return html`
    <button type="button" data-slot="button" data-variant="outline" data-size="default" data-toast="Event has been created" data-toast-description="Sunday, December 03, 2023 at 9:00 AM" data-toast-action="Undo" class=${buttonClass({ variant: 'outline' })}>
      Show Toast
    </button>
  `;
}
View Code

About

The kit's toaster is written for WebJs. It is one <ui-sonner> viewport you mount once, plus an imperative toast() function: toast.success(), toast.error(), toast.promise() and the rest.

Installation

pnpm dlx @webjsdev/ui add sonner

Usage

Mount the toast viewport once, typically in your root layout:

<ui-sonner position="bottom-right"></ui-sonner>

Then call toast() from browser code, such as a component's event handler:

import { toast } from '#components/ui/sonner.ts';
toast('Event has been created.');

Types

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';

// Each button fires one toast() call through its data-toast attributes:
// toast(), toast.success(), toast.info(), toast.warning(), toast.error(),
// and for toast.promise() its two-second loading state, since a promise has
// no declarative form.
export default function SonnerTypes() {
  return html`
    <div class="flex flex-wrap gap-2">
      <button type="button" data-slot="button" data-variant="outline" data-size="default" data-toast="Event has been created" class=${buttonClass({ variant: 'outline' })}>
        Default
      </button>
      <button type="button" data-slot="button" data-variant="outline" data-size="default" data-toast="Event has been created" data-toast-type="success" class=${buttonClass({ variant: 'outline' })}>
        Success
      </button>
      <button type="button" data-slot="button" data-variant="outline" data-size="default" data-toast="Be at the area 10 minutes before the event time" data-toast-type="info" class=${buttonClass({ variant: 'outline' })}>
        Info
      </button>
      <button type="button" data-slot="button" data-variant="outline" data-size="default" data-toast="Event start time cannot be earlier than 8am" data-toast-type="warning" class=${buttonClass({ variant: 'outline' })}>
        Warning
      </button>
      <button type="button" data-slot="button" data-variant="outline" data-size="default" data-toast="Event has not been created" data-toast-type="error" class=${buttonClass({ variant: 'outline' })}>
        Error
      </button>
      <button type="button" data-slot="button" data-variant="outline" data-size="default" data-toast="Loading..." data-toast-type="loading" data-toast-duration="2000" class=${buttonClass({ variant: 'outline' })}>
        Promise
      </button>
    </div>
  `;
}
View Code

Description

import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { buttonClass } from '#components/ui/button.ts';

// The button stands for
// toast('Event has been created', { description: 'Monday, January 3rd at 6:00pm' }).
export default function SonnerDescription() {
  return html`
    <button type="button" data-slot="button" data-variant="outline" data-size="default" data-toast="Event has been created" data-toast-description="Monday, January 3rd at 6:00pm" class=${cn(buttonClass({ variant: 'outline' }), 'w-fit')}>
      Show Toast
    </button>
  `;
}
View Code

Position

Use the position attribute on <ui-sonner> to change the position of the toast.

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';

const positions = [
  { value: 'top-left', label: 'Top Left' },
  { value: 'top-center', label: 'Top Center' },
  { value: 'top-right', label: 'Top Right' },
  { value: 'bottom-left', label: 'Bottom Left' },
  { value: 'bottom-center', label: 'Bottom Center' },
  { value: 'bottom-right', label: 'Bottom Right' },
];

// Each button stands for a toast shown in a <ui-sonner> mounted at that
// position.
export default function SonnerPosition() {
  return html`
    <div class="flex flex-wrap justify-center gap-2">
      ${positions.map((position) => html`
        <button type="button" data-slot="button" data-variant="outline" data-size="default" data-position=${position.value} data-toast="Event has been created" data-toast-position=${position.value} class=${buttonClass({ variant: 'outline' })}>
          ${position.label}
        </button>
      `)}
    </div>
  `;
}
View Code

API Reference

ui-sonner

AttributeTypeDefault
position"top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right""bottom-right"

The viewport is a polite live region labelled "Notifications", so a toast is announced as it appears, and an error toast is announced assertively. Every toast carries a close button. addToast(message, options, type) on the element shows a toast in that viewport only, for a page that mounts more than one.

toast

toast(message, options) shows a toast and returns its id. toast.success(), toast.info(), toast.warning(), toast.error() and toast.loading() take the same arguments. toast.promise(promise, { loading, success, error }) shows a loading toast that turns into the success or error toast when the promise settles, and toast.dismiss(id) removes one.

OptionTypeDefault
idstring | number-
descriptionstring-
durationnumber (ms)4000, 0 for loading
action{ label: string; onClick: () => void }-
cancel{ label: string; onClick: () => void }-

A repeated id updates the toast in place. The action and cancel buttons dismiss the toast after their onClick runs.