Alert
A note boxed off from the page; the destructive one in red pen.
The ink is still wet
Changes save as you type; give it a second before closing the tab.
The page couldn't be saved
The connection dropped halfway. Nothing was lost; try again.
import { Alert, AlertAction, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
import { InkGlyph } from "@/lib/ink-glyphs";
export default function AlertDemo() {
return (
<div className="flex w-full max-w-xl flex-col gap-6">
<Alert seed="alert-note">
<InkGlyph name="info-circle" />
<AlertTitle>The ink is still wet</AlertTitle>
<AlertDescription>Changes save as you type; give it a second before closing the tab.</AlertDescription>
</Alert>
<Alert variant="destructive" seed="alert-error">
<InkGlyph name="alert-circle" />
<AlertTitle>The page couldn't be saved</AlertTitle>
<AlertDescription>The connection dropped halfway. Nothing was lost; try again.</AlertDescription>
<AlertAction>
<Button size="xs" variant="destructive" seed="alert-retry">
Retry
</Button>
</AlertAction>
</Alert>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ alert"use client";
import { useId, useMemo, type ComponentProps } from "react";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/utils";
import { usePen, type Pen } from "@/hooks/use-ink-box";
import { Stroke } from "@/lib/ink";
import { InkOutline } from "@/lib/ink-outline";
import { hatchStrokes, roundedRectPath } from "@/lib/ink-sketch";
const alertVariants = cva(
"group/alert relative grid w-full gap-0.5 px-4 py-3 text-left text-base has-data-[slot=alert-action]:pr-20 has-[>svg:not(.ink-sketch)]:grid-cols-[auto_1fr] has-[>svg:not(.ink-sketch)]:gap-x-3 *:[svg:not(.ink-sketch)]:row-span-2 *:[svg:not(.ink-sketch)]:translate-y-1 *:[svg:not(.ink-sketch)]:text-current *:[svg:not(.ink-sketch):not([class*='size-'])]:size-5",
{
variants: {
variant: {
default: "text-card-foreground",
note: "text-card-foreground",
destructive: "text-destructive *:data-[slot=alert-description]:text-destructive",
},
},
defaultVariants: { variant: "default" },
},
);
/**
* A note boxed off from the page; the destructive one in red pen. The `note`
* variant is hatched lightly across, as if highlighted in the margin.
*/
function Alert({
className,
variant,
children,
seed,
roughness,
passes,
radius,
corners,
fill,
draw,
weight,
speed,
...props
}: ComponentProps<"div"> & VariantProps<typeof alertVariants> & Omit<Pen, "shadow"> & { seed?: string | number }) {
const pen = usePen({ roughness, passes, radius, corners, fill, draw, weight, speed });
return (
<div data-slot="alert" role="alert" className={cn(alertVariants({ variant }), className)} {...props}>
<InkOutline pen={pen} seed={seed} estimate={[480, 72]} maxRadius={18} className={variant === "destructive" ? "text-destructive" : "text-ink-line"}>
{({ w, h, r, s }) => variant === "note" && <AlertWash w={w} h={h} r={r} s={s} fill={pen.fill ?? "hatch"} draw={pen.draw} />}
</InkOutline>
{children}
</div>
);
}
/** The note's hatching, kept inside the box. */
function AlertWash({ w, h, r, s, fill, draw }: { w: number; h: number; r: number; s: number; fill: NonNullable<Pen["fill"]>; draw: Pen["draw"] }) {
const id = useId().replace(/[^\w-]/g, "");
const d = useMemo(() => (fill === "flat" ? "" : hatchStrokes(s + 30, w, h, { gap: 5.5, angle: -50, jitter: 0.5, inset: 1.5 }).join("")), [fill, s, w, h]);
return (
<>
<defs>
<clipPath id={`${id}-clip`}>
<path d={roundedRectPath(w, h, r)} />
</clipPath>
</defs>
<g clipPath={`url(#${id}-clip)`}>
{d ? <Stroke d={d} draw={draw ?? "auto"} delay={200} duration={560} width={0.9} opacity={0.32} /> : <path d={roundedRectPath(w, h, r)} className="ink-fill" style={{ opacity: 0.12 }} />}
</g>
</>
);
}
function AlertTitle({ className, ...props }: ComponentProps<"div">) {
return (
<div
data-slot="alert-title"
className={cn(
"relative font-bold group-has-[>svg:not(.ink-sketch)]/alert:col-start-2 [&_a]:underline [&_a]:underline-offset-3",
className,
)}
{...props}
/>
);
}
function AlertDescription({ className, ...props }: ComponentProps<"div">) {
return (
<div
data-slot="alert-description"
className={cn(
"relative text-base text-balance text-ink-2 group-has-[>svg:not(.ink-sketch)]/alert:col-start-2 md:text-pretty [&_a]:underline [&_a]:underline-offset-3 [&_p:not(:last-child)]:mb-4",
className,
)}
{...props}
/>
);
}
function AlertAction({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="alert-action" className={cn("absolute top-2.5 right-3", className)} {...props} />;
}
export { Alert, AlertTitle, AlertDescription, AlertAction };Usage
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
<Alert variant="destructive">
<AlertTitle>The page couldn't be saved</AlertTitle>
<AlertDescription>The connection dropped halfway.</AlertDescription>
</Alert>Examples
Without an icon
Title only, description only, and with an action.
Title only, no icon
A plain note: a single line of text boxed off from the page.
A new refill is ready
Swap it in before your next long session.
import { Alert, AlertAction, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
import { InkGlyph } from "@/lib/ink-glyphs";
export default function AlertSimple() {
return (
<div className="flex w-full max-w-xl flex-col gap-6">
<Alert seed="as-title">
<AlertTitle>Title only, no icon</AlertTitle>
</Alert>
<Alert seed="as-description">
<AlertDescription>A plain note: a single line of text boxed off from the page.</AlertDescription>
</Alert>
<Alert seed="as-action">
<InkGlyph name="info-circle" />
<AlertTitle>A new refill is ready</AlertTitle>
<AlertDescription>Swap it in before your next long session.</AlertDescription>
<AlertAction>
<Button size="xs" variant="outline" seed="as-action-btn">
Order
</Button>
</AlertAction>
</Alert>
</div>
);
}Variants
Pencil, a hatched note, and red pen, each with its glyph.
Default
Pencilled around the note, for anything worth a second look.
Note
Hatched lightly across, as if it had been highlighted in the margin.
Destructive
In red pen. The notebook will be deleted for good.
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { InkGlyph } from "@/lib/ink-glyphs";
export default function AlertVariants() {
return (
<div className="grid w-full max-w-xl gap-6">
<Alert seed="av-default">
<InkGlyph name="info-circle" />
<AlertTitle>Default</AlertTitle>
<AlertDescription>Pencilled around the note, for anything worth a second look.</AlertDescription>
</Alert>
<Alert variant="note" seed="av-note">
<InkGlyph name="info-circle" />
<AlertTitle>Note</AlertTitle>
<AlertDescription>Hatched lightly across, as if it had been highlighted in the margin.</AlertDescription>
</Alert>
<Alert variant="destructive" seed="av-destructive">
<InkGlyph name="alert-circle" />
<AlertTitle>Destructive</AlertTitle>
<AlertDescription>In red pen. The notebook will be deleted for good.</AlertDescription>
</Alert>
</div>
);
}Dismissible
An action in the corner closes the note, and brings it back.
Autosave is on
Your page is saved as you write. Turn it off in settings.
"use client";
import { useState } from "react";
import { Alert, AlertAction, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
import { InkGlyph } from "@/lib/ink-glyphs";
export default function AlertDismiss() {
const [open, setOpen] = useState(true);
return (
<div className="flex w-full max-w-xl flex-col items-start gap-4">
{open ? (
<Alert variant="note" seed="ad-note">
<InkGlyph name="info-circle" />
<AlertTitle>Autosave is on</AlertTitle>
<AlertDescription>Your page is saved as you write. Turn it off in settings.</AlertDescription>
<AlertAction>
<Button variant="ghost" size="icon-sm" seed="ad-close" onClick={() => setOpen(false)}>
<InkGlyph name="close" />
<span className="sr-only">Dismiss</span>
</Button>
</AlertAction>
</Alert>
) : (
<p className="text-ink-2">Dismissed.</p>
)}
{!open && (
<Button variant="outline" size="sm" seed="ad-undo" onClick={() => setOpen(true)}>
Bring it back
</Button>
)}
</div>
);
}API reference
- variantdefault
"default" "default" | "note" | "destructive"Pencil, hatched lightly across like a margin highlight, or red pen.
- fill
"hatch" | "flat"How the note variant is coloured in: hatching, or a flat wash.
- seed
string | numberPins the drawing.
Pen settings
The drawing also takes draw, roughness, passes, radius, corners, fill, weight and speed, as props or from the nearest InkProvider. What each one does.