---
title: Alert for Astro
description: "Inline Astro feedback with message, action, and semantic status styles."
seo:
    title: Alert for Astro | Shadcn & Tailwind CSS
related:
    - /components/field
    - /components/alert-dialog
sidebar:
    label: "Alert"
---

```astro
---
import CircleAlert from "@lucide/astro/icons/circle-exclamation-point"
import CircleCheck from "@lucide/astro/icons/circle-check"
import Info from "@lucide/astro/icons/info"
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"

const messages = [
  {
    variant: "default" as const,
    title: "Review window opens Monday",
    detail: "Add content notes to the project before the review starts.",
    icon: Info,
  },
  {
    variant: "info" as const,
    title: "Preview link updated",
    detail:
      "Use the new link in the project summary to check the current pages.",
    icon: Info,
  },
  {
    variant: "success" as const,
    title: "Content saved",
    detail: "Your page changes are ready for the next review.",
    icon: CircleCheck,
  },
  {
    variant: "warning" as const,
    title: "Two images need descriptions",
    detail: "Add useful alt text before publishing the gallery.",
    icon: CircleAlert,
  },
  {
    variant: "error" as const,
    title: "The draft could not be saved",
    detail: "Keep this page open and try again when the connection returns.",
    icon: CircleAlert,
  },
  {
    variant: "destructive" as const,
    title: "The upload was rejected",
    detail: "Choose an image file smaller than the project limit.",
    icon: CircleAlert,
  },
]
---

<div class="grid w-full max-w-xl gap-4">
  {messages.map(({ variant, title, detail, icon: Icon }) => (
    <Alert variant={variant} role="note">
      <Icon aria-hidden="true" />
      <AlertTitle>{title}</AlertTitle>
      <AlertDescription>{detail}</AlertDescription>
    </Alert>
  ))}
</div>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/alert
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/components/ui/alert/alert-variants.ts

```ts
import { cva, type VariantProps } from 'class-variance-authority'

export type AlertVariants = VariantProps<typeof alertVariants>

export const alertVariants = cva("relative grid w-full items-start gap-x-2 gap-y-0.5 rounded-xl border px-3.5 py-3 text-card-foreground text-sm has-[>svg]:has-data-[slot=alert-action]:grid-cols-[calc(var(--spacing)*4)_1fr_auto] has-[>svg]:grid-cols-[calc(var(--spacing)*4)_1fr] has-data-[slot=alert-action]:grid-cols-[1fr_auto] has-[>svg]:gap-x-2 [&>svg]:h-lh [&>svg]:w-4 group/alert", {
    defaultVariants: {
        variant: 'default',
    },
    variants: {
        variant: {
            destructive: "border-destructive/32 bg-destructive/4 [&>svg]:text-destructive",
            default: "bg-transparent dark:bg-input/32 [&>svg]:text-muted-foreground",
            error: 'border-destructive/32 bg-destructive/4 [&>svg]:text-destructive',
            info: 'border-info/32 bg-info/4 [&>svg]:text-info',
            success: 'border-success/32 bg-success/4 [&>svg]:text-success',
            warning: 'border-warning/32 bg-warning/4 [&>svg]:text-warning',
        },
    },
})
```

#### src/components/ui/alert/alert-action.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="alert-action"
  class={cn("flex gap-1 max-sm:col-start-2 max-sm:mt-2 sm:row-start-1 sm:row-end-3 sm:self-center sm:[[data-slot=alert-description]~&]:col-start-2 sm:[[data-slot=alert-title]~&]:col-start-2 sm:[svg~&]:col-start-2 sm:[svg~[data-slot=alert-description]~&]:col-start-3 sm:[svg~[data-slot=alert-title]~&]:col-start-3", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/alert/alert-description.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="alert-description"
  class={cn("flex flex-col gap-2.5 text-muted-foreground [svg~&]:col-start-2", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/alert/alert-title.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div data-slot="alert-title" class={cn("font-medium [svg~&]:col-start-2", className)} {...props}>
  <slot />
</div>
```

#### src/components/ui/alert/alert.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"

import type { AlertVariants } from "./alert-variants"
import { alertVariants } from "./alert-variants"

type Props = AlertVariants & HTMLAttributes<"div">

const { class: className, variant, ...props } = Astro.props
---

<div
  data-slot="alert"
  data-variant={variant ?? "default"}
  role="alert"
  class={cn(alertVariants({ variant }), className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/alert/index.ts

```ts
export { default as Alert } from "./alert.astro"
export { default as AlertAction } from "./alert-action.astro"
export { default as AlertTitle } from "./alert-title.astro"
export { default as AlertDescription } from "./alert-description.astro"
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```ts
import { Alert, AlertAction, AlertDescription, AlertTitle } from "@/components/ui/alert"
```

```astro
<Alert variant="default">
  <Terminal />
  <AlertTitle>Content review is ready</AlertTitle>
  <AlertDescription>
    Three pages have updated copy. Review the changes before publishing.
  </AlertDescription>
</Alert>
```

## Example

Use `AlertAction` when the alert should include a secondary control such as a
button.

```astro
---
import {
  Alert,
  AlertAction,
  AlertDescription,
  AlertTitle,
} from "@/components/ui/alert"
import { Button } from "@/components/ui/button"
---

<Alert role="note">
  <AlertTitle>Content review is ready</AlertTitle>
  <AlertDescription>
    Three page descriptions changed since the last review.
  </AlertDescription>
  <AlertAction>
    <Button as="a" href="/blocks/content" size="sm" variant="outline">
      Review content
    </Button>
  </AlertAction>
</Alert>
```

## Composition

Arrange the parts as shown for an `Alert`:

```text
Alert
├── Icon
├── AlertTitle
├── AlertDescription
└── AlertAction
```

## Notes

- Use `Alert` for inline feedback inside the page flow.
- Use `variant="error"` for a failed action and `variant="warning"` for a
  condition that needs attention. `destructive` uses the selected preset's
  destructive recipe; `error` keeps the Gnostic status treatment.
- For destructive confirmation flows, use
  [Alert Dialog](/components/alert-dialog) instead.

## API Reference

Read the [installed source](/r/alert.json) for the complete prop types.

## When to use Alert

Use an Alert near the content that needs attention. Put the main fact in `AlertTitle`, then put the cause or next step in `AlertDescription`. An optional icon is a direct child. Add `AlertAction` only when a short action helps the reader resolve the message. An Alert stays in the page layout; it is not a toast service.

## Keyboard and behavior

The root has `role="alert"` by default. Reserve an interrupting announcement for an urgent change. For an ordinary static note, pass an appropriate role such as `role="note"`. The component has no dismissal or notification queue script. A button inside an alert keeps its native keyboard behavior.

## Theme and layout

Choose `info`, `success`, `warning`, or `error` to match the message. `default` and `destructive` use the selected preset's recipes. Status variants keep their Gnostic treatment and use the shared semantic tokens. State the result in text as well as color. Shared semantic tokens control the border, icon, and tint in both color modes; the neutral variant suits general page guidance.
