Skip to content
Thrum

Alert

A message in the flow of the page: a note, a confirmation, a warning or an error about what is on screen. It stays until the reader or the page removes it. For a message about an action that just happened elsewhere, use a toast; for one that covers the whole page, a banner.

Install

npx shadcn@latest add https://thrumui.dev/r/alert.json

Adds components/ui/alert.tsx. Nothing else in your project changes.

import { Alert } from "@/components/ui/alert";

Automatically installs icons circle-check, info, octagon-alert, triangle-alert and x from the icon set.

Props

PropTypeDefaultDescription
Alert: variant"info" | "success" | "warning" | "destructive"Default "info"What kind of message it is. Each variant has its own glyph: a circled i, a circled tick, a triangle and an octagon.
Alert: urgentbooleanDefault falseUse the alert role, which interrupts a screen reader. Without it the role is status, which waits its turn.
Alert: iconReactNode–Replaces the variant's glyph. null leaves the glyph out; then the title alone must say what kind of message it is.
Alert: dismissiblebooleanDefault falseShow a 28 pixel close button in the top corner. Pressing it hides the alert.
Alert: onDismiss() => void–Called when the close button is pressed. Store the dismissal here if it should last.
Alert: dismissLabelstringDefault "Dismiss"The close button's accessible name.
Alert: soundbooleanDefault falsePlay the success, warning or error sound once when the alert appears, if the visitor has sound on. Info never plays one.
Alert: ...propsComponentProps<"div">–Every other div attribute is passed through. className is merged last.
AlertTitleComponentProps<"div">–The headline, semibold. Long words break instead of overflowing.
AlertDescriptionComponentProps<"div">–The explanation, in the muted text colour. Paragraphs inside it are 8 pixels apart.
AlertActionsComponentProps<"div">–A row of small buttons under the description. It wraps on a narrow screen.

Motion and sound

Motion

  • The alert does not move. It appears and leaves at once.

Sound

  • Silent by default. With sound set, a success alert plays the success chime, a warning the warning tone and a destructive alert the error tone, once when it appears and again if its variant changes.
  • An info alert never plays a sound.

Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works

Accessibility

  • The alert has the status role, a polite live region: a screen reader reads a new alert when it is added to the page, after what it is saying. urgent switches to the alert role, which interrupts.
  • Colour is never the only signal: each variant has its own glyph shape, and the title says what happened. The glyph is hidden from screen readers.
  • The glyph colour keeps 3 to 1 and the text 4.5 to 1 against the card in both themes.
  • The close button is 28 pixels square and named Dismiss, or dismissLabel.
  • Under a glass material the alert is a translucent panel. Its tint keeps the text at 4.5 to 1 over any page.
KeysAction
TabMove to the actions and then to the close button.
Enter / SpaceOn the close button: hide the alert.

Source

components/ui/alert.tsx
"use client";

import { useEffect, useRef, useState, type ComponentProps, type ReactNode } from "react";

import { cn } from "@/lib/utils";
import { CircleCheckIcon } from "@/components/icons/circle-check";
import { InfoIcon } from "@/components/icons/info";
import { OctagonAlertIcon } from "@/components/icons/octagon-alert";
import { TriangleAlertIcon } from "@/components/icons/triangle-alert";
import { XIcon } from "@/components/icons/x";
import { STATUS } from "@/lib/thrum-chart";
import { play, type SoundName } from "@/lib/thrum-sound";
import { IconButton } from "@/components/ui/icon-button";

export type AlertVariant = "info" | "success" | "warning" | "destructive";

export type AlertProps = ComponentProps<"div"> & {
  /** What kind of message it is. Each variant has its own glyph as well as its own colour. */
  variant?: AlertVariant;
  /** Interrupt a screen reader: the `alert` role instead of `status`. Only for a message that needs action now. */
  urgent?: boolean;
  /** Replace the variant's glyph. `null` leaves the glyph out. */
  icon?: ReactNode;
  /** Show a close button. Pressing it hides the alert and calls `onDismiss`. */
  dismissible?: boolean;
  /** Called when the close button is pressed. */
  onDismiss?: () => void;
  /** The close button's name. */
  dismissLabel?: string;
  /** Play the success, warning or error sound once when the alert appears, if the visitor has sound on. Info is always silent. */
  sound?: boolean;
};

