Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Alert for Astro

Inline Astro feedback with message, action, and semantic status styles.

Installation

bunx --bun shadcn@latest add @gnostic/alert
npx shadcn@latest add @gnostic/alert
pnpm dlx shadcn@latest add @gnostic/alert

Component classes: Default Gnostic.

Complete the Installation guide first. Then copy the component files below.

No extra packages are required.

Copy the files

src/components/ui/alert/alert-variants.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
---
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
---
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
---
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
---
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
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"

Usage

import { Alert, AlertAction, AlertDescription, AlertTitle } from "@/components/ui/alert"
<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.

Composition

Arrange the parts as shown for an Alert:

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 instead.

API Reference

Read the installed source 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.