{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"css-tabs-collection-with-radio-target-and-accessible-aria-tabs-in-six-styles","type":"registry:block","title":"CSS Tabs Collection with Radio, :target and Accessible ARIA Tabs in Six Styles","description":"Free CSS tabs in six styles: radio button tabs and :target tabs with no JavaScript, plus WAI-ARIA tabs with arrow keys. Live demos and copyable HTML and CSS.","dependencies":[],"files":[{"path":"components/we-inc/css-tabs-collection-with-radio-target-and-accessible-aria-tabs-in-six-styles.tsx","type":"registry:component","content":"import { createElement, useEffect, useId, useMemo, useRef, useState } from \"react\";\nimport type { ReactNode } from \"react\";\n\n/* ---------- one tree, two outputs ----------\n  Every piece is written once as a small tree of elements. The same tree renders the live\n  preview and the HTML you copy, so the code you take is the code you saw working.\n  \"&\" in a class, id or CSS rule is replaced with the piece's own class name. */\ntype Attrs = Record<string, string | boolean | undefined>;\ntype El = { t: string; a: Attrs; k: HNode[] };\ntype HNode = string | El;\ntype Kid = HNode | (HNode | null | undefined)[] | null | false | undefined;\n\nfunction h(t: string, a: Attrs = {}, ...k: Kid[]): El {\n  const kids: HNode[] = [];\n  for (const x of k) {\n    if (x === null || x === false || x === undefined) continue;\n    if (Array.isArray(x)) kids.push(...x.filter((y) => y !== null && y !== undefined));\n    else kids.push(x);\n  }\n  return { t, a, k: kids };\n}\n\nconst sc = (s: string, name: string) => s.replace(/&/g, name);\nconst esc = (s: string) => s.replace(/</g, \"&lt;\").replace(/>/g, \"&gt;\");\nconst SVG_SELF = new Set([\"path\", \"circle\", \"line\", \"rect\", \"polyline\"]);\n\nfunction attrStr(a: Attrs, name: string): string {\n  return Object.entries(a)\n    .filter(([, v]) => v !== undefined && v !== false)\n    .map(([k, v]) => (v === true ? ` ${k}` : ` ${k}=\"${sc(String(v), name)}\"`))\n    .join(\"\");\n}\n\nfunction toHtml(n: HNode, name: string, d = 0): string {\n  const pad = \"  \".repeat(d);\n  if (typeof n === \"string\") return pad + esc(n);\n  const open = `<${n.t}${attrStr(n.a, name)}`;\n  if (SVG_SELF.has(n.t)) return `${pad}${open} />`;\n  if (n.t === \"input\") return `${pad}${open}>`;\n  if (!n.k.length) return `${pad}${open}></${n.t}>`;\n  if (n.k.every((c) => typeof c === \"string\")) return `${pad}${open}>${n.k.map((c) => esc(c as string)).join(\"\")}</${n.t}>`;\n  return `${pad}${open}>\\n${n.k.map((c) => toHtml(c, name, d + 1)).join(\"\\n\")}\\n${pad}</${n.t}>`;\n}\n\nconst RMAP: Record<string, string> = {\n  class: \"className\",\n  for: \"htmlFor\",\n  tabindex: \"tabIndex\",\n  checked: \"defaultChecked\",\n  \"stroke-width\": \"strokeWidth\",\n  \"stroke-linecap\": \"strokeLinecap\",\n  \"stroke-linejoin\": \"strokeLinejoin\",\n};\n\nfunction toReact(n: HNode, name: string, key?: number): ReactNode {\n  if (typeof n === \"string\") return n;\n  const p: Record<string, unknown> = { key };\n  for (const [k, v] of Object.entries(n.a)) {\n    if (v === undefined || v === false) continue;\n    p[RMAP[k] ?? k] = v === true ? (k.startsWith(\"aria-\") || k.startsWith(\"data-\") ? \"\" : true) : sc(String(v), name);\n  }\n  return createElement(n.t, p, ...n.k.map((c, i) => toReact(c, name, i)));\n}\n\n/* ---------- copy helpers and shared UI ---------- */\nasync function copyText(text: string): Promise<boolean> {\n  try {\n    await navigator.clipboard.writeText(text);\n    return true;\n  } catch {\n    return false;\n  }\n}\n\nconst ICON_COPY = \"M9 9h11v11H9zM5 15H4V4h11v1\";\nconst ICON_CHECK = \"M5 12.5l4.5 4.5L19 7\";\nconst ICON_CODE = \"M8 7l-5 5 5 5M16 7l5 5-5 5\";\n\nfunction Svg({ d, className = \"h-4 w-4\" }: { d: string; className?: string }) {\n  return (\n    <svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth={2} strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\" className={className}>\n      <path d={d} />\n    </svg>\n  );\n}\n\nconst FOCUS = \"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2\";\n\n/** A live piece with its own Show code and Copy buttons. */\nfunction Card({\n  id,\n  name,\n  note,\n  code,\n  copyLabel,\n  stage,\n  stageClass,\n  wide,\n  ring,\n  onAnnounce,\n}: {\n  id: string;\n  name: string;\n  note?: string;\n  code: string;\n  copyLabel: string;\n  stage: ReactNode;\n  stageClass: string;\n  wide?: boolean;\n  ring: string;\n  onAnnounce: (s: string) => void;\n}) {\n  const [open, setOpen] = useState(false);\n  const [copied, setCopied] = useState(false);\n  const timer = useRef<ReturnType<typeof setTimeout> | null>(null);\n  useEffect(() => () => {\n    if (timer.current) clearTimeout(timer.current);\n  }, []);\n  async function copy() {\n    const ok = await copyText(code);\n    if (!ok) setOpen(true);\n    setCopied(ok);\n    onAnnounce(ok ? `${name}: ${copyLabel} copied` : \"Copy was blocked, the code is shown so you can select it\");\n    if (timer.current) clearTimeout(timer.current);\n    timer.current = setTimeout(() => setCopied(false), 1600);\n  }\n  return (\n    <li className={`overflow-hidden rounded-2xl border border-stone-200 bg-white shadow-[0_1px_2px_rgba(22,24,29,0.04)] ${wide || open ? \"col-span-full\" : \"\"}`}>\n      <div className={stageClass}>{stage}</div>\n      <div className=\"flex flex-wrap items-center gap-2 border-t border-stone-200 px-3 py-2.5\">\n        <div className=\"min-w-[7.5rem] flex-1\">\n          <p className=\"text-[13px] font-semibold leading-snug text-stone-900\">{name}</p>\n          {note && <p className=\"text-[12px] leading-snug text-stone-600\">{note}</p>}\n        </div>\n        <button\n          type=\"button\"\n          aria-expanded={open}\n          aria-controls={`${id}-code`}\n          aria-label={`${open ? \"Hide\" : \"Show\"} code for ${name}`}\n          onClick={() => setOpen(!open)}\n          className={`grid h-10 w-10 shrink-0 place-items-center rounded-lg text-stone-600 transition-colors hover:bg-stone-100 hover:text-stone-900 ${FOCUS} ${ring}`}\n        >\n          <Svg d={ICON_CODE} />\n        </button>\n        <button\n          type=\"button\"\n          onClick={copy}\n          aria-label={`Copy ${copyLabel} for ${name}`}\n          className={`inline-flex h-10 shrink-0 items-center gap-1.5 rounded-lg px-3 text-[12.5px] font-semibold transition-colors ${FOCUS} ${ring} ${copied ? \"bg-emerald-700 text-white\" : \"bg-stone-100 text-stone-900 hover:bg-stone-200\"}`}\n        >\n          <Svg d={copied ? ICON_CHECK : ICON_COPY} className=\"h-3.5 w-3.5\" />\n          {copied ? \"Copied\" : \"Copy\"}\n        </button>\n      </div>\n      {open && (\n        <pre id={`${id}-code`} tabIndex={0} className=\"max-h-96 overflow-auto whitespace-pre-wrap break-words border-t border-stone-800 bg-[#111318] p-4 font-mono text-[11.5px] leading-[1.65] text-slate-200\">\n          <code>{code}</code>\n        </pre>\n      )}\n    </li>\n  );\n}\n\n/** The view switcher at the top of each collection. */\nfunction Views<T extends string>({ views, value, onChange, ring, label }: { views: [T, string, number?][]; value: T; onChange: (v: T) => void; ring: string; label: string }) {\n  return (\n    <div className=\"flex flex-wrap gap-1.5\" role=\"group\" aria-label={label}>\n      {views.map(([k, text, n]) => {\n        const on = value === k;\n        return (\n          <button\n            key={k}\n            type=\"button\"\n            aria-pressed={on}\n            onClick={() => onChange(k)}\n            className={`inline-flex min-h-[40px] items-center gap-1.5 whitespace-nowrap rounded-full px-4 text-[13px] font-medium transition-colors ${FOCUS} ${ring} ${on ? \"bg-[#16181d] text-white\" : \"bg-white text-stone-700 ring-1 ring-inset ring-stone-300 hover:bg-stone-100\"}`}\n          >\n            {text}\n            {n !== undefined && <span className={`font-mono text-[11px] ${on ? \"text-white/70\" : \"text-stone-500\"}`}>{n}</span>}\n          </button>\n        );\n      })}\n    </div>\n  );\n}\n\n/** A block of source code with one Copy button, used for the React + Tailwind versions. */\nfunction CodeBlock({ code, label, ring, onAnnounce, id }: { code: string; label: string; ring: string; onAnnounce: (s: string) => void; id: string }) {\n  const [copied, setCopied] = useState(false);\n  const [show, setShow] = useState(true);\n  async function copy() {\n    const ok = await copyText(code);\n    setCopied(ok);\n    if (!ok) setShow(true);\n    onAnnounce(ok ? `${label} copied` : \"Copy was blocked, the code is shown so you can select it\");\n    setTimeout(() => setCopied(false), 1600);\n  }\n  return (\n    <div className=\"overflow-hidden rounded-2xl border border-stone-800 bg-[#111318]\">\n      <div className=\"flex items-center justify-between gap-2 border-b border-white/10 px-3 py-2\">\n        <p className=\"font-mono text-[12px] text-slate-300\">{label}</p>\n        <div className=\"flex gap-1.5\">\n          <button type=\"button\" aria-expanded={show} aria-controls={id} onClick={() => setShow(!show)} className={`h-10 rounded-lg px-3 text-[12.5px] font-semibold text-slate-200 hover:bg-white/10 ${FOCUS} ${ring} focus-visible:ring-offset-[#111318]`}>\n            {show ? \"Hide code\" : \"Show code\"}\n          </button>\n          <button type=\"button\" onClick={copy} className={`inline-flex h-10 items-center gap-1.5 rounded-lg px-3 text-[12.5px] font-semibold ${FOCUS} ${ring} focus-visible:ring-offset-[#111318] ${copied ? \"bg-emerald-600 text-white\" : \"bg-white/10 text-white hover:bg-white/20\"}`}>\n            <Svg d={copied ? ICON_CHECK : ICON_COPY} className=\"h-3.5 w-3.5\" />\n            {copied ? \"Copied\" : \"Copy\"}\n          </button>\n        </div>\n      </div>\n      {show && (\n        <pre id={id} tabIndex={0} className=\"max-h-[520px] overflow-auto p-4 font-mono text-[11.5px] leading-[1.65] text-slate-200\">\n          <code>{code}</code>\n        </pre>\n      )}\n    </div>\n  );\n}\n\nconst SECTION_FONT = { fontFamily: 'Inter, ui-sans-serif, system-ui, -apple-system, \"Segoe UI\", sans-serif' };\n\n/* ---------- tabs ---------- */\ntype Tech = \"radio\" | \"target\" | \"aria\";\ntype Tab = { label: string; count?: string; body: HNode[] };\ntype TItem = { name: string; cls: string; note: string; tech: Tech; tabs: Tab[]; css: string; vertical?: boolean; manual?: boolean; listLabel: string };\n\nconst REDUCE = `@media (prefers-reduced-motion: reduce) {\n  .& * { transition-duration: .01ms !important; animation: none !important; }\n}`;\n\nconst FONT = `font: 15px/1.55 system-ui, sans-serif; color: #0f172a;`;\n\n/* Shared rules for the radio technique: hidden radios, label tabs, panels shown by :checked. */\nconst radioCore = (n: number) => `.&__radio { position: absolute; width: 1px; height: 1px; margin: 0; opacity: 0; pointer-events: none; }\n.&__tab { cursor: pointer; }\n.&__panel { display: none; }\n${Array.from({ length: n }, (_, i) => `.&__radio:nth-of-type(${i + 1}):checked ~ .&__panels .&__panel:nth-child(${i + 1})`).join(\",\\n\")} { display: block; }\n.&__radio:focus-visible + .&__tab { outline: 2px solid #2563eb; outline-offset: 2px; }`;\n\nconst p = (t: string) => h(\"p\", {}, t);\nconst ul = (items: string[]) => h(\"ul\", {}, items.map((t) => h(\"li\", {}, t)));\nconst code = (t: string) => h(\"pre\", {}, h(\"code\", {}, t));\n\nconst TABS: TItem[] = [\n  {\n    name: \"Radio tabs, sliding underline\",\n    cls: \"tabs-line\",\n    note: \"CSS only: radios, labels and :checked\",\n    tech: \"radio\",\n    listLabel: \"Lamp details\",\n    tabs: [\n      { label: \"Overview\", body: [p(\"The Arbor desk lamp pairs a turned ash stem with a linen shade. One dimmer ring sets the light from reading bright to a low evening glow.\"), ul([\"Warm 2700K light\", \"Weighted base, no clamp needed\"])] },\n      { label: \"Specs\", body: [ul([\"Height 46 cm, shade 28 cm across\", \"Takes one E27 bulb up to 9 W LED\", \"2 m braided cable with inline switch\"])] },\n      { label: \"Delivery\", body: [p(\"Ships in five working days in recycled card. Returns are free within 30 days if the lamp is unused and boxed.\")] },\n    ],\n    css: `.& { position: relative; display: flex; flex-wrap: wrap; ${FONT} }\n${radioCore(3)}\n.&__tab { flex: 1; padding: 12px 8px; text-align: center; font-weight: 600; color: #475569; border-bottom: 1px solid #e2e8f0; transition: color .2s; }\n.&__radio:checked + .&__tab { color: #1d4ed8; }\n.&__bar { position: absolute; top: 46px; left: 0; width: calc(100% / 3); height: 2px; background: #1d4ed8; transition: transform .3s cubic-bezier(.2, .7, .2, 1); }\n.&__radio:nth-of-type(2):checked ~ .&__bar { transform: translateX(100%); }\n.&__radio:nth-of-type(3):checked ~ .&__bar { transform: translateX(200%); }\n.&__panels { flex-basis: 100%; padding: 16px 4px 0; }\n.&__panel p { margin: 0 0 10px; }\n.&__panel ul { margin: 0; padding-left: 20px; list-style: disc; color: #334155; }\n${REDUCE}`,\n  },\n  {\n    name: \"Radio tabs, segmented pill\",\n    cls: \"tabs-pill\",\n    note: \"CSS only, a sliding pill behind the labels\",\n    tech: \"radio\",\n    listLabel: \"Report period\",\n    tabs: [\n      { label: \"Day\", body: [h(\"p\", { class: \"&__big\" }, \"1,284 visits\"), p(\"Up from 1,102 on the same weekday last week. Most arrived from the spring newsletter.\")] },\n      { label: \"Week\", body: [h(\"p\", { class: \"&__big\" }, \"8,930 visits\"), p(\"A steady week. The pricing page held its share at a little under a fifth of all views.\")] },\n      { label: \"Month\", body: [h(\"p\", { class: \"&__big\" }, \"36,410 visits\"), p(\"The best month this year, mostly from search traffic to the new guides section.\")] },\n    ],\n    css: `.& { position: relative; display: grid; grid-template-columns: repeat(3, 1fr); padding: 4px; ${FONT} }\n.&::before { content: \"\"; position: absolute; top: 0; left: 0; right: 0; height: 48px; border-radius: 999px; background: #f1f5f9; }\n${radioCore(3)}\n.&__tab { position: relative; z-index: 1; display: grid; place-items: center; height: 40px; border-radius: 999px; text-align: center; font-weight: 600; color: #475569; transition: color .2s; }\n.&__radio:checked + .&__tab { color: #0f172a; }\n.&__pill { position: absolute; top: 4px; left: 4px; width: calc((100% - 8px) / 3); height: 40px; border-radius: 999px; background: #fff; box-shadow: 0 1px 2px rgba(15, 23, 42, .12), 0 2px 8px rgba(15, 23, 42, .06); transition: transform .3s cubic-bezier(.2, .7, .2, 1); }\n.&__radio:nth-of-type(2):checked ~ .&__pill { transform: translateX(100%); }\n.&__radio:nth-of-type(3):checked ~ .&__pill { transform: translateX(200%); }\n.&__panels { grid-column: 1 / -1; padding: 18px 8px 0; }\n.&__panel p { margin: 0; color: #334155; }\n.&__panel .&__big { margin-bottom: 4px; color: #0f172a; font-size: 24px; font-weight: 700; letter-spacing: -.02em; }\n${REDUCE}`,\n  },\n  {\n    name: \"Radio tabs, vertical\",\n    cls: \"tabs-side\",\n    note: \"CSS only, sidebar that stacks when narrow\",\n    tech: \"radio\",\n    listLabel: \"Account settings\",\n    tabs: [\n      { label: \"Profile\", body: [p(\"Your name and photo appear on shared boards and in comment threads. Your email address is only shown to workspace admins.\")] },\n      { label: \"Notifications\", body: [p(\"Choose a daily summary or one email per mention. Mobile alerts follow the quiet hours set on your phone.\")] },\n      { label: \"Billing\", body: [p(\"You are on the Team plan, billed yearly. The next invoice is due on 3 March and goes to accounts@ledgerline.example.\")] },\n      { label: \"Security\", body: [p(\"Two-step sign-in is on. You signed in from two devices this week; sign either out from here.\")] },\n    ],\n    css: `.& { container-type: inline-size; ${FONT} }\n.&__grid { display: grid; grid-template-columns: 170px 1fr; column-gap: 20px; }\n${radioCore(4).replace(/\\.&__radio:nth-of-type/g, \".&__radio:nth-of-type\")}\n.&__tab { grid-column: 1; padding: 10px 12px; border-radius: 8px; font-weight: 550; color: #475569; }\n.&__tab:hover { background: #f1f5f9; }\n.&__radio:checked + .&__tab { background: #eef2ff; color: #3730a3; box-shadow: inset 3px 0 0 #4f46e5; }\n.&__panels { grid-column: 2; grid-row: 1 / span 4; padding: 6px 4px; }\n.&__panel p { margin: 0; color: #334155; }\n@container (max-width: 460px) {\n  .&__grid { grid-template-columns: 1fr; }\n  .&__panels { grid-column: 1; grid-row: auto; padding-top: 14px; }\n}\n${REDUCE}`,\n  },\n  {\n    name: \":target tabs\",\n    cls: \"tabs-target\",\n    note: \"CSS only, each tab is a link with its own URL\",\n    tech: \"target\",\n    listLabel: \"Setup steps\",\n    tabs: [\n      { label: \"Install\", body: [p(\"Add the package to your project.\"), code(\"npm install ledger-sync\")] },\n      { label: \"Configure\", body: [p(\"Create a config file at the project root.\"), code(\"ledger-sync init --region eu\")] },\n      { label: \"Deploy\", body: [p(\"Push the schema, then start the worker.\"), code(\"ledger-sync push\\nledger-sync start\")] },\n    ],\n    css: `.& { ${FONT} }\n.&__list { display: flex; gap: 4px; border-bottom: 1px solid #cbd5e1; }\n.&__tab { margin-bottom: -1px; padding: 10px 16px; border: 1px solid transparent; border-radius: 8px 8px 0 0; color: #475569; font-weight: 600; text-decoration: none; }\n.&__tab:hover { color: #0f172a; }\n.&__tab:focus-visible { outline: 2px solid #2563eb; outline-offset: 2px; }\n.&__panel { display: none; padding: 16px; border: 1px solid #cbd5e1; border-top: 0; border-radius: 0 0 10px 10px; background: #fff; scroll-margin-top: 80px; }\n.&__panel:target { display: block; }\n.&__panels:not(:has(:target)) .&__panel:first-child { display: block; }\n.&:not(:has(:target)) .&__tab:first-child,\n${[\"install\", \"configure\", \"deploy\"].map((k) => `.&:has(#&-${k}:target) .&__tab[href=\"#&-${k}\"]`).join(\",\\n\")} { border-color: #cbd5e1; border-bottom-color: #fff; background: #fff; color: #0f172a; }\n.&__panel p { margin: 0 0 10px; color: #334155; }\n.&__panel pre { margin: 0; padding: 10px 12px; border-radius: 8px; background: #0f172a; color: #e2e8f0; font: 13px/1.5 ui-monospace, monospace; overflow-x: auto; }`,\n  },\n  {\n    name: \"ARIA tabs, automatic\",\n    cls: \"tabs-aria\",\n    note: \"JavaScript: arrow keys select, one tab stop\",\n    tech: \"aria\",\n    listLabel: \"Itinerary\",\n    tabs: [\n      { label: \"Day 1\", body: [p(\"Arrive in Porto by noon. Check in at the riverside guesthouse, then a slow walk up through Ribeira to the cathedral terrace for the evening light.\")] },\n      { label: \"Day 2\", body: [p(\"Morning train up the Douro valley. Lunch at a quinta above Pinhão and a boat back down the river before dinner.\")] },\n      { label: \"Day 3\", body: [p(\"Market breakfast at Bolhão, a tile workshop in the afternoon and the late flight home.\")] },\n    ],\n    css: `.& { ${FONT} }\n.&__list { display: flex; gap: 20px; border-bottom: 1px solid #e2e8f0; }\n.&__tab { position: relative; padding: 12px 2px; border: 0; background: none; color: #475569; font: inherit; font-weight: 600; cursor: pointer; }\n.&__tab::after { content: \"\"; position: absolute; left: 0; right: 0; bottom: -1px; height: 2px; background: #0f766e; transform: scaleX(0); transition: transform .25s cubic-bezier(.2, .7, .2, 1); }\n.&__tab[aria-selected=\"true\"] { color: #0f172a; }\n.&__tab[aria-selected=\"true\"]::after { transform: none; }\n.&__tab:focus-visible { outline: 2px solid #2563eb; outline-offset: 2px; border-radius: 4px; }\n.&__panel { padding: 16px 2px 0; }\n.&__panel:focus-visible { outline: 2px solid #2563eb; outline-offset: 4px; border-radius: 4px; }\n.&__panel p { margin: 0; color: #334155; }\n${REDUCE}`,\n  },\n  {\n    name: \"ARIA tabs, vertical with counts\",\n    cls: \"tabs-queue\",\n    note: \"JavaScript: arrows move focus, Enter or Space selects\",\n    tech: \"aria\",\n    vertical: true,\n    manual: true,\n    listLabel: \"Support queue\",\n    tabs: [\n      { label: \"Open\", count: \"12\", body: [p(\"Twelve tickets wait for a first reply. The oldest came in 3 hours ago about a double charge on an annual plan.\")] },\n      { label: \"Assigned to me\", count: \"4\", body: [p(\"Four tickets are yours. Two are waiting on the customer and two need an answer today.\")] },\n      { label: \"Closed\", count: \"31\", body: [p(\"Thirty-one tickets closed this week, most within a day. Reopened tickets come back to Open.\")] },\n    ],\n    css: `.& { container-type: inline-size; ${FONT} }\n.&__wrap { display: grid; grid-template-columns: 190px 1fr; gap: 16px; }\n.&__list { display: grid; align-content: start; gap: 4px; }\n.&__tab { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 44px; padding: 0 12px; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; color: #334155; font: inherit; font-weight: 600; text-align: left; cursor: pointer; }\n.&__tab:hover { border-color: #cbd5e1; }\n.&__tab[aria-selected=\"true\"] { border-color: #0f172a; background: #0f172a; color: #fff; }\n.&__count { min-width: 26px; padding: 1px 7px; border-radius: 999px; background: #f1f5f9; color: #0f172a; font-size: 12px; text-align: center; }\n.&__tab[aria-selected=\"true\"] .&__count { background: rgba(255, 255, 255, .16); color: #fff; }\n.&__tab:focus-visible, .&__panel:focus-visible { outline: 2px solid #2563eb; outline-offset: 2px; }\n.&__panel { padding: 14px 16px; border-radius: 12px; background: #f8fafc; }\n.&__panel p { margin: 0; color: #334155; }\n@container (max-width: 460px) {\n  .&__wrap { grid-template-columns: 1fr; }\n}`,\n  },\n];\n\nconst slugOf = (t: string) => t.toLowerCase().replace(/[^a-z0-9]+/g, \"-\").replace(/-$/, \"\");\n\nfunction tabsTree(it: TItem): El {\n  const n = it.tabs.length;\n  if (it.tech === \"radio\") {\n    const pairs = it.tabs.flatMap((t, i) => [\n      h(\"input\", { class: \"&__radio\", type: \"radio\", name: \"&\", id: `&-${i + 1}`, checked: i === 0 || undefined }),\n      h(\"label\", { class: \"&__tab\", for: `&-${i + 1}` }, t.label),\n    ]);\n    const extra = it.cls === \"tabs-line\" ? h(\"span\", { class: \"&__bar\", \"aria-hidden\": \"true\" }) : it.cls === \"tabs-pill\" ? h(\"span\", { class: \"&__pill\", \"aria-hidden\": \"true\" }) : null;\n    const panels = h(\"div\", { class: \"&__panels\" }, it.tabs.map((t) => h(\"div\", { class: \"&__panel\" }, t.body)));\n    const inner = [...pairs, extra, panels];\n    return it.cls === \"tabs-side\"\n      ? h(\"div\", { class: \"&\", role: \"group\", \"aria-label\": it.listLabel }, h(\"div\", { class: \"&__grid\" }, inner))\n      : h(\"div\", { class: \"&\", role: \"group\", \"aria-label\": it.listLabel }, inner);\n  }\n  if (it.tech === \"target\") {\n    return h(\n      \"div\",\n      { class: \"&\" },\n      h(\"nav\", { class: \"&__list\", \"aria-label\": it.listLabel }, it.tabs.map((t) => h(\"a\", { class: \"&__tab\", href: `#&-${slugOf(t.label)}` }, t.label))),\n      h(\"div\", { class: \"&__panels\" }, it.tabs.map((t) => h(\"section\", { class: \"&__panel\", id: `&-${slugOf(t.label)}`, \"aria-label\": t.label }, t.body))),\n    );\n  }\n  const list = h(\n    \"div\",\n    { class: \"&__list\", role: \"tablist\", \"aria-label\": it.listLabel, \"aria-orientation\": it.vertical ? \"vertical\" : undefined },\n    it.tabs.map((t, i) =>\n      h(\n        \"button\",\n        { class: \"&__tab\", type: \"button\", role: \"tab\", id: `&-tab-${i + 1}`, \"aria-selected\": i === 0 ? \"true\" : \"false\", \"aria-controls\": `&-panel-${i + 1}`, tabindex: i === 0 ? undefined : \"-1\" },\n        t.count ? [h(\"span\", {}, t.label), h(\"span\", { class: \"&__count\" }, t.count)] : t.label,\n      ),\n    ),\n  );\n  const panels = it.tabs.map((t, i) => h(\"div\", { class: \"&__panel\", role: \"tabpanel\", id: `&-panel-${i + 1}`, \"aria-labelledby\": `&-tab-${i + 1}`, tabindex: \"0\", hidden: i !== 0 || undefined }, t.body));\n  void n;\n  return it.vertical\n    ? h(\"div\", { class: \"&\", \"data-manual\": it.manual ? \"\" : undefined }, h(\"div\", { class: \"&__wrap\" }, list, h(\"div\", {}, panels)))\n    : h(\"div\", { class: \"&\", \"data-manual\": it.manual ? \"\" : undefined }, list, panels);\n}\n\nconst TABS_JS = `  // WAI-ARIA tabs: one tab stop, arrow keys move between tabs, Home and End jump to the ends.\n  // Add data-manual to the root to make arrows only move focus; Enter or Space then selects.\n  document.querySelectorAll(\".&\").forEach(function (root) {\n    var list = root.querySelector(\"[role=tablist]\");\n    var tabs = Array.prototype.slice.call(list.querySelectorAll(\"[role=tab]\"));\n    var manual = root.hasAttribute(\"data-manual\");\n    var vertical = list.getAttribute(\"aria-orientation\") === \"vertical\";\n    function select(tab) {\n      tabs.forEach(function (t) {\n        var on = t === tab;\n        t.setAttribute(\"aria-selected\", String(on));\n        if (on) t.removeAttribute(\"tabindex\"); else t.setAttribute(\"tabindex\", \"-1\");\n        document.getElementById(t.getAttribute(\"aria-controls\")).hidden = !on;\n      });\n    }\n    tabs.forEach(function (tab, i) {\n      tab.addEventListener(\"click\", function () { select(tab); });\n      tab.addEventListener(\"keydown\", function (event) {\n        var next = vertical ? \"ArrowDown\" : \"ArrowRight\";\n        var prev = vertical ? \"ArrowUp\" : \"ArrowLeft\";\n        var target = null;\n        if (event.key === next) target = tabs[(i + 1) % tabs.length];\n        else if (event.key === prev) target = tabs[(i - 1 + tabs.length) % tabs.length];\n        else if (event.key === \"Home\") target = tabs[0];\n        else if (event.key === \"End\") target = tabs[tabs.length - 1];\n        if (!target) return;\n        event.preventDefault();\n        if (!manual) select(target);\n        target.focus();\n      });\n    });\n  });`;\n\nfunction attachTabs(root: HTMLElement): () => void {\n  const list = root.querySelector<HTMLElement>(\"[role=tablist]\");\n  if (!list) return () => undefined;\n  const tabs = Array.from(list.querySelectorAll<HTMLElement>(\"[role=tab]\"));\n  const manual = root.hasAttribute(\"data-manual\");\n  const vertical = list.getAttribute(\"aria-orientation\") === \"vertical\";\n  const select = (tab: HTMLElement) => {\n    tabs.forEach((t) => {\n      const on = t === tab;\n      t.setAttribute(\"aria-selected\", String(on));\n      if (on) t.removeAttribute(\"tabindex\");\n      else t.setAttribute(\"tabindex\", \"-1\");\n      const panel = root.querySelector<HTMLElement>(`#${t.getAttribute(\"aria-controls\")}`);\n      if (panel) panel.hidden = !on;\n    });\n  };\n  const offs = tabs.map((tab, i) => {\n    const onClick = () => select(tab);\n    const onKey = (event: KeyboardEvent) => {\n      const next = vertical ? \"ArrowDown\" : \"ArrowRight\";\n      const prev = vertical ? \"ArrowUp\" : \"ArrowLeft\";\n      let target: HTMLElement | null = null;\n      if (event.key === next) target = tabs[(i + 1) % tabs.length];\n      else if (event.key === prev) target = tabs[(i - 1 + tabs.length) % tabs.length];\n      else if (event.key === \"Home\") target = tabs[0];\n      else if (event.key === \"End\") target = tabs[tabs.length - 1];\n      if (!target) return;\n      event.preventDefault();\n      if (!manual) select(target);\n      target.focus();\n    };\n    tab.addEventListener(\"click\", onClick);\n    tab.addEventListener(\"keydown\", onKey);\n    return () => {\n      tab.removeEventListener(\"click\", onClick);\n      tab.removeEventListener(\"keydown\", onKey);\n    };\n  });\n  return () => offs.forEach((f) => f());\n}\n\nconst tabsCode = (it: TItem) =>\n  `<!-- ${it.name} -->\\n${toHtml(tabsTree(it), it.cls)}\\n\\n<style>\\n${sc(it.css, it.cls)}\\n</style>\\n` + (it.tech === \"aria\" ? `\\n<script>\\n${sc(TABS_JS, it.cls)}\\n</script>\\n` : \"\");\n\nconst TECH_LABEL: Record<Tech, string> = { radio: \"Radio\", target: \":target\", aria: \"ARIA + JS\" };\n\nconst REF: [string, string, string, string][] = [\n  [\"JavaScript\", \"None\", \"None\", \"About 30 lines\"],\n  [\"Announced as\", \"Radio buttons in a group, for example \\\"Specs, radio button, 2 of 3\\\"\", \"Links, then the section you land on\", \"Tabs, for example \\\"Day 2, tab, selected, 2 of 3\\\"\"],\n  [\"Keyboard\", \"Tab reaches the group; arrow keys move and select\", \"Tab stops on every link; Enter follows it\", \"One Tab stop; arrows, Home and End; Tab again enters the panel\"],\n  [\"Address bar\", \"Unchanged\", \"Each tab sets a #hash, adds history and can be shared\", \"Unchanged unless you add it\"],\n  [\"Watch out for\", \"Panels are not tied to their label for screen readers\", \"The page may scroll to the panel; default panel needs :has()\", \"Script must run before people use the tabs\"],\n];\n\ntype View = \"all\" | Tech | \"ref\";\n\nexport default function CssTabsCollection() {\n  const [view, setView] = useState<View>(\"all\");\n  const [announce, setAnnounce] = useState(\"\");\n  const gridRef = useRef<HTMLUListElement>(null);\n  const styleSheet = useMemo(() => TABS.map((it, i) => sc(it.css, `tbx${i}`)).join(\"\\n\"), []);\n  const codes = useMemo(() => TABS.map(tabsCode), []);\n\n  useEffect(() => {\n    const grid = gridRef.current;\n    if (!grid) return;\n    const offs: (() => void)[] = [];\n    TABS.forEach((it, i) => {\n      if (it.tech !== \"aria\") return;\n      const el = grid.querySelector<HTMLElement>(`.tbx${i}`);\n      if (el) offs.push(attachTabs(el));\n    });\n    return () => offs.forEach((f) => f());\n  }, [view]);\n\n  const ring = \"focus-visible:ring-blue-700 focus-visible:ring-offset-[#f8fafc]\";\n  const shown = TABS.map((it, i) => ({ it, i })).filter(({ it }) => view === \"all\" || view === it.tech);\n\n  return (\n    <section className=\"bg-[#f8fafc] px-4 py-12 text-slate-900 sm:px-8 sm:py-14\" style={SECTION_FONT}>\n      <style>{styleSheet}</style>\n      <div className=\"mx-auto max-w-6xl\">\n        <header className=\"max-w-3xl\">\n          <p className=\"font-mono text-[12px] uppercase tracking-[0.18em] text-slate-600\">HTML · CSS · optional JavaScript</p>\n          <h2 className=\"mt-3 text-[32px] font-semibold leading-[1.05] tracking-[-0.03em] sm:text-[44px]\">CSS tabs in three techniques</h2>\n          <p className=\"mt-4 max-w-2xl text-[15px] leading-relaxed text-slate-600\">\n            Six working tab sets. Three use hidden radio buttons, one uses links and the :target selector, and two follow the WAI-ARIA tabs pattern with a short script. Click and use the keyboard on each, then copy the HTML, CSS and any script it needs.\n          </p>\n        </header>\n\n        <div className=\"mt-7 border-y border-slate-200 py-4\">\n          <Views\n            label=\"Filter by technique\"\n            ring={ring}\n            value={view}\n            onChange={setView}\n            views={[\n              [\"all\", \"All\", TABS.length],\n              [\"radio\", \"Radio, no JS\", TABS.filter((t) => t.tech === \"radio\").length],\n              [\"target\", \":target, no JS\", 1],\n              [\"aria\", \"ARIA with JS\", TABS.filter((t) => t.tech === \"aria\").length],\n              [\"ref\", \"Compare techniques\"],\n            ]}\n          />\n        </div>\n        <p className=\"sr-only\" aria-live=\"polite\">{announce}</p>\n\n        <div className=\"mt-6 h-[820px] overflow-y-auto overscroll-contain pr-1\" tabIndex={0} aria-label=\"Tab examples\">\n          {view !== \"ref\" ? (\n            <ul ref={gridRef} className=\"grid grid-cols-1 items-start gap-3 lg:grid-cols-2\">\n              {shown.map(({ it, i }) => (\n                <Card\n                  key={it.cls}\n                  id={`tb-${i}`}\n                  name={it.name}\n                  note={`${TECH_LABEL[it.tech]} · ${it.note}`}\n                  code={codes[i]}\n                  copyLabel={it.tech === \"aria\" ? \"HTML, CSS, JS\" : \"HTML and CSS\"}\n                  ring={ring}\n                  onAnnounce={setAnnounce}\n                  stageClass=\"min-h-[250px] bg-white p-5 sm:p-6\"\n                  stage={toReact(tabsTree(it), `tbx${i}`)}\n                />\n              ))}\n            </ul>\n          ) : (\n            <div className=\"space-y-5\">\n              <div className=\"overflow-x-auto rounded-2xl border border-slate-200 bg-white\">\n                <table className=\"w-full min-w-[720px] border-collapse text-left text-[13.5px]\">\n                  <caption className=\"sr-only\">Radio, :target and ARIA tabs compared</caption>\n                  <thead className=\"bg-slate-50\">\n                    <tr>\n                      <th scope=\"col\" className=\"w-36 px-4 py-3 font-semibold\"><span className=\"sr-only\">Property</span></th>\n                      <th scope=\"col\" className=\"px-4 py-3 font-semibold\">Radio tabs</th>\n                      <th scope=\"col\" className=\"px-4 py-3 font-semibold\">:target tabs</th>\n                      <th scope=\"col\" className=\"px-4 py-3 font-semibold\">ARIA tabs</th>\n                    </tr>\n                  </thead>\n                  <tbody>\n                    {REF.map((r) => (\n                      <tr key={r[0]} className=\"border-t border-slate-200 align-top\">\n                        <th scope=\"row\" className=\"px-4 py-3 font-semibold\">{r[0]}</th>\n                        <td className=\"px-4 py-3 text-slate-700\">{r[1]}</td>\n                        <td className=\"px-4 py-3 text-slate-700\">{r[2]}</td>\n                        <td className=\"px-4 py-3 text-slate-700\">{r[3]}</td>\n                      </tr>\n                    ))}\n                  </tbody>\n                </table>\n              </div>\n              <div className=\"rounded-2xl border border-slate-200 bg-white p-5\">\n                <h3 className=\"text-[15px] font-semibold\">Which one to pick</h3>\n                <p className=\"mt-2 max-w-3xl text-[14px] leading-relaxed text-slate-600\">\n                  Reach for the ARIA version in an app, where people expect tab keyboard behaviour and screen readers should hear tabs. Radio tabs suit a static page with short, simple panels. Use :target when each panel deserves its own shareable address, such as setup steps in documentation.\n                </p>\n              </div>\n            </div>\n          )}\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