const GLYPH: Record<AlertVariant, ReactNode> = {
  info: <InfoIcon size={20} />,
  success: <CircleCheckIcon size={20} />,
  warning: <TriangleAlertIcon size={20} />,
  destructive: <OctagonAlertIcon size={20} />,
};

/** The glyph and edge colour of each variant. Info follows the text colour. */
const TONE: Record<AlertVariant, string> = {
  info: "var(--foreground)",
  success: STATUS.ok,
  warning: STATUS.warn,
  destructive: STATUS.alarm,
};

const CUE: Partial<Record<AlertVariant, SoundName>> = { success: "success", warning: "warn", destructive: "error" };

/**
 * A message in the flow of the page: a note, a confirmation, a warning, an error.
 * Put an `AlertTitle`, an `AlertDescription` and optionally `AlertActions` inside.
 * It is a status region: a screen reader reads it when it appears, without interrupting. Set `urgent` for an error that blocks work.
 */
export function Alert({
  variant = "info",
  urgent = false,
  icon,
  dismissible = false,
  onDismiss,
  dismissLabel = "Dismiss",
  sound = true,
  className,
  style,
  children,
  ...props
}: AlertProps) {
  const [shown, setShown] = useState(true);
  const cue = CUE[variant];

  // Silent on initial mount; plays only when variant changes interactively or sound is explicitly turned on.
  const mounted = useRef(false);
  const prev = useRef({ variant, sound });
  useEffect(() => {
    if (!mounted.current) {
      mounted.current = true;
      prev.current = { variant, sound };
      return;
    }
    const changed = prev.current.variant !== variant || (!prev.current.sound && sound);
    prev.current = { variant, sound };
    if (changed && sound && cue) play(cue);
  }, [sound, variant, cue]);

  if (!shown) return null;
  const glyph = icon === undefined ? GLYPH[variant] : icon;
  return (
    <div
      data-slot="alert"
      data-variant={variant}
      data-surface="panel"
      role={urgent ? "alert" : "status"}
      className={cn("@container flex w-full items-start gap-3 rounded-lg border bg-card p-3 text-sm text-card-foreground", className)}
      // The edge takes a little of the variant's colour; the glyph and the title carry the meaning.
      style={{ borderColor: `color-mix(in oklab, ${TONE[variant]} 45%, var(--border))`, ...style }}
      {...props}
    >
      {glyph !== null && (
        <span data-slot="alert-icon" aria-hidden="true" className="grid h-5 flex-none place-items-center [&_svg]:size-5" style={{ color: TONE[variant] }}>
          {glyph}
        </span>
      )}
      <div className="grid min-w-0 flex-1 gap-1">{children}</div>
      {dismissible && (
        <IconButton
          aria-label={dismissLabel}
          size="sm"
          className="-my-1 -me-1 text-muted-foreground"
          onClick={() => {
            setShown(false);
            onDismiss?.();
          }}
        >
          <XIcon />
        </IconButton>
      )}
    </div>
  );
}

/** The alert's headline: what happened, in a few words. */
export function AlertTitle({ className, ...props }: ComponentProps<"div">) {
  return <div data-slot="alert-title" className={cn("leading-5 font-semibold break-words", className)} {...props} />;
}

/** What it means and what to do, in the muted text colour. */
export function AlertDescription({ className, ...props }: ComponentProps<"div">) {
  return <div data-slot="alert-description" className={cn("leading-5 break-words text-muted-foreground [&_p+p]:mt-2", className)} {...props} />;
}

/** A row of buttons under the description. Use small buttons. In a narrow alert it also takes the room under the glyph and the close button. */
export function AlertActions({ className, ...props }: ComponentProps<"div">) {
  return <div data-slot="alert-actions" className={cn("mt-2 flex flex-wrap items-center gap-2 @max-3xs:-ms-8 @max-3xs:-me-9", className)} {...props} />;
}

Files and dependencies

Adds
components/ui/alert.tsx
Packages
None
Brings along
Icon buttonChart foundationsSound engine
Icons
circle-checkinfooctagon-alerttriangle-alertx

Changelog

  1. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor