{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"hide-on-scroll-navbar","type":"registry:component","title":"Hide On Scroll Navbar","description":"A header that slips away as you scroll down and springs back the moment you scroll up. Clear at the top, frosted glass once you move, with a reading-progress line that stays visible as a hairline while the bar is away. Follows the page or any scroll box, no animation library.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/hide-on-scroll-navbar.tsx","content":"\"use client\";\n\nimport { useEffect, useRef, useState, useSyncExternalStore } from \"react\";\nimport type { ReactNode, RefObject } from \"react\";\n\nexport type HideOnScrollNavbarProps = {\n  /** The bar's own content (logo, links, a button). Defaults to a \"Brand\" wordmark. */\n  children?: ReactNode;\n  /**\n   * What scrolls. \"window\" (default) follows the page scroll, which is what a\n   * real site header wants; pass the ref of any overflow-y-auto element to\n   * follow that box instead.\n   */\n  scroller?: RefObject<HTMLElement | null> | \"window\";\n  /** \"fixed\" to the viewport or \"absolute\" inside the nearest positioned parent. Default: fixed for the window, absolute for a scroller box. */\n  position?: \"fixed\" | \"absolute\";\n  /** \"light\" paints a white bar with dark text, \"dark\" an ink bar with light text. Default \"light\". */\n  tone?: \"light\" | \"dark\";\n  /** Transparent at the very top, then frosted glass after the first scroll. false keeps a solid bar all the time. Default true. */\n  glass?: boolean;\n  /** Reading-progress line along the bottom edge (it stays visible as a hairline while the bar is away). Default false. */\n  progress?: boolean;\n  /** Colour of the progress line (any CSS colour). Default: the bar's text colour. */\n  accent?: string;\n  /** The bar never hides above this scroll offset, in px. Default 40. */\n  hideAfter?: number;\n  /** Scroll movement in px that counts as a change of direction. Absorbs trackpad jitter. Default 4. */\n  sensitivity?: number;\n  className?: string;\n};\n\nconst REDUCE_QUERY = \"(prefers-reduced-motion: reduce)\";\nconst subscribeReduce = (cb: () => void) => {\n  const m = window.matchMedia(REDUCE_QUERY);\n  m.addEventListener(\"change\", cb);\n  return () => m.removeEventListener(\"change\", cb);\n};\nconst getReduce = () => window.matchMedia(REDUCE_QUERY).matches;\nconst getReduceServer = () => false;\n\nconst SPRING_IN = \"transform 640ms cubic-bezier(0.3, 1.35, 0.5, 1)\";\nconst EASE_OUT = \"transform 300ms cubic-bezier(0.65, 0, 0.35, 1)\";\nconst SURFACE_FADE = \"background-color 320ms ease, box-shadow 320ms ease, backdrop-filter 320ms ease, -webkit-backdrop-filter 320ms ease\";\nconst COLOUR = /^[#a-zA-Z0-9(),.%\\s/-]{1,48}$/;\n\nconst TONES = {\n  light: {\n    fg: \"#0b0c0f\",\n    glass: \"rgba(255,255,255,0.72)\",\n    solid: \"rgba(255,255,255,0.92)\",\n    hair: \"rgba(11,12,15,0.09)\",\n    lift: \"0 10px 28px -14px rgba(11,12,15,0.28)\",\n  },\n  dark: {\n    fg: \"#f4f5f7\",\n    glass: \"rgba(10,11,14,0.58)\",\n    solid: \"rgba(10,11,14,0.9)\",\n    hair: \"rgba(255,255,255,0.09)\",\n    lift: \"0 14px 34px -14px rgba(0,0,0,0.7), inset 0 1px 0 rgba(255,255,255,0.05)\",\n  },\n} as const;\n\nexport function HideOnScrollNavbar({\n  children,\n  scroller = \"window\",\n  position,\n  tone = \"light\",\n  glass = true,\n  progress = false,\n  accent,\n  hideAfter = 40,\n  sensitivity = 4,\n  className = \"\",\n}: HideOnScrollNavbarProps) {\n  const rootRef = useRef<HTMLElement>(null);\n  const lineRef = useRef<HTMLDivElement>(null);\n  const [hidden, setHidden] = useState(false);\n  const [scrolled, setScrolled] = useState(false);\n  const reduce = useSyncExternalStore(subscribeReduce, getReduce, getReduceServer);\n\n  useEffect(() => {\n    const el = scroller === \"window\" ? null : scroller.current;\n    if (scroller !== \"window\" && !el) return;\n    const target: HTMLElement | Window = el ?? window;\n    const read = () => {\n      if (el) return { y: el.scrollTop, max: el.scrollHeight - el.clientHeight };\n      return { y: window.scrollY, max: document.documentElement.scrollHeight - window.innerHeight };\n    };\n    let last = read().y;\n    let isHidden = false;\n    let isScrolled = false;\n    let raf = 0;\n    const frame = () => {\n      raf = 0;\n      const { y: raw, max } = read();\n      // Clamp so rubber-band overscroll never reads as a direction change.\n      const y = Math.min(Math.max(raw, 0), Math.max(max, 0));\n      if (lineRef.current) lineRef.current.style.transform = \"scaleX(\" + (max > 0 ? y / max : 0).toFixed(4) + \")\";\n      const sc = y > 8;\n      if (sc !== isScrolled) {\n        isScrolled = sc;\n        setScrolled(sc);\n      }\n      let next = isHidden;\n      if (y <= hideAfter) {\n        next = false;\n        last = y;\n      } else if (Math.abs(y - last) > sensitivity) {\n        next = y > last;\n        last = y;\n      }\n      if (next !== isHidden) {\n        isHidden = next;\n        setHidden(next);\n      }\n    };\n    const schedule = () => {\n      if (!raf) raf = requestAnimationFrame(frame);\n    };\n    target.addEventListener(\"scroll\", schedule, { passive: true });\n    window.addEventListener(\"resize\", schedule);\n    schedule();\n    return () => {\n      target.removeEventListener(\"scroll\", schedule);\n      window.removeEventListener(\"resize\", schedule);\n      if (raf) cancelAnimationFrame(raf);\n    };\n  }, [scroller, hideAfter, sensitivity]);\n\n  // Once it has slid away, take it out of the tab order and the accessibility tree.\n  useEffect(() => {\n    const node = rootRef.current;\n    if (!node) return;\n    if (!hidden) {\n      node.toggleAttribute(\"inert\", false);\n      return;\n    }\n    const t = window.setTimeout(() => node.toggleAttribute(\"inert\", true), reduce ? 0 : 300);\n    return () => window.clearTimeout(t);\n  }, [hidden, reduce]);\n\n  const t = TONES[tone === \"dark\" ? \"dark\" : \"light\"];\n  const raised = glass ? scrolled && !hidden : true;\n  const bg = glass ? (scrolled ? t.glass : \"transparent\") : t.solid;\n  const blur = glass ? (scrolled ? \"blur(16px) saturate(1.7)\" : \"blur(0px)\") : \"blur(6px)\";\n  const pos = position ?? (scroller === \"window\" ? \"fixed\" : \"absolute\");\n  const line = accent && COLOUR.test(accent) ? accent : \"currentColor\";\n  const peek = progress ? \"calc(-100% + 2px)\" : \"-100%\";\n\n  return (\n    <header\n      ref={rootRef}\n      className={\n        (pos === \"fixed\" ? \"fixed\" : \"absolute\") + \" inset-x-0 top-0 z-40 flex items-center justify-between px-6 py-3 \" + className\n      }\n      style={{\n        color: t.fg,\n        backgroundColor: bg,\n        backdropFilter: blur,\n        WebkitBackdropFilter: blur,\n        boxShadow: hidden ? \"none\" : (raised ? t.lift + \", \" : \"\") + \"0 1px 0 \" + (glass && !scrolled ? \"transparent\" : t.hair),\n        transform: hidden && !reduce ? \"translateY(\" + peek + \")\" : \"translateY(0)\",\n        opacity: hidden && reduce ? 0 : 1,\n        pointerEvents: hidden ? \"none\" : undefined,\n        transition: reduce\n          ? \"opacity 160ms ease, \" + SURFACE_FADE\n          : (hidden ? EASE_OUT : SPRING_IN) + \", opacity 160ms ease, \" + SURFACE_FADE,\n        willChange: \"transform\",\n      }}\n    >\n      {/* Fills the gap above the bar while the spring overshoots. */}\n      <div\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute inset-x-0 bottom-full h-4\"\n        style={{ backgroundColor: bg, backdropFilter: blur, WebkitBackdropFilter: blur }}\n      />\n      {children ?? <span className=\"font-bold\">Brand</span>}\n      {progress ? (\n        <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-x-0 bottom-0 h-0.5 overflow-hidden\">\n          <div\n            ref={lineRef}\n            className=\"h-full w-full origin-left\"\n            style={{ background: line, opacity: 0.85, transform: \"scaleX(0)\", willChange: \"transform\" }}\n          />\n        </div>\n      ) : null}\n    </header>\n  );\n}\n","type":"registry:component","target":"components/viberdy/hide-on-scroll-navbar.tsx"}],"meta":{"tier":"free","tags":["navbar","scroll","header","sticky","glass","progress"],"href":"https://viberdy.dev/library/hide-on-scroll-navbar","aiPrompt":"Create a React + Tailwind (v4) component named HideOnScrollNavbar. React only: no framer-motion, no other dependency. One \"use client\" file that runs on React 18 and 19.\n\nBehaviour: a header that slides out of view when the user scrolls DOWN and springs back when they scroll UP.\n\nProps (all optional): children (default a bold \"Brand\" span), scroller (\"window\" by default, or a RefObject<HTMLElement | null> of an overflow-y-auto element), position (\"fixed\" | \"absolute\"; default fixed for the window, absolute for an element scroller), tone (\"light\" default: white bar with #0b0c0f text; \"dark\": ink bar with #f4f5f7 text), glass (true), progress (false), accent (any CSS colour for the progress line, validated against a short allowlist regex of colour characters, default currentColor), hideAfter (40), sensitivity (4), className.\n\nScroll logic (hand written, no library): one effect that attaches a passive scroll listener to the scroller (or window) plus a window resize listener, both calling a single requestAnimationFrame-coalesced frame function (schedule once on mount too). Each frame reads y (scrollTop or scrollY) and max (scrollHeight - clientHeight, or documentElement.scrollHeight - innerHeight) and clamps y to [0, max] so rubber-band overscroll never counts. Keep `last` (the last accepted offset) in a closure. If y <= hideAfter: show, last = y. Else if |y - last| > sensitivity: hidden = y > last; last = y. Only call setHidden / setScrolled when the value changes (scrolled = y > 8). Write the progress line's transform scaleX(y / max) straight to a ref'd element, no state. If a scroller ref is given but its current is null, do nothing.\n\nSurface: root is a <header> with classes \"inset-x-0 top-0 z-40 flex items-center justify-between px-6 py-3\" plus fixed or absolute. Colours are inline styles (never rely on site tokens). glass true: transparent with no hairline at the very top; once scrolled it becomes rgba(255,255,255,0.72) (light) or rgba(10,11,14,0.58) (dark) with backdrop-filter blur(16px) saturate(1.7) (also -webkit-) and a lift shadow (light \"0 10px 28px -14px rgba(11,12,15,0.28)\", dark \"0 14px 34px -14px rgba(0,0,0,0.7), inset 0 1px 0 rgba(255,255,255,0.05)\") plus a 1px hairline \"0 1px 0\" in rgba(11,12,15,0.09) / rgba(255,255,255,0.09), all faded with a 320ms ease transition. glass false: always solid rgba(255,255,255,0.92) / rgba(10,11,14,0.9), blur(6px), lifted. The lift shadow is removed while hidden.\n\nMotion: hidden = translateY(-100%), or calc(-100% + 2px) when progress is on, so the 2px progress line (absolute inset-x-0 bottom-0 h-0.5, origin-left, opacity 0.85) stays visible as a hairline at the top edge. Hide transition: transform 300ms cubic-bezier(0.65, 0, 0.35, 1). Reveal transition: transform 640ms cubic-bezier(0.3, 1.35, 0.5, 1) (a few px of overshoot); an aria-hidden filler div (absolute inset-x-0 bottom-full h-4, same background and backdrop-filter) hides the gap above the bar during overshoot. pointer-events none while hidden.\n\nAccessibility: after the bar has been hidden for 300ms set the inert attribute with el.toggleAttribute(\"inert\", true) in an effect (clear the timer in the cleanup; remove inert immediately when it starts to reveal). Use useSyncExternalStore on matchMedia(\"(prefers-reduced-motion: reduce)\") (server snapshot false): with reduced motion never translate; fade opacity 1 -> 0 over 160ms instead, and inert immediately. Progress line and filler are aria-hidden. Children set their own focus rings; do not use the site's colour tokens.","license":"https://viberdy.dev/license"}}