# Alert
An inline message about this page that stays put, with one optional action.
Source page: https://dinachi.dev/docs/components/alert

## Installation
```bash
npx @dinachi/cli@latest add alert
```
Adds to your project:
- `components/ui/alert/alert.tsx`
- `components/ui/alert/index.ts`
Installs:
- `class-variance-authority` ^0.7.1
- `lucide-react` ^0.552.0

## Usage
```tsx
import { Alert, AlertContent, AlertDescription, AlertIcon, AlertTitle } from "@/components/ui/alert"

export function FloorWarning() {
  return (
    <Alert variant="warning">
      <AlertIcon />
      <AlertContent>
        <AlertTitle>Sea View Double is under your floor</AlertTitle>
        <AlertDescription>£162 on Wed 14 is below the £170 floor.</AlertDescription>
      </AlertContent>
    </Alert>
  )
}
```

## Anatomy
```tsx
<Alert>
  <AlertIcon />
  <AlertContent>
    <AlertTitle />
    <AlertDescription />
  </AlertContent>
  <AlertAction />
  <AlertDismiss />
</Alert>
```
- `Alert`: The message box. Sets the tone, the role and whether it is shown.
- `AlertIcon`: The tinted icon. Hidden from assistive tech.
- `AlertContent`: Holds the text, and the action when it goes under the text.
- `AlertTitle`: The one line that says what needs attention. Optional.
- `AlertDescription`: The detail. Without a title, it is the message.
- `AlertAction`: The one button that fixes it. Optional.
- `AlertDismiss`: The close button, named Dismiss. Opt-in.

## Examples

### Variants
One per variant. The first has no title, so its description is the message.
```tsx
import { RefreshCw } from 'lucide-react';
import { Alert, AlertContent, AlertDescription, AlertIcon, AlertTitle } from '@/components/ui/alert';

export function Example() {
  return (
    <div className="grid gap-3">
      <Alert>
        <AlertIcon><RefreshCw /></AlertIcon>
        <AlertContent>
          <AlertDescription>Rates sync every 15 minutes. Last synced 10:42.</AlertDescription>
        </AlertContent>
      </Alert>
      <Alert variant="info">
        <AlertIcon />
        <AlertContent>
          <AlertTitle>New cancellation policy from 1 December</AlertTitle>
          <AlertDescription>Bookings made after that date get 48 hours of free cancellation.</AlertDescription>
        </AlertContent>
      </Alert>
      <Alert variant="success">
        <AlertIcon />
        <AlertContent>
          <AlertTitle>Rates published to 4 channels</AlertTitle>
          <AlertDescription>Booking.com, Expedia, Airbnb and direct now show £148 for 12 to 18 Dec.</AlertDescription>
        </AlertContent>
      </Alert>
      <Alert variant="warning">
        <AlertIcon />
        <AlertContent>
          <AlertTitle>Sea View Double is under your floor</AlertTitle>
          <AlertDescription>£162 on Wed 14 is below the £170 floor. It publishes to all channels at 18:00.</AlertDescription>
        </AlertContent>
      </Alert>
      <Alert variant="destructive">
        <AlertIcon />
        <AlertContent>
          <AlertTitle>Payout failed</AlertTitle>
          <AlertDescription>Barclays declined the £12,480.00 transfer. Update the account to retry.</AlertDescription>
        </AlertContent>
      </Alert>
    </div>
  );
}
```

### Action and dismiss
The action fixes the problem, so the alert leaves with it. Dismiss hides it until shown again.
```tsx
import { useState } from 'react';
import {
  Alert,
  AlertAction,
  AlertContent,
  AlertDescription,
  AlertDismiss,
  AlertIcon,
  AlertTitle,
} from '@/components/ui/alert';

export function Example() {
  const [open, setOpen] = useState(true);
  const [rate, setRate] = useState(162);
  return (
    <Alert variant="warning" open={open && rate < 170} onOpenChange={setOpen}>
      <AlertIcon />
      <AlertContent>
        <AlertTitle>Sea View Double is under your floor</AlertTitle>
        <AlertDescription>£162 on Wed 14 is below the £170 floor. It publishes to all channels at 18:00.</AlertDescription>
      </AlertContent>
      <AlertAction onClick={() => setRate(170)}>Raise to £170</AlertAction>
      <AlertDismiss />
    </Alert>
  );
}
```

