Animated Alert

Inline feedback messages that animate in and out by status.

Preview

"use client";

import { useRef, useState } from "react";
import { Alert, AlertClose, AlertContent, AlertDescription, AlertIcon, AlertTitle } from "@/components/motion/alert";
import { Button } from "@/components/motion/button";

export function AlertUsage() {
  const [open, setOpen] = useState(true);
  const showRef = useRef<HTMLButtonElement>(null);

  return (
    <div className="w-full max-w-md space-y-4">
      <Alert variant="info" open={open} onOpenChange={setOpen} returnFocusRef={showRef}>
        <AlertIcon />
        <AlertContent>
          <AlertTitle>A little heads up</AlertTitle>
          <AlertDescription>Your changes are saved automatically as you work.</AlertDescription>
        </AlertContent>
        <AlertClose />
      </Alert>
      <Button ref={showRef} variant="outline" size="sm" onClick={() => setOpen(true)} disabled={open}>Show alert</Button>
    </div>
  );
}

Install

Add it with the shadcn CLI, or copy the source manually.

$ bunx --bun shadcn add @beui/alert

API Reference

Alert

variant?
"default" | "success" | "info" | "warning" | "destructive"

Color and icon tone. Default: "default"

Defaultdefault
open?
boolean

Whether the alert shows, for controlled use.

defaultOpen?
boolean

Whether the alert starts shown when uncontrolled. Default: true

Defaulttrue
onOpenChange?
((open: boolean) => void)

Called with the new state when the alert shows or is dismissed.

returnFocusRef?
RefObject<HTMLElement | null>

Focus this element when closing hides the currently focused alert control.

contentClassName?
string

Layout classes for the inner row, separate from the animated surface.

role?
"alert" | "status"

Defaults to alert for warning/destructive, and status for other variants.

Defaultvariant === "warning" || variant === "destructive" ? "alert" : "status"
labels?
Partial<{ dismiss: string; }>

Override any built-in string.

className?
string

Classes merged onto the root element.

AlertIcon

className?
string

Classes merged onto the root element.

AlertContent

className?
string

Classes merged onto the root element.

AlertTitle

className?
string

Classes merged onto the root element.

AlertDescription

className?
string

Classes merged onto the root element.

AlertAction

className?
string

Classes merged onto the root element.

AlertClose

className?
string

Classes merged onto the root element.

Also on React Native. Feedback that eases in and folds away.

Updated