Components

Alert

A note or a problem in a #262626 box on Black: a Lucide icon, the subject in DM Sans Medium, a muted line under it and the button that goes with it. Red for what broke.

Install

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

npx spyglass-ds add alert
<script type="module" src="/spyglass-ds/elements/alert.js"></script>
Variants

Your first month is $10.

Then $20/month. Cancel any time.

Search results update every day.

Usage

A plan note, a failed payment, a search that needs a change: one box per message, with the action that settles it.

Never

  • A spectrum color fill or a glow
  • A border or a shadow around the box
  • Red for anything that did not break
  • Several messages in one box

Props

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

<Alert>

PropTypeDefault
titleReactNode

The subject, in DM Sans Medium.

variantAlertVariant

default, or destructive (red) for what broke.

'default'
iconReactNode

A 16px Lucide icon before it.

actionReactNode

A button at the end.

role'alert' | 'status'

alert (read out at once) or status (polite).

'alert'
classNamestring
childrenReactNode

The line under the subject.

Examples

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

react/examples/alert/Variants.tsx
import { CircleAlert, Info } from 'lucide-react'
import { Alert } from '@/react/components/ui/Alert'
import { Button } from '@/react/components/ui/Button'
// A note with the action that goes with it, and a problem in red with the fix.

export default function Variants() {
  return (
    <div className="flex w-160 max-w-full flex-col gap-3">
      <Alert role="status" title="Your first month is $10." icon={<Info size={16} strokeWidth={1.5} />} action={<Button size="sm">Start Creator</Button>}>
        Then $20/month. Cancel any time.
      </Alert>
      <Alert variant="destructive" title="Your payment did not go through." icon={<CircleAlert size={16} strokeWidth={1.5} />} action={<Button size="sm" variant="secondary">Update card</Button>}>
        Update your card to keep Acme's boards.
      </Alert>
      <Alert role="status" title="Search results update every day." icon={<Info size={16} strokeWidth={1.5} />} />
    </div>
  )
}
elements/examples/alert/Variants.html
<!-- A note with the action that goes with it, and a problem in red with the fix. -->
<div class="flex w-160 max-w-full flex-col gap-3">
  <spyglass-alert alert-role="status" heading="Your first month is $10.">
    <i data-lucide="info" data-size="16" data-stroke-width="1.5" slot="icon"></i>
    Then $20/month. Cancel any time.
    <spyglass-button slot="action" size="sm">Start Creator</spyglass-button>
  </spyglass-alert>
  <spyglass-alert variant="destructive" heading="Your payment did not go through.">
    <i data-lucide="circle-alert" data-size="16" data-stroke-width="1.5" slot="icon"></i>
    Update your card to keep Acme's boards.
    <spyglass-button slot="action" size="sm" variant="secondary">Update card</spyglass-button>
  </spyglass-alert>
  <spyglass-alert alert-role="status" heading="Search results update every day.">
    <i data-lucide="info" data-size="16" data-stroke-width="1.5" slot="icon"></i>
  </spyglass-alert>
</div>

Source

react/components/ui/Alert.tsx
import type { ReactNode } from 'react'
import { cn } from '@/react/lib/cn'

// shadcn's alert in the brand: a #262626 box on Black with the brand corner and no border or shadow, a 16px Lucide
// icon, the subject in DM Sans Medium and a line of muted DM Sans under it, and a button at the end if one helps.
// `destructive` turns the icon and the subject red for what broke. role="alert" so it is read out; for a note that
// was there from the start, pass role="status".
export type AlertVariant = 'default' | 'destructive'

const VARIANTS: Record<AlertVariant, string> = {
  default: 'text-fg',
  destructive: 'text-fg-danger',
}

export interface AlertProps {
  /** The subject, in DM Sans Medium. */
  title: ReactNode
  /** default, or destructive (red) for what broke. */
  variant?: AlertVariant
  /** A 16px Lucide icon before it. */
  icon?: ReactNode
  /** A button at the end. */
  action?: ReactNode
  /** alert (read out at once) or status (polite). */
  role?: 'alert' | 'status'
  className?: string
  /** The line under the subject. */
  children?: ReactNode
}

export function Alert({ title, variant = 'default', icon, action, role = 'alert', className, children }: AlertProps) {
  return (
    <div data-ds="alert" role={role} className={cn('flex w-full items-start gap-3 rounded-container bg-surface-raised p-4 max-sm:flex-wrap', className)}>
      {icon && <span className={cn('mt-0.5 flex shrink-0', VARIANTS[variant] ?? VARIANTS.default)} aria-hidden="true">{icon}</span>}
      <div className="flex min-w-0 flex-1 flex-col gap-1">
        <p className={cn('type-ui-strong', VARIANTS[variant] ?? VARIANTS.default)}>{title}</p>
        {children && <div className="type-ui text-fg-muted">{children}</div>}
      </div>
      {action && <div className="shrink-0 self-center max-sm:basis-full max-sm:pl-7">{action}</div>}
    </div>
  )
}

export default Alert
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/alert.ts
import { html, nothing } from 'lit'
import { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import styles from 'virtual:tw'

// Alert.tsx as <spyglass-alert>: a #262626 box on Black with the brand corner and no border or shadow, a 16px Lucide
// icon (the `icon` slot), the subject in DM Sans Medium (`heading`, React's `title`) and a line of muted DM Sans under
// it (the default slot), and a button at the end (the `action` slot). `variant="destructive"` turns the icon and the
// subject red. role="alert" so it is read out; `role="status"` for a note that was there from the start.
export type AlertVariant = 'default' | 'destructive'
const VARIANTS: Record<AlertVariant, string> = {
	default: 'text-fg',
	destructive: 'text-fg-danger',
}

/**
 * @summary A note or a problem in a #262626 box: an icon, the subject and a line.
 * @slot - The line under the subject.
 * @slot icon - A 16px Lucide icon.
 * @slot action - A button at the end.
 */
export class Alert extends DsElement {
	static styles = [DsElement.styles, styles]
	protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('[data-ds="alert"]'))
	static properties = { heading: {}, variant: { reflect: true }, alertRole: { attribute: 'alert-role' } }

	/** The subject (React's `title`). */
	heading = ''
	/** default, or destructive (red) for what broke. */
	variant: AlertVariant = 'default'
	/** alert (read out at once) or status (polite): React's `role`. */
	alertRole: 'alert' | 'status' = 'alert'

	render() {
		const tone = VARIANTS[this.variant] ?? VARIANTS.default
		return html`<div data-ds="alert" role=${this.alertRole} class="flex w-full items-start gap-3 rounded-container bg-surface-raised p-4 max-sm:flex-wrap">${this.hasSlot('icon') ? html`<span class=${['mt-0.5 flex shrink-0', tone].join(' ')} aria-hidden="true"><slot name="icon"></slot></span>` : nothing}<div class="flex min-w-0 flex-1 flex-col gap-1"><p class=${['type-ui-strong', tone].join(' ')}>${this.heading}</p>${this.hasSlot() ? html`<div class="type-ui text-fg-muted"><slot></slot></div>` : nothing}</div>${this.hasSlot('action') ? html`<div class="shrink-0 self-center max-sm:basis-full max-sm:pl-7"><slot name="action"></slot></div>` : nothing}</div>`
	}
}

define('alert', Alert)

Reference: Derived from the guidelines: the #262626 card fill on Black with the 10px canvas corner (8px here), DM Sans Medium for labels, the brand's voice ("$10 for your first month, then $20/month"). shadcn's alert for anatomy..