### Phone
A full-bleed strip above the rows it is about, with the action under the text as a 40px button.
```tsx
import { useState } from 'react';
import { Alert, AlertAction, AlertContent, AlertDescription, AlertDismiss, AlertIcon, AlertTitle } from '@/components/ui/alert';

export function Example() {
  const [open, setOpen] = useState(true);
  const [rate, setRate] = useState(162);
  return (
    <Alert
      variant="warning"
      open={open && rate < 170}
      onOpenChange={setOpen}
      className="rounded-none border-x-0 px-[18px] pb-4 pt-3.5"
    >
      <AlertIcon />
      <AlertContent>
        <AlertTitle className="text-[15px]">Sea View Double is under your floor</AlertTitle>
        <AlertDescription className="text-sm">£162 is below the £170 floor. It publishes at 18:00.</AlertDescription>
        <AlertAction size="default" onClick={() => setRate(170)}>
          Raise to £170
        </AlertAction>
      </AlertContent>
      <AlertDismiss className="h-9 w-9" />
    </Alert>
  );
}
```

## Notes

### Alert or toast
An alert is about the page in front of the person and stays until the problem is gone or they dismiss it. A toast reports something that just happened and leaves on its own. If the message still matters after five seconds, it is an alert.

### Hiding it when the problem is fixed
The action does not hide the alert, because the fix may fail. Derive `open` from the state the alert is about, so it leaves once the fix lands:

```tsx
<Alert variant="warning" open={rate < floor}>
  ...
  <AlertAction onClick={() => setRate(floor)}>Raise to £{floor}</AlertAction>
</Alert>
```

### On a phone
Let the alert run edge to edge above the rows it is about, and move the action under the text at full height. Nothing in the component fixes the layout, so the page decides:

```tsx
<Alert variant="warning" className="rounded-none border-x-0">
  <AlertIcon />
  <AlertContent>
    <AlertTitle>...</AlertTitle>
    <AlertDescription>...</AlertDescription>
    <AlertAction size="default">Raise to £170</AlertAction>
  </AlertContent>
  <AlertDismiss className="h-9 w-9" />
</Alert>
```

Inside `AlertContent` the action gets its own top margin.

### Motion
It fades in and drops 4px into place over 200ms when it appears, and fades out over 150ms when dismissed, then unmounts. Re-renders never replay it. An alert in the server-rendered page skips the entrance, since it is part of the page as it loads rather than a change to it. With reduced motion it only fades. Both durations follow `--motion-duration-base` and `--motion-duration-fast`.

## API reference
Props marked **agent** change what an agent reads or may do.

### Alert
The message box. Sets the tone, the role and whether it is shown.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` **agent** | `"default" \| "info" \| "success" \| "warning" \| "destructive"` | `"default"` | The tone. `warning` and `destructive` get `role="alert"`, the rest `role="status"`. The agent reads it as type. |
| `open` | `boolean` | - | Shown or dismissed, when the parent holds it. |
| `defaultOpen` | `boolean` | `true` | Whether it starts shown. |
| `onOpenChange` **agent** | `(open: boolean, details) => void` | - | Runs when `AlertDismiss` or the agent dismisses it. `details.cancel()` keeps it shown. |
| `role` | `string` | - | Overrides the role the variant sets. |

### AlertIcon
The tinted icon. Hidden from assistive tech.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | - | Replaces the variant's icon. |

### AlertContent
Holds the text, and the action when it goes under the text.

Takes the props of the element it renders.

### AlertTitle
The one line that says what needs attention. Optional.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` **agent** | `ReactNode` | - | The agent reads it as label. |

