# Lodestar — Animated Navigation for React

> A floating pill navbar — a frosted near-black bar with a subtle white bottom hairline — that hides on scroll down and reveals on scroll up. Scroll-velocity aware, and never hides while any child has keyboard focus.

- Page: https://crucibleui.com/components/navigation/lodestar
- Category: [navigation](https://crucibleui.com/components/navigation)
- Tier: free (full source)
- Engine: motion
- npm dependencies: motion

## Install

```bash
npx shadcn@latest add @crucible/lodestar
```

Registry JSON: https://crucibleui.com/r/lodestar.json

Import from `@/components/crucible/lodestar`.

## Props (`LodestarProps`)

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `React.ReactNode` | — | Nav content — links, buttons, a logo, whatever the bar should hold. |
| `scrollContainer?` | `React.RefObject<HTMLElement \| null>` | — | Scroll container to track instead of the window. Pass a ref to an internal `overflow-y-auto` element (used by this component's own demo). When provided, Lodestar positions itself `sticky` inside that container instead of `fixed` to the viewport. |
| `hideThreshold?` | `number` | `80` | Scroll distance in px from the top before the bar is allowed to hide. |

Also accepts: `Omit<HTMLMotionProps<"nav">, "children" | "ref" | "animate" | "variants" | "transition">`.

## Usage example

```tsx
"use client";

import * as React from "react";

import { Lodestar } from "@/components/crucible/lodestar";

const SECTIONS = [
  {
    id: "intro",
    title: "Introduction",
    body: "A quick tour of what lives on this page and how the pieces fit together before you dig into the details.",
  },
  {
    id: "features",
    title: "Features",
    body: "The core capabilities, each explained in a sentence or two, with links out to deeper reference material.",
  },
  {
    id: "examples",
    title: "Examples",
    body: "Short, copyable snippets that show the most common setups, from the minimal case to a fully configured one.",
  },
  {
    id: "faq",
    title: "FAQ",
    body: "Answers to the questions that come up most often, collected in one place so you can skim for yours.",
  },
  {
    id: "contact",
    title: "Contact",
    body: "Where to reach the team — for bug reports, feature requests, or anything the FAQ did not cover.",
  },
];

export default function LodestarDemo() {
  const scrollerRef = React.useRef<HTMLDivElement>(null);

  const scrollTo = (id: string) => {
    scrollerRef.current
      ?.querySelector(`#${id}`)
      ?.scrollIntoView({ behavior: "smooth", block: "start" });
  };

  return (
    <div
      ref={scrollerRef}
      className="relative isolate h-[420px] w-full overflow-y-auto overflow-x-hidden rounded-xl bg-neutral-950"
    >
      <Lodestar scrollContainer={scrollerRef}>
        {SECTIONS.map((section) => (
          <button
            key={section.id}
            type="button"
            onClick={() => scrollTo(section.id)}
            className="rounded-full px-3 py-1.5 text-xs font-medium text-white/70 transition-colors hover:text-white focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white/60"
          >
            {section.title}
          </button>
        ))}
      </Lodestar>
      <div className="space-y-16 px-8 pt-24 pb-16">
        <div>
          <p className="text-[11px] font-medium uppercase tracking-[0.2em] text-neutral-500">
            On this page
          </p>
          <p className="mt-2 text-sm text-neutral-500">
            Scroll down to tuck the bar away — scroll up to bring it back.
          </p>
        </div>
        {SECTIONS.map((section) => (
          <section key={section.id} id={section.id} className="scroll-mt-20">
            <h3 className="text-lg font-semibold tracking-tight text-white">{section.title}</h3>
            <p className="mt-2 text-sm leading-relaxed text-white/60">{section.body}</p>
          </section>
        ))}
      </div>
    </div>
  );
}
```

## Accessibility & performance

Honors `prefers-reduced-motion` with a designed static frame or fade (never a blank box) and passes `className` through.

## Source (`lodestar.tsx`)

```tsx
"use client";

import { motion, useMotionValueEvent, useScroll, type HTMLMotionProps } from "motion/react";
import * as React from "react";

import { useReducedMotion } from "@/hooks/use-reduced-motion";
import { cn } from "@/lib/utils";

