Skip to main content

Drift Pill Navbar

A floating pill navbar that leans with scroll velocity: scroll speed maps to a vertical offset through a spring, so the bar dips as you rush past and settles at rest. Hovering links slides one shared highlight pill between them, a dropdown opens a featured panel with hover-intent close, and crossing the scroll threshold slides in a CTA while the pill widens to make room.

NavigationReactMotionTailwind CSSSolar Icons
CSSTailwind

Manual

Create a file and paste the following code into it.

drift-pill-navbar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"use client";

import { type ReactNode, useCallback, useEffect, useRef, useState } from "react";
import { AnimatePresence, motion, useMotionValue, useSpring, useTransform, useVelocity } from "motion/react";
import { AltArrowDown } from "@solar-icons/react";
import { cn } from "@/lib/cn";

/* ------------------------------------------------------------------ */
/*  DriftPillNavbar                                                     */
/* ------------------------------------------------------------------ */

interface NavChild {
  label: string;
  href: string;
  description?: string;
  icon?: ReactNode;
}

export interface NavLink {
  label: string;
  href: string;
  children?: NavChild[];
  featured?: { label: string; description?: string; href: string; visual?: ReactNode };
}

interface DriftPillNavbarProps {
  logo?: ReactNode;
  links?: NavLink[];
  /** CTA that slides in once scrolled past the threshold [Optional] */
  cta?: ReactNode;
  /** Scroll distance in px before the CTA appears [Optional, default: 80] */
  ctaThreshold?: number;
  /** Scrollable ancestor to track instead of the window [Optional] */
  scrollRef?: React.RefObject<HTMLElement | null>;
  stickyTop?: string;
  className?: string;
}

/**
 * A floating pill navbar that leans with scroll velocity: scroll speed maps
 * to a vertical offset through a spring, so the bar dips as you rush past and
 * settles back at rest. Hovering links slides one shared highlight pill
 * between them, a dropdown opens a featured panel, and crossing the scroll
 * threshold slides in a CTA while the pill widens to make room.
 * @param {NavLink[]} links - Links, optionally with dropdown children [Optional]
 * @param {ReactNode} cta - Call to action revealed past the threshold [Optional]
 */