### AlertDescription
The detail. Without a title, it is the message.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` **agent** | `ReactNode` | - | The agent reads it as value, or as label when there is no title. |

### AlertAction
The one button that fixes it. Optional.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `size` | `"sm" \| "default"` | `"sm"` | `sm` sits beside the text. `default` is 40px tall, for a phone. |
| `onClick` | `(event) => void` | - | Runs the fix. The alert stays until you hide it. |

### AlertDismiss
The close button, named Dismiss. Opt-in.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | `<X />` | Replaces the icon. |
| `disabled` **agent** | `boolean` | - | Turns the button off, and withholds `dismiss()` from the agent. |

## Agent
Inside an `AgentProvider`, Alert appears in `agent.snapshot()` as `alert`.
Reads:
- `label`: The title, or the description when there is no title.
- `value`: The description, when there is a title.
- `type`: The variant, such as `warning`.
Actions:
- `dismiss()`: Dismisses it through `onOpenChange`. Only offered when the alert has an `AlertDismiss`.
It reports as an `alert`, so an agent can tell a message on the page from a `toast`. `onOpenChange` gets details that `isAgentChange` recognises; `details.cancel()` keeps it shown and fails the step. A disabled `AlertDismiss` withholds `dismiss()`. The action is listed separately as a `button` in the alert and takes `press`.
```ts
await agent.act({
  steps: [{ ref: "Sea View Double is under your floor", action: "dismiss" }],
})
```

## Accessibility
- `warning` and `destructive` are `role="alert"` and interrupt; the rest are `role="status"` and wait their turn.
- Only `warning` and `destructive` (`role="alert"`) are reliably announced when they appear. For a message that must be announced, use Toast.
- The icon is decorative. Say what is wrong in the text; the colour alone does not.
- Dismiss is a real button named Dismiss, a 44px target on phones. The alert never takes focus.
- If focus is inside the alert when it closes, it moves to the next control after it, or the one before.
### Keyboard
| Keys | Does |
| --- | --- |
| `Tab` | Moves to the action, then Dismiss |
| `Enter` / `Space` | Presses the focused button |

## Source
`components/ui/alert/alert.tsx`
```tsx
"use client"

import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { CircleAlert, CircleCheck, Info, TriangleAlert, X } from "lucide-react"
import { cn } from "@/lib/utils"
import { changeAsAgent, type AgentControl, useAgentControl, useAgentState, useHasAgent, useMergedRef } from "@/components/ui/agent"
import { Button } from "@/components/ui/button"

export type AlertVariant = "default" | "info" | "success" | "warning" | "destructive"

export interface AlertChangeDetails {
  reason: string
  event: Event | undefined
  isCanceled: boolean
  cancel(): void
}

const tinted =
  "bg-[color:color-mix(in_oklab,var(--alert-tone)_7%,var(--background))] border-[color:color-mix(in_oklab,var(--alert-tone)_16%,var(--border))]"

const alertVariants = cva(
  [
    "flex items-start gap-3 rounded-xl border px-3.5 py-[var(--alert-py,0.75rem)] text-sm text-foreground",
    // On a phone an action beside the text drops under it, so the text keeps the width.
    "max-sm:grid max-sm:grid-cols-[auto_minmax(0,1fr)_auto] max-sm:gap-0",
    // A one-line message, with no title and no action under it, centres on the icon and buttons.
    "[&:not(:has([data-slot=alert-title])):not(:has([data-slot=alert-content]_[data-slot=alert-action]))]:items-center",
    "[&:not(:has([data-slot=alert-title])):not(:has([data-slot=alert-content]_[data-slot=alert-action]))]:[--alert-py:0.625rem]",
    "transition-[opacity,transform,translate] duration-[var(--motion-duration-base,200ms)] ease-[var(--motion-ease-out,cubic-bezier(0.23,1,0.32,1))]",
    "data-[starting-style]:-translate-y-1 data-[starting-style]:opacity-0 motion-reduce:data-[starting-style]:translate-y-0",
    "data-[ending-style]:opacity-0 data-[ending-style]:duration-[var(--motion-duration-fast,150ms)]",
  ],
  {
    variants: {
      variant: {
        default: "border-border bg-card [--alert-tone:var(--muted-foreground)]",
        info: cn(tinted, "[--alert-tone:var(--info)]"),
        success: cn(tinted, "[--alert-tone:var(--success)]"),
        warning: cn(tinted, "[--alert-tone:var(--warning)]"),
        destructive: cn(tinted, "[--alert-tone:var(--destructive)]"),
      },
    },
    defaultVariants: { variant: "default" },
  }
)

