Installation
Run this in a project set up with shadcn/ui:
$ pnpm dlx shadcn@latest add https://uxelith.com/r/bottom-tab-bar.jsonIt adds these packages for you:
clsxtailwind-mergeThe source lands in your components folder as
bottom-tab-bar.tsx. It's yours: change anything.
Usage
"use client"
import * as React from "react"
import { BottomTabBar } from "@/components/bottom-tab-bar"
export function AppTabs() {
const [tab, setTab] = React.useState("home")
return (
<BottomTabBar
value={tab}
onValueChange={setTab}
onReselect={() => window.scrollTo({ top: 0, behavior: "smooth" })}
look="floating"
items={[
{ value: "home", label: "Home", icon: "home" },
{ value: "search", label: "Search", icon: "search" },
{ value: "inbox", label: "Inbox", icon: "inbox", badge: 3 },
{ value: "profile", label: "Profile", icon: "user" },
]}
/>
)
}Source
One file, 420 lines, nothing hidden. Read it before you install it.
"use client";
import * as React from "react";
import { cn } from "@/lib/utils";
/** Built-in icons, so a tab bar works with no icon library. Any React node works too. */
export type TabBarIconName =
| "home"
| "search"
| "compass"
| "plane"
| "calendar"
| "inbox"
| "chat"
| "bell"
| "heart"
| "bag"
| "chart"
| "library"
| "play"
| "user"
| "settings"
| "plus";
export interface BottomTabBarItem {
value: string;
label: string;
icon: TabBarIconName | React.ReactNode;
/** A number shows a count (99+ past 99); `true` shows a dot; a string shows as written. */
badge?: number | string | boolean;
/** Makes the tab a real link, so every section is crawlable. Pair with `linkComponent` for client-side navigation. */
href?: string;
disabled?: boolean;
}
export interface BottomTabBarAction {
label: string;
icon?: TabBarIconName | React.ReactNode;
href?: string;
onSelect?: () => void;
}
export interface BottomTabBarProps extends Omit<React.ComponentProps<"nav">, "defaultValue" | "onChange"> {
items: BottomTabBarItem[];
/** The current tab, when you control it (e.g. from the pathname). */
value?: string;
/** The tab shown first when uncontrolled. Defaults to the first one. */
defaultValue?: string;
onValueChange?: (value: string) => void;
/** Tapping the tab that's already current: scroll its page to the top, or go back to its first screen. */
onReselect?: (value: string) => void;
/** Classic spans the edge; floating and glass hover above the content as a capsule. */
look?: "classic" | "floating" | "glass";
/** What marks the current tab. */
indicator?: "pill" | "dot" | "line" | "none";
/** When the names show: always, only under the current tab, or never (icons only; names stay readable to screen readers). */
labels?: "always" | "active" | "never";
/** A raised button in the middle of the bar, for the app's main action. */
action?: BottomTabBarAction | false;
/** Slides the bar out of view, e.g. while the page scrolls down. */
hidden?: boolean;
/** Hide while the window scrolls down and bring back on the way up. For a bar fixed to the window. */
hideOnScroll?: boolean;
/** Fixed to the window, placed in the nearest positioned parent, stuck to the bottom of a scroller, or in the flow. */
position?: "fixed" | "absolute" | "sticky" | "static";
/** Renders tabs that have an `href`, e.g. Next.js `Link`. Defaults to a plain `<a>`. */
linkComponent?: React.ElementType;
/** Names the navigation landmark. */
label?: string;
}
// Motion presets (ADR-010), as CSS (ADR-015).
const CSS_SPRING_SMOOTH = "560ms linear(0, 0.05, 0.17, 0.31, 0.45, 0.58, 0.69, 0.78, 0.85, 0.9, 0.94, 0.96, 0.98, 0.99, 1, 1, 1.01, 1.01, 1.01, 1, 1, 1, 1, 1, 1)";
/** Reduced motion, read in the browser only: the server and the first render always say no (ADR-010). */
function useReducedMotion() {
return React.useSyncExternalStore(
(notify) => {
const query = window.matchMedia("(prefers-reduced-motion: reduce)");
query.addEventListener("change", notify);
return () => query.removeEventListener("change", notify);
},
() => window.matchMedia("(prefers-reduced-motion: reduce)").matches,
() => false,
);
}
const PATHS: Record<TabBarIconName, React.ReactNode> = {
home: <path d="M3.5 10.5 12 3.5l8.5 7V19a1.5 1.5 0 0 1-1.5 1.5h-4v-6h-6v6H5A1.5 1.5 0 0 1 3.5 19z" />,
search: (
<>
<circle cx="11" cy="11" r="6.5" />
<path d="m20 20-4.2-4.2" />
</>
),
compass: (
<>
<circle cx="12" cy="12" r="8.5" />
<path d="m15.5 8.5-2 5-5 2 2-5z" />
</>
),
plane: <path d="M21 15.5 13.5 11V5a1.5 1.5 0 0 0-3 0v6L3 15.5v2l7.5-2.5v4l-2 1.5v1.5l3.5-1 3.5 1v-1.5l-2-1.5v-4l7.5 2.5z" />,
calendar: (
<>
<rect x="3.5" y="5" width="17" height="15.5" rx="2" />
<path d="M3.5 10h17M8 3v4M16 3v4" />
</>
),
inbox: (
<>
<path d="M3.5 13.5 6 5h12l2.5 8.5V19a1.5 1.5 0 0 1-1.5 1.5H5A1.5 1.5 0 0 1 3.5 19z" />
<path d="M3.5 13.5H8l1.5 2.5h5l1.5-2.5h4.5" />
</>
),
chat: <path d="M20.5 11.5a8 8 0 0 1-11.8 7L3.5 20l1.4-4.6A8 8 0 1 1 20.5 11.5z" />,
bell: (
<>
<path d="M6 16.5V11a6 6 0 0 1 12 0v5.5l1.5 1.5h-15z" />
<path d="M10 21h4" />
</>
),
heart: <path d="M12 20s-7.5-4.5-7.5-10A4.5 4.5 0 0 1 12 7a4.5 4.5 0 0 1 7.5 3c0 5.5-7.5 10-7.5 10z" />,
bag: (
<>
<path d="M5 8h14l-1 12.5H6z" />
<path d="M9 8V6.5a3 3 0 0 1 6 0V8" />
</>
),
chart: <path d="M4 20.5h16M7 17v-5M12 17V7M17 17v-8" />,
library: <path d="M5 4v16M9.5 4v16M14 5l4.5 15M3.5 20.5h17" />,
play: (
<>
<circle cx="12" cy="12" r="8.5" />
<path d="m10 8.5 5.5 3.5-5.5 3.5z" />
</>
),
user: (
<>
<circle cx="12" cy="8" r="4" />
<path d="M4.5 20.5c.9-4 3.9-6.5 7.5-6.5s6.6 2.5 7.5 6.5" />
</>
),
settings: (
<>
<circle cx="12" cy="12" r="3" />
<path d="M12 2.5v3M12 18.5v3M4.2 6.5l2.6 1.5M17.2 16l2.6 1.5M4.2 17.5 6.8 16M17.2 8l2.6-1.5" />
</>
),
plus: <path d="M12 5v14M5 12h14" />,
};
/** One of the built-in icons, drawn at 24px in the current text colour. */
export function TabBarIcon({ name, className }: { name: TabBarIconName; className?: string }) {
return (
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={1.8}
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
className={cn("size-6 shrink-0", className)}
>
{PATHS[name]}
</svg>
);
}
function isIconName(icon: unknown): icon is TabBarIconName {
return typeof icon === "string" && icon in PATHS;
}
/** Draws a built-in icon by name, or passes any other node through. */
export function renderTabIcon(icon: TabBarIconName | React.ReactNode, className?: string) {
return isIconName(icon) ? <TabBarIcon name={icon} className={className} /> : icon;
}
/** What a screen reader hears for a badge, after the tab's name. */
export function badgeText(badge: BottomTabBarItem["badge"]) {
if (badge === undefined || badge === false || badge === 0) return "";
if (badge === true) return "new";
if (typeof badge === "number") return `${badge} new`;
return badge;
}
function Badge({ badge }: { badge: BottomTabBarItem["badge"] }) {
if (badge === undefined || badge === false || badge === 0) return null;
if (badge === true) {
return <span aria-hidden="true" className="absolute -top-0.5 left-[calc(50%+0.5rem)] size-2.5 rounded-full bg-red-600 ring-2 ring-background" />;
}
const text = typeof badge === "number" && badge > 99 ? "99+" : String(badge);
return (
<span
aria-hidden="true"
className="absolute -top-1.5 left-[calc(50%+0.25rem)] min-w-[1.125rem] rounded-full bg-red-600 px-1 text-center text-[0.6875rem] leading-[1.125rem] font-semibold text-white tabular-nums ring-2 ring-background"
>
{text}
</span>
);
}
type Box = { left: number; width: number };
/**
* The bar of sections along the bottom of a phone app.
*
* - A navigation landmark; the current tab has `aria-current="page"`, and a badge is read with the name ("Inbox, 3 new").
* - Tabs with `href` are real links (pass Next.js `Link` as `linkComponent`), so every section stays crawlable.
* - The marker glides between tabs on a spring, and jumps under `prefers-reduced-motion`.
* - Sits above the phone's home bar (`safe-area-inset-bottom`); every tab is at least 48px tall.
* - Tapping the current tab again calls `onReselect`: the place to scroll back to the top.
*/
export function BottomTabBar({
items,
value: valueProp,
defaultValue,
onValueChange,
onReselect,
look = "classic",
indicator = "pill",
labels = "always",
action = false,
hidden = false,
hideOnScroll = false,
position = "fixed",
linkComponent,
label = "Main",
className,
...props
}: BottomTabBarProps) {
const reduceMotion = useReducedMotion();
const [uncontrolled, setUncontrolled] = React.useState(defaultValue ?? items[0]?.value);
const current = valueProp ?? uncontrolled;
const listRef = React.useRef<HTMLUListElement>(null);
const refs = React.useRef(new Map<string, HTMLElement>());
const [boxes, setBoxes] = React.useState<Map<string, Box> | null>(null);
// The marker appears in place first, then glides on later changes.
const [settled, setSettled] = React.useState(false);
const [scrolledAway, setScrolledAway] = React.useState(false);
React.useLayoutEffect(() => {
const list = listRef.current;
if (!list) return;
const measure = () => {
const next = new Map<string, Box>();
refs.current.forEach((el, value) => next.set(value, { left: el.offsetLeft, width: el.offsetWidth }));
setBoxes(next);
};
measure();
const observer = new ResizeObserver(measure);
observer.observe(list);
return () => observer.disconnect();
}, [items, labels, action]);
React.useEffect(() => {
if (!boxes || settled) return;
const frame = requestAnimationFrame(() => setSettled(true));
return () => cancelAnimationFrame(frame);
}, [boxes, settled]);
React.useEffect(() => {
if (!hideOnScroll) return;
let last = window.scrollY;
const onScroll = () => {
const y = window.scrollY;
if (Math.abs(y - last) < 6) return;
setScrolledAway(y > last && y > 64);
last = y;
};
window.addEventListener("scroll", onScroll, { passive: true });
return () => window.removeEventListener("scroll", onScroll);
}, [hideOnScroll]);
const choose = (item: BottomTabBarItem) => {
if (item.value === current) {
onReselect?.(item.value);
return;
}
if (valueProp === undefined) setUncontrolled(item.value);
onValueChange?.(item.value);
};
const box = current !== undefined ? boxes?.get(current) : undefined;
const capsule = look !== "classic";
const away = hidden || (hideOnScroll && scrolledAway);
const Link = linkComponent ?? "a";
const middle = Math.floor(items.length / 2);
const marker =
box && indicator !== "none" ? (
<span
aria-hidden="true"
style={{
width: box.width,
translate: `${box.left}px 0`,
transition: settled && !reduceMotion ? `translate ${CSS_SPRING_SMOOTH}, width ${CSS_SPRING_SMOOTH}` : "none",
}}
className={cn(
"pointer-events-none absolute top-0 left-0 flex h-full justify-center",
indicator === "pill" && "py-1.5",
indicator === "dot" && "items-end pb-1",
indicator === "line" && "items-start",
)}
>
{indicator === "pill" && (
<span
className={cn(
"w-[calc(100%-0.5rem)] max-w-20 rounded-2xl bg-primary/12",
look === "glass" && "bg-white/55 shadow-[inset_0_1px_0_rgb(255_255_255/0.9),0_2px_8px_rgb(0_0_0/0.08)] dark:bg-white/12 dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.18)]",
)}
/>
)}
{indicator === "dot" && <span className="size-1.5 rounded-full bg-primary" />}
{indicator === "line" && <span className={cn("h-0.5 w-8 rounded-full bg-primary", capsule && "mt-0.5")} />}
</span>
) : null;
const actionButton = action ? (
<li key="__action" className="flex shrink-0 items-center justify-center px-1">
{React.createElement(
action.href ? Link : "button",
{
...(action.href ? { href: action.href } : { type: "button" }),
onClick: action.onSelect,
"aria-label": action.label,
className:
"grid size-12 place-items-center rounded-2xl bg-primary text-primary-foreground shadow-[0_8px_18px_-6px_color-mix(in_oklab,var(--color-primary)_70%,transparent)] outline-none transition-transform duration-200 ease-[cubic-bezier(0.22,1,0.36,1)] focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background active:scale-90 motion-reduce:transition-none",
},
renderTabIcon(action.icon ?? "plus", "size-6 stroke-[2.2]"),
)}
</li>
) : null;
return (
<nav
aria-label={label}
className={cn(
"z-40 transition-transform duration-300 ease-[cubic-bezier(0.32,0.72,0,1)] motion-reduce:transition-none",
position === "fixed" && "fixed inset-x-0 bottom-0",
position === "absolute" && "absolute inset-x-0 bottom-0",
position === "sticky" && "sticky bottom-0",
capsule && "px-3 pb-[max(env(safe-area-inset-bottom),0.75rem)]",
away && (capsule ? "translate-y-[calc(100%+1rem)]" : "translate-y-full"),
className,
)}
{...props}
inert={away || undefined}
>
<ul
ref={listRef}
className={cn(
"relative mx-auto flex items-stretch",
!capsule && "border-t border-border bg-background/90 px-1 pb-[env(safe-area-inset-bottom)] backdrop-blur-xl backdrop-saturate-150",
look === "floating" && "max-w-md rounded-[1.75rem] border border-border bg-background/90 p-1 shadow-[0_12px_30px_-10px_rgb(20_10_50/0.3)] backdrop-blur-xl backdrop-saturate-150",
look === "glass" &&
"max-w-md rounded-[1.875rem] border border-white/70 bg-background/45 p-1 shadow-[inset_0_1px_0_rgb(255_255_255/0.8),inset_0_-1px_1px_rgb(0_0_0/0.06),0_16px_34px_-12px_rgb(20_10_50/0.35)] backdrop-blur-md backdrop-saturate-200 backdrop-contrast-105 dark:border-white/12 dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.14),0_16px_34px_-12px_rgb(0_0_0/0.6)]",
)}
>
{marker}
{items.map((item, index) => {
const active = item.value === current;
const showLabel = labels === "always" || (labels === "active" && active);
const spoken = [item.label, badgeText(item.badge)].filter(Boolean).join(", ");
const inner = (
<>
<span className="relative">
<span className={cn("block transition-transform duration-300 ease-[cubic-bezier(0.34,1.56,0.64,1)] motion-reduce:transition-none", active && "-translate-y-px scale-110")}>
{renderTabIcon(item.icon)}
</span>
<Badge badge={item.badge} />
</span>
<span className={cn("max-w-full truncate px-0.5 text-[0.6875rem] leading-4 font-medium", !showLabel && "sr-only")}>{item.label}</span>
</>
);
const shared = {
ref: (el: HTMLElement | null) => {
if (el) refs.current.set(item.value, el);
else refs.current.delete(item.value);
},
"aria-current": active ? ("page" as const) : undefined,
"aria-label": spoken !== item.label ? spoken : undefined,
onClick: (event: React.MouseEvent) => {
if (item.disabled) {
event.preventDefault();
return;
}
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
choose(item);
},
className: cn(
"relative z-10 flex min-h-12 w-full flex-col items-center justify-center gap-0.5 rounded-2xl py-1.5 text-muted-foreground outline-none transition-colors duration-200 select-none [-webkit-tap-highlight-color:transparent] focus-visible:ring-2 focus-visible:ring-ring active:[&>span:first-child]:scale-90",
active && "text-primary",
item.disabled && "pointer-events-none opacity-40",
!showLabel && "gap-0",
),
};
return (
<React.Fragment key={item.value}>
{action && index === middle ? actionButton : null}
<li className="flex min-w-0 flex-1">
{item.href ? (
<Link href={item.href} aria-disabled={item.disabled || undefined} {...shared}>
{inner}
</Link>
) : (
<button type="button" disabled={item.disabled} {...shared}>
{inner}
</button>
)}
</li>
</React.Fragment>
);
})}
{action && middle >= items.length ? actionButton : null}
</ul>
</nav>
);
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
| items | BottomTabBarItem[] | — | { value, label, icon, badge?, href?, disabled? }. icon is a built-in name (home, search, inbox, user…) or any React node. |
| value | string | — | The current tab, when you control it (e.g. from the pathname). |
| defaultValue | string | first item | The tab shown first when uncontrolled. |
| onValueChange | (value: string) => void | — | Called when another tab is chosen. |
| onReselect | (value: string) => void | — | Called when the current tab is tapped again: scroll to the top or go back to its first screen. |
| look | "classic" | "floating" | "glass" | "classic" | Classic spans the edge; floating and glass hover above the content as a capsule. |
| indicator | "pill" | "dot" | "line" | "none" | "pill" | What marks the current tab. |
| labels | "always" | "active" | "never" | "always" | When names show under the icons. Hidden names stay readable to screen readers. |
| action | { label, icon?, href?, onSelect? } | false | — | A raised button in the middle for the app's main action. |
| hidden | boolean | false | Slides the bar out of view and out of the tab order. |
| hideOnScroll | boolean | false | Hides while the window scrolls down, back on the way up. |
| position | "fixed" | "absolute" | "sticky" | "static" | "fixed" | Fixed to the window, inside the nearest positioned parent, stuck to a scroller's bottom, or in the flow. |
| linkComponent | ElementType | "a" | Renders tabs that have an href, e.g. Next.js Link. |
| label | string | "Main" | Names the navigation landmark. |
Accessibility
- A navigation landmark with a list of tabs; the current one has aria-current="page".
- Badges are read with the tab's name ("Inbox, 3 new"); icon-only tabs keep their names for screen readers.
- Every tab is at least 48px tall, with a visible focus ring; the bar sits above the phone's home indicator (safe-area-inset-bottom).
- A hidden bar is inert, so it leaves the tab order; the marker jumps instead of gliding under prefers-reduced-motion.
Changelog
- v1.0.0Latest
First release.



