Components

Toast

One note, what just happened: the card's #262626 fill, a hairline and the brand corner, a DM Sans Medium title, a muted line, one small White button when there is something to do, and a close button.

Install

React apps get the React files; every other stack gets the web component.

npx spyglass-ds add toast
<script type="module" src="/spyglass-ds/elements/toast.js"></script>
Two notes

Saved to board

Acme hooks now has 24 creatives.

Brand added

New ads from Acme Studio show up here every day.

Usage

The surface of a note: Sonner stacks and times these in a corner. On its own, a note that stays where it is.

Never

  • Errors that need an answer: keep them where they happened
  • More than one button in a note
  • A colored fill or an icon in a spectrum color for success

Adds with it: button packages lucide-react

Props

The web component takes the same as attributes or properties (<spyglass-toast>); title is heading there.

<Toast>

PropTypeDefault
titlestring

What happened, in a few words.

descriptionstring

A line under the title.

action{ label: string, onClick?: () => void }

One button beside the text.

onDismiss() => void

Shows the close button; called when it is pressed.

classNamestring

Examples

The same example in React and as web components; both render identically.

react/examples/toast/Notes.tsx
'use client'
import { Toast } from '@/react/components/ui/Toast'
// Two notes as they read on their own: one with something to do, one that only says what happened.

export default function Notes() {
  return (
    <div className="flex w-90 max-w-full flex-col gap-3">
      <Toast title="Saved to board" description="Acme hooks now has 24 creatives." action={{ label: 'Undo' }} onDismiss={() => {}} />
      <Toast title="Brand added" description="New ads from Acme Studio show up here every day." onDismiss={() => {}} />
    </div>
  )
}
elements/examples/toast/Notes.html
<!-- Two notes as they read on their own: one with something to do, one that only says what happened. -->
<div class="flex w-90 max-w-full flex-col gap-3">
  <spyglass-toast heading="Saved to board" description="Acme hooks now has 24 creatives." action-label="Undo" dismissable></spyglass-toast>
  <spyglass-toast heading="Brand added" description="New ads from Acme Studio show up here every day." dismissable></spyglass-toast>
</div>

Source

react/components/ui/Toast.tsx
import { X } from 'lucide-react'
import { Button } from '@/react/components/ui/Button'
import { cn } from '@/react/lib/cn'

// One note: what just happened, in the card's #262626 fill with a #666666 hairline and the brand corner, no shadow. The
// title in DM Sans Medium, a muted line under it, one small White button beside them when there is something to do
// (Undo, View), and a close button when it can be dismissed. The Toaster (sonner) stacks these in a corner and times
// them; on its own it is a static note.
export interface ToastProps {
  /** What happened, in a few words. */
  title: string
  /** A line under the title. */
  description?: string
  /** One button beside the text. */
  action?: { label: string, onClick?: () => void }
  /** Shows the close button; called when it is pressed. */
  onDismiss?: () => void
  className?: string
}

export function Toast({ title, description, action, onDismiss, className }: ToastProps) {
  return (
    <div data-ds="toast" className={cn('flex w-full items-start gap-3 rounded-container border border-edge-strong bg-surface-raised p-4', className)}>
      <div className="flex min-w-0 flex-1 flex-col gap-1">
        <p className="type-ui-strong text-fg">{title}</p>
        {description && <p className="type-ui text-fg-muted">{description}</p>}
      </div>
      {action && <Button size="sm" className="-my-1.5" onClick={action.onClick}>{action.label}</Button>}
      {onDismiss && (
        <button type="button" className="-my-1.5 -mr-1.5 inline-flex size-control-sm shrink-0 cursor-pointer items-center justify-center rounded-control text-fg-muted transition-[background-color,color] duration-160 ease-standard hover:bg-action-ghost-hover hover:text-fg" aria-label="Dismiss" onClick={onDismiss}>
          <X size={16} strokeWidth={1.5} aria-hidden="true" />
        </button>
      )}
    </div>
  )
}

export default Toast
react/lib/cn.ts
// Joins class names, skipping falsy ones: cn('a', on && 'b', ['c', undefined]). It does not merge conflicting
// utilities (no tailwind-merge), as Vue's class binding does not: the library's classes are written not to compete,
// and a className from the caller is appended after them, as Vue appends a fallthrough class.
export type ClassValue = string | false | null | undefined | 0 | ClassValue[]

export function cn(...values: ClassValue[]): string {
	const out: string[] = []
	for (const v of values) {
		if (!v) continue
		if (Array.isArray(v)) {
			const s = cn(...v)
			if (s) out.push(s)
		}
		else out.push(v)
	}
	return out.join(' ')
}
elements/components/ui/toast.ts
import { html, nothing } from 'lit'
import { X } from 'lucide'
import { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import { icon } from '@/elements/lib/icon'
import '@/elements/components/ui/button'
import styles from 'virtual:tw'

// Toast.tsx as <spyglass-toast>: one note, what just happened, in the card's #262626 fill with a hairline and the
// brand corner. The title in DM Sans Medium, a muted line under it, one small White button when there is something to
// do (`action-label`, firing `action`), and a close button when `dismissable` (firing `dismiss`). React's `title` is
// `heading`. The toaster (sonner) stacks these in a corner and times them.

/**
 * @summary One note: what just happened.
 * @fires action - The action button was pressed.
 * @fires dismiss - The close button was pressed.
 */
export class Toast extends DsElement {
	static styles = [DsElement.styles, styles]
	protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('[data-ds="toast"]'))
	static properties = { heading: {}, description: {}, actionLabel: { attribute: 'action-label' }, dismissable: { type: Boolean } }

	/** What happened, in a few words (React's `title`). */
	heading = ''
	/** A line under the title. */
	description?: string
	/** The action button's label. */
	actionLabel?: string
	/** Shows the close button. */
	dismissable = false

	render() {
		return html`<div data-ds="toast" class="flex w-full items-start gap-3 rounded-container border border-edge-strong bg-surface-raised p-4">
			<div class="flex min-w-0 flex-1 flex-col gap-1">
				<p class="type-ui-strong text-fg">${this.heading}</p>
				${this.description ? html`<p class="type-ui text-fg-muted">${this.description}</p>` : nothing}
			</div>
			${this.actionLabel ? html`<spyglass-button size="sm" class="-my-1.5" @click=${() => this.emit('action')}>${this.actionLabel}</spyglass-button>` : nothing}
			${this.dismissable ? html`<button type="button" class="-my-1.5 -mr-1.5 inline-flex size-control-sm shrink-0 cursor-pointer items-center justify-center rounded-control text-fg-muted transition-[background-color,color] duration-160 ease-standard hover:bg-action-ghost-hover hover:text-fg" aria-label="Dismiss" @click=${() => this.emit('dismiss')}>${icon(X, 'x', { size: 16, strokeWidth: 1.5, attrs: { 'aria-hidden': 'true' } })}</button>` : nothing}
		</div>`
	}
}

define('toast', Toast)

Reference: Derived from the brand guidelines (no toast in the brand assets): the plan card's #262626 fill and corner, DM Sans Medium for the title, the White primary button.