export function DriftPillNavbar({
  logo,
  links = [],
  cta,
  ctaThreshold = 80,
  scrollRef,
  stickyTop = "1rem",
  className,
}: DriftPillNavbarProps) {
  const [scrolled, setScrolled] = useState(false);
  const [showCta, setShowCta] = useState(false);
  const [hoveredLink, setHoveredLink] = useState<string | null>(null);
  const [openMenu, setOpenMenu] = useState<string | null>(null);
  const closeTimer = useRef<ReturnType<typeof setTimeout> | null>(null);

  /* Scroll velocity → lean: fast scrolling dips the bar, springs restore. */
  const scrollY = useMotionValue(0);
  const smoothY = useSpring(scrollY, { stiffness: 200, damping: 15 });
  const velocity = useVelocity(smoothY);
  const lean = useTransform(velocity, [-2000, -1000, 0, 1000, 2000], [12, 8, 0, -8, -12]);

  useEffect(() => {
    const read = () =>
      scrollRef?.current ? scrollRef.current.scrollTop : window.scrollY;
    const onScroll = () => {
      const y = read();
      scrollY.set(y);
      setScrolled(y > 0);
      setShowCta(!!cta && y > ctaThreshold);
    };
    const target: HTMLElement | Window = scrollRef?.current ?? window;
    target.addEventListener("scroll", onScroll, { passive: true });
    onScroll();
    return () => target.removeEventListener("scroll", onScroll);
  }, [cta, ctaThreshold, scrollY, scrollRef]);

  const scheduleClose = useCallback(() => {
    if (closeTimer.current) clearTimeout(closeTimer.current);
    closeTimer.current = setTimeout(() => setOpenMenu(null), 180);
  }, []);
  const cancelClose = useCallback(() => {
    if (closeTimer.current) clearTimeout(closeTimer.current);
  }, []);
  useEffect(() => () => cancelClose(), [cancelClose]);

  return (
    <div
      className={cn("pointer-events-none sticky z-50 flex w-full justify-center", className)}
      style={{ top: stickyTop }}
    >
      <motion.div className="pointer-events-auto" style={{ y: lean }}>
        <div
          className={cn(
            "flex items-center justify-between gap-6 rounded-full border border-zinc-200 bg-white px-6 py-3 transition-all duration-300 dark:border-zinc-800 dark:bg-zinc-950",
            scrolled && "shadow-lg",
            showCta ? "min-w-[640px]" : "min-w-[540px]",
          )}
        >
          <a href="#" className="flex shrink-0 items-center gap-2 text-sm font-semibold tracking-tight text-zinc-900 dark:text-zinc-100">
            {logo ?? "Logo"}
          </a>

          <nav
            className="relative flex items-center gap-0.5"
            onMouseLeave={() => setHoveredLink(null)}
          >
            {links.map((link) => {
              const hasMenu = !!link.children?.length;
              return (
                <div
                  key={link.label}
                  className="relative"
                  onMouseEnter={() => {
                    setHoveredLink(link.label);
                    if (hasMenu) {
                      cancelClose();
                      setOpenMenu(link.label);
                    } else {
                      scheduleClose();
                    }
                  }}
                  onMouseLeave={() => {
                    if (hasMenu) scheduleClose();
                  }}
                >
                  <a
                    href={link.href}
                    onClick={(e) => e.preventDefault()}
                    aria-haspopup={hasMenu ? "menu" : undefined}
                    aria-expanded={hasMenu ? openMenu === link.label : undefined}
                    className="relative flex items-center gap-1 rounded-full px-3 py-1.5 text-sm text-zinc-500 transition-colors hover:text-zinc-900 dark:text-zinc-400 dark:hover:text-zinc-100"
                  >
                    {hoveredLink === link.label && (
                      <motion.span
                        layoutId="drift-pill-highlight"
                        className="absolute inset-0 -z-10 rounded-full bg-zinc-100 dark:bg-zinc-800"
                        transition={{ type: "spring", stiffness: 400, damping: 30 }}
                      />
                    )}
                    {link.label}
                    {hasMenu && (
                      <AltArrowDown
                        size={12}
                        weight="Bold"
                        className={cn(
                          "transition-transform duration-300",
                          openMenu === link.label && "rotate-180",
                        )}
                      />
                    )}
                  </a>

                  {/* Dropdown */}
                  {hasMenu && (
                    <AnimatePresence>
                      {openMenu === link.label && (
                        <motion.div
                          role="menu"
                          initial={{ opacity: 0, y: 8, scale: 0.97 }}
                          animate={{ opacity: 1, y: 0, scale: 1 }}
                          exit={{ opacity: 0, y: 6, scale: 0.97 }}
                          transition={{ type: "spring", stiffness: 380, damping: 30 }}
                          onMouseEnter={cancelClose}
                          onMouseLeave={scheduleClose}
                          className="absolute left-1/2 top-full z-50 mt-3 w-[420px] -translate-x-1/2 rounded-xl border border-zinc-200 bg-white p-1.5 shadow-lg dark:border-zinc-800 dark:bg-zinc-950"
                        >
                          <div
                            className={cn(
                              "grid gap-0",
                              link.featured ? "grid-cols-[0.85fr_1fr]" : "grid-cols-1",
                            )}
                          >
                            {link.featured && (
                              <a
                                href={link.featured.href}
                                onClick={(e) => e.preventDefault()}
                                className="row-span-full m-0.5 flex min-h-[140px] flex-col justify-end rounded-lg border border-zinc-200 bg-zinc-50 p-4 no-underline outline-none select-none dark:border-zinc-800 dark:bg-zinc-900"
                              >
                                {link.featured.visual && (
                                  <div className="mb-3 text-zinc-300 dark:text-zinc-600">
                                    {link.featured.visual}
                                  </div>
                                )}
                                <div className="text-sm font-semibold text-zinc-900 dark:text-zinc-100">
                                  {link.featured.label}
                                </div>
                                {link.featured.description && (
                                  <p className="mt-1 text-xs leading-snug text-zinc-500">
                                    {link.featured.description}
                                  </p>
                                )}
                              </a>
                            )}
                            <div>
                              {link.children!.map((child) => (
                                <a
                                  key={child.href}
                                  href={child.href}
                                  onClick={(e) => e.preventDefault()}
                                  className="block rounded-lg px-3 py-2 transition-colors hover:bg-zinc-100 dark:hover:bg-zinc-800"
                                >
                                  <div className="flex items-center justify-between gap-2">
                                    <span className="text-sm font-medium text-zinc-900 dark:text-zinc-100">
                                      {child.label}
                                    </span>
                                    {child.icon}
                                  </div>
                                  {child.description && (
                                    <p className="mt-0.5 text-xs leading-snug text-zinc-500">
                                      {child.description}
                                    </p>
                                  )}
                                </a>
                              ))}
                            </div>
                          </div>
                        </motion.div>
                      )}
                    </AnimatePresence>
                  )}
                </div>
              );
            })}
          </nav>

          {/* CTA slides in past the scroll threshold */}
          <AnimatePresence>
            {showCta && (
              <motion.div
                key="cta"
                initial={{ opacity: 0, x: 16, width: 0 }}
                animate={{ opacity: 1, x: 0, width: "auto" }}
                exit={{ opacity: 0, x: 16, width: 0 }}
                transition={{ type: "spring", stiffness: 380, damping: 32 }}
                className="shrink-0 overflow-hidden"
              >
                {cta}
              </motion.div>
            )}
          </AnimatePresence>
        </div>
      </motion.div>
    </div>
  );
}

Update the import paths to match your project setup.

Similar components

New

Docs Tree Sidebar

Peek Image Index

Max

Lens Dock

Unfold Panel Navbar

Install via CLI

Resource details

PublishedJuly 17, 2026
CategoryNavigation
ReactMotionTailwind CSSSolar Icons