Alert

Preview

import { Alert, AlertDescription, AlertTitle } from "@/registry/components/alert/react/alert";
import { AlertCircleIcon, CheckCircle2Icon, PopcornIcon } from "lucide-react";
export function AlertDemo() {
return (
<div className="flow w-full">
<Alert>
<CheckCircle2Icon />
<div className="flow">
<AlertTitle>Success! Your changes have been saved</AlertTitle>
<AlertDescription className="flow-space-3xs">
This is an alert with icon, title and description.
</AlertDescription>
</div>
</Alert>
<Alert>
<PopcornIcon />
<AlertTitle>This Alert has a title and an icon. No description.</AlertTitle>
</Alert>
<Alert data-intent="danger">
<AlertCircleIcon />
<div className="flow">
<AlertTitle>Unable to process your payment.</AlertTitle>
<AlertDescription className="flow-space-3xs">
<p>Please verify your billing information and try again.</p>
<ul className="list-inside">
<li>Check your card details</li>
<li>Ensure sufficient funds</li>
<li>Verify billing address</li>
</ul>
</AlertDescription>
</div>
</Alert>
</div>
);
}

Installation

You can add the alert component in two ways:

  1. During project initialization:
pnpm dlx make-sugarcube@latest init
npx make-sugarcube@latest init
yarn make-sugarcube@latest init
bunx --bun make-sugarcube@latest init

Then select the alert component when prompted.

  1. After initialization:
pnpm dlx make-sugarcube@latest components alert --framework react
npx make-sugarcube@latest components alert --framework react
yarn make-sugarcube@latest components alert --framework react
bunx --bun make-sugarcube@latest components alert --framework react

What’s included

When you add the button component, you get:

  • alert.tsx - The React component implementation
  • alert.css - The base styles for the alert
  • @clsx - The clsx utility for conditional classes
import cn from "clsx";
function Alert({ className, ...props }: React.ComponentProps<"div">) {
return <div data-slot="alert" role="alert" className={cn("alert", className)} {...props} />;
}
function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
return <div data-slot="alert-title" className={cn("alert-title", className)} {...props} />;
}
function AlertDescription({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="alert-description"
className={cn("alert-description", className)}
{...props}
/>
);
}
export { Alert, AlertTitle, AlertDescription };
.alert {
--tinted-shadows: 1;
--_alert-border-mix: 20%;
--_alert-shadow-mix: 30%;
--_alert-bg: var(--_alert-color, var(--color-bg));
--_alert-border-color: color-mix(in oklab, var(--_alert-bg), black var(--_alert-border-mix));
--_alert-shadow: 0px 4px 6px -1px
color-mix(
in oklab,
color-mix(in oklab, black, var(--_alert-bg, black) calc(var(--tinted-shadows) * 100%))
var(--_alert-shadow-mix),
transparent
), 0px 2px 4px -2px
color-mix(
in oklab,
color-mix(in oklab, black, var(--_alert-bg, black) calc(var(--tinted-shadows) * 100%))
var(--_alert-shadow-mix),
transparent
);
--_alert-title-col-start: 2;
--_alert-description-col-start: 2;
--_alert-action-col-start: 3;
position: relative;
background-color: var(--_alert-bg);
border: var(--border-width) var(--border-style) var(--_alert-border-color, var(--border-color));
padding-inline: var(--space-sm);
padding-block: var(--space-xs);
border-radius: var(--radius-panel);
box-shadow: var(--_alert-shadow);
&:has(svg) {
display: grid;
grid-template-columns: 1rem 1fr;
row-gap: var(--space-3xs);
column-gap: var(--space-xs);
}
.button {
--_button-fg: var(--color-text-primary);
grid-column: var(--_alert-action-col-start);
}
svg {
transform: translateY(4px);
justify-self: start;
align-self: start;
}
.alert-title {
display: -webkit-box;
-webkit-line-clamp: 1;
line-clamp: 1;
-webkit-box-orient: vertical;
overflow: hidden;
grid-column-start: var(--_alert-title-col-start);
font-weight: var(--font-weight-medium);
font-size: var(--text-base);
letter-spacing: var(--tracking-tight);
}
.alert-description {
grid-column-start: var(--_alert-description-col-start);
font-size: var(--text-base);
}
}
.alert-error {
--_alert-color: var(--color-error);
--_alert-border-color: var(--color-error);
color: var(--color-error-content);
}
.alert-info {
--_alert-color: var(--color-info);
--_alert-border-color: var(--color-info);
color: var(--color-info-content);
}
.alert-success {
--_alert-color: var(--color-success);
--_alert-border-color: var(--color-success);
color: var(--color-success-content);
}
.alert-warning {
--_alert-color: var(--color-warning);
--_alert-border-color: var(--color-warning);
color: var(--color-warning-content);
}
.alert-outline {
color: var(--_alert-color);
box-shadow: none;
background-color: #0000;
background-image: none;
}
.alert-soft {
--_alert-soft-bg-mix: 8%;
--_alert-soft-border-mix: 10%;
color: var(--_alert-color);
background: color-mix(in oklab, var(--_alert-color) var(--_alert-soft-bg-mix), transparent);
border-color: color-mix(
in oklab,
var(--_alert-color) var(--_alert-soft-border-mix),
transparent
);
box-shadow: none;
background-image: none;
}

Examples

:::note The following examples show how to use data attributes for variants. This is just one possible approach. See the principles section for more information on how to approach component variants. :::