Banner
A bar across the top of a page or an app for news that concerns all of it: maintenance, a trial that ends, an outage. One message, one link, one action and a close button. For a message about one part of the page use an alert; for the result of something the reader just did, a toast.
Install
npx shadcn@latest add https://thrumui.dev/r/banner.jsonAdds components/ui/banner.tsx. Nothing else in your project changes.
import { Banner } from "@/components/ui/banner";Automatically installs icons info, octagon-alert, triangle-alert and x from the icon set.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
Banner: variant | "neutral" | "info" | "warning" | "destructive" | Default "neutral" | What kind of announcement it is. Info, warning and destructive add their own glyph and tint the bottom edge. |
Banner: open | boolean | – | Whether it is shown, when the parent holds the state. Use it with onOpenChange. |
Banner: defaultOpen | boolean | Default true | Whether it starts shown, when the banner holds its own state. |
Banner: onOpenChange | (open: boolean) => void | – | Called with false when the close button is pressed. Remember the dismissal here; the banner stores nothing. |
Banner: dismissible | boolean | Default true | Show the 28 pixel close button at the end of the bar. |
Banner: dismissLabel | string | Default "Dismiss" | The close button's accessible name. |
Banner: sticky | boolean | Default false | Stick to the top of the scrolling container while the page scrolls under it. |
Banner: framed | boolean | Default false | Frame the banner with rounded corners and a full border, for inset or floating placement. |
Banner: icon | ReactNode | – | Replaces the variant's glyph. null leaves it out. |
Banner: sound | boolean | Default true | Play a warning or error cue when the banner opens or switches into warning or destructive, if sound is on. |
Banner: aria-label | string | Default "Announcement" | The name of the banner's region landmark. |
Banner: ...props | ComponentProps<"div"> | – | Every other div attribute is passed through. className is merged last. |
BannerContent | ComponentProps<"p"> | – | The message, with an inline link in it. Long words break instead of overflowing. |
BannerAction | ComponentProps<"div"> | – | The one action, usually a small outline button. It moves under the message when there is no room beside it. |
Motion and sound
Motion
- On dismissal the bar folds its height to nothing over 200 ms, and the content below moves up with it.
- Reduced motion: it disappears at once.
Sound
- A warning tone for warning and an error tone for destructive when the banner opens or switches into that variant. Neutral and info are silent.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- The banner is a region landmark named Announcement, so screen reader users can find it in the landmarks list. Name it otherwise with aria-label.
- It is not a live region: it is part of the page, not news about something that just happened. Use an alert or a toast for that.
- The variant's glyph is hidden from screen readers; the message says what kind of news it is.
- Once dismissed it is invisible as well as folded, so nothing inside it can be focused or read.
- On a phone the message, link and action wrap. The close button keeps its own column and is never covered.
- Under a glass material the bar is a translucent panel. Its tint keeps the text at 4.5 to 1 over any page.
| Keys | Action |
|---|---|
| Tab | Move to the link, the action and the close button, in that order. |
| Enter / Space | On the close button: dismiss the banner. |
Source
"use client";
import { useEffect, useRef, useState, type ComponentProps, type ReactNode } from "react";
import { cn } from "@/lib/utils";
import { InfoIcon } from "@/components/icons/info";
import { OctagonAlertIcon } from "@/components/icons/octagon-alert";
import { TriangleAlertIcon } from "@/components/icons/triangle-alert";
import { XIcon } from "@/components/icons/x";
import { STATUS } from "@/lib/thrum-chart";
import { play, type SoundName } from "@/lib/thrum-sound";
import { IconButton } from "@/components/ui/icon-button";
export type BannerVariant = "neutral" | "info" | "warning" | "destructive";
export type BannerProps = Omit<ComponentProps<"div">, "children"> & {
/** What kind of announcement it is. Info, warning and destructive add a glyph of their own. */
variant?: BannerVariant;
/** Whether it is shown, when the parent holds the state. Use it with `onOpenChange`. */
open?: boolean;
/** Whether it starts shown, when the banner holds its own state. */
defaultOpen?: boolean;
/** Called with `false` when the close button is pressed. Store the dismissal here to remember it. */
onOpenChange?: (open: boolean) => void;
/** Show the close button. */
dismissible?: boolean;
/** The close button's name. */
dismissLabel?: string;
/** Stick to the top of the scrolling container while the page scrolls under it. */
sticky?: boolean;
/** Frame the banner with rounded corners and a full border, for inset or floating placement. */
framed?: boolean;
/** Replace the variant's glyph. `null` leaves it out. */
icon?: ReactNode;
/** Play a warning or error cue when the banner opens or transitions into a warning/destructive state interactively, if sound is on. */
sound?: boolean;
/** The message and its action: a `BannerContent` and a `BannerAction`. */
children?: ReactNode;
};
const GLYPH: Record<BannerVariant, ReactNode> = {
neutral: null,
info: <InfoIcon size={16} />,
warning: <TriangleAlertIcon size={16} />,
destructive: <OctagonAlertIcon size={16} />,
};
const TONE: Record<BannerVariant, string> = {
neutral: "var(--border)",
info: "var(--foreground)",
warning: STATUS.warn,
destructive: STATUS.alarm,
};
const CUE: Partial<Record<BannerVariant, SoundName>> = { warning: "warn", destructive: "error" };
/**
* A bar across the top of a page or an app for something that concerns all of it: maintenance, a trial ending, an outage.
* It is a landmark region named "Announcement" (change it with `aria-label`). On dismissal its height folds to nothing.
* The banner keeps no memory of being dismissed: store that in `onOpenChange` and pass `open`.
*/
export function Banner({
variant = "neutral",
open,
defaultOpen = true,
onOpenChange,
dismissible = true,
dismissLabel = "Dismiss",
sticky = false,
framed = false,
icon,
sound = true,
className,
children,
"aria-label": ariaLabel = "Announcement",
...props
}: BannerProps) {
const [own, setOwn] = useState(defaultOpen);
const shown = open ?? own;
const glyph = icon === undefined ? GLYPH[variant] : icon;
// Silent on initial mount; plays warning/error cues only on interactive open or variant transitions.
const mounted = useRef(false);
const prev = useRef({ shown, variant });
useEffect(() => {
if (!mounted.current) {
mounted.current = true;
prev.current = { shown, variant };
return;
}
const opened = !prev.current.shown && shown;
const changed = shown && prev.current.variant !== variant;
prev.current = { shown, variant };
const cue = CUE[variant];
if (sound && cue && (opened || changed)) play(cue);
}, [sound, shown, variant]);
return (
<div
data-slot="banner"
data-variant={variant}
data-state={shown ? "open" : "closed"}
role="region"
aria-label={ariaLabel}
// Folds its height through the grid row, then turns invisible so nothing inside can take the focus.
className={cn(
"grid w-full transition-[grid-template-rows,visibility] duration-200 ease-out motion-reduce:transition-none",
shown ? "grid-rows-[1fr]" : "invisible grid-rows-[0fr]",
sticky && "sticky top-0 z-40",
className,
)}
{...props}
>
<div className="min-h-0 overflow-hidden">
<div
data-surface="panel"
className={cn(
"flex items-start gap-3 bg-card px-4 py-2 text-sm text-card-foreground",
framed ? "rounded-lg border" : "border-b",
)}
style={{ borderColor: `color-mix(in oklab, ${TONE[variant]} 50%, var(--border))` }}
>
{glyph !== null && (
<span aria-hidden="true" className="grid h-7 flex-none place-items-center [&_svg]:size-4" style={{ color: TONE[variant] }}>
{glyph}
</span>
)}
{/* Message and action wrap below one another on a phone; the close button keeps its own column. */}
<div className="flex min-w-0 flex-1 flex-wrap items-center gap-x-4 gap-y-1">{children}</div>
{dismissible && (
<IconButton
aria-label={dismissLabel}
size="sm"
className="-me-2 text-muted-foreground"
onClick={() => {
setOwn(false);
onOpenChange?.(false);
}}
>
<XIcon />
</IconButton>
)}
</div>
</div>
</div>
);
}
/** The message. Put an inline link in it with `Link` or `linkClass()`. */
export function BannerContent({ className, ...props }: ComponentProps<"p">) {
return <p data-slot="banner-content" className={cn("min-w-0 py-1 leading-5 break-words", className)} {...props} />;
}
/** The one action: a small button or a link styled as one. */
export function BannerAction({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="banner-action" className={cn("flex flex-none items-center gap-2", className)} {...props} />;
}Files and dependencies
- Adds
components/ui/banner.tsx- Packages
- None
- Brings along
- Icon buttonChart foundationsSound engine
- Icons
infooctagon-alerttriangle-alertx
Changelog
- 1.0.0 · 2026-12-03First release.