-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathTabList.tsx
More file actions
183 lines (172 loc) · 5.57 KB
/
Copy pathTabList.tsx
File metadata and controls
183 lines (172 loc) · 5.57 KB
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
// Copyright (c) Meta Platforms, Inc. and affiliates.
'use client';
/**
* @file TabList.tsx
* @input Uses React, StyleX, TabListContext, useListFocus
* @output Exports TabList component and TabListProps type
* @position Nav wrapper; provides TabListContext to Tab and TabMenu children.
* Owns roving-tabindex keyboard navigation (Arrow/Home/End) across the tab
* strip via the shared useListFocus hook so it is a single Tab stop.
*
* SYNC: When modified, update:
* - /packages/core/src/TabList/TabList.doc.mjs
* - /packages/core/src/TabList/index.ts
* - /packages/core/src/TabList/TabList.test.tsx
* - /packages/cli/templates/blocks/components/TabList/ (showcase blocks)
*/
import React, {useMemo, type ReactNode} from 'react';
import * as stylex from '@stylexjs/stylex';
import {borderVars, colorVars, spacingVars} from '../theme/tokens.stylex';
import type {BaseProps} from '../BaseProps';
import {TabListContext} from './TabListContext';
import type {TabListOrientation, TabListSize} from './TabListContext';
import {useSize} from '../SizeContext/SizeContext';
import {mergeProps, mergeRefs} from '../utils';
import {useListFocus} from '../hooks/useListFocus';
import {EDGE_COMP_ATTR} from '../Layout/edgeCompensation.stylex';
import {themeProps} from '../utils/themeProps';
/**
* Selector matching the focusable stops in the tab strip: every Tab
* (`[data-tab-value]`) and every TabMenu trigger (`[data-tab-menu]`),
* in DOM order. Disabled stops are filtered out by the handler.
*/
const TAB_STOP_SELECTOR = '[data-tab-value],[data-tab-menu]';
export interface TabListProps extends Omit<BaseProps<HTMLElement>, 'onChange'> {
ref?: React.Ref<HTMLElement>;
/**
* The currently selected tab value.
*/
value: string;
/**
* Callback fired when a tab is selected.
*/
onChange: (value: string) => void;
/**
* Size of the tab hover targets. Uses the same element size tokens
* as Button and TextInput (`sm` = 28px, `md` = 32px, `lg` = 36px).
* @default 'md'
*/
size?: TabListSize;
/**
* Layout mode for tab sizing.
* - `'hug'` (default): each tab hugs its content width.
* - `'fill'`: tabs stretch equally to fill the container width.
* @default 'hug'
*/
layout?: 'hug' | 'fill';
/**
* Whether to show a bottom divider under the tab list.
* @default false
*/
hasDivider?: boolean;
/**
* Orientation of the tab strip, controlling which arrow keys move
* focus between tabs.
* - `'horizontal'` (default): ArrowLeft / ArrowRight (ArrowUp / ArrowDown
* also work per the WAI-ARIA APG).
* - `'vertical'`: ArrowUp / ArrowDown (ArrowLeft / ArrowRight also work).
* @default 'horizontal'
*/
orientation?: TabListOrientation;
/**
* Tab and TabMenu children.
*/
children: ReactNode;
}
const styles = stylex.create({
nav: {
display: 'flex',
alignItems: 'stretch',
gap: spacingVars['--spacing-0-5'],
maxWidth: '100%',
minWidth: 0,
},
fill: {
width: '100%',
},
divider: {
borderBottomWidth: borderVars['--border-width'],
borderBottomStyle: 'solid',
borderBottomColor: colorVars['--color-border'],
},
});
/**
* Tab navigation wrapper. Provides context for value/onChange/size
* to Tab and TabMenu children.
*
* @example
* ```
* <TabList value={activeTab} onChange={setActiveTab}>
* <Tab value="home" label="Home" />
* <Tab value="settings" label="Settings" />
* <TabMenu label="More">
* <Tab value="analytics" label="Analytics" />
* <Tab value="reports" label="Reports" />
* </TabMenu>
* </TabList>
* ```
*/
export function TabList({
ref,
value,
onChange,
size: sizeProp,
layout = 'hug',
hasDivider = false,
orientation = 'horizontal',
xstyle,
className,
style,
children,
...restProps
}: TabListProps) {
const size = useSize(sizeProp, 'md');
// Roving-tabindex keyboard navigation across the tab strip via the shared
// hook. `orientation: 'both'` accepts both arrow axes per the WAI-ARIA APG
// allowance for tab strips (ArrowRight/ArrowDown advance, ArrowLeft/ArrowUp
// retreat) regardless of the component's `orientation` prop, which only
// drives the reported `aria-orientation`.
//
// `hasRovingTabIndex` makes the hook own the single tab stop: it stamps
// tabindex 0/-1, repairs the stop on mount and as stops mount/unmount or
// toggle disabled, and — via `handleFocus` on the nav — keeps the stop in
// sync after clicks or programmatic focus. Individual Tabs still render
// `tabIndex={isSelected ? 0 : -1}` (see Tab.tsx) as the initial source of
// truth; the hook's repair preserves an existing tab stop and only promotes
// the first enabled stop when none is tabbable.
const {listRef, handleKeyDown, handleFocus} = useListFocus<HTMLElement>({
itemSelector: TAB_STOP_SELECTOR,
orientation: 'both',
hasRovingTabIndex: true,
});
const contextValue = useMemo(
() => ({value, onChange, size, layout}),
[value, onChange, size, layout],
);
return (
<TabListContext value={contextValue}>
<nav
ref={mergeRefs(ref, listRef)}
aria-label="Tabs"
aria-orientation={orientation}
onKeyDown={handleKeyDown}
onFocus={handleFocus}
{...{[EDGE_COMP_ATTR]: ''}}
{...restProps}
{...mergeProps(
themeProps('tab-list', {size}),
stylex.props(
styles.nav,
layout === 'fill' && styles.fill,
hasDivider && styles.divider,
xstyle,
),
className,
style,
)}>
{children}
</nav>
</TabListContext>
);
}
TabList.displayName = 'TabList';