Switch between related panels without leaving the page. Arrow keys move between their tabs.
Tabs example
Example.jsx
import { Tabs } from "./ui/tabs";
import "./example.css";
export default function Example() {
return (
<Tabs
label="Release review"
items={[
{
id: "summary",
label: "Summary",
content: (
<div class="tile">
<h2>Winter issue</h2>
<p>Copy edits have cleared and the jacket proof is ready for sign-off.</p>
</div>
),
},
{
id: "changes",
label: "Changes",
content: (
<div class="tile">
<h2>Three files changed</h2>
<ul>
<li>Cover typography</li>
<li>Contributor biographies</li>
<li>Press-ready PDF</li>
</ul>
</div>
),
},
]}
/>
);
}
example.css
@media screen and (prefers-reduced-motion: no-preference) {
#example button:is(.v-primary, .v-secondary):enabled {
transition: transform 120ms ease-out;
}
#example button:is(.v-primary, .v-secondary):enabled:active {
transform: scale(0.98);
}
}
@media (prefers-reduced-motion: reduce) {
#example button:is(.v-primary, .v-secondary):enabled:active {
transition: none;
transform: none;
}
}
* {
box-sizing: border-box;
}
:root {
--example-seed: var(--aster-primary);
--example-secondary-text: var(--aster-on-surface-subtle);
}
body {
--aster-primary: color-mix(in srgb, var(--example-seed) 84%, var(--aster-on-surface));
--aster-on-surface-subtle: color-mix(in srgb, var(--example-secondary-text) 84%, var(--aster-on-surface));
--aster-input-border: color-mix(in srgb, var(--aster-on-surface) 55%, var(--aster-surface));
margin: 0;
padding: clamp(0.8rem, 4vw, 1.5rem);
color: var(--aster-on-surface);
background: var(--aster-surface-muted);
font: 1rem/1.5 var(--aster-font-sans);
}
#example {
display: grid;
gap: 0.8rem;
min-width: 0;
align-content: start;
}
#example > button,
.sample > button {
justify-self: start;
}
.sample,
.tile {
background: var(--aster-surface);
border: 1px solid var(--aster-border);
border-radius: var(--aster-radius-lg);
}
.sample {
display: grid;
gap: 0.8rem;
padding: clamp(0.85rem, 3vw, 1.35rem);
box-shadow: var(--aster-shadow-xs);
min-width: 0;
}
.tile {
padding: 1rem;
min-width: 0;
}
h2,
h3,
p,
dl,
ul {
margin-block: 0;
}
h2,
h3 {
letter-spacing: -0.025em;
line-height: 1.2;
}
h2 {
font-size: 1.25rem;
}
h3 {
font-size: 1rem;
}
.sample > header,
.identity > div,
.receipt {
display: grid;
gap: 0.25rem;
}
.sample > header p,
.tile p,
small {
color: var(--aster-on-surface-muted);
}
small {
font-size: 0.82rem;
}
.split,
.actions,
.identity {
display: flex;
align-items: center;
gap: 0.65rem;
flex-wrap: wrap;
}
.split {
justify-content: space-between;
padding-block: 0.4rem;
border-bottom: 1px solid var(--aster-border);
}
.split:last-child {
border-bottom: 0;
}
.metric {
font-size: 2rem;
letter-spacing: -0.06em;
}
.facts {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.45rem 1rem;
}
.facts dt {
color: var(--aster-on-surface-muted);
}
.facts dd {
margin: 0;
font-weight: 600;
}
.type-sample {
font:
2rem/1.2 Georgia,
serif;
}
.listing {
padding-inline-start: 1.25rem;
}
.ink-strip {
height: 1.2rem;
background: var(--aster-primary);
border-radius: var(--aster-radius-sm);
}
.ink-proof {
display: grid;
gap: 0.15rem;
border-left: 0.6rem solid;
padding-left: 0.8rem;
}
.ink-proof strong {
font-size: 1.25rem;
letter-spacing: 0.06em;
}
.glyph {
display: inline-grid;
place-items: center;
width: 2rem;
height: 2rem;
border-radius: var(--aster-radius-sm);
color: var(--aster-on-primary-subtle);
background: var(--aster-primary-subtle);
}
.cover-box {
width: min(100%, 18rem);
}
.cover {
display: flex;
min-height: 7.5rem;
height: 100%;
padding: 1rem 1.25rem;
flex-direction: column;
justify-content: space-between;
gap: 1.6rem;
color: #fff;
background: repeating-linear-gradient(150deg, transparent 0 8px, #fff2 8px 9px), #204a48;
}
.cover strong {
font:
italic 2.45rem/1 Georgia,
serif;
}
.cover span {
font: 0.78rem var(--aster-font-mono);
}
.cover.grove {
background-color: #3d4b27;
}
.cover.dune {
background-color: #7c4428;
}
button:not([class]),
.entry {
font: inherit;
font-size: 0.875rem;
padding: 0.45rem 0.8rem;
border: 1px solid var(--aster-border-strong);
border-radius: var(--aster-radius-control);
background: var(--aster-surface);
color: var(--aster-on-surface);
}
button:not([class]) {
cursor: pointer;
font-weight: 600;
}
button:not([class]):hover:enabled {
background: var(--aster-primary-subtle);
}
button:disabled {
cursor: default;
}
:focus-visible {
outline: 2px solid var(--aster-primary);
outline-offset: 3px;
}
output:not(:empty) {
display: block;
color: var(--aster-on-primary-subtle);
font-weight: 600;
font-size: 0.9rem;
}
Site-owned component source and scoped styles
src/ui/tabs
import './theme.css';
import { scope, type Ctx, type View } from '@fierst/aster';
const tabsScope = scope`
:scope { display: grid; gap: 1rem; min-width: 0; }
/* Variant 1: Segmented (shadcn default) */
.tablist.v-segmented {
display: inline-flex;
align-items: center;
gap: 2px;
padding: 3px;
border-radius: var(--aster-radius-control);
background: var(--aster-surface-muted);
width: fit-content;
max-width: 100%;
overflow-x: auto;
scrollbar-width: none;
-webkit-overflow-scrolling: touch;
}
.tablist.v-segmented .tab {
flex-shrink: 0;
height: calc(var(--aster-control-sm) - 2px);
padding: 0 0.75rem;
border-radius: calc(var(--aster-radius-control) - 2px);
border: none;
background: transparent;
color: var(--aster-on-surface-muted);
font-family: var(--aster-font-sans);
font-size: 0.84rem;
font-weight: 500;
cursor: pointer;
touch-action: manipulation;
transition:
background-color var(--aster-dur-fast) var(--aster-ease-standard),
color var(--aster-dur-fast) var(--aster-ease-standard),
box-shadow var(--aster-dur-fast) var(--aster-ease-standard);
}
.tablist.v-segmented .tab:hover { color: var(--aster-on-surface); }
.tablist.v-segmented .tab.is-active {
background: var(--aster-surface);
color: var(--aster-on-surface);
box-shadow: var(--aster-shadow-xs);
}
/* Variant 2: Underline (TailwindUI classic) */
.tablist.v-underline {
display: flex;
align-items: center;
gap: 1.25rem;
border-bottom: 1px solid var(--aster-border);
max-width: 100%;
overflow-x: auto;
scrollbar-width: none;
-webkit-overflow-scrolling: touch;
}
.tablist.v-underline .tab {
flex-shrink: 0;
padding: 0.55rem 0.1rem;
margin-bottom: -1px;
border: none;
border-bottom: 2px solid transparent;
background: transparent;
color: var(--aster-on-surface-muted);
font-family: var(--aster-font-sans);
font-size: 0.875rem;
font-weight: 500;
cursor: pointer;
touch-action: manipulation;
transition: color var(--aster-dur-fast), border-color var(--aster-dur-fast);
}
.tablist.v-underline .tab:hover { color: var(--aster-on-surface); border-bottom-color: var(--aster-border-strong); }
.tablist.v-underline .tab.is-active { color: var(--aster-primary); border-bottom-color: var(--aster-primary); }
.tablist::-webkit-scrollbar { display: none; }
.tab:focus-visible { outline: none; box-shadow: var(--aster-focus-ring); }
.tabpanel { outline: none; min-width: 0; }
`;
export interface TabItem {
id: string;
label: string;
content: View | (() => View);
}
let tabSeq = 0;
export function* Tabs(
props: {
items: readonly TabItem[];
variant?: 'segmented' | 'underline';
value?: string;
defaultValue?: string;
label?: string;
onChange?: (id: string) => void;
class?: string;
},
ctx: Ctx,
) {
const uid = `aster-tabs-${++tabSeq}`;
let internalId = props.defaultValue ?? props.items[0]?.id ?? '';
for (props of ctx) {
const isControlled = props.value !== undefined;
const activeId = isControlled ? props.value! : internalId;
const current = props.items.find((t) => t.id === activeId) ?? props.items[0];
const currentIdx = props.items.findIndex((t) => t.id === current?.id);
const select = (id: string, moveFocus = false) => {
if (!isControlled) internalId = id;
props.onChange?.(id);
if (moveFocus) {
queueMicrotask(() => document.getElementById(`${uid}-tab-${id}`)?.focus());
}
};
const onKeyDown = (e: KeyboardEvent) => {
const len = props.items.length;
if (len === 0) return;
let nextIdx = -1;
if (e.key === 'ArrowRight') nextIdx = (currentIdx + 1) % len;
else if (e.key === 'ArrowLeft') nextIdx = (currentIdx - 1 + len) % len;
else if (e.key === 'Home') nextIdx = 0;
else if (e.key === 'End') nextIdx = len - 1;
if (nextIdx !== -1) {
e.preventDefault();
select(props.items[nextIdx].id, true);
}
};
const panelContent =
current && typeof current.content === 'function'
? current.content()
: (current?.content ?? null);
const variant = props.variant ?? 'segmented';
yield (
<div class={[tabsScope, props.class].filter(Boolean).join(' ')}>
<div
role="tablist"
aria-label={props.label ?? 'Tabs'}
class={`tablist v-${variant}`}
onKeyDown={onKeyDown}
>
{props.items.map((item) => {
const selected = item.id === current?.id;
return (
<button
key={item.id}
id={`${uid}-tab-${item.id}`}
type="button"
role="tab"
aria-selected={selected}
aria-controls={`${uid}-panel`}
tabIndex={selected ? 0 : -1}
class="tab"
class:is-active={selected}
onClick={() => select(item.id)}
>
{item.label}
</button>
);
})}
</div>
<div
id={`${uid}-panel`}
role="tabpanel"
aria-labelledby={current ? `${uid}-tab-${current.id}` : undefined}
class="tabpanel"
>
{typeof panelContent === 'function' ? panelContent() : panelContent}
</div>
</div>
);
}
}
Install
sh
npx @fierst/ui@1.0.2 add tabsRegistry dependencies: theme.
Exports from ./ui/tabs: TabItem, Tabs.
Download the registry source for tabs 1.0.2.
API contracts
ts
export interface TabItem {
id: string;
label: string;
content: View | (() => View);
}
export function* Tabs(
props: {
items: readonly TabItem[];
variant?: 'segmented' | 'underline';
value?: string;
defaultValue?: string;
label?: string;
onChange?: (id: string) => void;
class?: string;
},
ctx: Ctx,
)