const ICONS: Record<AlertVariant, React.ComponentType<{ className?: string }>> = {
  default: Info,
  info: Info,
  success: CircleCheck,
  warning: TriangleAlert,
  destructive: CircleAlert,
}

interface AlertContextValue {
  variant: AlertVariant
  title: React.MutableRefObject<HTMLElement | null>
  description: React.MutableRefObject<HTMLElement | null>
  dismiss: React.MutableRefObject<HTMLButtonElement | null>
  close: (event?: Event) => void
}

const AlertContext = React.createContext<AlertContextValue | null>(null)

function useAlert(part: string): AlertContextValue {
  const context = React.useContext(AlertContext)
  if (!context) throw new Error(`${part} must be used inside Alert.`)
  return context
}

const useIsomorphicLayoutEffect = typeof window === "undefined" ? React.useEffect : React.useLayoutEffect
const subscribeNothing = () => () => {}

const textOf = (el: Element | null) => el?.textContent?.replace(/\s+/g, " ").trim() || undefined

const FOCUSABLE =
  'a[href], button:not(:disabled), input:not(:disabled), select:not(:disabled), textarea:not(:disabled), [tabindex]:not([tabindex="-1"])'

/** Moves focus out of a closing alert: to the next focusable after it, else the one before. */
function focusNeighbour(el: HTMLElement) {
  const all = Array.from(el.ownerDocument.querySelectorAll<HTMLElement>(FOCUSABLE)).filter(
    (node) => !el.contains(node) && !node.closest("[hidden], [inert]")
  )
  const next = all.find((node) => el.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_FOLLOWING)
  ;(next ?? all.filter((node) => el.compareDocumentPosition(node) & Node.DOCUMENT_POSITION_PRECEDING).pop())?.focus()
}

export interface AlertProps extends React.ComponentPropsWithoutRef<"div">, VariantProps<typeof alertVariants> {
  /** Shown or dismissed, when the parent holds it. */
  open?: boolean
  /** Whether it starts shown. Defaults to true. */
  defaultOpen?: boolean
  /** Runs when AlertDismiss or the agent dismisses it. `details.cancel()` keeps it. */
  onOpenChange?: (open: boolean, details: AlertChangeDetails) => void
}

