Banner
114 lines · 1 file · MIT
One notice above a page: a title, a line of facts and a single action, in info, warning or danger.
Over the Free plan
- Links
- 184 of 30
- Members
- 23 of 3
- Domain stops
- Nov 6
Nothing is deleted. Everything stays readable, and your links keep redirecting.
A domain stopped working
- Domain
- go.northwind-traders.example.com
- Its links
- answer “not found”
A new version is available
import type { ReactNode } from "react";
import { cn } from "./cn";
import { X } from "./icons";
export type BannerTone = "info" | "warning" | "danger";
const TONE = {
info: {
box: "bg-[color-mix(in_oklab,var(--accent)_6%,var(--surface))] shadow-[0_0_0_1px_color-mix(in_oklab,var(--accent)_30%,transparent)]",
title: "text-accent",
},
warning: {
box: "bg-[color-mix(in_oklab,var(--butter)_8%,var(--surface))] shadow-[0_0_0_1px_color-mix(in_oklab,var(--butter)_40%,transparent)]",
title: "text-butter",
},
danger: {
box: "bg-[color-mix(in_oklab,var(--danger)_6%,var(--surface))] shadow-[0_0_0_1px_color-mix(in_oklab,var(--danger)_35%,transparent)]",
title: "text-danger",
},
} satisfies Record<BannerTone, { box: string; title: string }>;
export interface BannerFact {
label: string;
value: string;
/** This figure is the problem: set it in the danger colour. */
over?: boolean;
/** An identifier (a hostname): mono, and shortened rather than wrapped. */
mono?: boolean;
}
/**
* One notice above a page: a title, a line of facts, and one action. Never
* stack two: fold the facts into one line instead.
*
* Over a plan is "warning", not "danger": it means you have more than you
* pay for, not that something broke. Danger is for things already broken.
* The state is named in the title, so the tone never carries it alone.
*/
export function Banner({
tone = "info",
title,
facts,
action,
note,
onDismiss,
className,
}: {
tone?: BannerTone;
title: string;
facts?: BannerFact[];
/** One button or link, at the right. */
action?: ReactNode;
/** A small line of advice under the notice. */
note?: ReactNode;
/** Adds a close button. */
onDismiss?: () => void;
className?: string;
}) {
const t = TONE[tone];
return (
<div className={cn("flex flex-col gap-2", className)}>
<div
role={tone === "danger" ? "alert" : "status"}
// On a phone the title, the facts and the action stack, so the facts
// get the full width instead of a narrow column beside the title.
className={cn(
"flex flex-wrap items-center gap-x-5 gap-y-2 rounded-xl py-2 pr-2 pl-4 max-sm:flex-col max-sm:flex-nowrap max-sm:items-stretch max-sm:py-3 max-sm:pr-3",
t.box,
)}
>
<span className={cn("inline-flex items-center gap-1.5 text-sm font-semibold", t.title)}>
<span aria-hidden className="size-1.5 shrink-0 rounded-full bg-current" />
{title}
</span>
{facts && facts.length > 0 && (
<dl className="flex min-w-0 flex-1 flex-wrap gap-x-5 gap-y-1 text-sm">
{facts.map((f) => (
<div key={f.label} className="flex max-w-full min-w-0 gap-1.5">
<dt className="shrink-0 text-muted">{f.label}</dt>
<dd
title={f.mono ? f.value : undefined}
className={cn(
"tnum min-w-0 font-semibold",
f.over && "text-danger",
f.mono && "truncate font-mono text-[0.8125rem] font-normal",
)}
>
{f.value}
</dd>
</div>
))}
</dl>
)}
{(action || onDismiss) && (
<div className="ml-auto flex items-center gap-1 max-sm:ml-0 max-sm:[&>:first-child]:flex-1">
{action}
{onDismiss && (
<button
type="button"
aria-label="Dismiss"
onClick={onDismiss}
className="grid size-8 shrink-0 cursor-pointer place-items-center rounded-md text-muted transition-colors duration-150 hover:text-text focus-visible:outline-2 focus-visible:outline-accent"
>
<X size={16} aria-hidden />
</button>
)}
</div>
)}
</div>
{note && <p className="max-w-prose px-1 text-xs text-pretty text-muted">{note}</p>}
</div>
);
}
Install
One command. The files land in your repo, with everything they import.
$ bunx shadcn@latest add @blocco/banner- Category
- Feedback
- Files
- banner.tsx