Alert

A message in the flow of the page.

An alert is a status region, so screen readers announce it when it appears. Pass role="alert" for something that needs attention right away. An icon as the first child gets its own column.

Preview
variants.tsx
import { TriangleAlert } from "lucide-react"

import { Alert, AlertDescription, AlertTitle } from "~/components/ui/alert"

export default function Example() {
  return (
    <div className="grid w-full max-w-md gap-md">
      <Alert>
        <AlertTitle>Agent execution is not available yet</AlertTitle>
        <AlertDescription>
          OpenRouter is not configured. Human workflows remain available.
        </AlertDescription>
      </Alert>
      <Alert variant="warning">
        <TriangleAlert />
        <AlertTitle>Copy runs long</AlertTitle>
        <AlertDescription>The lead story overflows its frame by 12 lines.</AlertDescription>
      </Alert>
      <Alert variant="destructive" role="alert">
        <AlertTitle>Export failed</AlertTitle>
        <AlertDescription>Two pages have unresolved errors.</AlertDescription>
      </Alert>
    </div>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesTextprimitive
Used byNothing yet.
Packages tailwind-variants@4.1.0

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/alert.json

Manually

Install its packages:

terminal
npm install tailwind-variants@4.1.0

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/alert.tsx
import type * as React from "react"
import { tv, type VariantProps } from "tailwind-variants"

import { textVariants } from "~/components/ui/text"
import { cn } from "~/lib/utils"

const alertVariants = tv({
  base: [
    textVariants({ size: "sm" }),
    "relative grid w-full gap-3xs rounded-lg border border-border bg-card px-lg py-md text-left text-card-foreground",
    "has-data-[slot=alert-action]:pr-4xl has-[>svg]:grid-cols-[auto_1fr] has-[>svg]:gap-x-md",
    "*:[svg]:row-span-2 *:[svg]:translate-y-0.5 *:[svg]:text-current *:[svg:not([class*='size-'])]:size-4",
  ],
  variants: {
    variant: {
      default: "",
      destructive: "text-destructive *:data-[slot=alert-description]:text-destructive/90",
      warning: "text-warning *:data-[slot=alert-description]:text-warning/90",
    },
  },
  defaultVariants: { variant: "default" },
})

type AlertProps = React.ComponentProps<"div"> & {
  /** Its tone. `destructive` and `warning` tint the text, not the surface. */
  variant?: NonNullable<VariantProps<typeof alertVariants>["variant"]>
}

/**
 * A message in the flow of the page. It's a `status` region, read out when it
 * appears; pass `role="alert"` for something urgent.
 */
function Alert({ variant, className, ...props }: AlertProps) {
  return (
    <div
      data-slot="alert"
      // oxlint-disable-next-line jsx-a11y/prefer-tag-over-role -- <output> is for a form's computed result, not a general message
      role="status"
      className={cn(alertVariants({ variant }), className)}
      {...props}
    />
  )
}

/** The alert's heading. */
function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="alert-title"
      className={cn(
        textVariants({ weight: "medium" }),
        "[&_a]:underline [&_a]:underline-offset-3",
        "[[data-slot=alert]:has(>svg)_&]:col-start-2",
        className,
      )}
      {...props}
    />
  )
}

/** The alert's body. */
function AlertDescription({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="alert-description"
      className={cn(
        textVariants({ size: "sm", tone: "muted" }),
        "text-balance md:text-pretty [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground [&_p:not(:last-child)]:mb-lg",
        "[[data-slot=alert]:has(>svg)_&]:col-start-2",
        className,
      )}
      {...props}
    />
  )
}

/** An action pinned to the alert's top-right corner, like a dismiss button. */
function AlertAction({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="alert-action"
      className={cn("absolute top-2.5 right-3", className)}
      {...props}
    />
  )
}

export { Alert, AlertAction, AlertDescription, AlertTitle, alertVariants }
export type { AlertProps }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Alert

A message in the flow of the page. It's a status region, read out when it appears; pass role="alert" for something urgent.

variantours

Its tone. destructive and warning tint the text, not the surface.

Type NonNullable<"default" | "destructive" | "warning">

AlertAction

An action pinned to the alert's top-right corner, like a dismiss button.

Takes the props of a native div.

AlertDescription

The alert's body.

Takes the props of a native div.

AlertTitle

The alert's heading.

Takes the props of a native div.