← Components

Communication

Alert

An inline message in the page or container where the problem is. Rises in; dismissing collapses the space smoothly.

Examples and variants

Tones

neutral, info, success, warning, danger.

Neutral
A new version is available
Domain connected
Storage almost full
Free up space to keep syncing.
Example
function AlertTones() {
  return (
    <div className="flex w-full max-w-sm flex-col gap-2">
      <Alert icon={<InfoIcon />}>
        <AlertTitle>Neutral</AlertTitle>
      </Alert>
      <Alert tone="info" icon={<InfoIcon />}>
        <AlertTitle>A new version is available</AlertTitle>
      </Alert>
      <Alert tone="success" icon={<CircleCheckIcon />}>
        <AlertTitle>Domain connected</AlertTitle>
      </Alert>
      <Alert tone="warning" icon={<TriangleAlertIcon />}>
        <AlertTitle>Storage almost full</AlertTitle>
        <AlertDescription>Free up space to keep syncing.</AlertDescription>
      </Alert>
      <Alert tone="danger" icon={<OctagonAlertIcon />} urgent>
        <AlertTitle>Payment failed</AlertTitle>
      </Alert>
    </div>
  )
}

Dismissible

The space closes after the content fades.

Tip
Dismiss me: the space closes smoothly.
Example
function AlertDismissible() {
  const [shown, setShown] = useState(true)
  return shown ? (
    <Alert tone="info" icon={<InfoIcon />} onDismiss={() => setShown(false)} className="w-full max-w-sm">
      <AlertTitle>Tip</AlertTitle>
      <AlertDescription>Dismiss me: the space closes smoothly.</AlertDescription>
    </Alert>
  ) : (
    <Button variant="secondary" onClick={() => setShown(true)}>
      Show again
    </Button>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/alert.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install class-variance-authority cn lucide-react
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/alert.tsxalert
      components/ui/alert.tsx
      "use client"
      
      import * as React from "react"
      import { cva, type VariantProps } from "class-variance-authority"
      import { XIcon } from "lucide-react"
      import { cn } from "cn"
      import { tokenMs } from "@/lib/motion/token-ms"
      import { motionLevel } from "@/lib/overlay"
      
      /*
        Alert: the inline feedback surface (component-model.md §5). It sits in the
        page or container where the problem is. Rises in; the icon pops a beat
        later; warning and danger icons give one small wiggle. Dismissing fades the
        content, then collapses the height so the page glides up.
      */
      const alertVariants = cva(
        "alert-root grid grid-cols-[auto_1fr_auto] items-start gap-x-(--alert-gap) gap-y-1 rounded-(--alert-corner) px-(--alert-padding-x) py-(--alert-padding-y) text-sm",
        {
          variants: {
            tone: {
              neutral: "bg-surface-sunken text-foreground",
              info: "bg-(--color-info-subtle) text-(--color-info-fg)",
              success: "bg-(--color-success-subtle) text-(--color-success-fg)",
              warning: "bg-(--color-warning-subtle) text-(--color-warning-fg)",
              danger: "bg-(--color-danger-subtle) text-(--color-danger-fg)",
            },
          },
          defaultVariants: { tone: "neutral" },
        }
      )
      
      type AlertProps = React.ComponentProps<"div"> &
        VariantProps<typeof alertVariants> & {
          icon?: React.ReactNode
          /** Shows a close button; called after the collapse finishes. */
          onDismiss?: () => void
          /** role="alert" (interrupts screen readers). Only for urgent problems. */
          urgent?: boolean
        }
      
      function Alert({ className, tone = "neutral", icon, onDismiss, urgent = false, children, ...props }: AlertProps) {
        const [leaving, setLeaving] = React.useState(false)
        const ref = React.useRef<HTMLDivElement>(null)
      
        function dismiss() {
          const node = ref.current
          if (!node || motionLevel(node) === "still") {
            onDismiss?.()
            return
          }
          setLeaving(true)
          window.setTimeout(() => onDismiss?.(), tokenMs("--duration-medium", node) + tokenMs("--duration-quick", node))
        }
      
        return (
          <div ref={ref} data-slot="alert-wrap" data-leaving={leaving || undefined} className="alert-wrap grid">
            <div className="min-h-0 overflow-hidden">
              <div
                role={urgent ? "alert" : "status"}
                data-slot="alert"
                data-tone={tone}
                className={cn(alertVariants({ tone }), className)}
                {...props}
              >
                <span className="alert-icon col-start-1 mt-0.5 flex [&_svg]:size-icon-md">{icon}</span>
                <div className="col-start-2 flex flex-col gap-0.5">{children}</div>
                {onDismiss ? (
                  <button
                    type="button"
                    aria-label="Dismiss"
                    onClick={dismiss}
                    className="liquid-press col-start-3 -mr-1 inline-flex size-6 items-center justify-center rounded-(--radius-pill) opacity-70 outline-none hover:bg-[color-mix(in_oklch,currentColor_10%,transparent)] hover:opacity-100 focus-visible:shadow-(--focus-ring)"
                  >
                    <XIcon aria-hidden className="size-icon-sm" />
                  </button>
                ) : null}
              </div>
            </div>
          </div>
        )
      }
      
      function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
        return <div data-slot="alert-title" className={cn("font-medium", className)} {...props} />
      }
      
      function AlertDescription({ className, ...props }: React.ComponentProps<"div">) {
        return <div data-slot="alert-description" className={cn("opacity-90", className)} {...props} />
      }
      
      export { Alert, AlertTitle, AlertDescription, alertVariants }
      

Imports

What the examples on this page import:

Imports
import { CircleCheckIcon, InfoIcon, OctagonAlertIcon, TriangleAlertIcon } from "lucide-react"
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
import { Button } from "@/components/ui/button"

Anatomy

  1. 1Container · --alert-corner, --alert-padding-*, tinted by tone.
  2. 2Icon · --alert-icon-size; pops a beat after the alert.
  3. 3Title and description · Short.
  4. 4Dismiss · Optional close button.

Props

PropTypeDefaultDescription
tone"neutral" | "info" | "success" | "warning" | "danger""neutral"Colour and meaning.
iconReactNode—Leading icon.
onDismiss() => void—Shows a close button; called after the collapse.
urgentbooleanfalserole=alert instead of status.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--alert-cornervar(--shape-md)14pxsame
--alert-padding-xvar(--space-4)16pxsame
--alert-padding-yvar(--space-3)12pxsame
--alert-gapvar(--space-3)12pxsame
--alert-icon-sizevar(--size-icon-md)16pxsame

Motion

TriggerWhat happensTokens
EnterRises in; the icon pops; warning and danger wiggle once.--duration-settle, --ease-bounce
DismissContent fades, then the height collapses.--duration-quick, --duration-medium, --ease-sheet

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • role=status by default; urgent uses role=alert.
  • Text uses the tone's -fg colour (4.5:1).

Usage

Do

  • Put it next to what it's about.

Don’t

  • Don't stack many alerts; fix the cause.

Code

components/ui/alert.tsx
"use client"

import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { XIcon } from "lucide-react"
import { cn } from "cn"
import { tokenMs } from "@/lib/motion/token-ms"
import { motionLevel } from "@/lib/overlay"

/*
  Alert: the inline feedback surface (component-model.md §5). It sits in the
  page or container where the problem is. Rises in; the icon pops a beat
  later; warning and danger icons give one small wiggle. Dismissing fades the
  content, then collapses the height so the page glides up.
*/
const alertVariants = cva(
  "alert-root grid grid-cols-[auto_1fr_auto] items-start gap-x-(--alert-gap) gap-y-1 rounded-(--alert-corner) px-(--alert-padding-x) py-(--alert-padding-y) text-sm",
  {
    variants: {
      tone: {
        neutral: "bg-surface-sunken text-foreground",
        info: "bg-(--color-info-subtle) text-(--color-info-fg)",
        success: "bg-(--color-success-subtle) text-(--color-success-fg)",
        warning: "bg-(--color-warning-subtle) text-(--color-warning-fg)",
        danger: "bg-(--color-danger-subtle) text-(--color-danger-fg)",
      },
    },
    defaultVariants: { tone: "neutral" },
  }
)

type AlertProps = React.ComponentProps<"div"> &
  VariantProps<typeof alertVariants> & {
    icon?: React.ReactNode
    /** Shows a close button; called after the collapse finishes. */
    onDismiss?: () => void
    /** role="alert" (interrupts screen readers). Only for urgent problems. */
    urgent?: boolean
  }

function Alert({ className, tone = "neutral", icon, onDismiss, urgent = false, children, ...props }: AlertProps) {
  const [leaving, setLeaving] = React.useState(false)
  const ref = React.useRef<HTMLDivElement>(null)

  function dismiss() {
    const node = ref.current
    if (!node || motionLevel(node) === "still") {
      onDismiss?.()
      return
    }
    setLeaving(true)
    window.setTimeout(() => onDismiss?.(), tokenMs("--duration-medium", node) + tokenMs("--duration-quick", node))
  }

  return (
    <div ref={ref} data-slot="alert-wrap" data-leaving={leaving || undefined} className="alert-wrap grid">
      <div className="min-h-0 overflow-hidden">
        <div
          role={urgent ? "alert" : "status"}
          data-slot="alert"
          data-tone={tone}
          className={cn(alertVariants({ tone }), className)}
          {...props}
        >
          <span className="alert-icon col-start-1 mt-0.5 flex [&_svg]:size-icon-md">{icon}</span>
          <div className="col-start-2 flex flex-col gap-0.5">{children}</div>
          {onDismiss ? (
            <button
              type="button"
              aria-label="Dismiss"
              onClick={dismiss}
              className="liquid-press col-start-3 -mr-1 inline-flex size-6 items-center justify-center rounded-(--radius-pill) opacity-70 outline-none hover:bg-[color-mix(in_oklch,currentColor_10%,transparent)] hover:opacity-100 focus-visible:shadow-(--focus-ring)"
            >
              <XIcon aria-hidden className="size-icon-sm" />
            </button>
          ) : null}
        </div>
      </div>
    </div>
  )
}

function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
  return <div data-slot="alert-title" className={cn("font-medium", className)} {...props} />
}

function AlertDescription({ className, ...props }: React.ComponentProps<"div">) {
  return <div data-slot="alert-description" className={cn("opacity-90", className)} {...props} />
}

export { Alert, AlertTitle, AlertDescription, alertVariants }