/Navigation
Navigation • React
Glide Pill Tabs
A pill of filter tabs where the selection slides from option to option and a softer hover highlight glides behind them; when the options overflow, the row scrolls sideways to keep the chosen one in view.
Installation
Terminal
npx shadcn@latest add @microkit/glide-pill-tabsCode
1"use client";23import { useEffect, useLayoutEffect, useRef, useState } from "react";45const options = [6{ key: "all", label: "All" },7{ key: "music", label: "Music" },8{ key: "films", label: "Films" },9{ key: "books", label: "Books" },10];1112export function GlidePillTabs() {13const [value, setValue] = useState("all");14const trackRef = useRef<HTMLDivElement>(null);15const indicatorRef = useRef<HTMLSpanElement>(null);16const glideRef = useRef<HTMLSpanElement>(null);17const placedRef = useRef(false);1819useLayoutEffect(() => {20const track = trackRef.current;21const indicator = indicatorRef.current;22if (!track || !indicator) return;2324const place = (animate: boolean) => {25const tab = track.querySelector<HTMLElement>(`[data-key="${CSS.escape(value)}"]`);26if (!tab) return;27if (!animate) indicator.style.transition = "none";28indicator.style.transform = `translateX(${tab.offsetLeft}px)`;29indicator.style.width = `${tab.offsetWidth}px`;30indicator.dataset.visible = "";31if (!animate) {32void indicator.offsetWidth;33indicator.style.transition = "";34}35// The chosen tab is scrolled into view horizontally only, so the page itself never jumps.36const left = tab.offsetLeft - 24;37const right = tab.offsetLeft + tab.offsetWidth + 24;38if (left < track.scrollLeft) track.scrollTo({ left, behavior: animate ? "smooth" : "auto" });39else if (right > track.scrollLeft + track.clientWidth) {40track.scrollTo({ left: right - track.clientWidth, behavior: animate ? "smooth" : "auto" });41}42};4344// The first placement appears in place; every change after it slides.45place(placedRef.current);46placedRef.current = true;4748// Fonts loading or the row resizing move the tabs, so the pill follows them without sliding.49let lastWidth = track.getBoundingClientRect().width;50const observer = new ResizeObserver(() => {51const width = track.getBoundingClientRect().width;52if (Math.abs(width - lastWidth) < 0.5) return;53lastWidth = width;54place(false);55});56observer.observe(track);57return () => observer.disconnect();58}, [value]);5960useEffect(() => {61const track = trackRef.current;62const glide = glideRef.current;63if (!track || !glide) return;6465const controller = new AbortController();66const { signal } = controller;67let shown = false;6869const place = (tab: HTMLElement) => {70const bounds = track.getBoundingClientRect();71const rect = tab.getBoundingClientRect();72const x = rect.left - bounds.left + track.scrollLeft - track.clientLeft;73const y = rect.top - bounds.top + track.scrollTop - track.clientTop;74// The first placement fades in where it lands; sliding in from wherever it last was would read as a glitch.75if (!shown) glide.style.transition = "opacity 180ms ease";76glide.style.transform = `translate(${x}px, ${y}px)`;77glide.style.width = `${rect.width}px`;78glide.style.height = `${rect.height}px`;79if (!shown) {80void glide.offsetWidth;81glide.style.transition = "";82shown = true;83}84glide.dataset.visible = "";85};8687const hide = () => {88shown = false;89delete glide.dataset.visible;90};9192for (const type of ["pointerover", "focusin"]) {93track.addEventListener(94type,95(event) => {96const tab = event.target instanceof Element ? event.target.closest<HTMLElement>("[data-pill-tab]") : null;97if (tab && track.contains(tab)) place(tab);98},99{ signal },100);101}102track.addEventListener("pointerleave", hide, { signal });103track.addEventListener(104"focusout",105(event) => {106const inside = event.relatedTarget instanceof Node && track.contains(event.relatedTarget);107if (!inside && !track.matches(":hover")) hide();108},109{ signal },110);111112return () => controller.abort();113}, []);114115return (116<div117className="relative flex w-fit max-w-full gap-0.5 overflow-x-auto rounded-full border border-[color:light-dark(rgba(20,24,30,.06),rgba(242,244,246,.06))] bg-[light-dark(rgba(20,24,30,.035),rgba(242,244,246,.035))] p-1 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"118role="tablist"119aria-label="Categories"120ref={trackRef}121>122<span123className="pointer-events-none absolute left-0 top-0 z-0 block size-0 rounded-full bg-[light-dark(rgba(20,24,30,.07),rgba(242,244,246,.07))] opacity-0 transition-[transform,width,height,opacity] duration-300 ease-[cubic-bezier(.32,.72,0,1)] data-[visible]:opacity-100 motion-reduce:transition-opacity"124ref={glideRef}125aria-hidden="true"126/>127<span128className="pointer-events-none absolute inset-y-1 left-0 z-0 block w-0 rounded-full bg-[light-dark(#14181e,#f2f4f6)] opacity-0 transition-[transform,width,opacity] duration-[380ms] ease-[cubic-bezier(.32,.72,0,1)] data-[visible]:opacity-100 motion-reduce:transition-opacity"129ref={indicatorRef}130aria-hidden="true"131/>132{options.map((option) => {133const selected = value === option.key;134return (135<button136key={option.key}137type="button"138role="tab"139className="relative z-[1] m-0 flex h-[30px] shrink-0 cursor-pointer items-center whitespace-nowrap rounded-full border-0 bg-transparent px-[13px] text-[12px] font-medium leading-none text-[color:light-dark(rgba(20,24,30,.6),rgba(242,244,246,.6))] transition-colors duration-[280ms] ease-[cubic-bezier(.4,0,.2,1)] hover:text-[color:light-dark(#14181e,#f2f4f6)] focus-visible:outline focus-visible:outline-[1.5px] focus-visible:-outline-offset-[1.5px] focus-visible:outline-[color:light-dark(rgba(20,24,30,.6),rgba(242,244,246,.6))] data-[selected]:text-[color:light-dark(#ffffff,#101216)] data-[selected]:hover:text-[color:light-dark(#ffffff,#101216)]"140data-pill-tab=""141data-key={option.key}142data-selected={selected || undefined}143aria-selected={selected}144onClick={() => setValue(option.key)}145>146{option.label}147</button>148);149})}150</div>151);152}