Alert
The shadcn/ui Alert with tinted success, warning and info variants for dark UIs and a slot for actions — plus two animated React alert blocks built with Motion: a dismissible alert that collapses smoothly and can auto-dismiss with a progress line, and an announcement banner that slides in from the top of the page.
- Drop-in shadcn API
- Status variants
- Motion blocks
- prefers-reduced-motion
Content below slides up as the alerts close.
"use client";
import * as React from "react";
import { RotateCcw } from "lucide-react";
import { Button } from "@/components/ui/button";
import { DismissibleAlert } from "@/components/dusk/alert/dismissible-alert";
export default function DismissibleAlertDemo() {
// Bumping the key remounts both alerts, bringing them back.
const [round, setRound] = React.useState(0);
return (
<div className="flex w-full max-w-lg flex-col gap-3">
<div key={round} className="flex flex-col gap-3">
<DismissibleAlert
variant="info"
title="New: branch previews"
actions={
<>
<Button size="sm" variant="soft">Try it</Button>
<Button size="sm" variant="ghost">Learn more</Button>
</>
}
>
Every pull request now gets its own URL. Share it with reviewers in one click.
</DismissibleAlert>
<DismissibleAlert variant="success" title="Settings saved" autoDismiss={5000}>
Closes by itself in 5 seconds. Hover or focus it to pause.
</DismissibleAlert>
</div>
<p className="text-sm text-muted-foreground">Content below slides up as the alerts close.</p>
<Button variant="outline" size="sm" className="self-start" onClick={() => setRound((r) => r + 1)}>
<RotateCcw />
Reset
</Button>
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/alert.json1. Install the dependencies.
npm install class-variance-authority2. Copy the source into
components/ui/alert.tsx.alert.tsx import * as React from "react"; import { cva, type VariantProps } from "class-variance-authority"; import { cn } from "@/lib/utils"; /** * DuskUI Alert — a drop-in upgrade of the shadcn/ui Alert. * * Same parts, props and data-slot attributes as shadcn (Alert, AlertTitle, * AlertDescription, variants `default` and `destructive`) plus: * - Tinted status variants for dark UIs: `success`, `warning`, `info`. * - `AlertAction`: a top-right slot for a close button or menu; the text * makes room for it automatically. * - Titles wrap instead of being clipped to one line. * CSS only, so it works in Server Components. */ const alertVariants = cva( [ "relative grid w-full grid-cols-[0_1fr] items-start gap-y-1 rounded-xl border px-4 py-3.5 text-sm", "has-[>svg]:grid-cols-[calc(var(--spacing)*4)_1fr] has-[>svg]:gap-x-3", "has-data-[slot=alert-action]:pr-12", "[&>svg]:size-4 [&>svg]:translate-y-0.5 [&>svg]:text-current", ], { variants: { variant: { default: "bg-card text-card-foreground", destructive: "border-destructive/30 bg-destructive/[0.08] text-destructive *:data-[slot=alert-description]:text-destructive", success: "border-emerald-400/20 bg-emerald-400/[0.06] text-emerald-300 *:data-[slot=alert-description]:text-emerald-100/70", warning: "border-amber-400/20 bg-amber-400/[0.06] text-amber-300 *:data-[slot=alert-description]:text-amber-100/70", info: "border-sky-400/20 bg-sky-400/[0.06] text-sky-300 *:data-[slot=alert-description]:text-sky-100/70", }, }, defaultVariants: { variant: "default", }, } ); export interface AlertProps extends React.ComponentProps<"div">, VariantProps<typeof alertVariants> {} /** * A callout for important messages. Put an optional lucide icon first, then * AlertTitle and/or AlertDescription. Uses role="alert" like shadcn; pass * role="status" for non-urgent messages so screen readers don't interrupt. */ function Alert({ className, variant, ...props }: AlertProps) { return ( <div data-slot="alert" data-variant={variant ?? "default"} role="alert" 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("col-start-2 min-h-4 leading-snug font-medium tracking-tight", className)} {...props} /> ); } /** Supporting text. Paragraphs, lists and links inside are spaced for you. */ function AlertDescription({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="alert-description" className={cn( "col-start-2 grid justify-items-start gap-1 text-sm text-muted-foreground [&_p]:leading-relaxed", "[&_a]:font-medium [&_a]:text-current [&_a]:underline [&_a]:underline-offset-4", className )} {...props} /> ); } /** Top-right slot for a close button or menu. The alert's text makes room for it. */ function AlertAction({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="alert-action" className={cn("absolute top-2.5 right-2.5 flex items-center gap-1", className)} {...props} /> ); } export { Alert, AlertTitle, AlertDescription, AlertAction, alertVariants };
Same exports as shadcn/ui (Alert, AlertTitle, AlertDescription) plus AlertAction and the success, warning and info variants. CSS only, so it works in Server Components.
Usage
import { TriangleAlert } from "lucide-react"
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
export function BuildWarning() {
return (
<Alert variant="warning">
<TriangleAlert />
<AlertTitle>You're at 90% of your build minutes</AlertTitle>
<AlertDescription>Builds pause when you hit the limit.</AlertDescription>
</Alert>
)
}Examples
Default and destructive
The shadcn/ui default and destructive variants. Alerts use role="alert" like shadcn; pass role="status" for messages that shouldn't interrupt a screen reader.
Sign in again to keep editing. Unsaved changes are stored locally.
import { CircleAlert, Terminal } from "lucide-react";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
export default function AlertBasicDemo() {
return (
<div className="grid w-full max-w-lg gap-4">
<Alert role="status">
<Terminal />
<AlertTitle>Heads up!</AlertTitle>
<AlertDescription>
You can add components to your app with the shadcn CLI.
</AlertDescription>
</Alert>
<Alert variant="destructive">
<CircleAlert />
<AlertTitle>Your session has expired</AlertTitle>
<AlertDescription>
<p>Sign in again to keep editing. Unsaved changes are stored locally.</p>
</AlertDescription>
</Alert>
</div>
);
}Status variants
success, warning and info use translucent tints that read well on near-black surfaces. Links inside the description inherit the alert's color and are underlined.
Builds pause when you hit the limit. Upgrade your plan to keep shipping.
import { CircleCheck, Info, TriangleAlert } from "lucide-react";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
export default function AlertVariantsDemo() {
return (
<div className="grid w-full max-w-lg gap-4">
<Alert variant="success" role="status">
<CircleCheck />
<AlertTitle>Deployment complete</AlertTitle>
<AlertDescription>dusk-web is live on 3 regions in 42 seconds.</AlertDescription>
</Alert>
<Alert variant="warning">
<TriangleAlert />
<AlertTitle>You're at 90% of your build minutes</AlertTitle>
<AlertDescription>
<p>
Builds pause when you hit the limit. <a href="#">Upgrade your plan</a> to keep shipping.
</p>
</AlertDescription>
</Alert>
<Alert variant="info" role="status">
<Info />
<AlertTitle>Scheduled maintenance on Sunday, 02:00 UTC</AlertTitle>
</Alert>
</div>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Dismissible Alert
An Alert with a close button. On dismiss it fades out while its height collapses, so the content below slides up instead of jumping. Set autoDismiss (ms) and it closes itself, drawing a shrinking line along the bottom; the countdown pauses while the alert is hovered or focused and while the tab is hidden. Escape inside the alert dismisses it too.
Content below slides up as the alerts close.
"use client";
import * as React from "react";
import { RotateCcw } from "lucide-react";
import { Button } from "@/components/ui/button";
import { DismissibleAlert } from "@/components/dusk/alert/dismissible-alert";
export default function DismissibleAlertDemo() {
// Bumping the key remounts both alerts, bringing them back.
const [round, setRound] = React.useState(0);
return (
<div className="flex w-full max-w-lg flex-col gap-3">
<div key={round} className="flex flex-col gap-3">
<DismissibleAlert
variant="info"
title="New: branch previews"
actions={
<>
<Button size="sm" variant="soft">Try it</Button>
<Button size="sm" variant="ghost">Learn more</Button>
</>
}
>
Every pull request now gets its own URL. Share it with reviewers in one click.
</DismissibleAlert>
<DismissibleAlert variant="success" title="Settings saved" autoDismiss={5000}>
Closes by itself in 5 seconds. Hover or focus it to pause.
</DismissibleAlert>
</div>
<p className="text-sm text-muted-foreground">Content below slides up as the alerts close.</p>
<Button variant="outline" size="sm" className="self-start" onClick={() => setRound((r) => r + 1)}>
<RotateCcw />
Reset
</Button>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/dismissible-alert.json1. Install the dependencies.
npm install motion lucide-react2. Copy the source into
components/dusk/alert/dismissible-alert.tsx.dismissible-alert.tsx "use client"; import * as React from "react"; import { animate, AnimatePresence, motion, useMotionValue, useReducedMotion, type AnimationPlaybackControls, } from "motion/react"; import { CircleAlert, CircleCheck, Info, TriangleAlert, X } from "lucide-react"; import { Alert, AlertAction, AlertDescription, AlertTitle, type AlertProps } from "@/components/ui/alert"; import { cn } from "@/lib/utils"; type Variant = NonNullable<AlertProps["variant"]>; const defaultIcons: Record<Variant, React.ReactNode> = { default: <Info />, info: <Info />, success: <CircleCheck />, warning: <TriangleAlert />, destructive: <CircleAlert />, }; // The auto-dismiss line takes the alert's text color. const barColor: Record<Variant, string> = { default: "bg-foreground", info: "bg-sky-400", success: "bg-emerald-400", warning: "bg-amber-400", destructive: "bg-destructive", }; export interface DismissibleAlertProps extends Omit<AlertProps, "title" | "children"> { title?: React.ReactNode; /** The alert's description. */ children?: React.ReactNode; /** Leading icon. Defaults to one matching the variant; pass `null` to hide it. */ icon?: React.ReactNode; /** Extra content under the description, e.g. buttons. */ actions?: React.ReactNode; open?: boolean; /** Uncontrolled initial state. */ defaultOpen?: boolean; /** Called with `false` when the alert is dismissed (button, Escape or timeout). */ onOpenChange?: (open: boolean) => void; /** Called after the exit animation, once the alert is gone from the layout. */ onExitComplete?: () => void; /** Dismiss on its own after this many ms, with a progress line along the bottom. Pauses on hover, focus and hidden tabs. */ autoDismiss?: number; /** Show the close button. */ dismissible?: boolean; /** Accessible label for the close button. */ dismissLabel?: string; } const collapse = { type: "spring", duration: 0.4, bounce: 0 } as const; /** * An Alert that can be closed. On dismiss it fades out while its * height (and margin) collapses, so the content below slides up instead of * jumping. With `autoDismiss` it closes itself after a delay and draws a * shrinking progress line; the countdown pauses while the alert is hovered or * focused and while the tab is hidden. Press Escape inside it to dismiss. * Urgent variants (destructive, warning) use role="alert", the rest * role="status", so screen readers only interrupt when it matters. */ export function DismissibleAlert({ open: openProp, defaultOpen = true, onOpenChange, onExitComplete, ...props }: DismissibleAlertProps) { const reduceMotion = useReducedMotion(); const [internalOpen, setInternalOpen] = React.useState(defaultOpen); const open = openProp ?? internalOpen; const dismiss = () => { if (openProp === undefined) setInternalOpen(false); onOpenChange?.(false); }; return ( <AnimatePresence initial={false} onExitComplete={onExitComplete}> {open && ( <motion.div key="alert" className="overflow-hidden" initial={{ height: 0, opacity: 0 }} animate={{ height: "auto", opacity: 1 }} exit={{ height: 0, opacity: 0 }} transition={reduceMotion ? { duration: 0.15 } : collapse} > <motion.div initial={reduceMotion ? false : { scale: 0.98 }} animate={{ scale: 1 }} exit={reduceMotion ? undefined : { scale: 0.98 }} transition={collapse} > <AlertSurface {...props} onDismiss={dismiss} /> </motion.div> </motion.div> )} </AnimatePresence> ); } type AlertSurfaceProps = Omit< DismissibleAlertProps, "open" | "defaultOpen" | "onOpenChange" | "onExitComplete" > & { onDismiss: () => void }; /** The visible alert. Mounted only while open, so the countdown state starts fresh every time. */ function AlertSurface({ variant = "default", title, children, icon, actions, autoDismiss, dismissible = true, dismissLabel = "Dismiss", onDismiss, className, role, onKeyDown, onPointerEnter, onPointerLeave, onFocus, onBlur, ...props }: AlertSurfaceProps) { const v = variant ?? "default"; const shownIcon = icon === undefined ? defaultIcons[v] : icon; // A motion value drives both the timer and the progress line, so they can't drift apart. const progress = useMotionValue(1); const controls = React.useRef<AnimationPlaybackControls | null>(null); const [hovered, setHovered] = React.useState(false); const [focused, setFocused] = React.useState(false); const tabHidden = useDocumentHidden(); const paused = hovered || focused || tabHidden; const onTimeout = React.useEffectEvent(() => onDismiss()); const isPaused = React.useEffectEvent(() => paused); React.useEffect(() => { if (!autoDismiss) return; progress.set(1); const animation = animate(progress, 0, { duration: autoDismiss / 1000, ease: "linear", onComplete: () => onTimeout(), }); if (isPaused()) animation.pause(); controls.current = animation; return () => { animation.stop(); controls.current = null; }; }, [autoDismiss, progress]); React.useEffect(() => { if (paused) controls.current?.pause(); else controls.current?.play(); }, [paused]); return ( <Alert variant={variant} role={role ?? (v === "destructive" || v === "warning" ? "alert" : "status")} className={cn("overflow-hidden", className)} {...props} onPointerEnter={(event) => { onPointerEnter?.(event); setHovered(true); }} onPointerLeave={(event) => { onPointerLeave?.(event); setHovered(false); }} onFocus={(event) => { onFocus?.(event); setFocused(true); }} onBlur={(event) => { onBlur?.(event); if (!event.currentTarget.contains(event.relatedTarget as Node | null)) setFocused(false); }} onKeyDown={(event) => { onKeyDown?.(event); if (dismissible && event.key === "Escape" && !event.defaultPrevented) onDismiss(); }} > {shownIcon} {title && <AlertTitle>{title}</AlertTitle>} {children && <AlertDescription>{children}</AlertDescription>} {actions && <div className="col-start-2 mt-2 flex flex-wrap gap-2">{actions}</div>} {dismissible && ( <AlertAction> <button type="button" aria-label={dismissLabel} onClick={onDismiss} className={cn( "inline-flex size-7 items-center justify-center rounded-md text-current opacity-70 outline-none", "transition-[opacity,background-color,box-shadow] duration-150 hover:bg-white/10 hover:opacity-100", "focus-visible:opacity-100 focus-visible:ring-[3px] focus-visible:ring-ring/50" )} > <X className="size-4" aria-hidden /> </button> </AlertAction> )} {autoDismiss ? ( <motion.span aria-hidden data-slot="alert-progress" className={cn("absolute inset-x-0 bottom-0 h-0.5 origin-left opacity-60", barColor[v])} style={{ scaleX: progress }} /> ) : null} </Alert> ); } function subscribeVisibility(callback: () => void) { document.addEventListener("visibilitychange", callback); return () => document.removeEventListener("visibilitychange", callback); } /** `true` while the tab is in the background. Always `false` on the server. */ function useDocumentHidden() { return React.useSyncExternalStore( subscribeVisibility, () => document.hidden, () => false ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | "default" | "destructive" | "success" | "warning" | "info" | "default" | Alert variant. destructive and warning use role="alert", the rest role="status". |
| title | ReactNode | — | Alert title. |
| children | ReactNode | — | The description. |
| icon | ReactNode | null | — | Leading icon. Defaults to one per variant; null hides it. |
| actions | ReactNode | — | Buttons under the description. |
| open / defaultOpen / onOpenChange | boolean / boolean / (open: boolean) => void | — / true / — | Controlled or uncontrolled visibility. |
| onExitComplete | () => void | — | Runs once the exit animation has finished. |
| autoDismiss | number | — | Close after this many ms, with a progress line. |
| dismissible | boolean | true | Show the close button and allow Escape. |
| dismissLabel | string | "Dismiss" | Accessible label of the close button. |
Banner Alert
A full-width announcement bar for the top of a page. It slides down from above while its height grows, so the page eases down rather than jumping. Pass storageKey to remember the dismissal in localStorage; the stored value is read after hydration with useSyncExternalStore, so server and client HTML always match. For zero layout shift, render it inside a fixed or sticky header.
Usage-based billing starts June 1. Review your plan before then.See what changes
"use client";
import * as React from "react";
import { ArrowRight, RotateCcw } from "lucide-react";
import { Button } from "@/components/ui/button";
import { BannerAlert } from "@/components/dusk/alert/banner-alert";
export default function BannerAlertDemo() {
const [open, setOpen] = React.useState(true);
return (
<div className="flex w-full max-w-2xl flex-col items-center gap-4">
{/* A mock app window so the banner has a "top of page" to live at. */}
<div className="w-full overflow-hidden rounded-xl border bg-background">
<BannerAlert
open={open}
onOpenChange={setOpen}
action={
<a href="#" className="inline-flex items-center gap-1 font-medium text-foreground underline-offset-4 hover:underline">
See what changes
<ArrowRight className="size-3.5" aria-hidden />
</a>
}
>
<span className="font-medium">Usage-based billing starts June 1.</span>
<span className="text-muted-foreground"> Review your plan before then.</span>
</BannerAlert>
<div className="flex items-center gap-3 border-b px-4 py-3">
<span className="size-5 rounded-md bg-white/15" aria-hidden />
<span className="text-sm font-medium">Acme</span>
<span className="ml-auto h-2 w-24 rounded-full bg-white/10" aria-hidden />
</div>
<div className="grid gap-2 p-4" aria-hidden>
<div className="h-3 w-2/3 rounded-full bg-white/[0.06]" />
<div className="h-3 w-1/2 rounded-full bg-white/[0.06]" />
<div className="h-3 w-3/5 rounded-full bg-white/[0.06]" />
</div>
</div>
<Button variant="outline" size="sm" onClick={() => setOpen(true)} disabled={open}>
<RotateCcw />
Show banner
</Button>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/banner-alert.json1. Install the dependencies.
npm install motion lucide-react2. Copy the source into
components/dusk/alert/banner-alert.tsx.banner-alert.tsx "use client"; import * as React from "react"; import { AnimatePresence, motion, useReducedMotion } from "motion/react"; import { CircleAlert, CircleCheck, Info, Megaphone, TriangleAlert, X } from "lucide-react"; import { cn } from "@/lib/utils"; type BannerVariant = "default" | "info" | "success" | "warning" | "destructive"; const variantStyles: Record<BannerVariant, { surface: string; icon: string }> = { default: { surface: "border-white/10 bg-card", icon: "bg-white/10 text-foreground", }, info: { surface: "border-sky-400/20 bg-sky-950/60", icon: "bg-sky-400/15 text-sky-300", }, success: { surface: "border-emerald-400/20 bg-emerald-950/60", icon: "bg-emerald-400/15 text-emerald-300", }, warning: { surface: "border-amber-400/20 bg-amber-950/60", icon: "bg-amber-400/15 text-amber-300", }, destructive: { surface: "border-destructive/30 bg-red-950/60", icon: "bg-destructive/20 text-red-300", }, }; const defaultIcons: Record<BannerVariant, React.ReactNode> = { default: <Megaphone />, info: <Info />, success: <CircleCheck />, warning: <TriangleAlert />, destructive: <CircleAlert />, }; export interface BannerAlertProps extends Omit<React.ComponentProps<"section">, "children" | "onAnimationStart" | "onDrag" | "onDragStart" | "onDragEnd"> { /** The announcement text. Keep it to one line on desktop. */ children: React.ReactNode; variant?: BannerVariant; /** Leading icon. Defaults to one matching the variant; pass `null` to hide it. */ icon?: React.ReactNode; /** A call to action after the text, e.g. a link. */ action?: React.ReactNode; open?: boolean; defaultOpen?: boolean; onOpenChange?: (open: boolean) => void; /** Show the close button. */ dismissible?: boolean; dismissLabel?: string; /** * Remember the dismissal in localStorage under this key, so the banner stays * closed on later visits. Change the key to show a new announcement. */ storageKey?: string; /** Slide in after mount instead of rendering open. */ animateOnMount?: boolean; /** Seconds to wait before sliding in. */ delay?: number; /** Accessible name of the region. */ label?: string; } const slide = { type: "spring", duration: 0.4, bounce: 0 } as const; /** * A full-width announcement bar for the top of a page or app shell. It slides * down from above while its height grows, so the page eases down instead of * jumping. Dismissing slides it * back up. Pass `storageKey` to remember the dismissal across visits; the * stored value is read after hydration, so server and client HTML always match. * Renders a labelled <section> (not role="alert"), so it never interrupts * screen readers. */ export function BannerAlert({ children, variant = "default", icon, action, open: openProp, defaultOpen = true, onOpenChange, dismissible = true, dismissLabel = "Dismiss announcement", storageKey, animateOnMount = true, delay = 0.2, label = "Announcement", className, ...props }: BannerAlertProps) { const reduceMotion = useReducedMotion(); const [internalOpen, setInternalOpen] = React.useState(defaultOpen); const storedDismissed = useStoredDismissal(storageKey); const open = (openProp ?? internalOpen) && !storedDismissed; const styles = variantStyles[variant]; const shownIcon = icon === undefined ? defaultIcons[variant] : icon; const dismiss = () => { if (storageKey) writeDismissal(storageKey); if (openProp === undefined) setInternalOpen(false); onOpenChange?.(false); }; return ( <AnimatePresence initial={animateOnMount}> {open && ( <motion.div key="banner" className="overflow-hidden" initial={{ height: 0 }} animate={{ height: "auto", transition: reduceMotion ? { duration: 0.2, delay } : { ...slide, delay } }} exit={{ height: 0 }} transition={reduceMotion ? { duration: 0.2 } : slide} > <motion.section aria-label={label} data-slot="banner-alert" data-variant={variant} className={cn( "relative flex min-h-11 items-center gap-3 overflow-hidden border-b px-4 py-2 text-sm text-foreground sm:justify-center sm:px-12", styles.surface, className )} initial={{ y: "-100%", opacity: 0 }} animate={{ y: 0, opacity: 1, transition: { ...slide, delay } }} exit={{ y: "-100%", opacity: 0 }} transition={slide} {...props} > {shownIcon && ( <span aria-hidden className={cn("grid size-6 shrink-0 place-items-center rounded-full [&_svg]:size-3.5", styles.icon)} > {shownIcon} </span> )} <p className="min-w-0 leading-snug text-pretty"> {children} {action && <span className="ml-2 inline-flex align-baseline">{action}</span>} </p> {dismissible && ( <button type="button" aria-label={dismissLabel} onClick={dismiss} className={cn( "ml-auto inline-flex size-7 shrink-0 items-center justify-center rounded-md text-muted-foreground outline-none sm:absolute sm:top-1/2 sm:right-3 sm:-translate-y-1/2", "transition-[color,background-color,box-shadow] duration-150 hover:bg-white/10 hover:text-foreground", "focus-visible:ring-[3px] focus-visible:ring-ring/50" )} > <X className="size-4" aria-hidden /> </button> )} </motion.section> </motion.div> )} </AnimatePresence> ); } // ---- Dismissal persistence (localStorage, hydration-safe via useSyncExternalStore) ---- const listeners = new Set<() => void>(); function subscribe(callback: () => void) { listeners.add(callback); window.addEventListener("storage", callback); return () => { listeners.delete(callback); window.removeEventListener("storage", callback); }; } function readDismissal(key: string) { try { return window.localStorage.getItem(key) === "dismissed"; } catch { return false; } } function writeDismissal(key: string) { try { window.localStorage.setItem(key, "dismissed"); } catch { // Storage can be unavailable (private mode, quotas). The banner still closes for this visit. } listeners.forEach((listener) => listener()); } /** Whether `key` was dismissed before. Always `false` on the server and during hydration. */ function useStoredDismissal(key: string | undefined) { return React.useSyncExternalStore( subscribe, () => (key ? readDismissal(key) : false), () => false ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| children | ReactNode | — | The announcement text. |
| variant | "default" | "info" | "success" | "warning" | "destructive" | "default" | Color scheme. default is a neutral card surface. |
| action | ReactNode | — | A call to action after the text, e.g. a link. |
| icon | ReactNode | null | — | Leading icon. Defaults to one per variant; null hides it. |
| open / defaultOpen / onOpenChange | boolean / boolean / (open: boolean) => void | — / true / — | Controlled or uncontrolled visibility. |
| storageKey | string | — | Remember the dismissal under this localStorage key. Change it for a new announcement. |
| animateOnMount | boolean | true | Slide in after mount instead of rendering open. |
| delay | number | 0.2 | Seconds before sliding in. |
| dismissible | boolean | true | Show the close button. |
| label | string | "Announcement" | Accessible name of the region. |
API Reference
Same parts as shadcn/ui: Alert, AlertTitle and AlertDescription, plus AlertAction. Each forwards the props of a div.
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | "default" | "destructive" | "success" | "warning" | "info" | "default" | Color scheme (on Alert). |
| role | string | "alert" | Override with "status" for non-urgent messages (on Alert). |
| AlertAction | div props | — | Top-right slot for a close button or menu; the text makes room for it. |
| alertVariants | cva | — | The variant classes, exported for custom elements. |
Accessibility
- Alerts render
role="alert", which interrupts screen readers. Userole="status"for confirmations and tips; Dismissible Alert picks the right one per variant. - Put a leading icon inside the Alert; it is decorative, so the title must carry the meaning (don't rely on color alone).
- The Dismissible Alert close button is a real button with an accessible label, and Escape anywhere inside the alert dismisses it. Its auto-dismiss pauses on focus, so keyboard users get time to read and act.
- The Banner Alert is a labelled region, not an alert, so it never interrupts; its close button has an accessible label.
- Under prefers-reduced-motion the blocks skip scale and slide, and only fade and collapse.