codefast/ui

Command Palette

Search for a command to run...

Source
Display

Alert

Contextual banner with icon, title, and body. Supports default and destructive variants.

Examples

Basic

A basic alert with an icon, title and description.

14 lines
import { Alert, AlertDescription, AlertTitle } from "@codefast/ui/alert";
import { CheckCircle2Icon } from "lucide-react";

export function AlertBasic() {
  return (
    <Alert className="max-w-md">
      <CheckCircle2Icon />
      <AlertTitle>Account updated successfully</AlertTitle>
      <AlertDescription>
        Your profile information has been saved. Changes will be reflected immediately.
      </AlertDescription>
    </Alert>
  );
}

Destructive

Use variant=destructive to create a destructive alert.

14 lines
import { Alert, AlertDescription, AlertTitle } from "@codefast/ui/alert";
import { AlertCircleIcon } from "lucide-react";

export function AlertDestructive() {
  return (
    <Alert variant="destructive" className="max-w-md">
      <AlertCircleIcon />
      <AlertTitle>Payment failed</AlertTitle>
      <AlertDescription>
        Your payment could not be processed. Please check your payment method and try again.
      </AlertDescription>
    </Alert>
  );
}

Action

Use AlertAction to add a button or other action element to the alert.

16 lines
import { Alert, AlertAction, AlertDescription, AlertTitle } from "@codefast/ui/alert";
import { Button } from "@codefast/ui/button";

export function AlertActionExample() {
  return (
    <Alert className="max-w-md">
      <AlertTitle>Dark mode is now available</AlertTitle>
      <AlertDescription>Enable it under your profile settings to get started.</AlertDescription>
      <AlertAction>
        <Button size="xs" variant="default">
          Enable
        </Button>
      </AlertAction>
    </Alert>
  );
}

Custom colors

Customize the alert colors by adding utility classes to the Alert component.

14 lines
import { Alert, AlertDescription, AlertTitle } from "@codefast/ui/alert";
import { AlertTriangleIcon } from "lucide-react";

export function AlertColors() {
  return (
    <Alert className="max-w-md border-amber-200 bg-amber-50 text-amber-900 dark:border-amber-900 dark:bg-amber-950 dark:text-amber-50">
      <AlertTriangleIcon />
      <AlertTitle>Your subscription will expire in 3 days.</AlertTitle>
      <AlertDescription>
        Renew now to avoid service interruption or upgrade to a paid plan to continue using the service.
      </AlertDescription>
    </Alert>
  );
}

RTL

Right-to-left layout support for languages such as Arabic and Hebrew.

Translations are AI-generated for demonstration and may be imperfect.

67 lines
import { Alert, AlertDescription, AlertTitle } from "@codefast/ui/alert";
import { CheckCircle2Icon, InfoIcon } from "lucide-react";

import type { Translations } from "#/features/components-catalog/components/detail/language";
import { useTranslation } from "#/features/components-catalog/components/detail/language-context";

const translations: Translations = {
  en: {
    dir: "ltr",
    values: {
      paymentTitle: "Payment successful",
      paymentDescription: "Your payment of $29.99 has been processed. A receipt has been sent to your email address.",
      featureTitle: "New feature available",
      featureDescription: "We've added dark mode support. You can enable it in your account settings.",
    },
  },
  ar: {
    dir: "rtl",
    values: {
      paymentTitle: "تم الدفع بنجاح",
      paymentDescription: "تمت معالجة دفعتك البالغة 29.99 دولارًا. تم إرسال إيصال إلى عنوان بريدك الإلكتروني.",
      featureTitle: "ميزة جديدة متاحة",
      featureDescription: "لقد أضفنا دعم الوضع الداكن. يمكنك تفعيله في إعدادات حسابك.",
    },
  },
  he: {
    dir: "rtl",
    values: {
      paymentTitle: "התשלום בוצע בהצלחה",
      paymentDescription: "התשלום שלך בסך 29.99 דולר עובד. קבלה נשלחה לכתובת האימייל שלך.",
      featureTitle: "תכונה חדשה זמינה",
      featureDescription: "הוספנו תמיכה במצב כהה. אתה יכול להפעיל אותו בהגדרות החשבון שלך.",
    },
  },
};

const alerts = [
  {
    icon: CheckCircle2Icon,
    titleKey: "paymentTitle" as const,
    descriptionKey: "paymentDescription" as const,
  },
  {
    icon: InfoIcon,
    titleKey: "featureTitle" as const,
    descriptionKey: "featureDescription" as const,
  },
] as const;

export function AlertRtl() {
  const { dir, t } = useTranslation(translations, "ar");

  return (
    <div className="grid w-full max-w-md items-start gap-4" dir={dir}>
      {alerts.map((alert, index) => {
        const Icon = alert.icon;
        return (
          <Alert key={index}>
            <Icon />
            <AlertTitle>{t[alert.titleKey]}</AlertTitle>
            <AlertDescription>{t[alert.descriptionKey]}</AlertDescription>
          </Alert>
        );
      })}
    </div>
  );
}

Anatomy

How the parts nest — every slot the component exposes, in composition order.

Alert
├── AlertTitle
├── AlertDescription
└── AlertAction

Features

  • Renders with role="alert" — screen readers announce it as soon as it mounts, with no extra wiring.
  • The layout shifts to a two-column grid automatically once a leading <svg> icon is present as a direct child.
  • AlertAction is absolutely positioned in the top-right corner for a dismiss button or similar trailing control.

API reference

Props for each part of the component. All native element props are also forwarded.

Alert

variant"default" | "destructive"

Neutral information, or an error/danger banner.

Default"default"

AlertTitle

childrenReactNode

The headline text.

AlertDescription

childrenReactNode

The body text.

AlertAction

Absolutely positioned in the top-right corner for a dismiss button or similar trailing control.

childrenReactNode

An action element, e.g. a dismiss button.

Accessibility

Built to be keyboard-navigable and screen-reader friendly out of the box.

  • Use role=alert for messages that must be announced immediately; otherwise keep it static.
  • Convey severity in the text, not colour alone.
  • Keep the icon decorative — the meaning lives in the title and description.

Guidelines

Conventions that keep usage consistent across an app.

Do

  • Use inline alerts for contextual, non-blocking messages.
  • Pair destructive alerts with a clear next step.

Don’t

  • Don’t use an inline Alert for a decision that must block — use Alert Dialog.
  • Don’t stack many alerts; summarise instead.

Explore further

Ready to integrate?

Follow the Getting Started guide to install @codefast/ui, or browse the full component gallery.