Announcement Bar
FreeNewA full-width bar for a sale, a code or a notice: five color themes, badge, picture, call to action and close; static, rotating or as a marquee.
Installation
First time? Add the @viewport registry to components.json. Setup takes one minute.
Usage
The code behind the preview above.
"use client";
import { useState } from "react";
import { Gift, ShoppingBag, Truck } from "lucide-react";
import { AnnouncementBar, type Announcement } from "@/components/viewport/announcement-bar";
const rotating: Announcement[] = [
{ id: "sale", badge: "Sale", text: <>Black Friday! Save up to 70% storewide. Use code <strong>BSALE70</strong> at checkout.</>, action: { label: "Shop Now", href: "#shop" } },
{ id: "ship", icon: <Truck />, text: "Free express shipping on every order over $50.", action: { label: "See details", href: "#shipping" } },
{ id: "gift", icon: <Gift />, text: "Gift wrapping is on us until the end of the month.", action: { label: "Add a note", href: "#gift" } },
];
const ticker: Announcement[] = [
{ id: "a", badge: "Christmas sale", text: "Celebrate Christmas with us! Special discounts await you." },
{ id: "b", icon: <Truck />, text: "Order by December 20 for delivery before the holidays." },
{ id: "c", icon: <Gift />, text: "A free gift with every order over $80." },
];
const modes = ["static", "rotate", "marquee"] as const;
export default function AnnouncementBarDemo() {
const [mode, setMode] = useState<(typeof modes)[number]>("static");
const [round, setRound] = useState(0);
return (
<div className="w-full max-w-4xl min-w-0">
<div className="mb-4 flex flex-wrap items-center justify-center gap-1" role="group" aria-label="Mode">
{modes.map((m) => (
<button key={m} type="button" aria-pressed={m === mode} onClick={() => setMode(m)} className="rounded-md px-3 py-1 text-xs font-medium capitalize text-muted-foreground outline-none transition-colors hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring aria-pressed:bg-foreground aria-pressed:text-background">
{m}
</button>
))}
<button type="button" onClick={() => setRound((n) => n + 1)} className="rounded-md px-3 py-1 text-xs font-medium text-muted-foreground outline-none transition-colors hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring">
Reset
</button>
</div>
<div key={`${mode}-${round}`} className="flex min-h-[19rem] flex-col gap-3 [&>*]:overflow-hidden [&>*]:rounded-xl [&>*]:shadow-sm">
{mode === "static" ? (
<>
<AnnouncementBar theme="purple" label="New product" announcements={[{ image: "/demo/product-headphones.jpg", text: "Be the first to experience our latest innovative Quantum headphones!", action: { label: "Shop Now", href: "#shop" } }]} />
<AnnouncementBar theme="light" label="Sale event" announcements={[{ badge: "Update", text: "Don't miss our special sale event! Save up to 50% on top products", action: { label: "Try Now", href: "#sale" } }]} />
<AnnouncementBar theme="red" label="Christmas sale" announcements={[{ badge: "Christmas sale", text: "Celebrate Christmas with us! Special discounts await you.", action: { label: "Shop Now", href: "#christmas" } }]} />
<AnnouncementBar theme="gradient" label="Coupon" announcements={[{ icon: <ShoppingBag />, text: <>Get 20% off your next purchase! Use code: <strong>SAVE20</strong> at checkout</>, action: { label: "Use Code", copy: "SAVE20" } }]} />
<AnnouncementBar theme="dark" label="Black Friday" announcements={[{ badge: "Sale", text: <>Black Friday! Save up to 70% storewide. Use code <strong>BSALE70</strong> at checkout.</>, action: { label: "Shop Now", href: "#black-friday" } }]} />
</>
) : mode === "rotate" ? (
<>
<AnnouncementBar mode="rotate" theme="dark" interval={3500} announcements={rotating} />
<AnnouncementBar mode="rotate" theme="light" interval={4500} label="Store updates" announcements={[...rotating].reverse().map((a) => ({ ...a, badge: a.badge ? "Update" : undefined }))} />
</>
) : (
<>
<AnnouncementBar mode="marquee" theme="red" announcements={ticker} action={{ label: "Shop Now", href: "#christmas" }} />
<AnnouncementBar mode="marquee" theme="gradient" speed={45} dismissible={false} label="Shipping updates" announcements={ticker.map((a) => ({ ...a, badge: a.badge ? "New" : undefined }))} />
</>
)}
</div>
</div>
);
}Props
| Prop | Type | Default |
|---|---|---|
| announcements* | Announcement[]One announcement, or several for the `rotate` and `marquee` modes. | — |
| mode | "static" | "rotate" | "marquee"`static` shows the first announcement, `rotate` swaps them in turn, `marquee` scrolls them all in a line. | "static" |
| theme | "light" | "dark" | "purple" | "red" | "gradient"Color theme. `light` follows your light and dark tokens; the others keep their colors in both. | "light" |
| colors | { background?: string; text?: string; accent?: string; accentText?: string; badge?: string; badgeText?: string }Overrides for single colors of the theme. Any CSS color; `background` also takes a gradient. | — |
| position | "static" | "sticky" | "top" | "bottom"`static` sits in the page flow, `sticky` stays at the top while scrolling, `top` and `bottom` are fixed to the viewport. | "static" |
| interval | numberMilliseconds each announcement stays in `rotate` mode. | 5000 |
| speed | numberMarquee speed in pixels per second. | 60 |
| action | AnnouncementActionFallback action for announcements that have none; in `marquee` mode it is the one action, pinned to the end. | — |
| dismissible | booleanShows the close button. | true |
| onDismiss | () => voidCalled after the bar has closed. | — |
| label | stringAccessible name of the bar. | "Announcement" |
| className | string | — |
Accessibility
- The bar is a named region, and its action, arrows, pause and close controls are real links and buttons.
- Rotation rests under the pointer or focus and stops once you page by hand; messages you page to are announced.
- The marquee has a Pause button and its second copy is hidden from assistive tech and the tab order.
- Under reduced motion messages swap without sliding, rotation does not start, and the marquee becomes wrapped static text.
- A copy action reports “SAVE20 copied to clipboard” through a status.