Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | 16x 237x 237x 206x 1034x 466x 237x 8x 8x 8x 7x 2x 2x 1x 1x 1x 1x 1x 1x 2x 5x 5x 5x 5x 237x 807x 807x 2x 804x 804x 45x 777x 777x 2x 774x 544x 230x | import * as React from "react";
import { cn } from "../../lib/utils";
/**
* Tabs — the full WAI-ARIA Tabs pattern, same public props as before.
*
* What was missing: the triggers had `role="tab"` and `aria-selected` but no
* `aria-controls`, the panel had no `aria-labelledby`, every trigger was in the
* tab sequence (so Tab walked through all of them instead of arrowing between
* them), and the panel itself could not be focused. A screen-reader user got
* "tab, selected" with nothing connecting it to the content it switched.
*
* Now: ids wire trigger ↔ panel both ways, a roving tabindex puts exactly one
* trigger in the tab sequence, and Arrow/Home/End move between triggers with
* automatic activation (the pattern's default for tabs whose panels are cheap
* to show, and the behaviour the click-driven API already implied).
*
* Hand-rolled rather than Radix on purpose: the portal has no
* `@radix-ui/react-tabs` dependency and this phase is not allowed to add one
* (`package.json` belongs to the release, not to a primitives change). The full
* pattern is ~40 lines, so the dependency would not have paid for itself.
*
* `aria-controls` is set only on the SELECTED trigger. Inactive panels are not
* rendered — keeping them mounted would mount their children and their data
* fetches — and `aria-controls` pointing at an id that is not in the document
* is itself an accessibility failure. One attribute, present exactly when the
* thing it references is.
*/
interface TabsProps {
value: string;
onValueChange: (value: string) => void;
children: React.ReactNode;
className?: string;
}
interface TabsListProps {
children: React.ReactNode;
className?: string;
}
interface TabsTriggerProps {
value: string;
children: React.ReactNode;
className?: string;
}
interface TabsContentProps {
value: string;
children: React.ReactNode;
className?: string;
}
const TabsContext = React.createContext<{
value: string;
onValueChange: (value: string) => void;
triggerId: (value: string) => string;
panelId: (value: string) => string;
} | null>(null);
export function Tabs({ value, onValueChange, children, className }: TabsProps) {
const baseId = React.useId();
const context = React.useMemo(
() => ({
value,
onValueChange,
triggerId: (tab: string) => `${baseId}-tab-${tab}`,
panelId: (tab: string) => `${baseId}-panel-${tab}`,
}),
[baseId, value, onValueChange],
);
return (
<TabsContext.Provider value={context}>
<div className={cn("w-full", className)}>{children}</div>
</TabsContext.Provider>
);
}
/**
* Arrow / Home / End move between triggers, wrapping at both ends.
*
* The handler lives on the tablist and reads the triggers out of the DOM, so
* it needs no registry and cannot disagree with the rendered order. Activation
* is delegated to the trigger's own click handler, which keeps "what selecting
* a tab does" in exactly one place.
*/
function handleTablistKeyDown(event: React.KeyboardEvent<HTMLDivElement>) {
const tabs = Array.from(
event.currentTarget.querySelectorAll<HTMLButtonElement>(
'[role="tab"]:not([disabled])',
),
);
const current = tabs.indexOf(document.activeElement as HTMLButtonElement);
if (current === -1 || tabs.length === 0) return;
let next: number;
switch (event.key) {
case "ArrowRight":
next = (current + 1) % tabs.length;
break;
case "ArrowLeft":
next = (current - 1 + tabs.length) % tabs.length;
break;
case "Home":
next = 0;
break;
case "End":
next = tabs.length - 1;
break;
default:
return;
}
event.preventDefault();
const target = tabs[next] as HTMLButtonElement;
target.focus();
target.click();
}
export function TabsList({ children, className }: TabsListProps) {
return (
<div
role="tablist"
aria-orientation="horizontal"
onKeyDown={handleTablistKeyDown}
className={cn(
"inline-flex h-10 items-center justify-center rounded-md bg-muted p-1 text-muted-foreground overflow-x-auto",
className,
)}
>
{children}
</div>
);
}
export function TabsTrigger({ value, children, className }: TabsTriggerProps) {
const context = React.useContext(TabsContext);
if (!context) {
throw new Error("TabsTrigger must be used within Tabs");
}
const isActive = context.value === value;
return (
<button
type="button"
role="tab"
id={context.triggerId(value)}
aria-selected={isActive}
aria-controls={isActive ? context.panelId(value) : undefined}
// Roving tabindex: one stop for the whole tablist, arrows for the rest.
tabIndex={isActive ? 0 : -1}
onClick={() => context.onValueChange(value)}
className={cn(
"inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-sm font-medium ring-offset-background transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
isActive
? "bg-background text-foreground shadow-sm"
: "text-muted-foreground hover:text-foreground",
className,
)}
>
{children}
</button>
);
}
export function TabsContent({ value, children, className }: TabsContentProps) {
const context = React.useContext(TabsContext);
if (!context) {
throw new Error("TabsContent must be used within Tabs");
}
if (context.value !== value) {
return null;
}
return (
<div
role="tabpanel"
key={value}
id={context.panelId(value)}
aria-labelledby={context.triggerId(value)}
// The panel is a tab stop so the keyboard path out of the tablist
// is "arrow to the tab, Tab into its content". Required by the
// Tabs pattern and what Radix's own Content does.
// biome-ignore lint/a11y/noNoninteractiveTabindex: role="tabpanel" is the ARIA Tabs pattern's focus target; without tabIndex a panel of plain text is unreachable by keyboard.
tabIndex={0}
className={cn(
"mt-2 ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 animate-tab-in",
className,
)}
>
{children}
</div>
);
}
|