Announcement bar
A slim banner that rotates messages.
Installation
Run the CLI
Run
pnpm dlx asta-ui@latest initonce, then this. The file lands incomponents/astabyte/and the colour tokens go into your CSS.$ pnpm dlx asta-ui@latest add announcement-barOr install the package
Don’t want the file? Import from
asta-ui/announcement-barand update withpnpm update asta-ui. Add the stylesheet once. More.$ pnpm add asta-uiimport { AnnouncementBar } from "asta-ui/announcement-bar";Or copy it by hand
Copy this file. Keep the paths, or fix the
@/imports. Then add the tokens fromglobals.css."use client"; import { useEffect, useRef, useState, useSyncExternalStore, type ReactNode } from "react"; const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" "); const KEYFRAMES = `@keyframes asta-announce-in { from { opacity: 0; transform: translateY(100%) } } @keyframes asta-announce-out { to { opacity: 0; transform: translateY(-100%) } }`; export interface AnnouncementMessage { text: ReactNode; href?: string; /** Link text after the message. Defaults to "Learn more" when there's an `href`. */ linkLabel?: string; /** A short tag before the message, e.g. "New". */ badge?: string; } export type AnnouncementBarVariant = "ink" | "brand" | "subtle"; export interface AnnouncementBarProps { /** One message stays put. Two or more take turns, sliding up. */ messages: AnnouncementMessage[]; variant?: AnnouncementBarVariant; /** Time each message stays, in ms. Hover or focus pauses it. */ interval?: number; dismissible?: boolean; /** Controlled visibility. Set it to false to play the exit. */ open?: boolean; /** Called when the close button is pressed. */ onDismiss?: () => void; /** Called once the bar has slid away. */ onExited?: () => void; /** Remember the dismissal in localStorage under this key, so it stays closed on the next visit. */ storageKey?: string; /** Accessible name for the region. */ label?: string; className?: string; } const noop = () => () => {}; const readFlag = (key?: string) => { if (!key) return false; try { return localStorage.getItem(key) === "1"; } catch { return false; } }; const SURFACE: Record<AnnouncementBarVariant, { bar: string; badge: string; link: string; close: string }> = { ink: { bar: "bg-ink text-paper", badge: "bg-brand text-on-brand", link: "text-paper", close: "text-paper/60 hover:text-paper hover:bg-paper/10" }, brand: { bar: "bg-brand text-on-brand", badge: "bg-on-brand/15 text-on-brand", link: "text-on-brand", close: "text-on-brand/70 hover:text-on-brand hover:bg-on-brand/10" }, subtle: { bar: "bg-paper-2 text-ink", badge: "bg-[color-mix(in_srgb,var(--color-brand)_14%,transparent)] text-brand", link: "text-brand", close: "text-mute hover:text-ink hover:bg-paper" }, }; function Message({ m, tone, tabbable }: { m: AnnouncementMessage; tone: (typeof SURFACE)[AnnouncementBarVariant]; tabbable: boolean }) { return ( <p className="flex min-w-0 items-center justify-center gap-2.5 text-[13px] leading-snug"> {m.badge && <span className={cx("shrink-0 rounded-md px-1.5 py-0.5 text-[11px] font-medium", tone.badge)}>{m.badge}</span>} <span className="min-w-0 truncate">{m.text}</span> {m.href && ( <a href={m.href} tabIndex={tabbable ? undefined : -1} className={cx("group/link inline-flex shrink-0 items-center gap-1 rounded-sm font-medium underline-offset-4 outline-none hover:underline focus-visible:ring-2 focus-visible:ring-brand/60", tone.link)} > {m.linkLabel ?? "Learn more"} <svg viewBox="0 0 24 24" className="h-3.5 w-3.5 transition-transform duration-300 ease-[cubic-bezier(.16,1,.3,1)] group-hover/link:translate-x-0.5" fill="none" stroke="currentColor" strokeWidth="2.7" strokeLinecap="round" strokeLinejoin="round" aria-hidden> <path d="M4 12h15" /> <path d="m13 6 6 6-6 6" /> </svg> </a> )} </p> ); } /** * A slim banner for the top of a page: a message and a link, with an optional close button. Pass several * messages and they take turns, the old one sliding up as the next rises in. Hovering or focusing pauses it, * and reduced motion stops the rotation. Closing slides it up and fades it out, then it unmounts. */ export function AnnouncementBar({ messages, variant = "ink", interval = 5000, dismissible = true, open, onDismiss, onExited, storageKey, label = "Announcement", className }: AnnouncementBarProps) { // A remembered dismissal. The server renders the bar, the client hides it on hydration if it was closed before. const remembered = useSyncExternalStore(noop, () => readFlag(storageKey), () => false); const [inner, setInner] = useState(true); const visible = open ?? inner; const [present, setPresent] = useState(visible); if (visible && !present) setPresent(true); // `prev` is the message that just left (it plays its exit), or null before the first turn. const [{ cur, prev }, setTurn] = useState<{ cur: number; prev: number | null }>({ cur: 0, prev: null }); const [paused, setPaused] = useState(false); const [calm, setCalm] = useState(false); const root = useRef<HTMLDivElement>(null); const tone = SURFACE[variant]; const count = messages.length; const index = count ? cur % count : 0; const rotating = count > 1 && !paused && !calm; const rendered = present && count > 0 && !(open === undefined && remembered); // Off screen (scrolled past, or in a hidden panel) the rotation stops; it resumes when the bar comes back. const [onScreen, setOnScreen] = useState(true); useEffect(() => { const el = root.current; if (!rendered || !el) return; const io = new IntersectionObserver(([e]) => setOnScreen(e.isIntersecting)); io.observe(el); return () => io.disconnect(); }, [rendered]); useEffect(() => { const mq = window.matchMedia("(prefers-reduced-motion: reduce)"); const sync = () => setCalm(mq.matches); sync(); mq.addEventListener("change", sync); return () => mq.removeEventListener("change", sync); }, []); // Next message every `interval`, only while the tab is visible. useEffect(() => { if (!rotating || !present || !onScreen) return; const id = setInterval(() => { if (document.hidden) return; setTurn((t) => ({ cur: (t.cur + 1) % count, prev: t.cur % count })); }, interval); return () => clearInterval(id); }, [rotating, present, onScreen, interval, count]); const dismiss = () => { // Focus would be lost with the bar: hand it to the page. if (root.current?.contains(document.activeElement)) (document.activeElement as HTMLElement).blur(); if (open === undefined) setInner(false); onDismiss?.(); }; if (!rendered) return null; return ( <div ref={root} role="region" aria-label={label} data-state={visible ? "open" : "closed"} onPointerEnter={(e) => e.pointerType === "mouse" && setPaused(true)} onPointerLeave={(e) => e.pointerType === "mouse" && setPaused(false)} onFocus={() => setPaused(true)} onBlur={(e) => { if (!e.currentTarget.contains(e.relatedTarget as Node)) setPaused(false); }} onAnimationEnd={(e) => { if (e.target !== e.currentTarget || visible) return; // Remember it once the exit has played (writing it earlier would hide the bar mid-animation). if (storageKey) { try { localStorage.setItem(storageKey, "1"); } catch {} } setPresent(false); onExited?.(); }} className={cx( "relative flex min-h-10 w-full items-center gap-2 px-3 sm:px-4", "data-[state=closed]:pointer-events-none data-[state=closed]:animate-[asta-announce-out_.32s_cubic-bezier(.16,1,.3,1)_both] motion-reduce:![animation-duration:1ms]", tone.bar, className, )} > <style href="asta-announcement-bar" precedence="asta"> {KEYFRAMES} </style> {/* Balances the close button so the message stays centred. */} {dismissible && <span aria-hidden className="w-7 shrink-0" />} {count === 1 ? ( <div className="min-w-0 flex-1 py-2.5"> <Message m={messages[0]} tone={tone} tabbable /> </div> ) : ( // Live only while still, so a rotating bar doesn't talk over the page. <div className="relative h-10 min-w-0 flex-1 overflow-hidden" aria-live={rotating ? "off" : "polite"}> {messages.map((m, i) => { const isCur = i === index; const isPrev = i === prev && !isCur; return ( <div key={i} aria-hidden={!isCur} className={cx( "absolute inset-0 flex items-center justify-center", isCur && prev !== null && !calm && "animate-[asta-announce-in_.6s_cubic-bezier(.16,1,.3,1)_both]", isPrev && !calm && "animate-[asta-announce-out_.6s_cubic-bezier(.16,1,.3,1)_both]", !isCur && (isPrev && !calm ? "pointer-events-none" : "invisible opacity-0"), )} > <Message m={m} tone={tone} tabbable={isCur} /> </div> ); })} </div> )} {dismissible && ( <button type="button" aria-label="Dismiss announcement" onClick={dismiss} className={cx("grid h-7 w-7 shrink-0 place-items-center rounded-md outline-none transition-[background-color,color,transform] duration-200 active:scale-90 focus-visible:ring-2 focus-visible:ring-brand/60", tone.close)} > <svg viewBox="0 0 24 24" className="h-3.5 w-3.5" fill="none" stroke="currentColor" strokeWidth="2.7" strokeLinecap="round" strokeLinejoin="round" aria-hidden> <path d="m6 6 12 12" /> <path d="M18 6 6 18" /> </svg> </button> )} </div> ); } export default AnnouncementBar;Or ask your AI agent
Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.
Add the Astabyte UI Announcement bar component to my project. A slim banner that rotates messages. It includes: rotating, dismissible, badge, ink, brand, subtle, storageKey. Install it: ```sh pnpm dlx asta-ui@latest add announcement-bar --radix ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/announcement-bar.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import { AnnouncementBar, type AnnouncementMessage } from "@/components/astabyte/announcement-bar"; ``` Usage example and props: https://ui.astabyte.com/announcement-bar Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]Or use plain HTML + CSS
Not on React? Use it from Django, Rails, Laravel, Go
html/template, htmx or a static page. Loadasta.cssandasta.jsonce (thehead.htmltab), then paste the markup. Theui.astabyte.com/cdnlink is temporary: pin a version from jsDelivr for production. More in HTML + CSS.<!-- Needs asta.js: rotates the messages every data-interval ms and handles dismiss. --> <div style="display:flex;flex-direction:column;gap:12px;overflow:hidden;border-radius:12px"> <div role="region" aria-label="Announcement" data-asta-announce data-interval="3500" class="asta-announcement"> <span aria-hidden="true" class="asta-announcement-spacer"></span> <div aria-live="off" data-asta-announce-track class="asta-announcement-track"> <div data-asta-announce-msg data-state="cur" class="asta-announcement-slide"><p class="asta-announcement-text"><span class="asta-announcement-badge">New</span><span>Astabyte UI now ships a CSS-only layer.</span><a href="#announcement-bar" class="asta-announcement-link">Learn more<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.5 8h9M9 4.5 12.5 8 9 11.5"/></svg></a></p></div> <div data-asta-announce-msg aria-hidden="true" class="asta-announcement-slide"><p class="asta-announcement-text"><span>Dark mode follows your system.</span><a href="#announcement-bar" class="asta-announcement-link">Try it<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.5 8h9M9 4.5 12.5 8 9 11.5"/></svg></a></p></div> </div> <button type="button" data-asta-announce-close aria-label="Dismiss announcement" class="asta-announcement-close"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" aria-hidden="true"><path d="m4.5 4.5 7 7m0-7-7 7"/></svg></button> </div> <div role="region" aria-label="Announcement" data-asta-announce class="asta-announcement asta-announcement-brand"> <span aria-hidden="true" class="asta-announcement-spacer"></span> <div class="asta-announcement-single"><p class="asta-announcement-text"><span>Launch week starts Monday.</span><a href="#announcement-bar" class="asta-announcement-link">Learn more<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.5 8h9M9 4.5 12.5 8 9 11.5"/></svg></a></p></div> <button type="button" data-asta-announce-close aria-label="Dismiss announcement" class="asta-announcement-close"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" aria-hidden="true"><path d="m4.5 4.5 7 7m0-7-7 7"/></svg></button> </div> <div role="region" aria-label="Announcement" data-asta-announce class="asta-announcement asta-announcement-subtle"> <div class="asta-announcement-single"><p class="asta-announcement-text"><span class="asta-announcement-badge">Beta</span><span>Server search in the command palette.</span></p></div> </div> </div>
Usage
The demo above, as code.
"use client";
import { useState } from "react";
import { AnnouncementBar, type AnnouncementMessage } from "@/components/astabyte/announcement-bar";
const ROTATING: AnnouncementMessage[] = [
{ badge: "New", text: "Saved views are here. Pin the filters you use every day.", href: "#views", linkLabel: "See how" },
{ text: "Spring plans are 20% off for the first year.", href: "#pricing", linkLabel: "View plans" },
{ text: "Join the live product walkthrough on Thursday.", href: "#event", linkLabel: "Save a seat" },
];
export default function AnnouncementBarDemo() {
const [round, setRound] = useState(0);
const [gone, setGone] = useState(0);
return (
<div className="flex min-h-[420px] w-full items-center justify-center p-6 sm:p-10">
<div className="flex w-full max-w-xl flex-col gap-4">
{/* A page frame: the bars sit on top, the header below. */}
<div key={round} className="overflow-hidden rounded-xl bg-paper-2">
<AnnouncementBar messages={ROTATING} onExited={() => setGone((g) => g + 1)} />
<div className="flex items-center justify-between px-5 py-4">
<span className="text-[14px] font-medium tracking-[-0.01em] text-ink">Astabyte</span>
<span className="flex gap-4 text-[12.5px] text-mute">
<span>Product</span>
<span>Pricing</span>
<span>Docs</span>
</span>
</div>
<div className="flex flex-col gap-2 px-5 pb-6">
<span className="h-3 w-2/3 rounded bg-paper" />
<span className="h-3 w-1/2 rounded bg-paper" />
</div>
</div>
<div key={`s${round}`} className="overflow-hidden rounded-xl">
<AnnouncementBar variant="subtle" messages={[{ text: "Scheduled maintenance on Sunday, 02:00–03:00 UTC.", href: "#status", linkLabel: "Status page" }]} onExited={() => setGone((g) => g + 1)} />
</div>
<p className="text-center text-[12.5px] text-mute">
Hover to pause the rotation. Close a bar to see it leave.
{gone > 0 && (
<>
{" "}
<button type="button" onClick={() => {
setRound((r) => r + 1);
setGone(0);
}} className="rounded-sm font-medium text-ink-2 underline underline-offset-4 outline-none hover:text-ink focus-visible:ring-2 focus-visible:ring-brand/50">
Show again
</button>
</>
)}
</p>
</div>
</div>
);
}