# Alert

Displays a callout for user attention with optional title, description, and action.

## Installation

```bash
npx shadcn@latest add https://reactdocs.canceydejean.dev/r/alert.json
```

[Registry JSON](https://reactdocs.canceydejean.dev/r/alert.json)

## Preview

```tsx
import { IconTerminal } from "@tabler/icons-react";

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

export function Preview() {
  return (
    <Alert className="w-full max-w-md">
      <IconTerminal />
      <AlertTitle>Heads up!</AlertTitle>
      <AlertDescription>
        You can add components and dependencies to your app using the CLI.
      </AlertDescription>
    </Alert>
  );
}
```


## Source

### alert.tsx

```tsx
import { type VariantProps } from "class-variance-authority";
import * as React from "react";

import { cn } from "cn";

import { alertVariants } from "./alert.variants";

function Alert({
  className,
  variant,
  ...props
}: React.ComponentProps<"div"> & VariantProps<typeof alertVariants>) {
  return (
    <div
      data-slot="alert"
      role="alert"
      className={cn(alertVariants({ variant }), className)}
      {...props}
    />
  );
}

function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="alert-title"
      className={cn(
        "[&_a]:hover:text-foreground font-medium group-has-[>svg]/alert:col-start-2 [&_a]:underline [&_a]:underline-offset-3",
        className,
      )}
      {...props}
    />
  );
}

function AlertDescription({
  className,
  ...props
}: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="alert-description"
      className={cn(
        "text-muted-foreground [&_a]:hover:text-foreground text-sm text-balance md:text-pretty [&_a]:underline [&_a]:underline-offset-3 [&_p:not(:last-child)]:mb-4",
        className,
      )}
      {...props}
    />
  );
}

function AlertAction({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="alert-action"
      className={cn("absolute top-2 right-2", className)}
      {...props}
    />
  );
}

export { Alert, AlertTitle, AlertDescription, AlertAction };
```


### alert.variants.ts

```ts
import { cva } from "class-variance-authority";

import { cn } from "cn"

const alertBase = cn(
  "group/alert relative grid w-full gap-0.5 rounded-lg border px-2.5 py-2 text-left text-sm has-data-[slot=alert-action]:relative has-data-[slot=alert-action]:pr-18 has-[>svg]:grid-cols-[auto_1fr] has-[>svg]:gap-x-2 *:[svg]:row-span-2 *:[svg]:translate-y-0.5 *:[svg]:text-current *:[svg:not([class*='size-'])]:size-4",
);

const alertConfig = {
  variants: {
    variant: {
      default: "bg-card text-card-foreground",
      destructive:
        "bg-card text-destructive *:data-[slot=alert-description]:text-destructive/90 *:[svg]:text-current",
    },
  },
  compoundVariants: [],
  defaultVariants: {
    variant: "default",
  },
} as const;

export const alertCva = cva(alertBase, {
  ...alertConfig,
  compoundVariants: [...alertConfig.compoundVariants],
});

// Export the variant variants
export const ALERT_VARIANTS = Object.keys(
  alertConfig.variants.variant,
) as (keyof typeof alertConfig.variants.variant)[];

export const alertVariants = alertCva;
```



## Usage

Use alerts for status messages, warnings, and important callouts.

```tsx
import {
  Alert,
  AlertAction,
  AlertDescription,
  AlertTitle,
} from "@/components/alert/alert";

export function Example() {
  return (
    <Alert>
      <AlertTitle>Heads up!</AlertTitle>
      <AlertDescription>
        You can add components and dependencies to your app using the CLI.
      </AlertDescription>
    </Alert>
  );
}
```

Pass `variant="destructive"` for error-style callouts. Compose an icon as a child of `Alert` for
leading indicators, and use `AlertAction` for a corner action.

