Navbars & Headersv0.3.0
Ticker Header
A scrolling announcement strip above the nav that tucks away on scroll.
Installation
CLI
npx shadcn@latest add https://hantui-theta.vercel.app/r/ticker-header.jsonManual
Install the dependencies
npm install clsx lucide-react motion tailwind-mergeAdd the shared helpers
Components use
cn()fromlib/utils.tsand the motion tokens below. See Installation if you have not set them up yet.lib/nav/mobile-menu.tsx"use client"; import { useEffect, useId, useState } from "react"; import { AnimatePresence, motion, useReducedMotion } from "motion/react"; import { ChevronDown } from "lucide-react"; import { cn } from "@/lib/utils"; import type { NavAction, NavItem } from "@/lib/nav/types"; export interface MobileMenuProps { items: NavItem[]; actions?: NavAction[]; /** Colors for light or dark navbars. @default "light" */ tone?: "light" | "dark"; /** Classes for the toggle button. */ className?: string; /** Classes for the panel, to place it under narrow navbars. */ panelClassName?: string; } /** * The compact menu every navbar falls back to on narrow widths: a hamburger that morphs into an X, * and a panel with expandable groups and the actions. Escape closes it and returns focus. */ export function MobileMenu({ items, actions = [], tone = "light", className, panelClassName }: MobileMenuProps) { const id = useId(); const reduceMotion = useReducedMotion(); const [open, setOpen] = useState(false); const [group, setGroup] = useState<string | null>(null); const dark = tone === "dark"; useEffect(() => { if (!open) return; const onKey = (e: KeyboardEvent) => { if (e.key !== "Escape") return; setOpen(false); document.getElementById(`${id}-toggle`)?.focus(); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [open, id]); const line = cn("absolute left-1/2 h-[1.5px] w-4 -translate-x-1/2 rounded-full transition-transform duration-300 motion-reduce:transition-none", dark ? "bg-white" : "bg-[#0b0b0c]"); return ( <> <button id={`${id}-toggle`} type="button" aria-expanded={open} aria-controls={`${id}-panel`} aria-label={open ? "Close menu" : "Open menu"} onClick={() => setOpen((o) => !o)} className={cn("relative grid size-9 shrink-0 cursor-pointer place-items-center rounded-full outline-none focus-visible:ring-2 focus-visible:ring-ring", dark ? "hover:bg-white/10" : "hover:bg-black/5", className)} > <span aria-hidden="true" className={cn(line, open ? "top-1/2 rotate-45" : "top-[13px]")} /> <span aria-hidden="true" className={cn(line, open ? "top-1/2 -rotate-45" : "top-[21px]")} /> </button> <AnimatePresence> {open ? ( <motion.div id={`${id}-panel`} initial={reduceMotion ? { opacity: 0 } : { opacity: 0, y: -8, scale: 0.98 }} animate={{ opacity: 1, y: 0, scale: 1 }} exit={{ opacity: 0, transition: { duration: 0.12 } }} transition={{ duration: 0.22, ease: [0.22, 1, 0.36, 1] }} className={cn("absolute inset-x-2 top-full z-50 mt-2 origin-top rounded-2xl border p-2 shadow-xl", dark ? "border-white/10 bg-[#111113] text-white" : "border-black/[0.06] bg-white text-[#0b0b0c]", panelClassName)} > <ul> {items.map((item) => item.items?.length ? ( <li key={item.label}> <button type="button" aria-expanded={group === item.label} onClick={() => setGroup((g) => (g === item.label ? null : item.label))} className={cn("flex h-11 w-full cursor-pointer items-center justify-between rounded-xl px-3 text-[15px] font-medium outline-none focus-visible:ring-2 focus-visible:ring-ring", dark ? "hover:bg-white/5" : "hover:bg-black/[0.03]")} > {item.label} <ChevronDown className={cn("size-4 opacity-60 transition-transform", group === item.label && "rotate-180")} aria-hidden="true" /> </button> <div className={cn("grid transition-[grid-template-rows] duration-300 motion-reduce:transition-none", group === item.label ? "grid-rows-[1fr]" : "grid-rows-[0fr]")}> <ul className="min-h-0 overflow-hidden" inert={group !== item.label}> {item.items.map((link) => ( <li key={link.label}> <a href={link.href} className={cn("flex items-center gap-2.5 rounded-lg py-2 pr-3 pl-6 text-[14px] outline-none focus-visible:ring-2 focus-visible:ring-ring", dark ? "text-white/75 hover:text-white" : "text-[#52525b] hover:text-[#0b0b0c]")}> {link.icon ? <span aria-hidden="true" className="opacity-70 [&_svg]:size-4">{link.icon}</span> : null} {link.label} </a> </li> ))} </ul> </div> </li> ) : ( <li key={item.label}> <a href={item.href ?? "#"} className={cn("flex h-11 items-center rounded-xl px-3 text-[15px] font-medium outline-none focus-visible:ring-2 focus-visible:ring-ring", dark ? "hover:bg-white/5" : "hover:bg-black/[0.03]")}> {item.label} </a> </li> ), )} </ul> {actions.length ? ( <div className={cn("mt-2 grid gap-2 border-t pt-3", dark ? "border-white/10" : "border-black/[0.06]")}> {actions.map((a, i) => ( <a key={a.label} href={a.href} className={cn( "flex h-11 items-center justify-center rounded-xl text-[14px] font-medium outline-none focus-visible:ring-2 focus-visible:ring-ring", i === actions.length - 1 ? (dark ? "bg-white text-[#0b0b0c]" : "bg-[#0b0b0c] text-white") : dark ? "border border-white/15" : "border border-black/10", )} > {a.label} </a> ))} </div> ) : null} </motion.div> ) : null} </AnimatePresence> </> ); }lib/nav/types.tsimport type { ReactNode } from "react"; /** A link in a menu or panel. */ export interface NavLink { label: string; href: string; /** A short line under the label in rich menus. */ description?: string; /** An icon shown beside the label in rich menus. */ icon?: ReactNode; } /** A top-level navbar entry: a plain link, or a trigger with links under it. */ export interface NavItem { label: string; /** Destination when the item is a plain link. */ href?: string; /** Links shown in the item's menu; makes the item a menu trigger. */ items?: NavLink[]; } /** A call-to-action button. */ export interface NavAction { label: string; href: string; }lib/nav/use-scroll-state.ts"use client"; import { useEffect, useState, type RefObject } from "react"; export interface ScrollState { /** Scroll offset in px. */ y: number; /** Direction of the last meaningful scroll. */ direction: "up" | "down"; /** Share of the scrollable distance covered, 0–1. */ progress: number; } /** * Scroll position, direction and progress of the window, or of `container` when given (handy for * navbars inside a scrolling panel). Updates once per frame at most. */ export function useScrollState(container?: RefObject<HTMLElement | null>, threshold = 4): ScrollState { const [state, setState] = useState<ScrollState>({ y: 0, direction: "up", progress: 0 }); useEffect(() => { const el = container?.current ?? null; const target: HTMLElement | Window = el ?? window; let last = 0; let direction: ScrollState["direction"] = "up"; let frame = 0; const read = () => { frame = 0; const y = el ? el.scrollTop : window.scrollY; const max = el ? el.scrollHeight - el.clientHeight : document.documentElement.scrollHeight - window.innerHeight; // Small moves keep the last direction; tracked here, not in the updater, which may run twice. if (Math.abs(y - last) >= threshold) { direction = y > last ? "down" : "up"; last = y; } setState({ y, direction, progress: max > 0 ? Math.min(1, y / max) : 0 }); }; const onScroll = () => { if (!frame) frame = requestAnimationFrame(read); }; target.addEventListener("scroll", onScroll, { passive: true }); read(); return () => { target.removeEventListener("scroll", onScroll); cancelAnimationFrame(frame); }; }, [container, threshold]); return state; }Copy the source
components/ui/ticker-header/ticker-header.tsx"use client"; import { useState, type ReactNode, type RefObject } from "react"; import { motion, useReducedMotion } from "motion/react"; import { X } from "lucide-react"; import { cn } from "@/lib/utils"; import { MobileMenu } from "@/lib/nav/mobile-menu"; import type { NavAction, NavItem } from "@/lib/nav/types"; import { useScrollState } from "@/lib/nav/use-scroll-state"; export interface TickerHeaderProps { /** Logo and name. */ brand: ReactNode; /** Top-level links. */ items: NavItem[]; /** Actions on the right; the last one is filled. */ actions?: NavAction[]; /** Messages that scroll across the strip above the nav. */ announcements: ReactNode[]; /** Seconds for one full loop of the strip. @default 28 */ duration?: number; /** Scroll container to watch; the window when left out. */ scrollContainer?: RefObject<HTMLElement | null>; /** Accessible name for the navigation. @default "Main" */ label?: string; /** Classes for the header. */ className?: string; } /** * A header with a scrolling announcement strip above the navigation. The strip pauses on hover or * focus, can be dismissed, and scrolls away with the page while the nav stays pinned. */ export function TickerHeader({ brand, items, actions = [], announcements, duration = 28, scrollContainer, label = "Main", className }: TickerHeaderProps) { const reduceMotion = useReducedMotion(); const { y } = useScrollState(scrollContainer); const [dismissed, setDismissed] = useState(false); const [paused, setPaused] = useState(false); // The header sticks 32px above the top (-top-8), so the strip scrolls off with the page and the nav // pins. Nothing changes size on scroll, so the page never moves under the reader. Collapsing the // strip at a scroll threshold used to shift the page, and scroll anchoring then undid the scroll, // so the strip opened and shut without settling. const stripInView = !dismissed && y < 32; const loop = [...announcements, ...announcements]; return ( <header className={cn("@container sticky z-40 border-b border-black/[0.06] bg-white/90 backdrop-blur-xl", dismissed ? "top-0" : "-top-8", className)}> {/* Dismissing folds the strip away (grid rows); scrolling never does. */} <div className={cn("grid transition-[grid-template-rows] duration-300 motion-reduce:transition-none", dismissed ? "grid-rows-[0fr]" : "grid-rows-[1fr]")}> <div className="min-h-0 overflow-hidden" inert={!stripInView}> <div role="region" aria-label="Announcements" className="relative flex h-8 items-center overflow-hidden border-b border-black/[0.06] bg-[#fafafa] text-[12px] text-[#3f3f46] [mask-image:linear-gradient(90deg,transparent,#000_6%,#000_94%,transparent)]" onPointerEnter={() => setPaused(true)} onPointerLeave={() => setPaused(false)} onFocus={() => setPaused(true)} onBlur={() => setPaused(false)} > {reduceMotion ? ( <p className="w-full truncate px-10 text-center">{announcements[0]}</p> ) : ( <motion.div className="flex shrink-0 gap-10 pr-10 whitespace-nowrap" animate={paused || !stripInView ? undefined : { x: ["0%", "-50%"] }} transition={{ duration, ease: "linear", repeat: Infinity }} > {loop.map((msg, i) => ( <span key={i} aria-hidden={i >= announcements.length || undefined} className="flex items-center gap-2"> <span aria-hidden="true" className="size-1 rounded-full bg-[#f97316]" /> {msg} </span> ))} </motion.div> )} <button type="button" onClick={() => setDismissed(true)} aria-label="Dismiss announcements" className="after:absolute after:-inset-2 absolute right-1.5 grid size-6 cursor-pointer place-items-center rounded-md bg-[#fafafa] text-[#71717a] outline-none hover:text-[#09090b] focus-visible:ring-2 focus-visible:ring-ring"> <X className="size-3.5" aria-hidden="true" /> </button> </div> </div> </div> <nav aria-label={label} className="relative mx-auto flex h-14 max-w-6xl items-center gap-1 px-4"> <div className="mr-4">{brand}</div> <ul className="hidden items-center @2xl:flex"> {items.map((item) => ( <li key={item.label}> <a href={item.href ?? item.items?.[0]?.href ?? "#"} className="flex h-9 relative after:absolute after:inset-x-0 after:-inset-y-0.5 items-center rounded-md px-3 text-[14px] text-[#3f3f46] outline-none hover:text-[#09090b] focus-visible:ring-2 focus-visible:ring-ring"> {item.label} </a> </li> ))} </ul> <div className="ml-auto hidden items-center gap-2 @2xl:flex"> {actions.map((a, i) => ( <a key={a.label} href={a.href} className={cn("flex h-9 relative after:absolute after:inset-x-0 after:-inset-y-0.5 items-center rounded-md px-3.5 text-[14px] outline-none focus-visible:ring-2 focus-visible:ring-ring", i === actions.length - 1 ? "bg-[#09090b] font-medium text-white hover:bg-black/80" : "text-[#3f3f46] hover:text-[#09090b]")}> {a.label} </a> ))} </div> <div className="ml-auto @2xl:hidden"> <MobileMenu items={items} actions={actions} /> </div> </nav> </header> ); }
Usage
import { TickerHeader } from "@/components/ui/ticker-header/ticker-header";
<TickerHeader brand={<Logo />} items={items} actions={actions} announcements={["Version 4 is out", "Join the live workshop"]} />API reference
| Prop | Type | Default | Description |
|---|---|---|---|
brand* | ReactNode | – | Logo and name. |
items* | NavItem[] | – | Top-level links. |
actions | NavAction[] | – | Actions on the right; the last one is filled. |
announcements* | ReactNode[] | – | Messages that scroll across the strip above the nav. |
duration | number | 28 | Seconds for one full loop of the strip. |
scrollContainer | RefObject<HTMLElement | null> | – | Scroll container to watch; the window when left out. |
label | string | "Main" | Accessible name for the navigation. |
className | string | – | Classes for the header. |
- The strip scrolls away with the page and the nav stays pinned: the header sticks 32px above the top, so nothing changes size and the page never moves under the reader. Only the dismiss button folds the strip away. Pass `scrollContainer` for a scrolling panel.
- Once the strip has scrolled out of view it stops moving and drops out of the tab order.
- `duration` sets the seconds per loop.
Accessibility
- The strip pauses on hover and while it holds focus, and has a dismiss button.
- Under reduced motion it shows the first message, still.
- Repeated copies of the messages are hidden from screen readers.