Announcement bar
A slim top-of-site bar that rotates through announcements with a vertical roll, includes a live countdown to a launch, and can be dismissed.
"use client";
import { useEffect, useState } from "react";
import "./announcement-bar.css";
const LAUNCH = new Date("2026-12-12T17:00:00Z"); // change to your date
const MESSAGES = [
{
text: "Version 4 launches in",
countdown: true,
link: "Join the waitlist",
href: "#waitlist",
},
{
text: "Free shipping on every order over €60 this week",
link: "Shop now",
href: "#shop",
},
{
text: "New: dark mode is here for everyone",
link: "See what changed",
href: "#changelog",
},
];
function useCountdown(to: Date) {
const [now, setNow] = useState<number | null>(null); // null on the server, so markup matches
useEffect(() => {
const tick = () => setNow(Date.now());
const raf = requestAnimationFrame(tick);
const t = setInterval(tick, 1000);
return () => {
cancelAnimationFrame(raf);
clearInterval(t);
};
}, []);
if (now === null) return null;
const s = Math.max(0, Math.floor((to.getTime() - now) / 1000));
return {
d: Math.floor(s / 86400),
h: Math.floor((s % 86400) / 3600),
m: Math.floor((s % 3600) / 60),
s: s % 60,
};
}
/**
* AnnouncementBar
* - Messages rotate every 5s with a vertical roll; hovering or focusing pauses.
* - Closing collapses the bar's height so the page below slides up smoothly.
*/
export function AnnouncementBar({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const [i, setI] = useState(0);
const [paused, setPaused] = useState(false);
const [closed, setClosed] = useState(false);
const left = useCountdown(LAUNCH);
useEffect(() => {
if (paused || closed) return;
const t = setTimeout(() => setI((x) => (x + 1) % MESSAGES.length), 5000);
return () => clearTimeout(t);
}, [i, paused, closed]);
const m = MESSAGES[i];
const pad = (n: number) => String(n).padStart(2, "0");
return (
<div className="gu-annc-wrap" data-theme={theme}>
<div
className="gu-annc"
data-closed={closed ? "" : undefined}
aria-hidden={closed}
>
<div
className="gu-annc__inner"
onMouseEnter={() => setPaused(true)}
onMouseLeave={() => setPaused(false)}
onFocus={() => setPaused(true)}
onBlur={() => setPaused(false)}
onTouchStart={() => setPaused(true)}
onTouchEnd={() => setPaused(false)}
>
<p key={i} className="gu-annc__msg" aria-live="polite">
<span>{m.text}</span>
{m.countdown && left && (
<span
className="gu-annc__clock"
aria-label={`${left.d} days, ${left.h} hours and ${left.m} minutes`}
>
<b>{left.d}d</b>
<b>{pad(left.h)}h</b>
<b>{pad(left.m)}m</b>
<b>{pad(left.s)}s</b>
</span>
)}
<a href={m.href} tabIndex={closed ? -1 : 0}>
{m.link} →
</a>
</p>
<div className="gu-annc__dots" aria-hidden="true">
{MESSAGES.map((_, k) => (
<i key={k} data-on={k === i ? "" : undefined} />
))}
</div>
</div>
<button
type="button"
className="gu-annc__close"
aria-label="Dismiss announcement"
onClick={() => setClosed(true)}
tabIndex={closed ? -1 : 0}
>
<svg viewBox="0 0 16 16" aria-hidden="true">
<path d="m4 4 8 8m0-8-8 8" />
</svg>
</button>
</div>
{/* Stand-in for the page header below the bar */}
<div className="gu-annc__page">
<b>yoursite</b>
<span>Features</span>
<span>Pricing</span>
{closed && (
<button type="button" onClick={() => setClosed(false)}>
Show bar again
</button>
)}
</div>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Announcement bar
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A slim announcement bar for the top of a website. It rotates through three messages with a vertical roll, one of them with a live countdown in small lime chips, each with an underlined link. It pauses on hover/focus/touch and can be dismissed (the bar collapses and the page slides up).
## Layout and look
- Light theme: Ink bar with Cream text; dark theme: an orange bar with Ink text.
- Message row: centered, 14–15px weight 600; countdown chips "6d 03h 41m 12s" (lime #d7ff3a with Ink, tabular numbers); link bold with a 2px orange underline (Ink on orange).
- Progress dots under the message: 5px dots, the active one stretches to 16px.
- 40px round close button on the right.
## Behavior and motion
1. Rotate every 5s; the new message rolls up from below (translateY 100% → 0, 500ms cubic-bezier(0.16, 1, 0.3, 1)).
2. Countdown updates each second; it renders only on the client to avoid hydration mismatches.
3. Close: grid-template-rows 1fr → 0fr and fade (450ms), links become unfocusable.
## Accessibility
- The message area is aria-live="polite"; the countdown has a readable aria-label; the close button is labelled. Rotation pauses on focus.
- prefers-reduced-motion: no roll or collapse animation.
## Props
theme. Edit MESSAGES and LAUNCH.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### AnnouncementBar.tsx
```tsx
"use client";
import { useEffect, useState } from "react";
import "./announcement-bar.css";
const LAUNCH = new Date("2026-12-12T17:00:00Z"); // change to your date
const MESSAGES = [
{
text: "Version 4 launches in",
countdown: true,
link: "Join the waitlist",
href: "#waitlist",
},
{
text: "Free shipping on every order over €60 this week",
link: "Shop now",
href: "#shop",
},
{
text: "New: dark mode is here for everyone",
link: "See what changed",
href: "#changelog",
},
];
function useCountdown(to: Date) {
const [now, setNow] = useState<number | null>(null); // null on the server, so markup matches
useEffect(() => {
const tick = () => setNow(Date.now());
const raf = requestAnimationFrame(tick);
const t = setInterval(tick, 1000);
return () => {
cancelAnimationFrame(raf);
clearInterval(t);
};
}, []);
if (now === null) return null;
const s = Math.max(0, Math.floor((to.getTime() - now) / 1000));
return {
d: Math.floor(s / 86400),
h: Math.floor((s % 86400) / 3600),
m: Math.floor((s % 3600) / 60),
s: s % 60,
};
}
/**
* AnnouncementBar
* - Messages rotate every 5s with a vertical roll; hovering or focusing pauses.
* - Closing collapses the bar's height so the page below slides up smoothly.
*/
export function AnnouncementBar({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const [i, setI] = useState(0);
const [paused, setPaused] = useState(false);
const [closed, setClosed] = useState(false);
const left = useCountdown(LAUNCH);
useEffect(() => {
if (paused || closed) return;
const t = setTimeout(() => setI((x) => (x + 1) % MESSAGES.length), 5000);
return () => clearTimeout(t);
}, [i, paused, closed]);
const m = MESSAGES[i];
const pad = (n: number) => String(n).padStart(2, "0");
return (
<div className="gu-annc-wrap" data-theme={theme}>
<div
className="gu-annc"
data-closed={closed ? "" : undefined}
aria-hidden={closed}
>
<div
className="gu-annc__inner"
onMouseEnter={() => setPaused(true)}
onMouseLeave={() => setPaused(false)}
onFocus={() => setPaused(true)}
onBlur={() => setPaused(false)}
onTouchStart={() => setPaused(true)}
onTouchEnd={() => setPaused(false)}
>
<p key={i} className="gu-annc__msg" aria-live="polite">
<span>{m.text}</span>
{m.countdown && left && (
<span
className="gu-annc__clock"
aria-label={`${left.d} days, ${left.h} hours and ${left.m} minutes`}
>
<b>{left.d}d</b>
<b>{pad(left.h)}h</b>
<b>{pad(left.m)}m</b>
<b>{pad(left.s)}s</b>
</span>
)}
<a href={m.href} tabIndex={closed ? -1 : 0}>
{m.link} →
</a>
</p>
<div className="gu-annc__dots" aria-hidden="true">
{MESSAGES.map((_, k) => (
<i key={k} data-on={k === i ? "" : undefined} />
))}
</div>
</div>
<button
type="button"
className="gu-annc__close"
aria-label="Dismiss announcement"
onClick={() => setClosed(true)}
tabIndex={closed ? -1 : 0}
>
<svg viewBox="0 0 16 16" aria-hidden="true">
<path d="m4 4 8 8m0-8-8 8" />
</svg>
</button>
</div>
{/* Stand-in for the page header below the bar */}
<div className="gu-annc__page">
<b>yoursite</b>
<span>Features</span>
<span>Pricing</span>
{closed && (
<button type="button" onClick={() => setClosed(false)}>
Show bar again
</button>
)}
</div>
</div>
);
}
```
### announcement-bar.css
```css
/* AnnouncementBar: change these colors to match your brand. */
.gu-annc-wrap {
--bar: #0e0e10;
--bar-text: #f3eee3;
--accent: #ff5a1f;
--volt: #d7ff3a;
--page: #fbf8f2;
--text: #0e0e10;
--line: rgb(14 14 16 / 0.1);
--focus: #d7ff3a;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
width: min(100%, 860px);
overflow: hidden;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--page);
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-annc-wrap[data-theme="dark"] {
--bar: #ff5a1f;
--bar-text: #0e0e10;
--accent: #0e0e10;
--page: #18181b;
--text: #f3eee3;
--line: rgb(243 238 227 / 0.1);
--focus: #0e0e10;
}
/* Height collapse with the grid-rows trick */
.gu-annc {
display: grid;
grid-template-columns: 1fr 40px;
grid-template-rows: 1fr;
align-items: center;
min-height: 0;
padding: 0 8px 0 16px;
background: var(--bar);
color: var(--bar-text);
transition:
grid-template-rows 0.45s var(--ease),
padding 0.45s var(--ease),
opacity 0.3s;
}
.gu-annc > * {
min-height: 0;
overflow: hidden;
}
.gu-annc[data-closed] {
grid-template-rows: 0fr;
opacity: 0;
}
.gu-annc__inner {
display: grid;
justify-items: center;
padding: 10px 0 8px;
}
.gu-annc__msg {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
gap: 6px 12px;
margin: 0;
font-size: 0.9rem;
font-weight: 600;
text-align: center;
animation: gu-annc-roll 0.5s var(--ease);
}
@keyframes gu-annc-roll {
from {
opacity: 0;
transform: translateY(100%);
}
}
.gu-annc__clock {
display: inline-flex;
gap: 4px;
font-variant-numeric: tabular-nums;
}
.gu-annc__clock b {
padding: 2px 6px;
border-radius: 6px;
background: var(--volt);
color: #0e0e10;
font-size: 0.82rem;
font-weight: 900;
}
.gu-annc-wrap[data-theme="dark"] .gu-annc__clock b {
background: #0e0e10;
color: #ff5a1f;
}
.gu-annc a {
color: inherit;
font-weight: 800;
text-decoration: underline 2px var(--accent);
text-underline-offset: 4px;
}
.gu-annc :focus-visible {
outline: 2px solid var(--focus);
outline-offset: 3px;
border-radius: 4px;
}
.gu-annc__dots {
display: flex;
gap: 4px;
margin-top: 6px;
}
.gu-annc__dots i {
width: 5px;
height: 5px;
border-radius: 5px;
background: currentColor;
opacity: 0.3;
transition:
width 0.4s var(--ease),
opacity 0.3s;
}
.gu-annc__dots i[data-on] {
width: 16px;
opacity: 1;
}
.gu-annc__close {
display: grid;
place-items: center;
width: 40px;
height: 40px;
border: 0;
border-radius: 50%;
background: none;
color: inherit;
cursor: pointer;
}
.gu-annc__close:hover {
background: color-mix(in srgb, currentColor 15%, transparent);
}
.gu-annc__close svg {
width: 14px;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
}
.gu-annc__page {
display: flex;
align-items: center;
gap: 20px;
padding: 18px 20px 80px;
font-size: 0.9rem;
}
.gu-annc__page b {
margin-right: auto;
font-size: 1.1rem;
font-weight: 900;
}
.gu-annc__page span {
opacity: 0.6;
}
.gu-annc__page button {
min-height: 36px;
padding: 0 12px;
border: 1px solid var(--line);
border-radius: 999px;
background: none;
color: inherit;
font: inherit;
font-size: 0.8rem;
font-weight: 700;
cursor: pointer;
}
@media (prefers-reduced-motion: reduce) {
.gu-annc,
.gu-annc__dots i {
transition: none;
}
.gu-annc__msg {
animation: none;
}
}
```
You might also want
Next piece: Architect portfolioCollapsible sidebar
Components, intermediate
Command menu
Components, intermediate
Cookie consent banner
Components, intermediate