Inline alert
In-page alerts for info, success, warning and error messages, with a title, body, optional action buttons (InlineAlertAction) and an optional dismiss button. Errors and warnings use role="alert", info and success use role="status", and the role can be overridden for messages present on page load. Tinted for light and dark mode.
Source
"use client";
import { forwardRef, useState, type ButtonHTMLAttributes, type HTMLAttributes, type ReactNode } from "react";
import { CircleAlert, CircleCheck, Info, TriangleAlert, X } from "lucide-react";
import { cn } from "@/lib/utils";
export type InlineAlertVariant = "info" | "success" | "warning" | "error";
const tones: Record<InlineAlertVariant, { box: string; icon: string; Icon: typeof Info }> = {
info: {
box: "border-sky-500/25 bg-sky-500/[0.06] dark:border-sky-400/25 dark:bg-sky-400/[0.07]",
icon: "text-sky-600 dark:text-sky-400",
Icon: Info,
},
success: {
box: "border-emerald-500/25 bg-emerald-500/[0.06] dark:border-emerald-400/25 dark:bg-emerald-400/[0.07]",
icon: "text-emerald-600 dark:text-emerald-400",
Icon: CircleCheck,
},
warning: {
box: "border-amber-500/30 bg-amber-500/[0.08] dark:border-amber-400/25 dark:bg-amber-400/[0.07]",
icon: "text-amber-600 dark:text-amber-400",
Icon: TriangleAlert,
},
error: {
box: "border-destructive/30 bg-destructive/[0.06] dark:bg-destructive/[0.09]",
icon: "text-destructive",
Icon: CircleAlert,
},
};
export interface InlineAlertProps extends Omit<HTMLAttributes<HTMLDivElement>, "title"> {
variant?: InlineAlertVariant;
title?: ReactNode;
/** Buttons or links shown under the body. InlineAlertAction fits here. */
actions?: ReactNode;
/** Replaces the variant's icon; pass null for none. */
icon?: ReactNode | null;
/** Shows a dismiss button. Without onDismiss the alert hides itself. */
dismissible?: boolean;
onDismiss?: () => void;
dismissLabel?: string;
}
/**
* A message that sits in the page flow. Errors and warnings use role="alert" so they are announced when they appear;
* info and success use role="status". Override with the role prop for alerts present on page load.
*/
export const InlineAlert = forwardRef<HTMLDivElement, InlineAlertProps>(function InlineAlert(
{ variant = "info", title, actions, icon, dismissible, onDismiss, dismissLabel = "Dismiss", className, children, role, ...props },
ref,
) {
const [open, setOpen] = useState(true);
if (!open) return null;
const tone = tones[variant];
const showIcon = icon !== null;
return (
<div
ref={ref}
role={role ?? (variant === "error" || variant === "warning" ? "alert" : "status")}
data-variant={variant}
className={cn("relative flex gap-3 rounded-lg border p-4 text-sm text-foreground", tone.box, dismissible && "pr-11", className)}
{...props}
>
{showIcon && (
<span aria-hidden="true" className={cn("mt-px shrink-0 [&_svg]:size-[18px]", tone.icon)}>
{icon ?? <tone.Icon />}
</span>
)}
<div className="flex min-w-0 flex-1 flex-col gap-1">
{title && <p className="font-medium leading-5">{title}</p>}
{children && <div className="leading-5 text-foreground/75 [&_a]:font-medium [&_a]:text-foreground [&_a]:underline [&_a]:underline-offset-4">{children}</div>}
{actions && <div className="mt-2 flex flex-wrap items-center gap-2">{actions}</div>}
</div>
{dismissible && (
<button
type="button"
aria-label={dismissLabel}
onClick={() => (onDismiss ? onDismiss() : setOpen(false))}
className="absolute right-2.5 top-2.5 inline-flex size-7 items-center justify-center rounded-md text-foreground/50 outline-hidden transition-colors hover:bg-foreground/[0.06] hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50"
>
<X aria-hidden="true" className="size-4" />
</button>
)}
</div>
);
});
export interface InlineAlertActionProps extends ButtonHTMLAttributes<HTMLButtonElement> {
/** "solid" for the main action, "plain" for secondary ones. */
emphasis?: "solid" | "plain";
}
export const InlineAlertAction = forwardRef<HTMLButtonElement, InlineAlertActionProps>(function InlineAlertAction(
{ emphasis = "solid", type = "button", className, ...props },
ref,
) {
return (
<button
ref={ref}
type={type}
className={cn(
"inline-flex h-8 items-center justify-center gap-1.5 rounded-md px-3 text-[13px] font-medium outline-hidden transition-colors",
"focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50",
emphasis === "solid"
? "border border-foreground/10 bg-background text-foreground shadow-xs hover:bg-accent dark:bg-input/30 dark:hover:bg-input/50"
: "text-foreground/80 hover:bg-foreground/[0.06] hover:text-foreground",
className,
)}
{...props}
/>
);
});
