# Shrinking navbar

> A floating frosted navbar that gets narrower and shorter once you start scrolling, and grows back at the top.

- Category: [Navigation](https://library.aimran.es/components/navigation)
- Built with: React + Motion
- Live editor: https://library.aimran.es/blocks/shrinking-navbar
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## Where to use it

A shrinking navbar keeps your logo and search within reach while giving the content more room as people read. It suits long pages: a magazine article, a restaurant menu, the documentation of a product or a portfolio case study. If you have been searching for a sticky header that shrinks on scroll, this one does it with a soft spring.

The threshold decides how many pixels of scroll it takes before the bar shrinks; a small value feels responsive. Keep only two or three items in the compact state, since there is less room once it tightens up.

## Code

### React (Motion)

```tsx
import { useEffect, useRef, useState } from "react";
import { motion } from "framer-motion";
import { Bell, Search } from "lucide-react";

const spring = { type: "spring" as const, stiffness: 300, damping: 30 };

interface ShrinkingNavbarProps {
  threshold?: number;
  /** true: caja de demo con scroll propio. false: navbar fijo arriba que escucha el scroll de la página. */
  contained?: boolean;
}

export default function ShrinkingNavbar({
  threshold = 18,
  contained = true,
}: ShrinkingNavbarProps) {
  const containerRef = useRef<HTMLDivElement>(null);
  const [small, setSmall] = useState(false);

  // Escucha el scroll del contenedor propio si existe; si no, el de la ventana.
  useEffect(() => {
    const el = contained ? containerRef.current : null;
    const read = () => setSmall((el ? el.scrollTop : window.scrollY) > threshold);
    const target: HTMLElement | Window = el ?? window;
    read();
    target.addEventListener("scroll", read, { passive: true });
    return () => target.removeEventListener("scroll", read);
  }, [contained, threshold]);

  const nav = (
    <motion.nav
      animate={{ maxWidth: small ? 250 : 340, height: small ? 44 : 58 }}
      transition={spring}
      style={{ width: "100%" }}
      className="sticky top-0 z-10 flex shrink-0 items-center justify-between rounded-full bg-white/70 dark:bg-black/70 px-5 shadow-[0_1px_2px_rgba(0,0,0,0.03),0_8px_28px_rgba(0,0,0,0.05)] backdrop-blur-[20px]"
    >
      <b>AIMRAN</b>
      <div className="flex gap-3">
        <Search className="h-4 w-4" />
        <Bell className="h-4 w-4" />
      </div>
    </motion.nav>
  );

  if (!contained) {
    return <div className="sticky top-0 z-50 flex justify-center p-4 text-[#1d1d1f] dark:text-[#f5f5f7]">{nav}</div>;
  }

  return (
    <div
      ref={containerRef}
      className="flex h-52 w-full flex-col items-center overflow-y-auto rounded-2xl bg-black/5 p-4 text-[#1d1d1f] dark:text-[#f5f5f7] dark:bg-white/10"
    >
      {nav}
      {/* Contenido de relleno para poder hacer scroll; también da 340 px de ancho natural sin forzar un mínimo */}
      <svg aria-hidden className="h-[326px] w-[340px] max-w-full shrink-0" />
    </div>
  );
}
```

## Prompt for AI assistants

Paste this into ChatGPT, Claude, Cursor, v0 or Lovable to adapt the block to your stack (Tailwind, Vue, Svelte…):

````text
I want to add the “Shrinking navbar” block (Navigation) from AIMRAN Library to my project.

## What it is
A floating frosted navbar that gets narrower and shorter once you start scrolling, and grows back at the top.

## Chosen settings
- Threshold: 18px (from 5 to 60px)

## What I need
1. Add it to my project with the same look and animation as the reference code: same sizes, colours, radii, shadows, timings and easing.
2. It is built with React + TypeScript, with framer-motion for the animation and lucide-react for the icons. If my project uses something else (Vue, Svelte, Astro, plain HTML…), translate it without losing any behaviour. If I use Tailwind, turn the styles into classes; if not, keep the CSS in its own file.
3. Expose the settings above as props (or CSS variables) with those values as defaults.
4. Make it work well on phones: nothing that depends only on hover, touch targets of at least 44 px and no overflow on 320 px screens.
5. Make it look right in light and dark mode.
6. Keep it accessible: semantic HTML, roles and aria-* where needed, visible focus and keyboard support.
7. Respect prefers-reduced-motion: with reduced motion, no big animations but the same end result.

## Reference code
```tsx
import { useEffect, useRef, useState } from "react";
import { motion } from "framer-motion";
import { Bell, Search } from "lucide-react";

const spring = { type: "spring" as const, stiffness: 300, damping: 30 };

interface ShrinkingNavbarProps {
  threshold?: number;
  /** true: caja de demo con scroll propio. false: navbar fijo arriba que escucha el scroll de la página. */
  contained?: boolean;
}

export default function ShrinkingNavbar({
  threshold = 18,
  contained = true,
}: ShrinkingNavbarProps) {
  const containerRef = useRef<HTMLDivElement>(null);
  const [small, setSmall] = useState(false);

  // Escucha el scroll del contenedor propio si existe; si no, el de la ventana.
  useEffect(() => {
    const el = contained ? containerRef.current : null;
    const read = () => setSmall((el ? el.scrollTop : window.scrollY) > threshold);
    const target: HTMLElement | Window = el ?? window;
    read();
    target.addEventListener("scroll", read, { passive: true });
    return () => target.removeEventListener("scroll", read);
  }, [contained, threshold]);

  const nav = (
    <motion.nav
      animate={{ maxWidth: small ? 250 : 340, height: small ? 44 : 58 }}
      transition={spring}
      style={{ width: "100%" }}
      className="sticky top-0 z-10 flex shrink-0 items-center justify-between rounded-full bg-white/70 dark:bg-black/70 px-5 shadow-[0_1px_2px_rgba(0,0,0,0.03),0_8px_28px_rgba(0,0,0,0.05)] backdrop-blur-[20px]"
    >
      <b>AIMRAN</b>
      <div className="flex gap-3">
        <Search className="h-4 w-4" />
        <Bell className="h-4 w-4" />
      </div>
    </motion.nav>
  );

  if (!contained) {
    return <div className="sticky top-0 z-50 flex justify-center p-4 text-[#1d1d1f] dark:text-[#f5f5f7]">{nav}</div>;
  }

  return (
    <div
      ref={containerRef}
      className="flex h-52 w-full flex-col items-center overflow-y-auto rounded-2xl bg-black/5 p-4 text-[#1d1d1f] dark:text-[#f5f5f7] dark:bg-white/10"
    >
      {nav}
      {/* Contenido de relleno para poder hacer scroll; también da 340 px de ancho natural sin forzar un mínimo */}
      <svg aria-hidden className="h-[326px] w-[340px] max-w-full shrink-0" />
    </div>
  );
}
```

Give me the complete final code, ready to paste, and tell me in one line where to put it in my project.
````
