Skip to content
Application

Alert

Inline message with semantic variants, an icon, and an optional dismiss that animates out.

MotionAnimatePresence enter/exit on tween.quick

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { Alert, alertVariants } from "kineto/alert";

Preview

tsx
"use client";

import * as React from "react";
import { cva, type VariantProps } from "class-variance-authority";
import { AnimatePresence, motion, type HTMLMotionProps } from "motion/react";
import {
  CircleAlert,
  CircleCheck,
  Info,
  TriangleAlert,
  X,
  type LucideIcon,
} from "lucide-react";

import { cn } from "@/lib/cn";
import { tween } from "@/lib/motion-presets";

const alertVariants = cva("relative flex gap-3 rounded-xl border p-4", {
  variants: {
    variant: {
      default: "border-border bg-card",
      brand: "border-brand/30 bg-brand/[0.06]",
      success: "border-success/30 bg-success/[0.06]",
      warning: "border-warning/30 bg-warning/[0.06]",
      destructive: "border-destructive/30 bg-destructive/[0.06]",
    },
  },
  defaultVariants: { variant: "default" },
});

type AlertVariant = NonNullable<VariantProps<typeof alertVariants>["variant"]>;

const defaultIcons: Record<AlertVariant, LucideIcon> = {
  default: Info,
  brand: Info,
  success: CircleCheck,
  warning: TriangleAlert,
  destructive: CircleAlert,
};

const iconColor: Record<AlertVariant, string> = {
  default: "text-muted-foreground",
  brand: "text-brand",
  success: "text-success",
  warning: "text-warning",
  destructive: "text-destructive",
};

export interface AlertProps
  extends Omit<HTMLMotionProps<"div">, "title">,
    VariantProps<typeof alertVariants> {
  title?: React.ReactNode;
  /** Override the leading icon, or pass `null` to hide it. */
  icon?: LucideIcon | null;
  /** Render a trailing close button; the alert animates out on dismiss. */
  onClose?: () => void;
  children?: React.ReactNode;
}

const Alert = React.forwardRef<HTMLDivElement, AlertProps>(
  ({ className, variant, title, icon, onClose, children, ...props }, ref) => {
    const [open, setOpen] = React.useState(true);
    const v = variant ?? "default";
    const Icon = icon === null ? null : icon ?? defaultIcons[v];

    return (
      <AnimatePresence initial={false}>
        {open ? (
          <motion.div
            ref={ref}
            role="alert"
            initial={{ opacity: 0, y: -4 }}
            animate={{ opacity: 1, y: 0 }}
            exit={{ opacity: 0, scale: 0.98 }}
            transition={tween.quick}
            className={cn(alertVariants({ variant }), className)}
            {...props}
          >
            {Icon ? (
              <Icon className={cn("mt-0.5 size-5 shrink-0", iconColor[v])} />
            ) : null}
            <div className="flex-1">
              {title ? (
                <p className="text-sm font-medium text-foreground">{title}</p>
              ) : null}
              {children ? (
                <div className={cn("text-sm text-muted-foreground", title && "mt-0.5")}>
                  {children}
                </div>
              ) : null}
            </div>
            {onClose ? (
              <button
                type="button"
                aria-label="Dismiss"
                onClick={() => {
                  setOpen(false);
                  onClose();
                }}
                className="-mr-1 -mt-1 inline-flex size-7 shrink-0 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
              >
                <X className="size-4" />
              </button>
            ) : null}
          </motion.div>
        ) : null}
      </AnimatePresence>
    );
  }
);
Alert.displayName = "Alert";

export { Alert, alertVariants };

src/components/ui/alert.tsx

Props

PropTypeDefaultDescription
variant"default" | "brand" | "success" | "warning" | "destructive""default"Tone.
titleReactNode—Bold heading line.
iconLucideIcon | null—Override or hide the leading icon.
onClose() => void—Show a dismiss button.