Skip to content
Atoms

Alert

An inline callout for info, success or warning.

Installation

  1. Run the CLI

    Run pnpm dlx asta-ui@latest init once, then this. The file lands in components/astabyte/ and the colour tokens go into your CSS.

    $ pnpm dlx asta-ui@latest add alert

    Or with the shadcn CLI: pnpm dlx shadcn@latest add https://ui.astabyte.com/r/alert.json

  2. Or install the package

    Don’t want the file? Import from asta-ui/alert and update with pnpm update asta-ui. Add the stylesheet once. More.

    $ pnpm add asta-ui
    import { Alert } from "asta-ui/alert";
  3. Or copy it by hand

    Copy this file. Keep the paths, or fix the @/ imports. Then add the tokens from globals.css.

    "use client";
    
    import { useState, type ReactNode } from "react";
    
    const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" ");
    
    const KEYFRAMES = `@keyframes asta-alert-in { from { opacity: 0; transform: translateY(-4px) } }`;
    
    export type AlertTone = "info" | "success" | "warning" | "neutral";
    
    export interface AlertProps {
      tone?: AlertTone;
      title?: ReactNode;
      /** The message. */
      children?: ReactNode;
      /** A button or link shown under the message. */
      action?: ReactNode;
      /** Replace the icon, or pass `false` to hide it. */
      icon?: ReactNode | false;
      /** Show a close button. */
      dismissible?: boolean;
      /** Called when the close button is pressed. */
      onDismiss?: () => void;
      /** Called once the alert has faded and collapsed. */
      onExited?: () => void;
      /** Controlled visibility. Set it to false to play the exit. */
      open?: boolean;
      className?: string;
    }
    
    // Tone is told by the icon shape and the surface, never by hue: blue for info and success, ink for warnings.
    const SURFACE: Record<AlertTone, string> = {
      info: "bg-[color-mix(in_srgb,var(--color-brand)_7%,var(--color-paper))]",
      success: "bg-paper-2",
      warning: "bg-paper-2 shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_16%,transparent)]",
      neutral: "bg-paper-2",
    };
    
    function ToneIcon({ tone }: { tone: AlertTone }) {
      const common = { viewBox: "0 0 20 20", className: "h-5 w-5", "aria-hidden": true } as const;
      if (tone === "success")
        return (
          <svg {...common}>
            <circle cx="10" cy="10" r="8" className="fill-brand" />
            <path d="m6.6 10.2 2.3 2.3 4.5-5" fill="none" stroke="var(--color-on-brand)" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" />
          </svg>
        );
      if (tone === "warning")
        return (
          <svg {...common}>
            <path d="M10 2.8c.5 0 .9.25 1.15.68l6.3 11.1c.5.9-.14 2-1.15 2H3.7c-1 0-1.65-1.1-1.15-2l6.3-11.1c.25-.43.65-.68 1.15-.68z" className="fill-ink" />
            <path d="M10 7.4v4" className="stroke-paper" strokeWidth="1.8" strokeLinecap="round" />
            <circle cx="10" cy="14" r="1" className="fill-paper" />
          </svg>
        );
      return (
        <svg {...common} fill="none">
          <circle cx="10" cy="10" r="7.6" strokeWidth="1.6" className={tone === "info" ? "stroke-brand" : "stroke-mute"} />
          <path d="M10 9v4.4" strokeWidth="1.7" strokeLinecap="round" className={tone === "info" ? "stroke-brand" : "stroke-mute"} />
          <circle cx="10" cy="6.4" r="1" className={tone === "info" ? "fill-brand" : "fill-mute"} />
        </svg>
      );
    }
    
    /**
     * An inline callout for something the reader should know. The tone shows in the icon and surface, not in
     * colour. Dismissing fades it out, then the space it took collapses so the content below glides up.
     */
    export function Alert({ tone = "info", title, children, action, icon, dismissible, onDismiss, onExited, open, className }: AlertProps) {
      const [closed, setClosed] = useState(false);
      const [gone, setGone] = useState(false);
      const visible = open ?? !closed;
    
      // Reopening a controlled alert brings it back.
      if (visible && gone) setGone(false);
      if (gone) return null;
    
      const finish = () => {
        if (visible) return;
        setGone(true);
        onExited?.();
      };
    
      return (
        <div
          className="grid transition-[grid-template-rows] delay-150 duration-300 motion-reduce:!delay-0 motion-reduce:!duration-[1ms]"
          style={{ gridTemplateRows: visible ? "1fr" : "0fr", transitionTimingFunction: "cubic-bezier(.16,1,.3,1)" }}
          onTransitionEnd={(e) => {
            if (e.target === e.currentTarget) finish();
          }}
        >
          <style href="asta-alert" precedence="asta">{KEYFRAMES}</style>
          <div className="min-h-0 overflow-hidden">
            <div
              role={tone === "warning" ? "alert" : "status"}
              className={cx(
                "flex items-start gap-3 rounded-xl px-4 py-3.5 transition-[opacity,transform] duration-200 motion-reduce:!duration-[1ms]",
                "animate-[asta-alert-in_.36s_cubic-bezier(.16,1,.3,1)_backwards] motion-reduce:animate-none",
                visible ? "opacity-100" : "scale-[.98] opacity-0",
                SURFACE[tone],
                className,
              )}
            >
              {icon !== false && <span className="mt-px flex shrink-0 text-ink">{icon ?? <ToneIcon tone={tone} />}</span>}
              <div className="min-w-0 flex-1 pt-px">
                {title && <p className="text-[13.5px] font-medium tracking-[-0.01em] text-ink">{title}</p>}
                {children && <div className={cx("text-[13px] leading-relaxed text-ink-2", !!title && "mt-0.5")}>{children}</div>}
                {action && <div className="mt-2.5 flex flex-wrap items-center gap-2">{action}</div>}
              </div>
              {dismissible && (
                <button
                  type="button"
                  aria-label="Dismiss"
                  onClick={() => {
                    if (open === undefined) setClosed(true);
                    onDismiss?.();
                  }}
                  className="-mr-1.5 -mt-0.5 flex h-7 w-7 shrink-0 items-center justify-center rounded-md text-mute outline-none transition-colors hover:bg-[color-mix(in_srgb,var(--color-ink)_7%,transparent)] hover:text-ink focus-visible:ring-2 focus-visible:ring-brand/50"
                >
                  <svg viewBox="0 0 24 24" className="h-3.5 w-3.5" fill="none" stroke="currentColor" strokeWidth="2.55" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
                    <path d="m6 6 12 12" />
                    <path d="M18 6 6 18" />
                  </svg>
                </button>
              )}
            </div>
          </div>
        </div>
      );
    }
    
    export default Alert;
    
  4. Or ask your AI agent

    Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.

    Add the Astabyte UI Alert component to my project.
    
    An inline callout for info, success or warning.
    It includes: info, success, warning, neutral, title, action, dismiss.
    
    Install it:
    ```sh
    pnpm dlx asta-ui@latest add alert --radix
    ```
    Or with the shadcn CLI:
    ```sh
    pnpm dlx shadcn@latest add https://ui.astabyte.com/r/alert.json
    ```
    If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4).
    
    Import it like this:
    ```tsx
    import { Alert } from "@/components/astabyte/alert";
    ```
    
    Usage example and props: https://ui.astabyte.com/alert
    Full catalogue for context: https://ui.astabyte.com/llms.txt
    
    Rules:
    - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS.
    - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback.
    - Keep keyboard, focus and ARIA behaviour.
    - Use fake data and fake brand names in examples.
    
    Use it to: [describe where it goes and what it should do]
  5. Or use plain HTML + CSS

    Not on React? Use it from Django, Rails, Laravel, Go html/template, htmx or a static page. Load asta.css and asta.js once (the head.html tab), then paste the markup. The ui.astabyte.com/cdn link is temporary: pin a version from jsDelivr for production. More in HTML + CSS.

    <div style="display:grid;gap:12px">
    <div class="asta-alert" role="status"><span class="asta-alert-icon"><svg viewBox="0 0 20 20" fill="none" aria-hidden="true" stroke="currentColor"><circle cx="10" cy="10" r="7.6" stroke-width="1.6"/><path d="M10 9v4.4" stroke-width="1.7" stroke-linecap="round"/><circle cx="10" cy="6.4" r="1" fill="currentColor" stroke="none"/></svg></span><div class="asta-alert-body"><p class="asta-alert-title">Heads up</p><div class="asta-alert-text">Astabyte UI 0.8.0 is out.</div></div><label class="asta-alert-dismiss"><input type="checkbox" data-asta-dismiss aria-label="Dismiss"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" aria-hidden="true"><path d="m4 4 8 8M12 4l-8 8"/></svg></label></div><div class="asta-alert asta-alert-success" role="status"><span class="asta-alert-icon"><svg viewBox="0 0 20 20" aria-hidden="true"><circle cx="10" cy="10" r="8" fill="currentColor"/><path d="m6.6 10.2 2.3 2.3 4.5-5" fill="none" stroke="white" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg></span><div class="asta-alert-body"><p class="asta-alert-title">Saved</p><div class="asta-alert-text">Astabyte settings updated.</div></div></div><div class="asta-alert asta-alert-warning" role="alert"><span class="asta-alert-icon"><svg viewBox="0 0 20 20" aria-hidden="true"><path d="M10 2.8c.5 0 .9.25 1.15.68l6.3 11.1c.5.9-.14 2-1.15 2H3.7c-1 0-1.65-1.1-1.15-2l6.3-11.1c.25-.43.65-.68 1.15-.68z" fill="currentColor"/><path d="M10 7.4v4" style="stroke:var(--paper)" stroke-width="1.8" stroke-linecap="round"/><circle cx="10" cy="14" r="1" style="fill:var(--paper)"/></svg></span><div class="asta-alert-body"><p class="asta-alert-title">Careful</p><div class="asta-alert-text">Your Astabyte trial ends in 3 days.</div></div></div><div class="asta-alert asta-alert-neutral" role="status"><span class="asta-alert-icon"><svg viewBox="0 0 20 20" fill="none" aria-hidden="true" stroke="currentColor"><circle cx="10" cy="10" r="7.6" stroke-width="1.6"/><path d="M10 9v4.4" stroke-width="1.7" stroke-linecap="round"/><circle cx="10" cy="6.4" r="1" fill="currentColor" stroke="none"/></svg></span><div class="asta-alert-body"><p class="asta-alert-title">Note</p><div class="asta-alert-text">Billing runs on the 1st.</div></div></div>
    </div>

Usage

The demo above, as code.

"use client";

import { useState } from "react";
import { Alert } from "@/components/astabyte/alert";

const link = "text-[13px] font-medium text-brand outline-none underline-offset-4 hover:underline focus-visible:underline";

export default function AlertDemo() {
  const [key, setKey] = useState(0);

  return (
    <div className="flex min-h-[420px] w-full items-center justify-center p-6 sm:p-10">
      <div key={key} className="flex w-full max-w-md flex-col gap-3">
        <Alert tone="info" title="New billing dashboard" dismissible>
          Invoices and receipts now live in one place.
        </Alert>
        <Alert
          tone="warning"
          title="Your card expires next week"
          action={
            <button type="button" className={link}>
              Update card
            </button>
          }
        >
          Update it before 1 December to keep your plan running.
        </Alert>
        <Alert tone="success" dismissible>
          Domain verified. Emails now send from [email protected].
        </Alert>
        <Alert tone="neutral">Read-only access. Ask an admin to make changes.</Alert>
        <button type="button" onClick={() => setKey((k) => k + 1)} className="mt-3 self-center text-[12.5px] text-mute outline-none hover:text-ink focus-visible:text-ink">
          Dismiss one, then reset
        </button>
      </div>
    </div>
  );
}