Component
Icon tabs
Tabs that show only icons and name just the chosen one, on a lifted pill that glides to the next choice.
- #arc
- #component
- #tabs
- #icon
- #toggle
- #choice
- #selection
- #filter
1280px
Install
One command installs the files and packages this item needs.
TerminalTerminalTerminalbash
npx shadcn@latest add https://dsui.vercel.app/r/icon-tabs.jsonFiles
components/arc/icon-tabs/icon-tabs.module.csscomponents/arc/icon-tabs/icon-tabs.tsx
npm packages
motion@^14.2.0
Registry items it pulls in
Source
View as Markdownicon-tabs.module.cssicon-tabs.module.cssicon-tabs.module.csscss
/* Icon tabs: the same quiet track as the segmented control, with a lifted pill under the chosen tab. Arc tokens only. */
.root { display: inline-flex; min-width: 0; max-width: 100%; border-radius: var(--radius-pill); background: var(--surface-muted); }
.track { isolation: isolate; display: flex; gap: 2px; padding: 3px; border-radius: inherit; }
.tab { position: relative; border: 0; background: transparent; font: inherit; cursor: pointer; display: inline-flex; min-width: 2.5rem; height: 2.5rem; align-items: center; justify-content: center; border-radius: var(--radius-pill); padding: 0 var(--space-3); color: var(--text-muted); font-size: var(--label-md); font-weight: var(--label-weight); text-decoration: none; white-space: nowrap; -webkit-tap-highlight-color: transparent; transition: color var(--duration-fast) var(--ease-standard), background var(--duration-fast) var(--ease-standard), transform var(--duration-spring) var(--ease-spring); }
.tab[data-on] { color: var(--foreground); padding: 0 var(--space-4); }
.tab:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tab:active { transform: scale(.94); }
@media (hover: hover) and (pointer: fine) { .tab:hover:not([data-on]) { color: var(--foreground); background: var(--control-track-hover); } }
.pill { position: absolute; z-index: -1; inset: 0; border: 1px solid var(--border); border-radius: inherit; background: var(--surface); box-shadow: var(--shadow-resting); }
.face { position: relative; z-index: 1; display: inline-flex; align-items: center; }
.name { display: inline-block; overflow: hidden; white-space: nowrap; }
@media (prefers-reduced-motion: reduce) { .tab { transition: none; } .tab:active { transform: none; } }
icon-tabs.tsxicon-tabs.tsxicon-tabs.tsxtsx
"use client";
import { createElement, useId, useState } from "react";
import type { ReactNode } from "react";
import { motion } from "motion/react";
import { useReducedMotion } from "../lib/use-reduced-motion";
import { motionTokens } from "../lib/motion-tokens";
import { useArcLink, type ArcLinkComponent } from "../lib/arc-provider";
import { Tooltip } from "../tooltip/tooltip";
import { playUiSound } from "../sound/play";
import styles from "./icon-tabs.module.css";
export interface IconTabItem {
value: string;
/** The tab's name: shown beside the icon while chosen, in a tooltip otherwise, and always as its accessible name. */
label: string;
icon: ReactNode;
/** Makes the tab a link to this address (the server can draw what it asks for and the address can be shared). Without it the tab is a button. */
href?: string;
}
export interface IconTabsProps {
items: IconTabItem[];
/** The chosen tab. For tabs that are links this is the page that is open now. */
value: string;
/** Called when a tab is pressed. */
onValueChange?: (value: string) => void;
/** Names the group for assistive technology. */
label: string;
/** The element drawn for tabs with an `href`, such as `next/link`. Defaults to the link of the nearest `ArcProvider`. */
linkComponent?: ArcLinkComponent;
className?: string;
}
/**
* Tabs that show only their icons: the chosen one carries its name beside the icon on a lifted pill that glides to the next choice, the others name themselves in a tooltip.
* The pill moves as soon as a tab is pressed, before a page behind a link has caught up.
*/
export default function IconTabs({ items, value, onValueChange, label, linkComponent, className }: IconTabsProps) {
const reduced = useReducedMotion();
const id = useId();
const providerLink = useArcLink();
const Anchor = linkComponent ?? providerLink;
// The pressed tab leads until the value confirms it, so the pill does not wait for the network.
const [pending, setPending] = useState<string | null>(null);
const [seen, setSeen] = useState(value);
if (seen !== value) {
setSeen(value);
setPending(null);
}
const shown = pending ?? value;
const glide = reduced ? { duration: 0 } : motionTokens.spring.morph;
return (
<nav aria-label={label} className={`${styles.root} ${className ?? ""}`} data-duo="">
<div className={styles.track}>
{items.map((item) => {
const on = item.value === shown;
const press = () => {
if (!on) playUiSound("tick");
setPending(item.value);
onValueChange?.(item.value);
};
const face = (
<>
{on ? <motion.span layoutId={`${id}-pill`} className={styles.pill} transition={glide} /> : null}
<span className={styles.face}>
{item.icon}
<motion.span className={styles.name} initial={false} animate={{ width: on ? "auto" : 0, opacity: on ? 1 : 0, marginLeft: on ? 6 : 0 }} transition={reduced ? { duration: 0 } : { duration: motionTokens.duration.standard, ease: [...motionTokens.ease.standard] }}>
{item.label}
</motion.span>
</span>
</>
);
return (
<Tooltip key={item.value} content={item.label} side="bottom" disabled={on}>
{item.href !== undefined
? createElement(Anchor, { href: item.href, className: styles.tab, "aria-label": item.label, "aria-current": item.value === value ? "true" : undefined, onClick: press, ...{ "data-on": on ? "" : undefined } }, face)
: (
<button type="button" className={styles.tab} aria-label={item.label} aria-pressed={item.value === value} data-on={on ? "" : undefined} onClick={press}>{face}</button>
)}
</Tooltip>
);
})}
</div>
</nav>
);
}
Used items
- TooltipComponent
A short hint bubble that opens on hover or focus and skips the delay when opened in quick succession.