@g4rcez/components 3.0.0-0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai/SKILL.md +266 -0
- package/dist/ai/docs/Alert.md +167 -0
- package/dist/ai/docs/AnimatedList.md +205 -0
- package/dist/ai/docs/Autocomplete.md +225 -0
- package/dist/ai/docs/Button.md +182 -0
- package/dist/ai/docs/Calendar.md +219 -0
- package/dist/ai/docs/Card.md +174 -0
- package/dist/ai/docs/Checkbox.md +199 -0
- package/dist/ai/docs/CommandPalette.md +293 -0
- package/dist/ai/docs/DatePicker.md +171 -0
- package/dist/ai/docs/Dropdown.md +223 -0
- package/dist/ai/docs/Empty.md +163 -0
- package/dist/ai/docs/Expand.md +143 -0
- package/dist/ai/docs/FileUpload.md +225 -0
- package/dist/ai/docs/Form.md +107 -0
- package/dist/ai/docs/FormReset.md +117 -0
- package/dist/ai/docs/Heading.md +88 -0
- package/dist/ai/docs/Input.md +237 -0
- package/dist/ai/docs/InputField.md +170 -0
- package/dist/ai/docs/List.md +205 -0
- package/dist/ai/docs/Menu.md +166 -0
- package/dist/ai/docs/Modal.md +280 -0
- package/dist/ai/docs/MultiSelect.md +196 -0
- package/dist/ai/docs/Notifications.md +231 -0
- package/dist/ai/docs/PageCalendar.md +271 -0
- package/dist/ai/docs/Polymorph.md +159 -0
- package/dist/ai/docs/Progress.md +145 -0
- package/dist/ai/docs/Radiobox.md +128 -0
- package/dist/ai/docs/RenderOnView.md +138 -0
- package/dist/ai/docs/Resizable.md +159 -0
- package/dist/ai/docs/Select.md +284 -0
- package/dist/ai/docs/Shortcut.md +105 -0
- package/dist/ai/docs/Skeleton.md +166 -0
- package/dist/ai/docs/Slider.md +144 -0
- package/dist/ai/docs/Slot.md +173 -0
- package/dist/ai/docs/Spinner.md +118 -0
- package/dist/ai/docs/Stats.md +137 -0
- package/dist/ai/docs/Step.md +159 -0
- package/dist/ai/docs/Switch.md +167 -0
- package/dist/ai/docs/Table.md +298 -0
- package/dist/ai/docs/Tabs.md +191 -0
- package/dist/ai/docs/Tag.md +224 -0
- package/dist/ai/docs/TaskList.md +144 -0
- package/dist/ai/docs/Textarea.md +167 -0
- package/dist/ai/docs/Timeline.md +210 -0
- package/dist/ai/docs/Toolbar.md +132 -0
- package/dist/ai/docs/Tooltip.md +231 -0
- package/dist/ai/docs/TransferList.md +142 -0
- package/dist/ai/docs/Typography.md +187 -0
- package/dist/ai/docs/Wizard.md +213 -0
- package/dist/ai/docs/index.md +183 -0
- package/dist/components/core/button.d.ts +2 -8
- package/dist/components/core/button.d.ts.map +1 -1
- package/dist/components/core/polymorph.d.ts.map +1 -1
- package/dist/components/core/slot.d.ts +1 -1
- package/dist/components/core/slot.d.ts.map +1 -1
- package/dist/components/core/tag.d.ts +2 -2
- package/dist/components/core/tag.d.ts.map +1 -1
- package/dist/components/core/typography.d.ts.map +1 -1
- package/dist/components/display/alert.d.ts.map +1 -1
- package/dist/components/display/calendar.d.ts.map +1 -1
- package/dist/components/display/card.d.ts.map +1 -1
- package/dist/components/display/list.d.ts.map +1 -1
- package/dist/components/display/notifications.d.ts +2 -0
- package/dist/components/display/notifications.d.ts.map +1 -1
- package/dist/components/display/progress.d.ts.map +1 -1
- package/dist/components/display/skeleton.d.ts.map +1 -1
- package/dist/components/display/step.d.ts.map +1 -1
- package/dist/components/display/tabs.d.ts.map +1 -1
- package/dist/components/floating/command-palette.d.ts +1 -0
- package/dist/components/floating/command-palette.d.ts.map +1 -1
- package/dist/components/floating/dropdown.d.ts +1 -0
- package/dist/components/floating/dropdown.d.ts.map +1 -1
- package/dist/components/floating/menu.d.ts +2 -2
- package/dist/components/floating/menu.d.ts.map +1 -1
- package/dist/components/floating/modal.d.ts +20 -53
- package/dist/components/floating/modal.d.ts.map +1 -1
- package/dist/components/floating/tooltip.d.ts.map +1 -1
- package/dist/components/floating/wizard.d.ts +1 -1
- package/dist/components/floating/wizard.d.ts.map +1 -1
- package/dist/components/form/autocomplete.d.ts.map +1 -1
- package/dist/components/form/date-picker.d.ts.map +1 -1
- package/dist/components/form/free-text.d.ts.map +1 -1
- package/dist/components/form/input-field.d.ts +3 -2
- package/dist/components/form/input-field.d.ts.map +1 -1
- package/dist/components/form/multi-select.d.ts.map +1 -1
- package/dist/components/form/select.d.ts.map +1 -1
- package/dist/components/form/slider.d.ts.map +1 -1
- package/dist/components/index.d.ts +2 -0
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/page-calendar/calendar-header.d.ts +16 -0
- package/dist/components/page-calendar/calendar-header.d.ts.map +1 -0
- package/dist/components/page-calendar/day-view.d.ts +12 -0
- package/dist/components/page-calendar/day-view.d.ts.map +1 -0
- package/dist/components/page-calendar/event-pill.d.ts +9 -0
- package/dist/components/page-calendar/event-pill.d.ts.map +1 -0
- package/dist/components/page-calendar/index.d.ts +4 -0
- package/dist/components/page-calendar/index.d.ts.map +1 -0
- package/dist/components/page-calendar/month-view.d.ts +11 -0
- package/dist/components/page-calendar/month-view.d.ts.map +1 -0
- package/dist/components/page-calendar/page-calendar.d.ts +18 -0
- package/dist/components/page-calendar/page-calendar.d.ts.map +1 -0
- package/dist/components/page-calendar/page-calendar.types.d.ts +18 -0
- package/dist/components/page-calendar/page-calendar.types.d.ts.map +1 -0
- package/dist/components/page-calendar/page-calendar.utils.d.ts +18 -0
- package/dist/components/page-calendar/page-calendar.utils.d.ts.map +1 -0
- package/dist/components/page-calendar/week-view.d.ts +11 -0
- package/dist/components/page-calendar/week-view.d.ts.map +1 -0
- package/dist/components/table/index.d.ts.map +1 -1
- package/dist/components/table/inner-table.d.ts.map +1 -1
- package/dist/components/table/metadata.d.ts.map +1 -1
- package/dist/components/table/row.d.ts.map +1 -1
- package/dist/components/table/table-lib.d.ts.map +1 -1
- package/dist/components/table/thead.d.ts.map +1 -1
- package/dist/config/context.d.ts.map +1 -1
- package/dist/config/default-translations.d.ts +21 -4
- package/dist/config/default-translations.d.ts.map +1 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/hooks/use-components-provider.d.ts.map +1 -1
- package/dist/hooks/use-form.d.ts +11 -11
- package/dist/hooks/use-form.d.ts.map +1 -1
- package/dist/hooks/use-input-id.d.ts.map +1 -1
- package/dist/hooks/use-preferences.d.ts.map +1 -1
- package/dist/hooks/use-previous.d.ts.map +1 -1
- package/dist/hooks/use-reactive.d.ts.map +1 -1
- package/dist/hooks/use-resize-observer.d.ts.map +1 -1
- package/dist/hooks/use-stable-ref.d.ts.map +1 -1
- package/dist/hooks/use-swipe.d.ts.map +1 -1
- package/dist/hooks/use-translations.d.ts +21 -4
- package/dist/hooks/use-translations.d.ts.map +1 -1
- package/dist/index.css +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +28 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +13862 -12512
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +24 -17
- package/dist/index.umd.js.map +1 -1
- package/dist/lib/dom.d.ts +1 -0
- package/dist/lib/dom.d.ts.map +1 -1
- package/dist/lib/fns.d.ts.map +1 -1
- package/dist/preset/plugin.tailwind.d.ts +9 -0
- package/dist/preset/plugin.tailwind.d.ts.map +1 -0
- package/dist/preset/plugin.tailwind.js +27 -0
- package/dist/preset/preset.tailwind.d.ts +8 -0
- package/dist/preset/preset.tailwind.d.ts.map +1 -0
- package/dist/preset/preset.tailwind.js +54 -0
- package/dist/preset/src/styles/common.d.ts +2 -14
- package/dist/preset/src/styles/common.d.ts.map +1 -1
- package/dist/preset/src/styles/common.js +1 -0
- package/dist/preset/src/styles/dark.d.ts.map +1 -1
- package/dist/preset/src/styles/dark.js +119 -114
- package/dist/preset/src/styles/light.d.ts.map +1 -1
- package/dist/preset/src/styles/light.js +111 -106
- package/dist/preset/src/styles/theme.types.d.ts +17 -8
- package/dist/preset/src/styles/theme.types.d.ts.map +1 -1
- package/dist/styles/common.d.ts +2 -14
- package/dist/styles/common.d.ts.map +1 -1
- package/dist/styles/dark.d.ts.map +1 -1
- package/dist/styles/light.d.ts.map +1 -1
- package/dist/styles/theme.types.d.ts +17 -8
- package/dist/styles/theme.types.d.ts.map +1 -1
- package/package.json +299 -301
- package/dist/components/core/button.jsx +0 -86
- package/dist/components/core/heading.jsx +0 -4
- package/dist/components/core/polymorph.jsx +0 -5
- package/dist/components/core/render-on-view.jsx +0 -31
- package/dist/components/core/resizable.jsx +0 -51
- package/dist/components/core/slot.jsx +0 -163
- package/dist/components/core/tag.jsx +0 -51
- package/dist/components/core/typography.jsx +0 -26
- package/dist/components/display/alert.jsx +0 -56
- package/dist/components/display/calendar.jsx +0 -301
- package/dist/components/display/card.jsx +0 -43
- package/dist/components/display/empty.jsx +0 -11
- package/dist/components/display/list.jsx +0 -81
- package/dist/components/display/notifications.jsx +0 -98
- package/dist/components/display/progress.jsx +0 -13
- package/dist/components/display/shortcut.jsx +0 -23
- package/dist/components/display/skeleton.jsx +0 -14
- package/dist/components/display/spinner.jsx +0 -7
- package/dist/components/display/stats.jsx +0 -20
- package/dist/components/display/step.jsx +0 -131
- package/dist/components/display/tabs.jsx +0 -100
- package/dist/components/display/timeline.jsx +0 -25
- package/dist/components/floating/command-palette.jsx +0 -172
- package/dist/components/floating/dropdown.jsx +0 -53
- package/dist/components/floating/expand.jsx +0 -44
- package/dist/components/floating/menu.jsx +0 -147
- package/dist/components/floating/modal.jsx +0 -241
- package/dist/components/floating/toolbar.jsx +0 -5
- package/dist/components/floating/tooltip.jsx +0 -64
- package/dist/components/floating/wizard.jsx +0 -164
- package/dist/components/form/autocomplete.jsx +0 -275
- package/dist/components/form/checkbox.jsx +0 -12
- package/dist/components/form/date-picker.jsx +0 -115
- package/dist/components/form/file-upload.jsx +0 -133
- package/dist/components/form/form.jsx +0 -10
- package/dist/components/form/formReset.jsx +0 -17
- package/dist/components/form/free-text.jsx +0 -41
- package/dist/components/form/input-field.jsx +0 -54
- package/dist/components/form/input.jsx +0 -36
- package/dist/components/form/multi-select.jsx +0 -328
- package/dist/components/form/radiobox.jsx +0 -6
- package/dist/components/form/select.jsx +0 -42
- package/dist/components/form/slider.jsx +0 -45
- package/dist/components/form/switch.jsx +0 -46
- package/dist/components/form/task-list.jsx +0 -26
- package/dist/components/form/textarea.jsx +0 -12
- package/dist/components/form/transfer-list.jsx +0 -39
- package/dist/components/index.js +0 -43
- package/dist/components/table/filter.jsx +0 -141
- package/dist/components/table/group.jsx +0 -68
- package/dist/components/table/index.jsx +0 -60
- package/dist/components/table/inner-table.jsx +0 -104
- package/dist/components/table/metadata.jsx +0 -37
- package/dist/components/table/pagination.jsx +0 -73
- package/dist/components/table/row.jsx +0 -58
- package/dist/components/table/sort.jsx +0 -105
- package/dist/components/table/table-lib.js +0 -84
- package/dist/components/table/table.context.jsx +0 -4
- package/dist/components/table/thead.jsx +0 -103
- package/dist/config/context.js +0 -12
- package/dist/config/default-translations.jsx +0 -66
- package/dist/config/default-tweaks.js +0 -4
- package/dist/constants.js +0 -2
- package/dist/hooks/use-click-outside.js +0 -17
- package/dist/hooks/use-color-parser.js +0 -9
- package/dist/hooks/use-components-provider.jsx +0 -16
- package/dist/hooks/use-debounce.js +0 -12
- package/dist/hooks/use-floating-ref.js +0 -6
- package/dist/hooks/use-form.js +0 -549
- package/dist/hooks/use-hover.js +0 -18
- package/dist/hooks/use-input-id.js +0 -5
- package/dist/hooks/use-is-coarse-device.js +0 -12
- package/dist/hooks/use-locale.js +0 -10
- package/dist/hooks/use-media-query.js +0 -25
- package/dist/hooks/use-on-event.js +0 -7
- package/dist/hooks/use-parent.js +0 -21
- package/dist/hooks/use-preferences.js +0 -23
- package/dist/hooks/use-previous.js +0 -8
- package/dist/hooks/use-reactive.js +0 -8
- package/dist/hooks/use-remove-scroll.js +0 -61
- package/dist/hooks/use-resize-observer.js +0 -17
- package/dist/hooks/use-stable-ref.js +0 -8
- package/dist/hooks/use-swipe.js +0 -16
- package/dist/hooks/use-translations.js +0 -9
- package/dist/hooks/use-tweaks.js +0 -9
- package/dist/hooks/use-window-size.js +0 -14
- package/dist/lib/combi-keys.js +0 -60
- package/dist/lib/dict.js +0 -39
- package/dist/lib/dom.js +0 -44
- package/dist/lib/fns.js +0 -46
- package/dist/lib/fzf.js +0 -117
- package/dist/lib/keyboard-area.js +0 -14
- package/dist/preset/tailwindcssv4.d.ts +0 -3
- package/dist/preset/tailwindcssv4.d.ts.map +0 -1
- package/dist/preset/tailwindcssv4.js +0 -75
- package/dist/styles/common.js +0 -28
- package/dist/styles/dark.js +0 -209
- package/dist/styles/design-tokens.js +0 -69
- package/dist/styles/light.js +0 -209
- package/dist/styles/theme.js +0 -4
- package/dist/styles/theme.types.js +0 -1
- package/dist/types.js +0 -1
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Switch
|
|
3
|
+
description: Toggle switch for switching between two mutually exclusive states such as On/Off.
|
|
4
|
+
package: "@g4rcez/components"
|
|
5
|
+
export: "{ Switch }"
|
|
6
|
+
import: "import { Switch } from '@g4rcez/components/switch'"
|
|
7
|
+
category: form
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# Switch
|
|
11
|
+
|
|
12
|
+
Toggle switch for switching between two mutually exclusive states such as On/Off.
|
|
13
|
+
|
|
14
|
+
## Import
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { Switch } from "@g4rcez/components/switch";
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Props
|
|
21
|
+
|
|
22
|
+
Inherits all standard HTML `input[type="checkbox"]` attributes, plus:
|
|
23
|
+
|
|
24
|
+
| Prop | Type | Default | Description |
|
|
25
|
+
|------|------|---------|-------------|
|
|
26
|
+
| `children` | `React.ReactNode` | — | Label text or element displayed next to the switch. |
|
|
27
|
+
| `onCheck` | `(nextValue: boolean) => void` | — | Called with the new boolean value after toggling. |
|
|
28
|
+
| `error` | `string` | — | Error message displayed below the switch. |
|
|
29
|
+
| `loading` | `boolean` | `false` | Disables the switch and signals a pending operation. |
|
|
30
|
+
| `container` | `string` | — | Additional CSS classes for the outer `<fieldset>`. |
|
|
31
|
+
|
|
32
|
+
## Design Tokens
|
|
33
|
+
|
|
34
|
+
Tokens this component reads. Customize by overriding these CSS variables in your theme.
|
|
35
|
+
|
|
36
|
+
| Token | CSS Variable | Purpose |
|
|
37
|
+
|-------|-------------|---------|
|
|
38
|
+
| `bg-input-switch-bg` | `--input-switch-bg` | Track background when unchecked |
|
|
39
|
+
| `bg-primary` | `--primary` | Track background when checked |
|
|
40
|
+
| `bg-disabled` | `--disabled` | Thumb fill when unchecked |
|
|
41
|
+
| `bg-input-switch` | `--input-switch` | Thumb fill when checked |
|
|
42
|
+
| `focus:ring-primary` | `--primary` | Focus ring on the toggle button |
|
|
43
|
+
| `text-danger` | `--danger` | Error message color |
|
|
44
|
+
| `text-foreground` | `--foreground` | Label text color |
|
|
45
|
+
|
|
46
|
+
## Themes
|
|
47
|
+
|
|
48
|
+
The switch appearance is driven entirely by design tokens. Override them in your `@theme` block to change the checked/unchecked colors across the application.
|
|
49
|
+
|
|
50
|
+
## Examples
|
|
51
|
+
|
|
52
|
+
### Basic toggle
|
|
53
|
+
|
|
54
|
+
```tsx
|
|
55
|
+
import { Switch } from "@g4rcez/components/switch";
|
|
56
|
+
|
|
57
|
+
export default function NotificationsToggle() {
|
|
58
|
+
return (
|
|
59
|
+
<Switch defaultChecked onChange={(e) => console.log(e.target.checked)}>
|
|
60
|
+
Enable notifications
|
|
61
|
+
</Switch>
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Controlled switch
|
|
67
|
+
|
|
68
|
+
```tsx
|
|
69
|
+
import { useState } from "react";
|
|
70
|
+
import { Switch } from "@g4rcez/components/switch";
|
|
71
|
+
|
|
72
|
+
export default function DarkModeToggle() {
|
|
73
|
+
const [enabled, setEnabled] = useState(false);
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
<Switch checked={enabled} onCheck={setEnabled}>
|
|
77
|
+
Dark mode
|
|
78
|
+
</Switch>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### With loading state
|
|
84
|
+
|
|
85
|
+
```tsx
|
|
86
|
+
import { Switch } from "@g4rcez/components/switch";
|
|
87
|
+
|
|
88
|
+
export default function SyncSwitch() {
|
|
89
|
+
return (
|
|
90
|
+
<Switch loading>
|
|
91
|
+
Syncing data…
|
|
92
|
+
</Switch>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### With error message
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
import { Switch } from "@g4rcez/components/switch";
|
|
101
|
+
|
|
102
|
+
export default function TermsSwitch() {
|
|
103
|
+
return (
|
|
104
|
+
<Switch error="You must accept the terms to continue">
|
|
105
|
+
Accept terms and conditions
|
|
106
|
+
</Switch>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### In a settings form
|
|
112
|
+
|
|
113
|
+
```tsx
|
|
114
|
+
import { Switch } from "@g4rcez/components/switch";
|
|
115
|
+
|
|
116
|
+
export default function PrivacySettings() {
|
|
117
|
+
return (
|
|
118
|
+
<div className="flex flex-col gap-base">
|
|
119
|
+
<Switch defaultChecked onCheck={(v) => updateSetting("emails", v)}>
|
|
120
|
+
Receive marketing emails
|
|
121
|
+
</Switch>
|
|
122
|
+
<Switch onCheck={(v) => updateSetting("analytics", v)}>
|
|
123
|
+
Share analytics data
|
|
124
|
+
</Switch>
|
|
125
|
+
<Switch defaultChecked onCheck={(v) => updateSetting("notifications", v)}>
|
|
126
|
+
Push notifications
|
|
127
|
+
</Switch>
|
|
128
|
+
</div>
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Do
|
|
134
|
+
|
|
135
|
+
- Use `Switch` for actions that take effect immediately without a "Save" button.
|
|
136
|
+
- Provide a clear, descriptive label via `children`.
|
|
137
|
+
- Use `onCheck` when you only need the new boolean value — it avoids parsing `e.target.checked`.
|
|
138
|
+
- Use design-token classes on wrapper elements (`bg-background`, `text-foreground`).
|
|
139
|
+
|
|
140
|
+
## Don't
|
|
141
|
+
|
|
142
|
+
- Don't use `Switch` for selecting multiple items from a list — use `Checkbox` or `MultiSelect`.
|
|
143
|
+
- Don't use `Switch` when the action involves a long or complex process without additional loading feedback.
|
|
144
|
+
- Don't pass raw Tailwind color classes (`bg-green-500`, `border-gray-300`) — use design tokens instead.
|
|
145
|
+
- Don't use arbitrary Tailwind values (`bg-[#abc]`) — override CSS variables in your `@theme` block instead.
|
|
146
|
+
|
|
147
|
+
## Accessibility
|
|
148
|
+
|
|
149
|
+
- A hidden `<input type="checkbox">` ensures correct native form submission and value reading.
|
|
150
|
+
- The visual toggle is a `<button role="switch">` with `aria-checked` reflecting the current state.
|
|
151
|
+
- `aria-labelledby` links the button to the rendered label element.
|
|
152
|
+
- Space and Enter toggle the switch via the button's `onClick` handler.
|
|
153
|
+
- When `loading` or `disabled` is true, both the hidden input and the button receive `disabled`, preventing interaction.
|
|
154
|
+
|
|
155
|
+
## Data Attributes
|
|
156
|
+
|
|
157
|
+
| Attribute | Element | Value | Description |
|
|
158
|
+
|-----------|---------|-------|-------------|
|
|
159
|
+
| `data-component` | `fieldset` | `"switch"` | Identifies the component. |
|
|
160
|
+
| `data-checked` | `input`, `button`, thumb `span` | `"true" \| "false"` | Reflects the checked state for CSS targeting. |
|
|
161
|
+
| `data-trigger` | `input` | `"change"` | Used internally to track the synthetic change event. |
|
|
162
|
+
|
|
163
|
+
## Notes
|
|
164
|
+
|
|
165
|
+
- The component manages its own `innerChecked` state and re-syncs when `props.checked` changes, supporting both controlled and semi-controlled usage patterns.
|
|
166
|
+
- A synthetic `change` event is dispatched on the hidden input after toggling, so external form libraries that listen to native events will pick up the change.
|
|
167
|
+
- Smooth track and thumb transitions use `duration-300 ease-in-out`.
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Table
|
|
3
|
+
description: Virtualized, sortable, filterable data table with column reordering, grouping, and pagination.
|
|
4
|
+
package: "@g4rcez/components"
|
|
5
|
+
export: "{ Table, createColumns, useTablePreferences, ColType }"
|
|
6
|
+
import: "import { Table, createColumns, useTablePreferences, ColType } from '@g4rcez/components/table'"
|
|
7
|
+
category: table
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# Table
|
|
11
|
+
|
|
12
|
+
Virtualized, sortable, filterable data table with column reordering, grouping, and pagination.
|
|
13
|
+
|
|
14
|
+
## Import
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { Table, createColumns, useTablePreferences, ColType } from "@g4rcez/components/table";
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Props
|
|
21
|
+
|
|
22
|
+
### Table
|
|
23
|
+
|
|
24
|
+
| Prop | Type | Default | Description |
|
|
25
|
+
|------|------|---------|-------------|
|
|
26
|
+
| `name` | `string` | — | Unique identifier used to persist user preferences in `localStorage`. |
|
|
27
|
+
| `cols` | `Col<T>[]` | — | Column definitions produced by `createColumns`. |
|
|
28
|
+
| `rows` | `T[]` | — | Data array to render. Each element maps to a table row. |
|
|
29
|
+
| `loading` | `boolean` | `false` | Replaces rows with animated skeleton cells while data loads. |
|
|
30
|
+
| `loadingMore` | `boolean` | `false` | Shows a pulse footer bar for infinite-scroll loading indicators. |
|
|
31
|
+
| `operations` | `boolean` | `true` | Toggles the metadata bar (bulk filter, sort, group controls). |
|
|
32
|
+
| `inlineFilter` | `boolean` | `true` | Enables per-column filter dropdowns in the header row. |
|
|
33
|
+
| `inlineSorter` | `boolean` | `true` | Enables per-column sort toggles in the header row. |
|
|
34
|
+
| `sticky` | `number \| null` | — | `top` offset in px for the sticky header. Pass `null` to disable stickiness. |
|
|
35
|
+
| `Aside` | `React.FC<CellAsideElement<T>>` | — | Component rendered before the first cell in every row (e.g., row-level actions). |
|
|
36
|
+
| `pagination` | `TablePagination` | — | Pagination configuration. When provided, a footer with page controls is shown. |
|
|
37
|
+
| `reference` | `keyof T` | — | Field used as the stable row key. |
|
|
38
|
+
| `useControl` | `boolean` | `false` | When `true`, disables internal filtering/sorting so you can drive it externally. |
|
|
39
|
+
| `onScrollEnd` | `() => void` | — | Called when the user scrolls to the last row (infinite scroll trigger). |
|
|
40
|
+
| `getScrollRef` | `() => HTMLElement \| undefined` | — | Returns a custom scroll container instead of `window`. |
|
|
41
|
+
| `getRowProps` | `(row: T) => ComponentProps<"tr">` | — | Merge arbitrary `<tr>` props (e.g., `onClick`, `className`) per row. |
|
|
42
|
+
| `set` | `(v: TableGetters<T>) => void` | — | External callback invoked whenever internal table state changes. |
|
|
43
|
+
|
|
44
|
+
### createColumns
|
|
45
|
+
|
|
46
|
+
```tsx
|
|
47
|
+
createColumns<T>((c) => {
|
|
48
|
+
c.add(id, thead, options?)
|
|
49
|
+
c.remove(id)
|
|
50
|
+
c.filter(predicate)
|
|
51
|
+
c.getAll()
|
|
52
|
+
})
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
| Builder method | Signature | Description |
|
|
56
|
+
|----------------|-----------|-------------|
|
|
57
|
+
| `add` | `(id: AllPaths<T>, thead: ReactNode, options?: ColOptions<T, K>) => void` | Registers a column. |
|
|
58
|
+
| `remove` | `(id: AllPaths<T>) => void` | Removes a column by id. |
|
|
59
|
+
| `filter` | `(c: (col: Col<T>) => boolean) => Col<T>[]` | Filters the column list in-place. |
|
|
60
|
+
| `getAll` | `() => Col<T>[]` | Returns a copy of all registered columns. |
|
|
61
|
+
|
|
62
|
+
### ColOptions
|
|
63
|
+
|
|
64
|
+
| Option | Type | Default | Description |
|
|
65
|
+
|--------|------|---------|-------------|
|
|
66
|
+
| `type` | `ColType` | `ColType.Text` | Data type (`Text`, `Number`, `Boolean`, `Select`). Affects filter operators. |
|
|
67
|
+
| `allowSort` | `boolean` | `true` | Whether this column can be sorted. |
|
|
68
|
+
| `allowFilter` | `boolean` | `true` | Whether this column shows a filter control. |
|
|
69
|
+
| `headerLabel` | `string` | — | Overrides the column header text used in the filter/sort metadata bar. |
|
|
70
|
+
| `Element` | `React.FC<CellPropsElement<T, K>>` | — | Custom cell renderer. Receives `{ row, value, rowIndex, matrix, col }`. |
|
|
71
|
+
| `thProps` | `HTMLAttributes<HTMLTableCellElement>` | — | Extra props forwarded to the `<th>` element. |
|
|
72
|
+
| `cellProps` | `HTMLAttributes<HTMLTableCellElement>` | — | Extra props forwarded to each `<td>` element. |
|
|
73
|
+
|
|
74
|
+
### TablePagination
|
|
75
|
+
|
|
76
|
+
| Field | Type | Description |
|
|
77
|
+
|-------|------|-------------|
|
|
78
|
+
| `size` | `number` | Current page size. |
|
|
79
|
+
| `pages` | `number` | Total number of pages. |
|
|
80
|
+
| `current` | `number` | Current page index (1-based). |
|
|
81
|
+
| `hasNext` | `boolean` | Whether a next page exists. |
|
|
82
|
+
| `hasPrevious` | `boolean` | Whether a previous page exists. |
|
|
83
|
+
| `totalItems` | `number` | Total row count across all pages. |
|
|
84
|
+
| `sizes` | `number[]` | Optional list of selectable page sizes. |
|
|
85
|
+
| `onChangeSize` | `(size: number) => void` | Called when the user selects a different page size. |
|
|
86
|
+
| `asLink` | `React.FC<{ href: number \| "previous" \| "next"; className: string }>` | Polymorphic page button — supply a router `Link` for deep-linkable pages. |
|
|
87
|
+
|
|
88
|
+
### useTablePreferences
|
|
89
|
+
|
|
90
|
+
Persists column order, active filters, sorters, and groups in `localStorage` keyed by `name`.
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
const prefs = useTablePreferences("users-table", columns, options?);
|
|
94
|
+
// Spread into <Table {...prefs} rows={data} />
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
| Parameter | Type | Description |
|
|
98
|
+
|-----------|------|-------------|
|
|
99
|
+
| `name` | `string` | Storage key (`@components/table-{name}`). |
|
|
100
|
+
| `cols` | `Col<T>[]` | Initial column definitions. |
|
|
101
|
+
| `options` | `Partial<TableGetters<T>>` | Optional initial state overrides for filters, sorters, groups, pagination. |
|
|
102
|
+
|
|
103
|
+
## Design Tokens
|
|
104
|
+
|
|
105
|
+
Tokens this component reads. Customize by overriding these CSS variables in your theme.
|
|
106
|
+
|
|
107
|
+
| Token | CSS Variable | Purpose |
|
|
108
|
+
|-------|-------------|---------|
|
|
109
|
+
| `bg-table-header` | `--table-header` | Column header background |
|
|
110
|
+
| `bg-table-background` | `--table-background` | Table body background |
|
|
111
|
+
| `border-table-border` | `--table-border` | Column separator and row divider color |
|
|
112
|
+
| `shadow-shadow-table` | `--shadow-table` | Table card shadow |
|
|
113
|
+
| `bg-card-background` | `--card-background` | Footer and loading-more bar background |
|
|
114
|
+
| `text-foreground` | `--foreground` | Default cell text color |
|
|
115
|
+
| `text-primary` | `--primary` | Active filter / sort accent color |
|
|
116
|
+
| `bg-muted` | `--muted` | Hover background for metadata bar items |
|
|
117
|
+
|
|
118
|
+
The `--table-cell-padding` CSS variable controls cell padding (default `0.75rem`). Override it via `className` on `<Table>`.
|
|
119
|
+
|
|
120
|
+
## Column Types
|
|
121
|
+
|
|
122
|
+
| `ColType` | Enum value | Filter operators available |
|
|
123
|
+
|-----------|-----------|---------------------------|
|
|
124
|
+
| `ColType.Text` | `"text"` | contains, equals, starts with, ends with |
|
|
125
|
+
| `ColType.Number` | `"number"` | =, ≠, >, <, ≥, ≤ |
|
|
126
|
+
| `ColType.Boolean` | `"boolean"` | is true, is false |
|
|
127
|
+
| `ColType.Select` | `"select"` | equals |
|
|
128
|
+
|
|
129
|
+
## Examples
|
|
130
|
+
|
|
131
|
+
### Basic table
|
|
132
|
+
|
|
133
|
+
```tsx
|
|
134
|
+
import { Table, createColumns } from "@g4rcez/components/table";
|
|
135
|
+
|
|
136
|
+
type User = { id: string; name: string; email: string };
|
|
137
|
+
|
|
138
|
+
const columns = createColumns<User>((c) => {
|
|
139
|
+
c.add("id", "ID");
|
|
140
|
+
c.add("name", "Name");
|
|
141
|
+
c.add("email", "Email");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
export function UsersTable({ users }: { users: User[] }) {
|
|
145
|
+
return (
|
|
146
|
+
<Table
|
|
147
|
+
name="users"
|
|
148
|
+
cols={columns}
|
|
149
|
+
rows={users}
|
|
150
|
+
/>
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Custom cell renderer
|
|
156
|
+
|
|
157
|
+
```tsx
|
|
158
|
+
import { Table, createColumns, ColType } from "@g4rcez/components/table";
|
|
159
|
+
import { Tag } from "@g4rcez/components/tag";
|
|
160
|
+
|
|
161
|
+
type Product = { sku: string; name: string; price: number; active: boolean };
|
|
162
|
+
|
|
163
|
+
const columns = createColumns<Product>((c) => {
|
|
164
|
+
c.add("sku", "SKU");
|
|
165
|
+
c.add("name", "Product Name", { allowFilter: true, allowSort: true });
|
|
166
|
+
c.add("price", "Price", {
|
|
167
|
+
type: ColType.Number,
|
|
168
|
+
Element: ({ value }) => (
|
|
169
|
+
<span className="font-mono tabular-nums">
|
|
170
|
+
{value.toLocaleString("en-US", { style: "currency", currency: "USD" })}
|
|
171
|
+
</span>
|
|
172
|
+
),
|
|
173
|
+
});
|
|
174
|
+
c.add("active", "Status", {
|
|
175
|
+
type: ColType.Boolean,
|
|
176
|
+
Element: ({ value }) => (
|
|
177
|
+
<Tag theme={value ? "success" : "neutral"} size="small">
|
|
178
|
+
{value ? "Active" : "Inactive"}
|
|
179
|
+
</Tag>
|
|
180
|
+
),
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Persisted preferences
|
|
186
|
+
|
|
187
|
+
```tsx
|
|
188
|
+
import { Table, createColumns, useTablePreferences } from "@g4rcez/components/table";
|
|
189
|
+
|
|
190
|
+
const baseColumns = createColumns<Order>((c) => {
|
|
191
|
+
c.add("id", "Order ID");
|
|
192
|
+
c.add("status", "Status");
|
|
193
|
+
c.add("total", "Total");
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
export function OrdersTable({ orders }: { orders: Order[] }) {
|
|
197
|
+
const prefs = useTablePreferences("orders-table", baseColumns);
|
|
198
|
+
return <Table {...prefs} rows={orders} />;
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Server-side pagination
|
|
203
|
+
|
|
204
|
+
```tsx
|
|
205
|
+
<Table
|
|
206
|
+
name="invoices"
|
|
207
|
+
cols={columns}
|
|
208
|
+
rows={pageData}
|
|
209
|
+
useControl
|
|
210
|
+
pagination={{
|
|
211
|
+
current: page,
|
|
212
|
+
pages: totalPages,
|
|
213
|
+
size: pageSize,
|
|
214
|
+
totalItems: totalCount,
|
|
215
|
+
hasNext: page < totalPages,
|
|
216
|
+
hasPrevious: page > 1,
|
|
217
|
+
sizes: [10, 25, 50],
|
|
218
|
+
onChangeSize: setPageSize,
|
|
219
|
+
asLink: ({ href, className, children }) => (
|
|
220
|
+
<a href={`?page=${href}`} className={className}>{children}</a>
|
|
221
|
+
),
|
|
222
|
+
}}
|
|
223
|
+
/>
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### Row-level actions with Aside
|
|
227
|
+
|
|
228
|
+
```tsx
|
|
229
|
+
import { TrashIcon, PencilIcon } from "lucide-react";
|
|
230
|
+
|
|
231
|
+
<Table
|
|
232
|
+
name="contacts"
|
|
233
|
+
cols={columns}
|
|
234
|
+
rows={contacts}
|
|
235
|
+
Aside={({ row }) => (
|
|
236
|
+
<div className="flex gap-1">
|
|
237
|
+
<button onClick={() => edit(row)} aria-label="Edit">
|
|
238
|
+
<PencilIcon size={14} />
|
|
239
|
+
</button>
|
|
240
|
+
<button onClick={() => remove(row)} aria-label="Delete" className="hover:text-danger">
|
|
241
|
+
<TrashIcon size={14} />
|
|
242
|
+
</button>
|
|
243
|
+
</div>
|
|
244
|
+
)}
|
|
245
|
+
/>
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Infinite scroll
|
|
249
|
+
|
|
250
|
+
```tsx
|
|
251
|
+
<Table
|
|
252
|
+
name="feed"
|
|
253
|
+
cols={columns}
|
|
254
|
+
rows={items}
|
|
255
|
+
loadingMore={isFetchingNextPage}
|
|
256
|
+
onScrollEnd={fetchNextPage}
|
|
257
|
+
/>
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## Do
|
|
261
|
+
|
|
262
|
+
- Provide a unique `name` to ensure each table has its own isolated `localStorage` slot.
|
|
263
|
+
- Use `createColumns` for full TypeScript path inference on deeply nested fields.
|
|
264
|
+
- Supply a custom `Element` for cells that need formatting, interactive controls, or icons.
|
|
265
|
+
- Use `useTablePreferences` when users should be able to persist their column arrangement across sessions.
|
|
266
|
+
- Pass `useControl={true}` and manage data externally when filtering/sorting must happen server-side.
|
|
267
|
+
- Use design-token classes for wrapper elements (`bg-table-header`, `bg-table-background`, `border-table-border`).
|
|
268
|
+
|
|
269
|
+
## Don't
|
|
270
|
+
|
|
271
|
+
- Don't pass raw Tailwind color classes (`bg-blue-500`, `text-white`, `border-gray-300`) — use design tokens instead.
|
|
272
|
+
- Don't use arbitrary Tailwind values (`bg-[#abc]`, `bg-[--my-var]`) — override CSS variables in your `@theme` block instead.
|
|
273
|
+
- Don't render `<Table>` without a `name` — preferences and DOM ids rely on it.
|
|
274
|
+
- Don't put heavy rendering logic directly inside `rows` array transformation; use `Element` cells instead so virtualization can skip off-screen rows.
|
|
275
|
+
- Don't use `Table` for single-row or trivial datasets — a plain list or card layout is more appropriate.
|
|
276
|
+
|
|
277
|
+
## Accessibility
|
|
278
|
+
|
|
279
|
+
- Renders `role="table"`, `role="rowgroup"`, `role="row"`, and `role="columnheader"` for screen-reader semantics.
|
|
280
|
+
- Sort state is exposed via `aria-sort="ascending | descending | none"` on each `<th>`.
|
|
281
|
+
- Column headers are keyboard-accessible drag targets; column resizing also supports `ArrowLeft`/`ArrowRight` keys (hold `Shift` for larger steps; double-click resets to auto width).
|
|
282
|
+
- `aria-busy` on column headers signals loading state to assistive technologies.
|
|
283
|
+
- Empty state renders the `Empty` component with a visible placeholder instead of an empty table body.
|
|
284
|
+
- Virtualization uses `react-virtuoso` with `useWindowScroll` so native keyboard scrolling and focus management continue to work.
|
|
285
|
+
|
|
286
|
+
## Data Attributes
|
|
287
|
+
|
|
288
|
+
- `data-tableheader="{colId}"` — present on every `<th>`, used by the column-resizing logic to update `style.width`.
|
|
289
|
+
- `data-resized="true"` — set on a `<th>` after the user manually resizes that column.
|
|
290
|
+
- `data-type="resizer"` — identifies the drag handle element within each header cell.
|
|
291
|
+
|
|
292
|
+
## Notes
|
|
293
|
+
|
|
294
|
+
- Column reordering uses `motion/react`'s `Reorder.Group` / `Reorder.Item`, so columns animate smoothly to their new positions.
|
|
295
|
+
- Internal filtering uses `linq-arrays`. Numeric filters operate on `Number.isNaN`-safe values; empty string filters are skipped.
|
|
296
|
+
- `useTablePreferences` merges saved columns back against the current definition so new columns added in code always appear, even if a user has a stale snapshot in storage.
|
|
297
|
+
- The `--table-cell-padding` variable can be set per breakpoint by adding it directly to `className`: `className="[--table-cell-padding:0.5rem] @md:[--table-cell-padding:1rem]"`.
|
|
298
|
+
- Pass `getScrollRef={getModalScrollerRef}` (exported from the same subpath) when embedding a `Table` inside a `Modal` to fix virtualization scroll detection.
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Tabs
|
|
3
|
+
description: Keyboard-accessible tab container for organizing content into switchable panels.
|
|
4
|
+
package: "@g4rcez/components"
|
|
5
|
+
export: "{ Tabs }"
|
|
6
|
+
import: "import { Tabs, Tab } from '@g4rcez/components/tabs'"
|
|
7
|
+
category: display
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# Tabs
|
|
11
|
+
|
|
12
|
+
Keyboard-accessible tab container for organizing content into switchable panels.
|
|
13
|
+
|
|
14
|
+
## Import
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { Tabs, Tab } from "@g4rcez/components/tabs";
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Props
|
|
21
|
+
|
|
22
|
+
### Tabs (Container)
|
|
23
|
+
|
|
24
|
+
| Prop | Type | Default | Description |
|
|
25
|
+
|------|------|---------|-------------|
|
|
26
|
+
| `active` | `string` | — | ID of the currently active tab |
|
|
27
|
+
| `onChange` | `(id: string) => void` | — | Called when the active tab changes |
|
|
28
|
+
| `container` | `string` | — | Additional classes for the outer card container |
|
|
29
|
+
| `className` | `string` | — | Additional classes for the card body (content area) |
|
|
30
|
+
| `children` | `Tab[]` | — | `Tab` panel components |
|
|
31
|
+
|
|
32
|
+
### Tab (Panel)
|
|
33
|
+
|
|
34
|
+
| Prop | Type | Default | Description |
|
|
35
|
+
|------|------|---------|-------------|
|
|
36
|
+
| `id` | `string` | — | Unique identifier; matched against `Tabs.active` |
|
|
37
|
+
| `title` | `string` | — | Tab button label (use when label is a plain string) |
|
|
38
|
+
| `label` | `string` | — | Accessible label when `title` is a non-string React element |
|
|
39
|
+
| `disabled` | `boolean` | `false` | Disables the tab button and skips it during keyboard navigation |
|
|
40
|
+
| `children` | `React.ReactNode` | — | Panel content rendered when this tab is active |
|
|
41
|
+
|
|
42
|
+
## Design Tokens
|
|
43
|
+
|
|
44
|
+
Tokens this component reads. Customize by overriding these CSS variables in your theme.
|
|
45
|
+
|
|
46
|
+
| Token | CSS Variable | Purpose |
|
|
47
|
+
|-------|-------------|---------|
|
|
48
|
+
| `bg-card-background` | `--card-background` | Tab panel surface |
|
|
49
|
+
| `border-card-border` | `--card-border` | Tab bar bottom line and card border |
|
|
50
|
+
| `border-primary` | `--primary` | Active tab bottom indicator |
|
|
51
|
+
| `text-primary` | `--primary` | Active tab text color |
|
|
52
|
+
| `text-disabled` | `--disabled` | Disabled tab text |
|
|
53
|
+
|
|
54
|
+
## Examples
|
|
55
|
+
|
|
56
|
+
### Basic Tabs
|
|
57
|
+
|
|
58
|
+
```tsx
|
|
59
|
+
const [active, setActive] = useState("overview");
|
|
60
|
+
|
|
61
|
+
<Tabs active={active} onChange={setActive}>
|
|
62
|
+
<Tab id="overview" title="Overview">
|
|
63
|
+
<h3>Project Overview</h3>
|
|
64
|
+
<p>This is the overview of your project.</p>
|
|
65
|
+
</Tab>
|
|
66
|
+
|
|
67
|
+
<Tab id="details" title="Details">
|
|
68
|
+
<h3>Project Details</h3>
|
|
69
|
+
<p>Detailed information about your project.</p>
|
|
70
|
+
</Tab>
|
|
71
|
+
|
|
72
|
+
<Tab id="settings" title="Settings">
|
|
73
|
+
<h3>Project Settings</h3>
|
|
74
|
+
<p>Configure your project settings here.</p>
|
|
75
|
+
</Tab>
|
|
76
|
+
</Tabs>
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Tabs with Disabled State
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
<Tabs active={activeTab} onChange={setActiveTab}>
|
|
83
|
+
<Tab id="available" title="Available">
|
|
84
|
+
<p>This tab is available.</p>
|
|
85
|
+
</Tab>
|
|
86
|
+
|
|
87
|
+
<Tab id="locked" title="Locked" disabled>
|
|
88
|
+
<p>This content is not accessible yet.</p>
|
|
89
|
+
</Tab>
|
|
90
|
+
|
|
91
|
+
<Tab id="also-available" title="Also Available">
|
|
92
|
+
<p>This tab is also available.</p>
|
|
93
|
+
</Tab>
|
|
94
|
+
</Tabs>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Settings Panel
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
<Tabs active={activeTab} onChange={setActiveTab}>
|
|
101
|
+
<Tab id="general" title="General">
|
|
102
|
+
<GeneralSettings />
|
|
103
|
+
</Tab>
|
|
104
|
+
<Tab id="privacy" title="Privacy">
|
|
105
|
+
<PrivacySettings />
|
|
106
|
+
</Tab>
|
|
107
|
+
<Tab id="notifications" title="Notifications">
|
|
108
|
+
<NotificationSettings />
|
|
109
|
+
</Tab>
|
|
110
|
+
</Tabs>
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Dynamic Tabs
|
|
114
|
+
|
|
115
|
+
```tsx
|
|
116
|
+
function DynamicTabs() {
|
|
117
|
+
const [tabs, setTabs] = useState([
|
|
118
|
+
{ id: "tab1", title: "Tab 1", content: "Content 1" },
|
|
119
|
+
{ id: "tab2", title: "Tab 2", content: "Content 2" },
|
|
120
|
+
]);
|
|
121
|
+
const [activeTab, setActiveTab] = useState("tab1");
|
|
122
|
+
|
|
123
|
+
const addTab = () => {
|
|
124
|
+
const newId = `tab${tabs.length + 1}`;
|
|
125
|
+
setTabs([...tabs, { id: newId, title: `Tab ${tabs.length + 1}`, content: `Content ${tabs.length + 1}` }]);
|
|
126
|
+
setActiveTab(newId);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
return (
|
|
130
|
+
<div className="space-y-4">
|
|
131
|
+
<button onClick={addTab} className="px-3 py-1 bg-primary text-primary-foreground rounded-button">
|
|
132
|
+
Add Tab
|
|
133
|
+
</button>
|
|
134
|
+
|
|
135
|
+
<Tabs active={activeTab} onChange={setActiveTab}>
|
|
136
|
+
{tabs.map((tab) => (
|
|
137
|
+
<Tab key={tab.id} id={tab.id} title={tab.title}>
|
|
138
|
+
<p>{tab.content}</p>
|
|
139
|
+
</Tab>
|
|
140
|
+
))}
|
|
141
|
+
</Tabs>
|
|
142
|
+
</div>
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Form Wizard with Disabled Steps
|
|
148
|
+
|
|
149
|
+
```tsx
|
|
150
|
+
<Tabs active={currentStep} onChange={setCurrentStep}>
|
|
151
|
+
<Tab id="step1" title="Personal Info">
|
|
152
|
+
<PersonalInfoForm />
|
|
153
|
+
</Tab>
|
|
154
|
+
<Tab id="step2" title="Address" disabled={!step1Complete}>
|
|
155
|
+
<AddressForm />
|
|
156
|
+
</Tab>
|
|
157
|
+
<Tab id="step3" title="Payment" disabled={!step2Complete}>
|
|
158
|
+
<PaymentForm />
|
|
159
|
+
</Tab>
|
|
160
|
+
</Tabs>
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Do
|
|
164
|
+
|
|
165
|
+
- Always set a default `active` tab to prevent an empty initial state.
|
|
166
|
+
- Use clear, concise tab titles (one to three words).
|
|
167
|
+
- Arrange tabs in a logical order matching the user's workflow.
|
|
168
|
+
- Use `disabled` for tabs not yet accessible (e.g. incomplete wizard steps).
|
|
169
|
+
- Use design-token classes for any custom styling (`bg-background`, `border-border`).
|
|
170
|
+
|
|
171
|
+
## Don't
|
|
172
|
+
|
|
173
|
+
- Don't pass raw Tailwind color classes (`bg-blue-50`, `border-gray-300`) — use design tokens instead.
|
|
174
|
+
- Don't use arbitrary Tailwind values (`bg-[#abc]`, `bg-[--my-var]`) — override CSS variables in your `@theme` block.
|
|
175
|
+
- Don't use more than 6–8 tabs; prefer sidebar navigation for larger sets.
|
|
176
|
+
- Don't use tabs for content that needs to be compared side-by-side.
|
|
177
|
+
- Don't use tabs for primary page-level navigation; use a nav bar instead.
|
|
178
|
+
|
|
179
|
+
## Accessibility
|
|
180
|
+
|
|
181
|
+
- Tab buttons use `aria-current="page"` and `aria-disabled` for screen reader state.
|
|
182
|
+
- Keyboard navigation: `ArrowLeft` / `ArrowRight` moves between tabs; `Tab` key focuses the header row.
|
|
183
|
+
- Disabled tabs are skipped during arrow-key navigation.
|
|
184
|
+
- Tab panels are only rendered when active (unmounted otherwise) — use this for performance but be aware that form state inside inactive panels is lost.
|
|
185
|
+
|
|
186
|
+
## Notes
|
|
187
|
+
|
|
188
|
+
- Only the active panel's `children` are rendered — inactive panels are unmounted.
|
|
189
|
+
- The tab header list is horizontally scrollable on small screens (`overflow-x-auto`).
|
|
190
|
+
- `Tabs` is built on top of `Card`, so it inherits card design tokens and the `container` / `className` props map to the card's `container` and `className` respectively.
|
|
191
|
+
- If `active` does not match any `Tab` `id`, the component selects the first available non-disabled tab automatically.
|