const Alert = React.forwardRef<HTMLDivElement, AlertProps>(
  ({ className, variant, open: openProp, defaultOpen = true, onOpenChange, role, children, ...props }, ref) => {
    const tone: AlertVariant = variant ?? "default"
    const [open, change] = useAgentState<boolean, AlertChangeDetails>(openProp, defaultOpen, onOpenChange)
    const [present, setPresent] = React.useState(open)
    if (open && !present) setPresent(true)
    const ending = present && !open

    const root = React.useRef<HTMLDivElement | null>(null)
    const setRef = useMergedRef(ref, root)
    const title = React.useRef<HTMLElement | null>(null)
    const description = React.useRef<HTMLElement | null>(null)
    const dismiss = React.useRef<HTMLButtonElement | null>(null)

    // A server-rendered alert is part of the page as it loads, not a change to it.
    // Only that first commit skips the entrance.
    const hydrating = React.useSyncExternalStore(subscribeNothing, () => false, () => true)
    const animate = React.useRef(!hydrating)
    React.useEffect(() => {
      animate.current = true
    }, [])

    const openRef = React.useRef(open)
    openRef.current = open

    useIsomorphicLayoutEffect(() => {
      const el = root.current
      if (!present || !el || !animate.current) return
      el.setAttribute("data-starting-style", "")
      // Read the starting style now, so removing the attribute next frame transitions.
      el.getBoundingClientRect()
      const frame = requestAnimationFrame(() => el.removeAttribute("data-starting-style"))
      return () => {
        cancelAnimationFrame(frame)
        el.removeAttribute("data-starting-style")
      }
    }, [present])

    React.useEffect(() => {
      const el = root.current
      if (!ending || !el) return
      if (el.contains(el.ownerDocument.activeElement)) focusNeighbour(el)
      let live = true
      const done = () => {
        if (!live) return
        live = false
        setPresent(false)
      }
      const transitions = (el.getAnimations?.() ?? []).filter(
        (a) => typeof CSSTransition === "undefined" || a instanceof CSSTransition
      )
      Promise.all(transitions.map((a) => a.finished)).then(done, done)
      // A transition that never finishes must not keep the alert mounted.
      const timer = setTimeout(done, (parseFloat(getComputedStyle(el).transitionDuration) || 0) * 1000 + 100)
      return () => {
        live = false
        clearTimeout(timer)
      }
    }, [ending])

    const context = React.useMemo<AlertContextValue>(
      () => ({
        variant: tone,
        title,
        description,
        dismiss,
        close: (event) => {
          if (!openRef.current) return
          const details: AlertChangeDetails = {
            reason: "close-press",
            event,
            isCanceled: false,
            cancel: () => void (details.isCanceled = true),
          }
          change(false, details)
        },
      }),
      [tone, change]
    )

    useAgentControl((): AgentControl | null => {
      const el = root.current
      if (!open || !el) return null
      const heading = textOf(title.current)
      const body = textOf(description.current)
      const label = heading ?? body
      // Names the alert as the container its action sits in.
      if (label) el.setAttribute("data-agent-in", label)
      const button = dismiss.current
      const canDismiss = button && !button.disabled && button.getAttribute("aria-disabled") !== "true"
      return {
        kind: "alert",
        element: el,
        label,
        value: heading ? body : undefined,
        type: tone,
        actions: canDismiss ? { dismiss: () => changeAsAgent(change, false) } : {},
      }
    })

    if (!present) return null

    return (
      <AlertContext.Provider value={context}>
        <div
          ref={setRef}
          data-slot="alert"
          data-variant={tone}
          role={role ?? (tone === "warning" || tone === "destructive" ? "alert" : "status")}
          data-ending-style={ending ? "" : undefined}
          className={cn(alertVariants({ variant: tone }), className)}
          {...props}
        >
          {children}
        </div>
      </AlertContext.Provider>
    )
  }
)
Alert.displayName = "Alert"

export interface AlertIconProps extends React.ComponentPropsWithoutRef<"span"> {
  /** Replaces the variant's icon. */
  children?: React.ReactNode
}

const AlertIcon = React.forwardRef<HTMLSpanElement, AlertIconProps>(({ className, children, ...props }, ref) => {
  const { variant } = useAlert("AlertIcon")
  const Icon = ICONS[variant]
  return (
    <span
      ref={ref}
      data-slot="alert-icon"
      aria-hidden="true"
      className={cn(
        "mt-0.5 flex shrink-0 text-[color:var(--alert-tone)] max-sm:mr-3 [&_svg]:size-4",
        "[[data-slot=alert]:not(:has([data-slot=alert-title])):not(:has([data-slot=alert-content]_[data-slot=alert-action]))>&]:mt-0",
        className
      )}
      {...props}
    >
      {children ?? <Icon />}
    </span>
  )
})
AlertIcon.displayName = "AlertIcon"

const AlertContent = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<"div">>(
  ({ className, ...props }, ref) => (
    <div ref={ref} data-slot="alert-content" className={cn("min-w-0 flex-1 max-sm:col-start-2", className)} {...props} />
  )
)
AlertContent.displayName = "AlertContent"

const AlertTitle = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<"div">>(
  ({ className, ...props }, ref) => {
    const { title } = useAlert("AlertTitle")
    const setRef = useMergedRef(ref, title)
    return <div ref={setRef} data-slot="alert-title" className={cn("font-semibold leading-snug max-sm:text-[15px]", className)} {...props} />
  }
)
AlertTitle.displayName = "AlertTitle"

