Try 'magnetic button'
View code

Specification

Catalogue
No. 50 of 110
Section
Navigation
Interaction
Scroll
Element
Navigation
Preview
Live component
Imports
react 19.3.1
Added

Notes

The header slides away when the reader scrolls down and comes back as soon as they scroll up.

It gives a phone its screen back while reading, and is there the moment the reader looks for it. Keyboard focus always brings it back.

Hides after 56px of downward scroll, 200ms ease-in; returns on any upward scroll of 4px or more, 250ms. Hairline under it appears once the page has moved 16px (pairs with Header line on scroll). Changed from the library: the site's own header, not a floating glass pill. Here it reads its own frame; on a full page it reads the window.

The names, figures and pictures in the demo are sample content; a real build uses the client's own.

Code

React and plain CSS
"use client";

import { useEffect, useRef, useState } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "@/components/pieces/styles/scroll-frame.css";
import "./floating-navbar.css";

function FloatingNavbarDemo() {
  const frame = useRef<HTMLDivElement>(null);
  const [hidden, setHidden] = useState(false);
  const [lifted, setLifted] = useState(false);

  useEffect(() => {
    const f = frame.current;
    if (!f) return;
    let last = f.scrollTop;
    let raf = 0;
    const read = () => {
      raf = 0;
      const y = f.scrollTop;
      const d = y - last;
      if (Math.abs(d) > 4) {
        setHidden(d > 0 && y > 56);
        last = y;
      }
      setLifted(y > 16);
    };
    const onScroll = () => {
      if (!raf) raf = requestAnimationFrame(read);
    };
    f.addEventListener("scroll", onScroll, { passive: true });
    return () => {
      f.removeEventListener("scroll", onScroll);
      if (raf) cancelAnimationFrame(raf);
    };
  }, []);

  return (
    <div className="dm dm-scroll">
      <div ref={frame} className="sf fn" tabIndex={0} role="region" aria-label="Sample page. Scroll inside this frame">
        <header className="fn-head" data-hidden={hidden || undefined} data-lifted={lifted || undefined} onFocus={() => setHidden(false)}>
          <span className="dm-wordmark">Ledger &amp; Lane</span>
          <nav aria-label="Sample page">
            <a href="#">Services</a>
            <a href="#">Fees</a>
            <a href="#" className="fn-cta">
              Book a call
            </a>
          </nav>
        </header>
        <div className="fn-body" aria-hidden="true">
          {Array.from({ length: 4 }, (_, s) => (
            <div key={s} className="fn-sec">
              <i className="fn-h" />
              {Array.from({ length: 5 }, (_, i) => (
                <i key={i} style={{ width: `${60 + ((i * 29 + s * 13) % 38)}%` }} />
              ))}
            </div>
          ))}
        </div>
      </div>
      <p className="dm-hint">Scroll down: the header steps aside. Scroll up: it returns</p>
    </div>
  );
}

// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
  { scroll: ".sf", to: 0.7, ms: 1800, wait: 900 },
  { scroll: ".sf", to: 0.4, ms: 900, wait: 1000 },
  { scroll: ".sf", to: 0, ms: 700, wait: 300 },
];

export default function FloatingNavbarEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="Header that steps aside" sample tour={TOUR}>
      <FloatingNavbarDemo />
    </PieceStage>
  );
}

More like this

Menu that opens into link cards

Press, Layout AnimationNavigation

Cookie choice that folds away

FeedbackOverlays and toasts

Steps that scroll past a picture that holds

ScrollPage sections

Focus carousel

ScrollMedia and galleries

Timeline with a line that fills

ScrollPage sections

Section rail for long pages

ScrollNavigation