-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathTabList.stories.tsx
More file actions
500 lines (475 loc) · 16.1 KB
/
Copy pathTabList.stories.tsx
File metadata and controls
500 lines (475 loc) · 16.1 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Copyright (c) Meta Platforms, Inc. and affiliates.
import {useState} from 'react';
import type {Meta, StoryObj} from '@storybook/react';
import {TabList, Tab, TabMenu} from '@astryxdesign/core/TabList';
import {Button} from '@astryxdesign/core/Button';
import {PlusIcon, FunnelIcon} from '@heroicons/react/24/outline';
const meta: Meta<typeof TabList> = {
title: 'Core/TabList',
component: TabList,
tags: ['autodocs'],
argTypes: {
size: {
control: 'select',
options: ['sm', 'md', 'lg'],
description: 'Size of the tab hover targets',
},
},
};
export default meta;
type Story = StoryObj<typeof TabList>;
/**
* With no `role`, a strip of nothing but tabs speaks the WAI-ARIA tabs
* pattern: `role="tablist"` / `role="tab"` and `aria-selected`.
*/
export const Default: Story = {
args: {
size: 'md',
},
render: args => {
const [value, setValue] = useState('home');
return (
<TabList value={value} onChange={setValue} size={args.size}>
<Tab value="home" label="Home" />
<Tab value="projects" label="Projects" />
<Tab value="settings" label="Settings" />
</TabList>
);
},
};
/**
* A strip that really is page navigation says so with `role="navigation"`,
* and stays a `<nav>` landmark marking the current item with `aria-current`
* however few or many tabs it holds.
*/
export const NavigationLandmark: Story = {
render: () => {
const [value, setValue] = useState('overview');
return (
<TabList
value={value}
onChange={setValue}
role="navigation"
aria-label="Project sections">
<Tab value="overview" label="Overview" />
<Tab value="activity" label="Activity" />
<Tab value="members" label="Members" />
</TabList>
);
},
};
/**
* A menu is not a tab, so a strip holding one stays a `<nav>` landmark with
* `aria-current` — the tabs pattern would be invalid markup here.
*/
export const WithMenu: Story = {
args: {
size: 'md',
},
render: args => {
const [value, setValue] = useState('home');
return (
<TabList value={value} onChange={setValue} size={args.size}>
<Tab value="home" label="Home" />
<Tab value="projects" label="Projects" />
<TabMenu
label="More"
options={[
{value: 'analytics', label: 'Analytics'},
{value: 'reports', label: 'Reports'},
{value: 'billing', label: 'Billing'},
]}
/>
</TabList>
);
},
};
export const MenuWithSelectedChild: Story = {
args: {
size: 'md',
},
render: args => {
const [value, setValue] = useState('analytics');
return (
<TabList value={value} onChange={setValue} size={args.size}>
<Tab value="home" label="Home" />
<Tab value="projects" label="Projects" />
<TabMenu
label="More"
options={[
{value: 'analytics', label: 'Analytics'},
{value: 'reports', label: 'Reports'},
]}
/>
</TabList>
);
},
};
export const SizeVariants: Story = {
render: () => {
const [value, setValue] = useState('home');
return (
<div style={{display: 'flex', flexDirection: 'column', gap: '24px'}}>
{(['sm', 'md', 'lg'] as const).map(size => (
<div key={size}>
<div
style={{
marginBottom: '8px',
fontSize: '12px',
color: '#666',
fontFamily: 'monospace',
}}>
size=\"{size}\"
</div>
<div style={{border: '1px dashed #ccc', display: 'inline-flex'}}>
<TabList value={value} onChange={setValue} size={size}>
<Tab value="home" label="Home" />
<Tab value="projects" label="Projects" />
<Tab value="settings" label="Settings" />
</TabList>
</div>
</div>
))}
</div>
);
},
};
export const WithIcons: Story = {
args: {
size: 'md',
},
render: args => {
const [value, setValue] = useState('home');
const HomeIcon = (
<svg viewBox="0 0 16 16" fill="currentColor" width="100%" height="100%">
<path d="M8.543 2.232a.75.75 0 0 0-1.085 0l-5.25 5.5A.75.75 0 0 0 2.75 9H4v4a1 1 0 0 0 1 1h2a1 1 0 0 0 1-1v-2h1v2a1 1 0 0 0 1 1h2a1 1 0 0 0 1-1V9h1.25a.75.75 0 0 0 .543-1.268l-5.25-5.5Z" />
</svg>
);
const CogIcon = (
<svg viewBox="0 0 16 16" fill="currentColor" width="100%" height="100%">
<path
fillRule="evenodd"
d="M6.955 1.45A.5.5 0 0 1 7.452 1h1.096a.5.5 0 0 1 .497.45l.17 1.699c.484.12.94.312 1.356.562l1.321-.816a.5.5 0 0 1 .67.087l.774.774a.5.5 0 0 1 .087.67l-.816 1.321c.25.416.442.872.562 1.356l1.699.17a.5.5 0 0 1 .45.497v1.096a.5.5 0 0 1-.45.497l-1.699.17c-.12.484-.312.94-.562 1.356l.816 1.321a.5.5 0 0 1-.087.67l-.774.774a.5.5 0 0 1-.67.087l-1.321-.816c-.416.25-.872.442-1.356.562l-.17 1.699a.5.5 0 0 1-.497.45H7.452a.5.5 0 0 1-.497-.45l-.17-1.699a4.973 4.973 0 0 1-1.356-.562l-1.321.816a.5.5 0 0 1-.67-.087l-.774-.774a.5.5 0 0 1-.087-.67l.816-1.321a4.972 4.972 0 0 1-.562-1.356l-1.699-.17A.5.5 0 0 1 1 8.548V7.452a.5.5 0 0 1 .45-.497l1.699-.17c.12-.484.312-.94.562-1.356l-.816-1.321a.5.5 0 0 1 .087-.67l.774-.774a.5.5 0 0 1 .67-.087l1.321.816c.416-.25.872-.442 1.356-.562l.17-1.699ZM8 10.5a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5Z"
clipRule="evenodd"
/>
</svg>
);
return (
<TabList value={value} onChange={setValue} size={args.size}>
<Tab value="home" label="Home" icon={HomeIcon} />
<Tab value="settings" label="Settings" icon={CogIcon} />
</TabList>
);
},
};
export const IconOnly: Story = {
args: {
size: 'md',
},
render: args => {
const [value, setValue] = useState('desktop');
const DesktopIcon = (
<svg viewBox="0 0 16 16" fill="currentColor" width="100%" height="100%">
<path d="M2.5 3A1.5 1.5 0 0 0 1 4.5v5A1.5 1.5 0 0 0 2.5 11h4.75v1.5H5a.75.75 0 0 0 0 1.5h6a.75.75 0 0 0 0-1.5H8.75V11h4.75A1.5 1.5 0 0 0 15 9.5v-5A1.5 1.5 0 0 0 13.5 3h-11Zm0 1.5h11v5h-11v-5Z" />
</svg>
);
const PhoneIcon = (
<svg viewBox="0 0 16 16" fill="currentColor" width="100%" height="100%">
<path d="M5 1.5A1.5 1.5 0 0 0 3.5 3v10A1.5 1.5 0 0 0 5 14.5h6a1.5 1.5 0 0 0 1.5-1.5V3A1.5 1.5 0 0 0 11 1.5H5Zm0 1.5h6v10H5V3Zm2.25 8.5a.75.75 0 0 1 .75-.75h.01a.75.75 0 0 1 0 1.5H8a.75.75 0 0 1-.75-.75Z" />
</svg>
);
const ThemeIcon = (
<svg viewBox="0 0 16 16" fill="currentColor" width="100%" height="100%">
<path d="M8 1.5a6.5 6.5 0 0 0 0 13h.25a1.75 1.75 0 0 0 1.2-3.02.35.35 0 0 1 .23-.6h.97A3.85 3.85 0 0 0 14.5 7.03 5.53 5.53 0 0 0 8.97 1.5H8Zm-3 5a1 1 0 1 1 0-2 1 1 0 0 1 0 2Zm2-1.75a1 1 0 1 1 2 0 1 1 0 0 1-2 0ZM4.5 9a1 1 0 1 1 0-2 1 1 0 0 1 0 2Zm6-1.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z" />
</svg>
);
return (
<TabList value={value} onChange={setValue} size={args.size}>
<Tab
value="desktop"
label="Desktop preview"
icon={DesktopIcon}
isLabelHidden
/>
<Tab
value="phone"
label="Phone preview"
icon={PhoneIcon}
isLabelHidden
/>
<Tab value="theme" label="Theme" icon={ThemeIcon} isLabelHidden />
</TabList>
);
},
};
/**
* Demonstrates a common page header pattern: large tab list items on the left
* with action buttons on the right, separated by a full-width divider underneath.
*/
/**
* Action buttons rendered among the tabs are not tabs, so the strip keeps the
* navigation pattern. Put them outside the `TabList` if you want the tabs
* pattern as well.
*/
export const WithActions: Story = {
render: () => {
const [value, setValue] = useState('all');
return (
<TabList value={value} onChange={setValue} size="lg" hasDivider>
<Tab value="all" label="All items" />
<Tab value="active" label="Active" />
<Tab value="archived" label="Archived" />
<div
style={{
marginInlineStart: 'auto',
display: 'flex',
alignItems: 'center',
gap: '4px',
}}>
<Button
label="Filter"
variant="ghost"
size="lg"
icon={<FunnelIcon />}
isIconOnly
/>
<Button
label="New item"
variant="primary"
size="lg"
icon={<PlusIcon />}
/>
</div>
</TabList>
);
},
};
export const DividerGap: Story = {
name: 'Divider Gap (sm / md / lg)',
render: () => {
const [value, setValue] = useState('overview');
return (
<div style={{display: 'flex', flexDirection: 'column', gap: '32px'}}>
{(['sm', 'md', 'lg'] as const).map(size => (
<div
key={size}
style={{display: 'flex', flexDirection: 'column', gap: '8px'}}>
<span style={{font: '600 12px system-ui', color: '#4E606F'}}>
size="{size}" · hasDivider · matched Button size
</span>
<TabList value={value} onChange={setValue} size={size} hasDivider>
<Tab value="overview" label="Overview" />
<Tab value="activity" label="Activity" />
<Tab value="settings" label="Settings" />
<div
style={{
marginInlineStart: 'auto',
display: 'flex',
alignItems: 'center',
gap: '4px',
}}>
<Button
label="Filter"
variant="ghost"
size={size}
icon={<FunnelIcon />}
isIconOnly
/>
<Button
label="New item"
variant="primary"
size={size}
icon={<PlusIcon />}
/>
</div>
</TabList>
</div>
))}
</div>
);
},
};
export const FillLayout: Story = {
render: () => {
const [value, setValue] = useState('home');
return (
<div style={{width: '500px'}}>
<TabList value={value} onChange={setValue} layout="fill" hasDivider>
<Tab value="home" label="Home" />
<Tab value="projects" label="Projects" />
<Tab value="settings" label="Settings" />
</TabList>
</div>
);
},
};
/**
* A strip narrower than its tabs scrolls. Every tab stays a tab — nothing is
* hidden behind a menu — and the edges fade to show there is more. Pointers
* that can hover also get arrow affordances; keyboard users reach every tab
* with the arrow keys, which scrolls the focused tab into view.
*/
export const Overflow: Story = {
render: () => {
const [value, setValue] = useState('overview');
return (
<div style={{maxWidth: '400px', border: '1px dashed #ccc'}}>
<TabList value={value} onChange={setValue}>
<Tab value="overview" label="Overview" />
<Tab value="activity" label="Activity" />
<Tab value="members" label="Members" />
<Tab value="settings" label="Settings" />
<Tab value="integrations" label="Integrations" />
<Tab value="billing" label="Billing & Plans" />
<Tab value="security" label="Security" />
<Tab value="notifications" label="Notifications" />
<Tab value="api" label="API Keys" />
</TabList>
</div>
);
},
};
/**
* Overflow with divider — typical page header in a narrow viewport. The
* selected indicator still sits on the rail while the strip scrolls.
*/
export const OverflowWithDivider: Story = {
render: () => {
const [value, setValue] = useState('dashboard');
return (
<div style={{maxWidth: '350px'}}>
<TabList value={value} onChange={setValue} hasDivider size="lg">
<Tab value="dashboard" label="Dashboard" />
<Tab value="analytics" label="Analytics" />
<Tab value="reports" label="Reports" />
<Tab value="customers" label="Customers" />
<Tab value="products" label="Products" />
<Tab value="orders" label="Orders" />
</TabList>
</div>
);
},
};
/**
* A tab selected while it is out of view is scrolled back in — on mount and
* whenever the host changes `value` itself.
*/
export const OverflowSelectedOffscreen: Story = {
render: () => {
const [value, setValue] = useState('api');
return (
<div
style={{display: 'grid', gap: '8px', maxWidth: '400px', minWidth: 0}}>
<div style={{border: '1px dashed #ccc', minWidth: 0}}>
<TabList value={value} onChange={setValue}>
<Tab value="overview" label="Overview" />
<Tab value="activity" label="Activity" />
<Tab value="members" label="Members" />
<Tab value="settings" label="Settings" />
<Tab value="integrations" label="Integrations" />
<Tab value="billing" label="Billing & Plans" />
<Tab value="security" label="Security" />
<Tab value="notifications" label="Notifications" />
<Tab value="api" label="API Keys" />
</TabList>
</div>
<div style={{display: 'flex', gap: '4px'}}>
<Button
label="Select first"
variant="secondary"
size="sm"
onClick={() => setValue('overview')}
/>
<Button
label="Select last"
variant="secondary"
size="sm"
onClick={() => setValue('api')}
/>
</div>
</div>
);
},
};
/**
* `overflow="none"` opts out: the tabs keep their intrinsic widths and spill
* out of the strip, for a host that handles overflow itself.
*/
export const OverflowNone: Story = {
render: () => {
const [value, setValue] = useState('overview');
return (
<div style={{maxWidth: '400px', border: '1px dashed #ccc'}}>
<TabList value={value} onChange={setValue} overflow="none">
<Tab value="overview" label="Overview" />
<Tab value="activity" label="Activity" />
<Tab value="members" label="Members" />
<Tab value="settings" label="Settings" />
<Tab value="integrations" label="Integrations" />
<Tab value="billing" label="Billing & Plans" />
<Tab value="security" label="Security" />
<Tab value="notifications" label="Notifications" />
<Tab value="api" label="API Keys" />
</TabList>
</div>
);
},
};
/**
* `role="tablist"` asks for the WAI-ARIA tabs pattern: `role="tablist"` /
* `role="tab"`, `aria-selected`, and each tab pointing at the panel it
* controls. A screen reader announces "tab 2 of 3, selected" and can move to
* the panel it opens. A strip of nothing but tabs reaches the same pattern on
* its own — the explicit role is how you keep it when the strip also holds
* something else, and how you ask to be warned when it does.
*/
export const TabsPattern: Story = {
render: () => {
const [value, setValue] = useState('overview');
const panels = {
overview: 'Everything at a glance.',
activity: 'What happened recently.',
members: 'Who has access.',
};
return (
<div style={{display: 'grid', gap: '12px', maxWidth: '400px'}}>
<TabList
value={value}
onChange={setValue}
role="tablist"
aria-label="Project views"
hasDivider>
<Tab
value="overview"
label="Overview"
id="tab-overview"
panelId="panel-overview"
/>
<Tab
value="activity"
label="Activity"
id="tab-activity"
panelId="panel-activity"
/>
<Tab
value="members"
label="Members"
id="tab-members"
panelId="panel-members"
/>
</TabList>
{Object.entries(panels).map(([key, text]) => (
<div
key={key}
id={`panel-${key}`}
role="tabpanel"
aria-labelledby={`tab-${key}`}
tabIndex={0}
hidden={key !== value}>
{text}
</div>
))}
</div>
);
},
};