export interface LodestarProps
  extends Omit<HTMLMotionProps<"nav">, "children" | "ref" | "animate" | "variants" | "transition"> {
  /** Nav content — links, buttons, a logo, whatever the bar should hold. */
  children: React.ReactNode;
  /**
   * Scroll container to track instead of the window. Pass a ref to an
   * internal `overflow-y-auto` element (used by this component's own demo).
   * When provided, Lodestar positions itself `sticky` inside that container
   * instead of `fixed` to the viewport.
   */
  scrollContainer?: React.RefObject<HTMLElement | null>;
  /** Scroll distance in px from the top before the bar is allowed to hide. @default 80 */
  hideThreshold?: number;
}

/** Scroll speed (px/s) below which direction changes are ignored. */
const MIN_VELOCITY = 60;

/**
 * Lodestar — a floating pill navbar that hides on scroll down and reveals on
 * scroll up, scroll-velocity aware via `useScroll`/`useMotionValueEvent` (slow
 * drift below ~60px/s never toggles it). It
 * never hides while any child has `:focus-visible`, so keyboard users never
 * lose the bar mid-navigation. Collapses to an instant (non-springy)
 * transition under `prefers-reduced-motion` — it still hides/shows, since
 * that's the component's function, but without the springy travel.
 */
export function Lodestar({
  children,
  className,
  scrollContainer,
  hideThreshold = 80,
  ...props
}: LodestarProps) {
  const reducedMotion = useReducedMotion();
  const navRef = React.useRef<HTMLElement | null>(null);
  const [hidden, setHidden] = React.useState(false);

  const { scrollY } = useScroll(scrollContainer ? { container: scrollContainer } : undefined);

  useMotionValueEvent(scrollY, "change", (latest) => {
    const previous = scrollY.getPrevious() ?? latest;
    const scrollingDown = latest > previous;
    // Velocity gate: slow drift and trackpad jitter (< MIN_VELOCITY px/s, in
    // small steps) never toggles the bar — a deliberate scroll or a discrete
    // jump (PageDown, anchor) in either direction does.
    const velocity = Math.abs(scrollY.getVelocity());
    if (velocity < MIN_VELOCITY && Math.abs(latest - previous) < 12 && latest >= hideThreshold) return;

    setHidden((wasHidden) => {
      if (latest < hideThreshold) return false;

      const activeElement = typeof document !== "undefined" ? document.activeElement : null;
      const focusedInside =
        !!navRef.current &&
        !!activeElement &&
        navRef.current.contains(activeElement) &&
        (activeElement as HTMLElement).matches?.(":focus-visible");
      if (focusedInside) return false;

      if (scrollingDown && !wasHidden) return true;
      if (!scrollingDown && wasHidden) return false;
      return wasHidden;
    });
  });

  return (
    <motion.nav
      {...props}
      ref={navRef}
      data-crucible="lodestar"
      onFocusCapture={() => setHidden(false)}
      animate={hidden ? "hidden" : "visible"}
      // Sticky mode centers with mx-auto/w-fit — an x:-50% there shoves the
      // full-width flow box half out of the scroller and clips every item.
      variants={{
        visible: { y: 0, opacity: 1, ...(scrollContainer ? {} : { x: "-50%" }) },
        hidden: { y: "-140%", opacity: 0, ...(scrollContainer ? {} : { x: "-50%" }) },
      }}
      transition={
        reducedMotion
          ? { duration: 0.15, ease: "linear" }
          : { type: "spring", stiffness: 380, damping: 34 }
      }
      className={cn(
        "top-4 z-50 flex items-center gap-1 rounded-full border border-white/10 bg-neutral-950/70 px-2 py-2 shadow-[0_8px_30px_rgba(0,0,0,0.35)] backdrop-blur-md",
        "before:pointer-events-none before:absolute before:inset-x-3 before:bottom-[1px] before:h-px before:bg-white/0 before:transition-colors before:duration-500",
        "data-[hidden=false]:before:bg-white/25",
        scrollContainer ? "sticky mx-auto w-fit" : "fixed left-1/2",
        className
      )}
      data-hidden={hidden}
    >
      {children}
    </motion.nav>
  );
}
```
