Tabs
Tabs in two looks on one project card: a raised pill that slides inside a soft track, and an accent line that slides under the chosen tab. The arrow keys move between them.
Website redesignexample.com
1,284visits this week+12%
5,902visits this month+8%
61,340visits this year+31%
- HomeLive
- PricingIn review
- JournalDraft
- Maya Okafor changed Home2h
- Sam Jensen left a note on Pricing5h
- Maya Okafor added Journal1d
- Maya OkaforOwner
- Sam JensenEditor
- Ella MarshViewer
- Addressexample.com
- LanguageEnglish
- Search enginesAllowed
Options
Code
import { useEffect, useId, useRef, useState } from "react";
import "./tabs.css";
/* Tabs with a mark that slides to the chosen one. Anything passed as children
sits beside the tabs, in a header row.
<Tabs kind="pill" label="Period" items={[{ label: "Week", content: <p>1,284 visits</p> }]} /> */
export function Tabs({ kind = "line", label, items, initial = 0, children }) {
const id = useId();
const list = useRef(null);
const [chosen, setChosen] = useState(initial);
const [ready, setReady] = useState(false);
/* Sets the chosen tab's left edge and width on the list, and again when the list changes size. */
useEffect(() => {
const place = () => {
const tab = list.current.querySelector('[aria-selected="true"]');
list.current.style.setProperty("--x", tab.offsetLeft);
list.current.style.setProperty("--w", tab.offsetWidth);
list.current.style.setProperty("--on", 1);
};
place();
const watch = new ResizeObserver(place);
watch.observe(list.current);
return () => watch.disconnect();
}, [chosen]);
useEffect(() => {
const frame = requestAnimationFrame(() => setReady(true));
return () => cancelAnimationFrame(frame);
}, []);
const move = (event, index) => {
const last = items.length - 1;
const to = { ArrowRight: index === last ? 0 : index + 1, ArrowLeft: index === 0 ? last : index - 1, Home: 0, End: last }[event.key];
if (to === undefined) return;
event.preventDefault();
list.current.querySelectorAll('[role="tab"]')[to].focus();
setChosen(to);
};
const tabs = (
<div className="dh-tabs-list" role="tablist" aria-label={label} ref={list} data-ready={ready ? "" : undefined}>
{items.map((item, index) => (
<button
type="button"
role="tab"
key={item.label}
id={id + "tab" + index}
aria-controls={id + "panel" + index}
aria-selected={index === chosen}
tabIndex={index === chosen ? 0 : -1}
onClick={() => setChosen(index)}
onKeyDown={(event) => move(event, index)}
>
{item.label}
</button>
))}
<i className="dh-tabs-mark" aria-hidden="true" />
</div>
);
return (
<div className="dh-tabs" data-kind={kind}>
{children ? (
<header>
{children}
{tabs}
</header>
) : (
tabs
)}
{items.map((item, index) => (
<div
className="dh-tabs-panel"
role="tabpanel"
key={item.label}
id={id + "panel" + index}
aria-labelledby={id + "tab" + index}
tabIndex={0}
hidden={index !== chosen}
>
{item.content}
</div>
))}
</div>
);
}
function Figure({ value, label, change }) {
return (
<p className="dh-tabs-figure">
<b>{value}</b>
<span>{label}</span>
<em>{change}</em>
</p>
);
}
function Rows({ rows }) {
return (
<ul className="dh-tabs-rows">
{rows.map(([name, value, dot]) => (
<li key={name}>
<span>{name}</span>
<em data-dot={dot}>{value}</em>
</li>
))}
</ul>
);
}
export function Example() {
return (
<div className="dh-tabs-card">
<Tabs
kind="pill"
label="Period"
items={[
{ label: "Week", content: <Figure value="1,284" label="visits this week" change="+12%" /> },
{ label: "Month", content: <Figure value="5,902" label="visits this month" change="+8%" /> },
{ label: "Year", content: <Figure value="61,340" label="visits this year" change="+31%" /> },
]}
>
<p>
<b>Website redesign</b>
<span>example.com</span>
</p>
</Tabs>
<Tabs
label="Project"
items={[
{ label: "Pages", content: <Rows rows={[["Home", "Live", "good"], ["Pricing", "In review", "warn"], ["Journal", "Draft", "none"]]} /> },
{ label: "Activity", content: <Rows rows={[["Maya Okafor changed Home", "2h"], ["Sam Jensen left a note on Pricing", "5h"], ["Maya Okafor added Journal", "1d"]]} /> },
{ label: "Members", content: <Rows rows={[["Maya Okafor", "Owner"], ["Sam Jensen", "Editor"], ["Ella Marsh", "Viewer"]]} /> },
{ label: "Settings", content: <Rows rows={[["Address", "example.com"], ["Language", "English"], ["Search engines", "Allowed"]]} /> },
]}
/>
</div>
);
}/* Tabs in two looks, shown on one project card: a raised pill that slides
inside a soft track, and an accent line that slides under the chosen tab.
The script sets --x and --w on the list to the chosen tab's place. */
.dh-tabs-card {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--ease: cubic-bezier(0.23, 1, 0.32, 1);
overflow: hidden;
width: min(400px, 100%);
border-radius: 14px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-tabs-card :is(p, ul) {
margin: 0;
padding: 0;
list-style: none;
}
.dh-tabs header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 14px 14px 0 16px;
}
.dh-tabs header p {
display: grid;
min-width: 0;
}
.dh-tabs header b {
font-size: 14px;
font-weight: 500;
letter-spacing: -0.015em;
}
.dh-tabs header span {
color: var(--dh-muted, #5c5c5c);
font-size: 11px;
line-height: 16px;
}
.dh-tabs-list {
position: relative;
display: flex;
}
.dh-tabs-list button {
position: relative;
z-index: 1;
border: 0;
background: none;
color: var(--dh-muted, #5c5c5c);
font: inherit;
font-weight: 500;
letter-spacing: -0.01em;
white-space: nowrap;
cursor: pointer;
outline: none;
-webkit-tap-highlight-color: transparent;
transition: color 150ms var(--ease);
}
.dh-tabs-list button[aria-selected="true"] {
color: var(--dh-fg, #171717);
}
.dh-tabs-list button:focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-tabs-mark {
position: absolute;
left: 0;
width: calc(var(--w, 0) * 1px);
opacity: var(--on, 0);
pointer-events: none;
translate: calc(var(--x, 0) * 1px) 0;
}
.dh-tabs-list[data-ready] .dh-tabs-mark {
transition:
translate 320ms var(--ease),
width 320ms var(--ease);
}
/* The pill: a raised chip in a sunk track. */
.dh-tabs[data-kind="pill"] .dh-tabs-list {
flex: none;
padding: 2px;
border-radius: 8px;
background: var(--dh-well, #ededeb);
}
.dh-tabs[data-kind="pill"] .dh-tabs-list button {
height: 24px;
padding: 0 10px;
border-radius: 6px;
font-size: 12px;
}
.dh-tabs[data-kind="pill"] .dh-tabs-mark {
top: 2px;
bottom: 2px;
border-radius: 6px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 14%, transparent);
}
/* The line: an accent bar on the list's bottom edge. */
.dh-tabs[data-kind="line"] .dh-tabs-list {
gap: 18px;
padding: 0 16px;
box-shadow: inset 0 -1px 0 var(--line);
}
.dh-tabs[data-kind="line"] .dh-tabs-list button {
height: 36px;
padding: 0;
border-radius: 4px;
}
.dh-tabs[data-kind="line"] .dh-tabs-mark {
bottom: 0;
height: 2px;
border-radius: 2px 2px 0 0;
background: var(--accent);
}
.dh-tabs-panel {
outline: none;
animation: dh-tabs-in 280ms var(--ease);
}
.dh-tabs-panel:focus-visible {
box-shadow: inset 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-tabs-figure {
display: flex;
align-items: baseline;
gap: 6px;
padding: 12px 16px 14px !important;
color: var(--dh-muted, #5c5c5c);
}
.dh-tabs-figure b {
color: var(--dh-fg, #171717);
font-size: 24px;
font-weight: 500;
line-height: 28px;
letter-spacing: -0.03em;
font-variant-numeric: tabular-nums;
}
.dh-tabs-figure em {
align-self: center;
margin-left: auto;
padding: 1px 5px;
border-radius: 4px;
background: color-mix(in srgb, #1fa750 14%, transparent);
color: color-mix(in srgb, #1fa750 70%, var(--dh-fg, #171717));
font-size: 11px;
font-style: normal;
font-weight: 500;
line-height: 16px;
}
.dh-tabs-rows {
padding: 4px 0 6px !important;
}
.dh-tabs-rows li {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin: 0 16px;
padding: 7px 0;
}
.dh-tabs-rows li + li {
border-top: 1px solid var(--line);
}
.dh-tabs-rows span {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.dh-tabs-rows em {
display: flex;
flex: none;
align-items: center;
gap: 6px;
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
font-style: normal;
}
.dh-tabs-rows em[data-dot]::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--dh-faint, #8a8a8a);
}
.dh-tabs-rows em[data-dot="good"]::before {
background: #1fa750;
}
.dh-tabs-rows em[data-dot="warn"]::before {
background: #e39b0b;
}
@keyframes dh-tabs-in {
from {
opacity: 0;
filter: blur(4px);
transform: translateY(-4px);
}
}
@media (hover: hover) and (pointer: fine) {
.dh-tabs-list button:hover {
color: var(--dh-fg, #171717);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-tabs-list[data-ready] .dh-tabs-mark {
transition: none;
}
.dh-tabs-panel {
animation: dh-tabs-fade 150ms;
}
}
@keyframes dh-tabs-fade {
from {
opacity: 0;
}
}<div class="dh-tabs-card">
<div class="dh-tabs" data-kind="pill">
<header>
<p><b>Website redesign</b><span>example.com</span></p>
<div class="dh-tabs-list" role="tablist" aria-label="Period">
<button type="button" role="tab" id="dh-tabs-pill-1" aria-controls="dh-tabs-pill-panel-1" aria-selected="true">Week</button>
<button type="button" role="tab" id="dh-tabs-pill-2" aria-controls="dh-tabs-pill-panel-2" aria-selected="false" tabindex="-1">Month</button>
<button type="button" role="tab" id="dh-tabs-pill-3" aria-controls="dh-tabs-pill-panel-3" aria-selected="false" tabindex="-1">Year</button>
<i class="dh-tabs-mark" aria-hidden="true"></i>
</div>
</header>
<div class="dh-tabs-panel" role="tabpanel" id="dh-tabs-pill-panel-1" aria-labelledby="dh-tabs-pill-1" tabindex="0"><p class="dh-tabs-figure"><b>1,284</b><span>visits this week</span><em>+12%</em></p></div>
<div class="dh-tabs-panel" role="tabpanel" id="dh-tabs-pill-panel-2" aria-labelledby="dh-tabs-pill-2" tabindex="0" hidden><p class="dh-tabs-figure"><b>5,902</b><span>visits this month</span><em>+8%</em></p></div>
<div class="dh-tabs-panel" role="tabpanel" id="dh-tabs-pill-panel-3" aria-labelledby="dh-tabs-pill-3" tabindex="0" hidden><p class="dh-tabs-figure"><b>61,340</b><span>visits this year</span><em>+31%</em></p></div>
</div>
<div class="dh-tabs" data-kind="line">
<div class="dh-tabs-list" role="tablist" aria-label="Project">
<button type="button" role="tab" id="dh-tabs-line-1" aria-controls="dh-tabs-line-panel-1" aria-selected="true">Pages</button>
<button type="button" role="tab" id="dh-tabs-line-2" aria-controls="dh-tabs-line-panel-2" aria-selected="false" tabindex="-1">Activity</button>
<button type="button" role="tab" id="dh-tabs-line-3" aria-controls="dh-tabs-line-panel-3" aria-selected="false" tabindex="-1">Members</button>
<button type="button" role="tab" id="dh-tabs-line-4" aria-controls="dh-tabs-line-panel-4" aria-selected="false" tabindex="-1">Settings</button>
<i class="dh-tabs-mark" aria-hidden="true"></i>
</div>
<div class="dh-tabs-panel" role="tabpanel" id="dh-tabs-line-panel-1" aria-labelledby="dh-tabs-line-1" tabindex="0"><ul class="dh-tabs-rows"><li><span>Home</span><em data-dot="good">Live</em></li><li><span>Pricing</span><em data-dot="warn">In review</em></li><li><span>Journal</span><em data-dot="none">Draft</em></li></ul></div>
<div class="dh-tabs-panel" role="tabpanel" id="dh-tabs-line-panel-2" aria-labelledby="dh-tabs-line-2" tabindex="0" hidden><ul class="dh-tabs-rows"><li><span>Maya Okafor changed Home</span><em>2h</em></li><li><span>Sam Jensen left a note on Pricing</span><em>5h</em></li><li><span>Maya Okafor added Journal</span><em>1d</em></li></ul></div>
<div class="dh-tabs-panel" role="tabpanel" id="dh-tabs-line-panel-3" aria-labelledby="dh-tabs-line-3" tabindex="0" hidden><ul class="dh-tabs-rows"><li><span>Maya Okafor</span><em>Owner</em></li><li><span>Sam Jensen</span><em>Editor</em></li><li><span>Ella Marsh</span><em>Viewer</em></li></ul></div>
<div class="dh-tabs-panel" role="tabpanel" id="dh-tabs-line-panel-4" aria-labelledby="dh-tabs-line-4" tabindex="0" hidden><ul class="dh-tabs-rows"><li><span>Address</span><em>example.com</em></li><li><span>Language</span><em>English</em></li><li><span>Search engines</span><em>Allowed</em></li></ul></div>
</div>
</div>/* Tabs in two looks, shown on one project card: a raised pill that slides
inside a soft track, and an accent line that slides under the chosen tab.
The script sets --x and --w on the list to the chosen tab's place. */
.dh-tabs-card {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--ease: cubic-bezier(0.23, 1, 0.32, 1);
overflow: hidden;
width: min(400px, 100%);
border-radius: 14px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-tabs-card :is(p, ul) {
margin: 0;
padding: 0;
list-style: none;
}
.dh-tabs header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 14px 14px 0 16px;
}
.dh-tabs header p {
display: grid;
min-width: 0;
}
.dh-tabs header b {
font-size: 14px;
font-weight: 500;
letter-spacing: -0.015em;
}
.dh-tabs header span {
color: var(--dh-muted, #5c5c5c);
font-size: 11px;
line-height: 16px;
}
.dh-tabs-list {
position: relative;
display: flex;
}
.dh-tabs-list button {
position: relative;
z-index: 1;
border: 0;
background: none;
color: var(--dh-muted, #5c5c5c);
font: inherit;
font-weight: 500;
letter-spacing: -0.01em;
white-space: nowrap;
cursor: pointer;
outline: none;
-webkit-tap-highlight-color: transparent;
transition: color 150ms var(--ease);
}
.dh-tabs-list button[aria-selected="true"] {
color: var(--dh-fg, #171717);
}
.dh-tabs-list button:focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-tabs-mark {
position: absolute;
left: 0;
width: calc(var(--w, 0) * 1px);
opacity: var(--on, 0);
pointer-events: none;
translate: calc(var(--x, 0) * 1px) 0;
}
.dh-tabs-list[data-ready] .dh-tabs-mark {
transition:
translate 320ms var(--ease),
width 320ms var(--ease);
}
/* The pill: a raised chip in a sunk track. */
.dh-tabs[data-kind="pill"] .dh-tabs-list {
flex: none;
padding: 2px;
border-radius: 8px;
background: var(--dh-well, #ededeb);
}
.dh-tabs[data-kind="pill"] .dh-tabs-list button {
height: 24px;
padding: 0 10px;
border-radius: 6px;
font-size: 12px;
}
.dh-tabs[data-kind="pill"] .dh-tabs-mark {
top: 2px;
bottom: 2px;
border-radius: 6px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 14%, transparent);
}
/* The line: an accent bar on the list's bottom edge. */
.dh-tabs[data-kind="line"] .dh-tabs-list {
gap: 18px;
padding: 0 16px;
box-shadow: inset 0 -1px 0 var(--line);
}
.dh-tabs[data-kind="line"] .dh-tabs-list button {
height: 36px;
padding: 0;
border-radius: 4px;
}
.dh-tabs[data-kind="line"] .dh-tabs-mark {
bottom: 0;
height: 2px;
border-radius: 2px 2px 0 0;
background: var(--accent);
}
.dh-tabs-panel {
outline: none;
animation: dh-tabs-in 280ms var(--ease);
}
.dh-tabs-panel:focus-visible {
box-shadow: inset 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-tabs-figure {
display: flex;
align-items: baseline;
gap: 6px;
padding: 12px 16px 14px !important;
color: var(--dh-muted, #5c5c5c);
}
.dh-tabs-figure b {
color: var(--dh-fg, #171717);
font-size: 24px;
font-weight: 500;
line-height: 28px;
letter-spacing: -0.03em;
font-variant-numeric: tabular-nums;
}
.dh-tabs-figure em {
align-self: center;
margin-left: auto;
padding: 1px 5px;
border-radius: 4px;
background: color-mix(in srgb, #1fa750 14%, transparent);
color: color-mix(in srgb, #1fa750 70%, var(--dh-fg, #171717));
font-size: 11px;
font-style: normal;
font-weight: 500;
line-height: 16px;
}
.dh-tabs-rows {
padding: 4px 0 6px !important;
}
.dh-tabs-rows li {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin: 0 16px;
padding: 7px 0;
}
.dh-tabs-rows li + li {
border-top: 1px solid var(--line);
}
.dh-tabs-rows span {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.dh-tabs-rows em {
display: flex;
flex: none;
align-items: center;
gap: 6px;
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
font-style: normal;
}
.dh-tabs-rows em[data-dot]::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--dh-faint, #8a8a8a);
}
.dh-tabs-rows em[data-dot="good"]::before {
background: #1fa750;
}
.dh-tabs-rows em[data-dot="warn"]::before {
background: #e39b0b;
}
@keyframes dh-tabs-in {
from {
opacity: 0;
filter: blur(4px);
transform: translateY(-4px);
}
}
@media (hover: hover) and (pointer: fine) {
.dh-tabs-list button:hover {
color: var(--dh-fg, #171717);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-tabs-list[data-ready] .dh-tabs-mark {
transition: none;
}
.dh-tabs-panel {
animation: dh-tabs-fade 150ms;
}
}
@keyframes dh-tabs-fade {
from {
opacity: 0;
}
}/* Choosing a tab shows its panel and slides the mark under it.
The arrow keys, Home and End move between tabs. */
document.querySelectorAll(".dh-tabs").forEach((root) => {
const list = root.querySelector(".dh-tabs-list");
const tabs = Array.from(list.querySelectorAll('[role="tab"]'));
const place = () => {
const tab = tabs.find((item) => item.getAttribute("aria-selected") === "true");
list.style.setProperty("--x", tab.offsetLeft);
list.style.setProperty("--w", tab.offsetWidth);
list.style.setProperty("--on", 1);
};
const choose = (tab) => {
tabs.forEach((item) => {
const chosen = item === tab;
item.setAttribute("aria-selected", String(chosen));
item.tabIndex = chosen ? 0 : -1;
document.getElementById(item.getAttribute("aria-controls")).hidden = !chosen;
});
place();
};
tabs.forEach((tab, index) => {
tab.addEventListener("click", () => choose(tab));
tab.addEventListener("keydown", (event) => {
const last = tabs.length - 1;
const to = { ArrowRight: index === last ? 0 : index + 1, ArrowLeft: index === 0 ? last : index - 1, Home: 0, End: last }[event.key];
if (to === undefined) return;
event.preventDefault();
tabs[to].focus();
choose(tabs[to]);
});
});
/* The mark is placed again whenever the list changes size. */
new ResizeObserver(place).observe(list);
place();
requestAnimationFrame(() => requestAnimationFrame(() => list.setAttribute("data-ready", "")));
});Tabs is not in the Figma file yet.
Use the React or HTML code for now.