ComponentsPlays on its own. Click to take over.
Navigation
Sliding Tabs
Segmented tabs for React with a pill that slides to the active tab. Arrow-key navigation included.
Revenue, signups and uptime at a glance.
Install
Copies the source into components/ui/sliding-tabs.tsx and installs what it needs.
$ pnpm dlx shadcn@latest add https://makeui.xyz/r/sliding-tabs.jsonUsage
app/page.tsx
import { SlidingTabs } from "@/components/ui/sliding-tabs"
export default function Example() {
return (
<SlidingTabs
tabs={[
{ id: "overview", label: "Overview" },
{ id: "activity", label: "Activity", count: 12 },
{ id: "billing", label: "Billing" },
]}
onChange={(id) => console.log(id)}
/>
)
}Source
Prefer to copy by hand? It's one file. Requires motion and the shadcn cn helper.
components/ui/sliding-tabs.tsx
"use client"
import { useLayoutEffect, useRef, useState } from "react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
export type Tab = { id: string; label: string; count?: number }
type Props = {
tabs: Tab[]
/** Controlled. Leave out to let it manage itself. */
value?: string
defaultValue?: string
onChange?: (id: string) => void
className?: string
}
/** Segmented tabs with a pill that slides to the active tab. Arrow keys move, Home and End jump. */
export function SlidingTabs({ tabs, value, defaultValue, onChange, className }: Props) {
const reduce = useReducedMotion()
const [internal, setInternal] = useState(defaultValue ?? tabs[0]?.id)
const current = value ?? internal
const refs = useRef<Record<string, HTMLButtonElement | null>>({})
const list = useRef<HTMLDivElement>(null)
const [pill, setPill] = useState<{ x: number; w: number } | null>(null)
// measure with offsetLeft/offsetWidth, which ignore any scale applied by a parent
useLayoutEffect(() => {
const measure = () => {
const el = refs.current[current]
if (el) setPill({ x: el.offsetLeft, w: el.offsetWidth })
}
measure()
const ro = new ResizeObserver(measure)
if (list.current) ro.observe(list.current)
return () => ro.disconnect()
}, [current, tabs])
function select(id: string) {
setInternal(id)
onChange?.(id)
refs.current[id]?.focus()
}
function onKeyDown(e: React.KeyboardEvent) {
const i = tabs.findIndex((t) => t.id === current)
const next =
e.key === "ArrowRight" ? (i + 1) % tabs.length
: e.key === "ArrowLeft" ? (i - 1 + tabs.length) % tabs.length
: e.key === "Home" ? 0
: e.key === "End" ? tabs.length - 1
: -1
if (next < 0) return
e.preventDefault()
select(tabs[next].id)
}
return (
<div
ref={list}
role="tablist"
onKeyDown={onKeyDown}
className={cn("relative inline-flex rounded-full bg-neutral-100 p-1 dark:bg-neutral-900", className)}
>
{pill && (
<motion.span
aria-hidden
className="absolute inset-y-1 left-0 rounded-full bg-white shadow-[0_1px_3px_rgb(0_0_0/0.12)] dark:bg-neutral-700"
initial={false}
animate={{ x: pill.x, width: pill.w }}
transition={reduce ? { duration: 0 } : { type: "spring", stiffness: 420, damping: 36 }}
/>
)}
{tabs.map((tab) => {
const on = tab.id === current
return (
<button
key={tab.id}
ref={(el) => {
refs.current[tab.id] = el
}}
role="tab"
type="button"
aria-selected={on}
tabIndex={on ? 0 : -1}
onClick={() => select(tab.id)}
className={cn(
"relative z-10 flex items-center gap-1.5 rounded-full px-4 py-1.5 text-sm font-medium outline-none transition-colors focus-visible:ring-2 focus-visible:ring-neutral-400/60",
on
? "text-neutral-900 dark:text-neutral-50"
: "text-neutral-500 hover:text-neutral-800 dark:text-neutral-400 dark:hover:text-neutral-200"
)}
>
{tab.label}
{tab.count !== undefined && (
<span className="rounded-full bg-neutral-200/80 px-1.5 text-[11px] tabular-nums text-neutral-600 dark:bg-neutral-600/60 dark:text-neutral-300">
{tab.count}
</span>
)}
</button>
)
})}
</div>
)
}