const AlertDescription = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<"div">>(
  ({ className, ...props }, ref) => {
    const { description } = useAlert("AlertDescription")
    const setRef = useMergedRef(ref, description)
    return (
      <div
        ref={setRef}
        data-slot="alert-description"
        // With no title above it, the description is the message, so it reads at full strength.
        className={cn("mt-0.5 leading-normal text-muted-foreground first:mt-0 first:text-foreground", className)}
        {...props}
      />
    )
  }
)
AlertDescription.displayName = "AlertDescription"

export interface AlertActionProps extends Omit<React.ComponentPropsWithoutRef<typeof Button>, "variant" | "size"> {
  /** `sm` sits beside the text; `default` is 40px tall, for a phone. */
  size?: "sm" | "default"
}

const AlertAction = React.forwardRef<HTMLElement, AlertActionProps>(({ className, size = "sm", ...props }, ref) => {
  const { variant } = useAlert("AlertAction")
  return (
    <Button
      ref={ref}
      data-slot="alert-action"
      variant={variant === "destructive" ? "destructive" : "outline"}
      size={size === "sm" ? "sm" : "default"}
      className={cn(
        "shrink-0",
        size === "sm" && "h-7 px-2.5 text-[13px]",
        variant !== "destructive" && "border-[color:color-mix(in_oklab,var(--alert-tone)_22%,var(--border))]",
        // A touch darker than the button, for margin over 4.5:1 in the dark theme. Hover darkens further.
        variant === "destructive" &&
          "bg-[color:color-mix(in_oklab,var(--destructive)_94%,black)] hover:bg-[color:color-mix(in_oklab,var(--destructive)_86%,black)]",
        // Placed under the text instead of beside it.
        "mt-[var(--alert-action-mt,0)] [[data-slot=alert-content]_&]:[--alert-action-mt:0.75rem]",
        "max-sm:col-start-2 max-sm:row-start-2 max-sm:mt-3 max-sm:h-10 max-sm:justify-self-start max-sm:px-4 max-sm:text-[15px]",
        className
      )}
      {...props}
    />
  )
})
AlertAction.displayName = "AlertAction"

export type AlertDismissProps = Omit<React.ComponentPropsWithoutRef<typeof Button>, "variant" | "size">

const AlertDismiss = React.forwardRef<HTMLElement, AlertDismissProps>(
  ({ className, children, onClick, ...props }, ref) => {
    const { dismiss, close } = useAlert("AlertDismiss")
    const setRef = useMergedRef(ref, dismiss as React.MutableRefObject<HTMLElement | null>)
    const hasAgent = useHasAgent()
    return (
      <Button
        ref={setRef}
        data-slot="alert-dismiss"
        data-agent-part={hasAgent ? "" : undefined}
        variant="ghost"
        size="icon"
        aria-label="Dismiss"
        className={cn(
          "-my-1 -mr-1.5 h-7 w-7 shrink-0 rounded-md text-muted-foreground hover:text-foreground [&_svg]:size-4",
          "[[data-slot=alert-action]+&]:-ml-2",
          "max-sm:col-start-3 max-sm:row-start-1 max-sm:-mt-[7px] max-sm:mb-0 max-sm:-mr-[9px] max-sm:ml-2 max-sm:h-9 max-sm:w-9 max-sm:[[data-slot=alert-action]+&]:ml-2",
          // A 44px target on phones around the 36px button.
          "relative max-sm:after:absolute max-sm:after:-inset-1 max-sm:after:content-['']",
          className
        )}
        onClick={(event) => {
          onClick?.(event)
          if (!event.defaultPrevented) close(event.nativeEvent)
        }}
        {...props}
      >
        {children ?? <X />}
      </Button>
    )
  }
)
AlertDismiss.displayName = "AlertDismiss"

export { Alert, AlertIcon, AlertContent, AlertTitle, AlertDescription, AlertAction, AlertDismiss, alertVariants }
```
