@ui-sdk/react 0.0.0-stage → 0.1.0-beta.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/LICENSE +202 -0
- package/README.md +10 -2
- package/dist/accordion.d.ts +331 -0
- package/dist/accordion.js +25 -0
- package/dist/agent.d.ts +2 -0
- package/dist/agent.js +8 -0
- package/dist/alert-dialog.d.ts +56 -0
- package/dist/alert-dialog.js +45 -0
- package/dist/async-options.d.ts +22 -0
- package/dist/async-options.js +56 -0
- package/dist/autocomplete.d.ts +102 -0
- package/dist/autocomplete.js +195 -0
- package/dist/avatar.d.ts +301 -0
- package/dist/avatar.js +17 -0
- package/dist/button.d.ts +21 -0
- package/dist/button.js +45 -0
- package/dist/calendar-context.d.ts +10 -0
- package/dist/calendar-context.js +9 -0
- package/dist/calendar.d.ts +80 -0
- package/dist/calendar.js +257 -0
- package/dist/carousel.d.ts +52 -0
- package/dist/carousel.js +151 -0
- package/dist/checkbox.d.ts +24 -0
- package/dist/checkbox.js +98 -0
- package/dist/children.d.ts +38 -0
- package/dist/children.js +63 -0
- package/dist/collapsible.d.ts +302 -0
- package/dist/collapsible.js +19 -0
- package/dist/color-picker.d.ts +72 -0
- package/dist/color-picker.js +133 -0
- package/dist/color.d.ts +2 -0
- package/dist/color.js +22 -0
- package/dist/combobox.d.ts +152 -0
- package/dist/combobox.js +338 -0
- package/dist/command.d.ts +88 -0
- package/dist/command.js +233 -0
- package/dist/controller-target.d.ts +3 -0
- package/dist/controller-target.js +59 -0
- package/dist/date-input.d.ts +19 -0
- package/dist/date-input.js +58 -0
- package/dist/date-picker.d.ts +47 -0
- package/dist/date-picker.js +310 -0
- package/dist/date.d.ts +2 -0
- package/dist/date.js +60 -0
- package/dist/dialog-parts.d.ts +23 -0
- package/dist/dialog-parts.js +142 -0
- package/dist/dialog.d.ts +57 -0
- package/dist/dialog.js +32 -0
- package/dist/drawer.d.ts +70 -0
- package/dist/drawer.js +158 -0
- package/dist/editor-context.d.ts +4 -0
- package/dist/editor-context.js +9 -0
- package/dist/field-context.d.ts +47 -0
- package/dist/field-context.js +87 -0
- package/dist/field.d.ts +49 -0
- package/dist/field.js +169 -0
- package/dist/file-input.d.ts +46 -0
- package/dist/file-input.js +170 -0
- package/dist/form-inputs.d.ts +5 -0
- package/dist/form-inputs.js +22 -0
- package/dist/gesture.d.ts +1 -0
- package/dist/gesture.js +16 -0
- package/dist/history.d.ts +2 -0
- package/dist/history.js +12 -0
- package/dist/hover-card.d.ts +33 -0
- package/dist/hover-card.js +84 -0
- package/dist/image-cropper.d.ts +333 -0
- package/dist/image-cropper.js +37 -0
- package/dist/image.d.ts +2 -0
- package/dist/image.js +6 -0
- package/dist/input.d.ts +22 -0
- package/dist/input.js +51 -0
- package/dist/label.d.ts +294 -0
- package/dist/label.js +9 -0
- package/dist/list-items.d.ts +15 -0
- package/dist/list-items.js +39 -0
- package/dist/list-text.d.ts +4 -0
- package/dist/list-text.js +15 -0
- package/dist/listbox.d.ts +56 -0
- package/dist/listbox.js +122 -0
- package/dist/menu.d.ts +116 -0
- package/dist/menu.js +426 -0
- package/dist/menubar-context.d.ts +4 -0
- package/dist/menubar-context.js +7 -0
- package/dist/menubar.d.ts +11 -0
- package/dist/menubar.js +32 -0
- package/dist/meter.d.ts +312 -0
- package/dist/meter.js +30 -0
- package/dist/navigation-menu.d.ts +61 -0
- package/dist/navigation-menu.js +169 -0
- package/dist/number-input.d.ts +44 -0
- package/dist/number-input.js +185 -0
- package/dist/number.d.ts +2 -0
- package/dist/number.js +40 -0
- package/dist/observe.d.ts +2 -0
- package/dist/observe.js +8 -0
- package/dist/otp-input.d.ts +29 -0
- package/dist/otp-input.js +94 -0
- package/dist/overlays.d.ts +26 -0
- package/dist/overlays.js +82 -0
- package/dist/pagination.d.ts +336 -0
- package/dist/pagination.js +71 -0
- package/dist/password-input.d.ts +23 -0
- package/dist/password-input.js +65 -0
- package/dist/popover.d.ts +54 -0
- package/dist/popover.js +151 -0
- package/dist/popup.d.ts +41 -0
- package/dist/popup.js +180 -0
- package/dist/preferences.d.ts +35 -0
- package/dist/preferences.js +93 -0
- package/dist/primitive.d.ts +2 -0
- package/dist/primitive.js +14 -0
- package/dist/progress.d.ts +312 -0
- package/dist/progress.js +30 -0
- package/dist/radio.d.ts +22 -0
- package/dist/radio.js +86 -0
- package/dist/rating.d.ts +20 -0
- package/dist/rating.js +94 -0
- package/dist/render-element.d.ts +35 -0
- package/dist/render-element.js +103 -0
- package/dist/resizable.d.ts +302 -0
- package/dist/resizable.js +23 -0
- package/dist/scroll-area.d.ts +324 -0
- package/dist/scroll-area.js +49 -0
- package/dist/segmented-control.d.ts +24 -0
- package/dist/segmented-control.js +78 -0
- package/dist/segmented-input.d.ts +15 -0
- package/dist/segmented-input.js +48 -0
- package/dist/select.d.ts +125 -0
- package/dist/select.js +330 -0
- package/dist/separator.d.ts +290 -0
- package/dist/separator.js +9 -0
- package/dist/shortcuts.d.ts +32 -0
- package/dist/shortcuts.js +61 -0
- package/dist/slider.d.ts +38 -0
- package/dist/slider.js +163 -0
- package/dist/switch.d.ts +15 -0
- package/dist/switch.js +39 -0
- package/dist/tabs.d.ts +331 -0
- package/dist/tabs.js +28 -0
- package/dist/text-control.d.ts +22 -0
- package/dist/text-control.js +24 -0
- package/dist/text-match.d.ts +2 -0
- package/dist/text-match.js +8 -0
- package/dist/textarea.d.ts +10 -0
- package/dist/textarea.js +48 -0
- package/dist/time-input.d.ts +19 -0
- package/dist/time-input.js +63 -0
- package/dist/timeline.d.ts +327 -0
- package/dist/timeline.js +31 -0
- package/dist/toast-parts.d.ts +63 -0
- package/dist/toast-parts.js +144 -0
- package/dist/toast.d.ts +2 -0
- package/dist/toast.js +22 -0
- package/dist/toggle.d.ts +23 -0
- package/dist/toggle.js +76 -0
- package/dist/token-input.d.ts +35 -0
- package/dist/token-input.js +100 -0
- package/dist/toolbar-context.d.ts +6 -0
- package/dist/toolbar-context.js +16 -0
- package/dist/toolbar.d.ts +38 -0
- package/dist/toolbar.js +63 -0
- package/dist/tooltip.d.ts +39 -0
- package/dist/tooltip.js +108 -0
- package/dist/tree.d.ts +51 -0
- package/dist/tree.js +150 -0
- package/dist/trigger-menu.d.ts +83 -0
- package/dist/trigger-menu.js +167 -0
- package/dist/use-primitive.d.ts +178 -0
- package/dist/use-primitive.js +674 -0
- package/dist/waterfall.d.ts +80 -0
- package/dist/waterfall.js +108 -0
- package/docs/README.md +89 -0
- package/docs/agents.md +1523 -0
- package/docs/components/accordion.md +1391 -0
- package/docs/components/alert-dialog.md +850 -0
- package/docs/components/autocomplete.md +1963 -0
- package/docs/components/avatar.md +252 -0
- package/docs/components/button.md +444 -0
- package/docs/components/calendar.md +1687 -0
- package/docs/components/carousel.md +1244 -0
- package/docs/components/checkbox.md +1069 -0
- package/docs/components/collapsible.md +567 -0
- package/docs/components/color-picker.md +736 -0
- package/docs/components/combobox.md +3647 -0
- package/docs/components/command.md +1573 -0
- package/docs/components/date-input.md +608 -0
- package/docs/components/date-picker.md +1266 -0
- package/docs/components/dialog.md +1469 -0
- package/docs/components/drawer.md +1197 -0
- package/docs/components/field.md +549 -0
- package/docs/components/file-input.md +609 -0
- package/docs/components/hover-card.md +514 -0
- package/docs/components/image-cropper.md +579 -0
- package/docs/components/input.md +301 -0
- package/docs/components/label.md +217 -0
- package/docs/components/listbox.md +1130 -0
- package/docs/components/menu.md +1286 -0
- package/docs/components/menubar.md +455 -0
- package/docs/components/meter.md +304 -0
- package/docs/components/navigation-menu.md +1581 -0
- package/docs/components/number-input.md +1086 -0
- package/docs/components/otp-input.md +565 -0
- package/docs/components/pagination.md +648 -0
- package/docs/components/password-input.md +737 -0
- package/docs/components/popover.md +1552 -0
- package/docs/components/preferences.md +220 -0
- package/docs/components/progress.md +443 -0
- package/docs/components/radio.md +846 -0
- package/docs/components/rating.md +458 -0
- package/docs/components/resizable.md +982 -0
- package/docs/components/scroll-area.md +980 -0
- package/docs/components/segmented-control.md +465 -0
- package/docs/components/select.md +2133 -0
- package/docs/components/separator.md +243 -0
- package/docs/components/slider.md +1169 -0
- package/docs/components/switch.md +613 -0
- package/docs/components/tabs.md +1014 -0
- package/docs/components/textarea.md +318 -0
- package/docs/components/time-input.md +231 -0
- package/docs/components/timeline.md +677 -0
- package/docs/components/toast.md +1395 -0
- package/docs/components/toggle.md +541 -0
- package/docs/components/token-input.md +1007 -0
- package/docs/components/toolbar.md +787 -0
- package/docs/components/tooltip.md +884 -0
- package/docs/components/tree.md +1346 -0
- package/docs/components/trigger-menu.md +1257 -0
- package/docs/components/waterfall.md +754 -0
- package/docs/controllers.md +89 -0
- package/docs/core.md +8027 -0
- package/docs/custom-parts.md +120 -0
- package/docs/first-component.md +83 -0
- package/docs/forms.md +148 -0
- package/docs/hooks/async-options.md +366 -0
- package/docs/hooks/shortcuts.md +221 -0
- package/docs/installation.md +45 -0
- package/docs/introduction.md +32 -0
- package/docs/lists-and-menus.md +170 -0
- package/docs/llms.txt +1844 -0
- package/docs/parts-and-composition.md +93 -0
- package/docs/popups.md +117 -0
- package/docs/rendering.md +121 -0
- package/docs/server-rendering.md +253 -0
- package/docs/state-and-events.md +105 -0
- package/docs/styling-and-animation.md +176 -0
- package/package.json +333 -3
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
# Toggle
|
|
2
|
+
|
|
3
|
+
Keep a button pressed, or group toggles with a shared value and arrow-key navigation.
|
|
4
|
+
|
|
5
|
+
Use a toggle for a button that stays on until you press it again, like starring a document or turning on Bold. Put several in a toggle group to share one value, as a text editor's formatting buttons or a view switcher do.
|
|
6
|
+
|
|
7
|
+
```tsx title="basic.tsx"
|
|
8
|
+
import { Toggle, ToggleIcon } from "@ui-sdk/react/toggle"
|
|
9
|
+
import { StarIcon } from "lucide-react"
|
|
10
|
+
|
|
11
|
+
function Example() {
|
|
12
|
+
return (
|
|
13
|
+
<div data-slot="card">
|
|
14
|
+
<div data-slot="stack">
|
|
15
|
+
<h3>Q3 Roadmap</h3>
|
|
16
|
+
<p data-slot="hint">Edited by Hollis Grant</p>
|
|
17
|
+
</div>
|
|
18
|
+
<Toggle aria-label="Star">
|
|
19
|
+
<ToggleIcon data-variant="fill">
|
|
20
|
+
<StarIcon />
|
|
21
|
+
</ToggleIcon>
|
|
22
|
+
</Toggle>
|
|
23
|
+
</div>
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { Example }
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Press the star beside Q3 Roadmap, or focus it and press Enter or Space. It fills in while pressed, and its accessible name stays Star.
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install @ui-sdk/react @ui-sdk/core
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Composition
|
|
39
|
+
|
|
40
|
+
Use the following composition to build a `Toggle`:
|
|
41
|
+
|
|
42
|
+
```text
|
|
43
|
+
Toggle
|
|
44
|
+
└── ToggleIcon
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
And a `ToggleGroup`:
|
|
48
|
+
|
|
49
|
+
```text
|
|
50
|
+
ToggleGroup
|
|
51
|
+
├── Toggle
|
|
52
|
+
│ └── ToggleIcon
|
|
53
|
+
└── Toggle
|
|
54
|
+
└── ToggleIcon
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`Toggle` renders a native button that reports its state with `aria-pressed`. `ToggleIcon` is optional: it holds an icon and hides it from screen readers. In a group, give each toggle a `value`; the group's value lists the pressed ones.
|
|
58
|
+
|
|
59
|
+
## Usage
|
|
60
|
+
|
|
61
|
+
Import the parts and put them together:
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { Toggle, ToggleIcon } from "@ui-sdk/react/toggle"
|
|
65
|
+
import { StarIcon } from "lucide-react"
|
|
66
|
+
|
|
67
|
+
function Example() {
|
|
68
|
+
return (
|
|
69
|
+
<Toggle aria-label="Star">
|
|
70
|
+
<ToggleIcon>
|
|
71
|
+
<StarIcon />
|
|
72
|
+
</ToggleIcon>
|
|
73
|
+
</Toggle>
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { Example }
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Keep the label the same in both states: screen readers announce whether the button is pressed. An icon-only toggle needs an `aria-label`, as above. Set `defaultPressed` to start pressed. The toggle always uses `type="button"`, so pressing it never submits a form.
|
|
81
|
+
|
|
82
|
+
## Group
|
|
83
|
+
|
|
84
|
+
Put toggles in a `ToggleGroup`, from the same `@ui-sdk/react/toggle` entry, to share a value and arrow-key navigation. Give the group an accessible name and each toggle a `value`. Set `multiple` to let several toggles stay pressed.
|
|
85
|
+
|
|
86
|
+
```tsx title="group.tsx"
|
|
87
|
+
import { Toggle, ToggleGroup, ToggleIcon } from "@ui-sdk/react/toggle"
|
|
88
|
+
import { BoldIcon, ItalicIcon, UnderlineIcon } from "lucide-react"
|
|
89
|
+
|
|
90
|
+
function Example() {
|
|
91
|
+
return (
|
|
92
|
+
<ToggleGroup multiple aria-label="Text formatting">
|
|
93
|
+
<Toggle value="bold" aria-label="Bold">
|
|
94
|
+
<ToggleIcon>
|
|
95
|
+
<BoldIcon />
|
|
96
|
+
</ToggleIcon>
|
|
97
|
+
</Toggle>
|
|
98
|
+
<Toggle value="italic" aria-label="Italic">
|
|
99
|
+
<ToggleIcon>
|
|
100
|
+
<ItalicIcon />
|
|
101
|
+
</ToggleIcon>
|
|
102
|
+
</Toggle>
|
|
103
|
+
<Toggle value="underline" aria-label="Underline">
|
|
104
|
+
<ToggleIcon>
|
|
105
|
+
<UnderlineIcon />
|
|
106
|
+
</ToggleIcon>
|
|
107
|
+
</Toggle>
|
|
108
|
+
</ToggleGroup>
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export { Example }
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Press Bold and Italic to keep both pressed. Left and Right move focus without changing either.
|
|
116
|
+
|
|
117
|
+
The group owns which toggles are pressed, so a toggle's own `pressed` and `defaultPressed` don't apply inside it. Its value is always an array, in the toggles' document order.
|
|
118
|
+
|
|
119
|
+
## Single Selection
|
|
120
|
+
|
|
121
|
+
Without `multiple`, pressing a toggle unpresses the others. Pressing the pressed toggle again clears the selection, unless you set `deselectable` to `false`, as this alignment group does. `deselectable` doesn't choose an initial value for you, so pair it with `defaultValue`.
|
|
122
|
+
|
|
123
|
+
```tsx title="group-single.tsx"
|
|
124
|
+
import { Toggle, ToggleGroup, ToggleIcon } from "@ui-sdk/react/toggle"
|
|
125
|
+
import {
|
|
126
|
+
TextAlignCenterIcon,
|
|
127
|
+
TextAlignEndIcon,
|
|
128
|
+
TextAlignStartIcon,
|
|
129
|
+
} from "lucide-react"
|
|
130
|
+
|
|
131
|
+
const alignments = [
|
|
132
|
+
{
|
|
133
|
+
value: "start",
|
|
134
|
+
label: "Align start",
|
|
135
|
+
icon: TextAlignStartIcon,
|
|
136
|
+
mirror: true,
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
value: "center",
|
|
140
|
+
label: "Align center",
|
|
141
|
+
icon: TextAlignCenterIcon,
|
|
142
|
+
mirror: false,
|
|
143
|
+
},
|
|
144
|
+
{ value: "end", label: "Align end", icon: TextAlignEndIcon, mirror: true },
|
|
145
|
+
]
|
|
146
|
+
|
|
147
|
+
function Example() {
|
|
148
|
+
return (
|
|
149
|
+
<ToggleGroup defaultValue={["start"]} deselectable={false} aria-label="Text alignment">
|
|
150
|
+
{alignments.map((item) => (
|
|
151
|
+
<Toggle key={item.value} value={item.value} aria-label={item.label}>
|
|
152
|
+
<ToggleIcon mirror={item.mirror}>
|
|
153
|
+
<item.icon />
|
|
154
|
+
</ToggleIcon>
|
|
155
|
+
</Toggle>
|
|
156
|
+
))}
|
|
157
|
+
</ToggleGroup>
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export { Example }
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Press Align center to replace Align start, then press Align center again. One alignment always stays pressed.
|
|
165
|
+
|
|
166
|
+
`mirror` on Align start and Align end flips their icons in right-to-left text. Use [Radio](./radio.md) instead when the arrow keys should select an option as they move; a toggle group moves focus first and waits for a press.
|
|
167
|
+
|
|
168
|
+
## Icon and Label
|
|
169
|
+
|
|
170
|
+
Put an icon beside the label with `ToggleIcon`. The icon is hidden from screen readers, so the text alone names the button.
|
|
171
|
+
|
|
172
|
+
```tsx
|
|
173
|
+
<Toggle>
|
|
174
|
+
<ToggleIcon>
|
|
175
|
+
<PinIcon />
|
|
176
|
+
</ToggleIcon>
|
|
177
|
+
Pin
|
|
178
|
+
</Toggle>
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Disabled
|
|
182
|
+
|
|
183
|
+
Set `disabled` on a toggle or a group to prevent presses. A disabled toggle can still start pressed, and a disabled `<fieldset>` around it disables it too.
|
|
184
|
+
|
|
185
|
+
```tsx
|
|
186
|
+
<Toggle disabled defaultPressed>
|
|
187
|
+
Pin
|
|
188
|
+
</Toggle>
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
A disabled toggle on its own leaves the tab order. In a group, disabled toggles stay reachable with the arrow keys and report `aria-disabled`, but ignore presses.
|
|
192
|
+
|
|
193
|
+
```tsx
|
|
194
|
+
<ToggleGroup multiple aria-label="Text formatting">
|
|
195
|
+
<Toggle value="bold">Bold</Toggle>
|
|
196
|
+
<Toggle value="italic">Italic</Toggle>
|
|
197
|
+
<Toggle value="underline" disabled>
|
|
198
|
+
Underline
|
|
199
|
+
</Toggle>
|
|
200
|
+
</ToggleGroup>
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Focusable When Disabled
|
|
204
|
+
|
|
205
|
+
Set `focusableWhenDisabled` to keep a disabled toggle focusable. It uses `aria-disabled` and still ignores presses. Use `aria-describedby` to explain why it's unavailable:
|
|
206
|
+
|
|
207
|
+
```tsx
|
|
208
|
+
<Toggle disabled focusableWhenDisabled aria-describedby="pin-hint">
|
|
209
|
+
Pin
|
|
210
|
+
</Toggle>
|
|
211
|
+
<p id="pin-hint">Only owners can pin messages.</p>
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Controlled
|
|
215
|
+
|
|
216
|
+
Pass `pressed` and update it in `onPressedChange`. Call `details.cancel()` in the callback to keep the current state. A [controller](#toggle-controller) from `createToggleController()` presses the toggle from anywhere, such as a keyboard shortcut. Create one controller per mounted toggle.
|
|
217
|
+
|
|
218
|
+
```tsx
|
|
219
|
+
"use client"
|
|
220
|
+
|
|
221
|
+
import * as React from "react"
|
|
222
|
+
import {
|
|
223
|
+
createToggleController,
|
|
224
|
+
Toggle,
|
|
225
|
+
ToggleIcon,
|
|
226
|
+
} from "@ui-sdk/react/toggle"
|
|
227
|
+
import { StarIcon } from "lucide-react"
|
|
228
|
+
|
|
229
|
+
function StarButton() {
|
|
230
|
+
const [starred, setStarred] = React.useState(false)
|
|
231
|
+
const [star] = React.useState(createToggleController)
|
|
232
|
+
|
|
233
|
+
return (
|
|
234
|
+
<>
|
|
235
|
+
<Toggle
|
|
236
|
+
aria-label="Star"
|
|
237
|
+
pressed={starred}
|
|
238
|
+
onPressedChange={setStarred}
|
|
239
|
+
controller={star}
|
|
240
|
+
>
|
|
241
|
+
<ToggleIcon>
|
|
242
|
+
<StarIcon />
|
|
243
|
+
</ToggleIcon>
|
|
244
|
+
</Toggle>
|
|
245
|
+
<button type="button" onClick={() => star.toggle()}>
|
|
246
|
+
Toggle Star
|
|
247
|
+
</button>
|
|
248
|
+
</>
|
|
249
|
+
)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export { StarButton }
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
The callback receives the new state and `details`, whose `reason` is `"press"` or `"imperative"`.
|
|
256
|
+
|
|
257
|
+
## Controlled Group
|
|
258
|
+
|
|
259
|
+
Pass `value` and update it in `onValueChange` to own the group's value, or to show it somewhere else. A toggle's own `onPressedChange` runs before the group's, and calling `details.cancel()` in either cancels the change. A [controller](#togglegroup-controller) from `createToggleGroupController()` changes the value from anywhere: `setValue([])` clears it.
|
|
260
|
+
|
|
261
|
+
```tsx
|
|
262
|
+
"use client"
|
|
263
|
+
|
|
264
|
+
import * as React from "react"
|
|
265
|
+
import {
|
|
266
|
+
createToggleGroupController,
|
|
267
|
+
Toggle,
|
|
268
|
+
ToggleGroup,
|
|
269
|
+
} from "@ui-sdk/react/toggle"
|
|
270
|
+
|
|
271
|
+
function Formatting() {
|
|
272
|
+
const [formats, setFormats] = React.useState(["bold"])
|
|
273
|
+
const [editor] = React.useState(createToggleGroupController)
|
|
274
|
+
|
|
275
|
+
return (
|
|
276
|
+
<>
|
|
277
|
+
<ToggleGroup
|
|
278
|
+
multiple
|
|
279
|
+
aria-label="Text formatting"
|
|
280
|
+
value={formats}
|
|
281
|
+
onValueChange={setFormats}
|
|
282
|
+
controller={editor}
|
|
283
|
+
>
|
|
284
|
+
<Toggle value="bold">Bold</Toggle>
|
|
285
|
+
<Toggle value="italic">Italic</Toggle>
|
|
286
|
+
</ToggleGroup>
|
|
287
|
+
<button type="button" onClick={() => editor.setValue([])}>
|
|
288
|
+
Clear Formatting
|
|
289
|
+
</button>
|
|
290
|
+
</>
|
|
291
|
+
)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export { Formatting }
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
See [State and Events](../state-and-events.md) for controlled state, cancellation and debugging.
|
|
298
|
+
|
|
299
|
+
## Vertical
|
|
300
|
+
|
|
301
|
+
Set `orientation="vertical"` to move with Up and Down instead of Left and Right. The group and its toggles get `data-orientation`.
|
|
302
|
+
|
|
303
|
+
```tsx
|
|
304
|
+
<ToggleGroup orientation="vertical" aria-label="Tool">
|
|
305
|
+
...
|
|
306
|
+
</ToggleGroup>
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
## Loop
|
|
310
|
+
|
|
311
|
+
Arrow keys wrap from the last toggle to the first. Set `loop` to `false` to stop at the ends.
|
|
312
|
+
|
|
313
|
+
```tsx
|
|
314
|
+
<ToggleGroup multiple loop={false} aria-label="Text formatting">
|
|
315
|
+
...
|
|
316
|
+
</ToggleGroup>
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
## Forms
|
|
320
|
+
|
|
321
|
+
Toggles don't submit values. Use [Checkbox](./checkbox.md) for several form choices or [Radio](./radio.md) for one. If you keep a toggle group in a form, copy its value into hidden inputs yourself.
|
|
322
|
+
|
|
323
|
+
## Custom Rendering
|
|
324
|
+
|
|
325
|
+
Every part takes `render`. Pass an element and its props merge with the part's, or a function that receives the props and the part's state. A toggle's state has `pressed`, and a group's has `value`, its pressed values. Keep the part's props on the button, so its `aria-pressed` and keyboard behavior stay connected. See [Parts and Composition](../parts-and-composition.md) for the rendering contract.
|
|
326
|
+
|
|
327
|
+
```tsx title="render.tsx"
|
|
328
|
+
"use client"
|
|
329
|
+
|
|
330
|
+
import { Toggle, ToggleIcon } from "@ui-sdk/react/toggle"
|
|
331
|
+
import { HeartIcon } from "lucide-react"
|
|
332
|
+
|
|
333
|
+
function Example() {
|
|
334
|
+
return (
|
|
335
|
+
<Toggle
|
|
336
|
+
aria-label="Favorite"
|
|
337
|
+
render={(props, state) => (
|
|
338
|
+
<button {...props}>
|
|
339
|
+
<ToggleIcon>
|
|
340
|
+
<HeartIcon fill={state.pressed ? "currentColor" : "none"} />
|
|
341
|
+
</ToggleIcon>
|
|
342
|
+
</button>
|
|
343
|
+
)}
|
|
344
|
+
/>
|
|
345
|
+
)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export { Example }
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
Press Favorite. The heart renders filled or empty from the toggle's pressed state.
|
|
352
|
+
|
|
353
|
+
## Styling
|
|
354
|
+
|
|
355
|
+
Style the pressed state with `data-pressed`, and use `data-disabled` for unavailable toggles. A toggle with a `ToggleIcon` gets `data-has-icon`, and its group gets `data-has-icons` while any toggle has one, which helps align icon and text content. `data-orientation` tells a group's toggles which way it runs.
|
|
356
|
+
|
|
357
|
+
Each part has a `data-slot`, such as `toggle`, and its state attributes are listed under [Data Attributes](#data-attributes).
|
|
358
|
+
|
|
359
|
+
## Data Attributes
|
|
360
|
+
|
|
361
|
+
### Toggle
|
|
362
|
+
`[data-slot="toggle"]`
|
|
363
|
+
|
|
364
|
+
- `data-pressed`: Present while pressed.
|
|
365
|
+
- `data-disabled`: Present when disabled, by its own prop or its group.
|
|
366
|
+
- `data-orientation` (`horizontal`, `vertical`): Inside a group, the group's orientation.
|
|
367
|
+
- `data-has-icon`: Present while the toggle holds an icon part.
|
|
368
|
+
|
|
369
|
+
### ToggleIcon
|
|
370
|
+
`[data-slot="toggle-icon"]`
|
|
371
|
+
|
|
372
|
+
- `data-mirror`: Present on an icon that flips in right-to-left text, with `mirror`.
|
|
373
|
+
|
|
374
|
+
### ToggleGroup
|
|
375
|
+
`[data-slot="toggle-group"]`
|
|
376
|
+
|
|
377
|
+
- `data-orientation` (`horizontal`, `vertical`): Which arrow keys move between toggles.
|
|
378
|
+
- `data-disabled`: Present when the whole group is disabled.
|
|
379
|
+
- `data-has-icons`: Present while any toggle inside holds an icon part, so toggles without one can line up.
|
|
380
|
+
|
|
381
|
+
## API Reference
|
|
382
|
+
|
|
383
|
+
Props and callbacks are the same as below, as React props.
|
|
384
|
+
|
|
385
|
+
### Toggle
|
|
386
|
+
|
|
387
|
+
- `pressed?` (`boolean`): Whether it's pressed. Pass it to control the toggle. Inside a group, the group's value decides.
|
|
388
|
+
- `defaultPressed?` (`boolean`): Whether it's pressed at first, when uncontrolled.
|
|
389
|
+
- `onPressedChange?` (`((pressed: boolean, details: ChangeDetails<TogglePressedChangeReason>) => void)`): Runs when a press wants to change it. `details.reason` says why, and `details.cancel()` stops it. Inside a group, it runs before the group's `onValueChange`, and canceling it cancels the group's change too.
|
|
390
|
+
- `value?` (`string`): Identifies the toggle inside a group, whose value lists the pressed toggles' values. Defaults to a generated id.
|
|
391
|
+
- `disabled?` (`boolean`): Stops presses. Alone, the toggle is natively disabled, unless `focusableWhenDisabled` is set. Inside a group or a toolbar, it stays focusable with `aria-disabled`, so arrow keys still reach it.
|
|
392
|
+
- `focusableWhenDisabled?` (`boolean`): Keeps a disabled toggle in the tab order with `aria-disabled` instead of the native attribute. Toggles in a group or a toolbar always are.
|
|
393
|
+
- `controller?` (`{ isPressed: () => boolean; setPressed: (pressed: boolean, options?: ToggleChangeOptions) => boolean; toggle: (options?: ToggleChangeOptions) => boolean; focus: () => void; }`): Presses it from anywhere. Create one with `createToggleController()`.
|
|
394
|
+
- `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
|
|
395
|
+
|
|
396
|
+
### ToggleIcon
|
|
397
|
+
|
|
398
|
+
- `mirror?` (`boolean`): Flips the icon horizontally in right-to-left text, for icons that point a way, like arrows and chevrons. Icons that picture a thing, like a clock or a check, stay as they are.
|
|
399
|
+
|
|
400
|
+
### ToggleGroup
|
|
401
|
+
|
|
402
|
+
- `value?` (`T[]`): The pressed toggles' values. Pass it to control the group.
|
|
403
|
+
- `defaultValue?` (`T[]`): The toggles pressed at first, when uncontrolled.
|
|
404
|
+
- `onValueChange?` (`((value: T[], details: ChangeDetails<ToggleGroupValueChangeReason>) => void)`): Runs when a toggle wants to be pressed or unpressed. `details.reason` says why, and `details.cancel()` stops it.
|
|
405
|
+
- `multiple?` (`boolean`): Whether several toggles can be pressed at once. Otherwise pressing one unpresses the others.
|
|
406
|
+
- `deselectable?` (`boolean`): Whether the pressed toggle can be unpressed when only one may be pressed. With `false`, one toggle stays pressed once any is. Ignored with `multiple`.
|
|
407
|
+
- `disabled?` (`boolean`): Stops every toggle from being pressed. They stay focusable with `aria-disabled`, so the group keeps its tab stop and stays discoverable.
|
|
408
|
+
- `orientation?` (`ToggleGroupOrientation`): Which arrow keys move between toggles: left and right, or up and down.
|
|
409
|
+
- `loop?` (`boolean`): Whether arrow keys wrap from the last toggle to the first.
|
|
410
|
+
- `controller?` (`object`): Presses toggles from anywhere. Create one with `createToggleGroupController()`.
|
|
411
|
+
- `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
|
|
412
|
+
|
|
413
|
+
### State
|
|
414
|
+
|
|
415
|
+
What render functions, state-based class names and slots receive.
|
|
416
|
+
|
|
417
|
+
- `pressed` (`boolean`): Whether it's pressed. Inside a group, it follows the group's value.
|
|
418
|
+
- `disabled` (`boolean`): Whether it's disabled, by itself, its group or a fieldset around it.
|
|
419
|
+
|
|
420
|
+
In a group, a toggle's `disabled` includes the group's. The group:
|
|
421
|
+
|
|
422
|
+
- `value` (`T[]`): The pressed toggles' values.
|
|
423
|
+
- `multiple` (`boolean`): Whether several toggles can be pressed at once.
|
|
424
|
+
- `disabled` (`boolean`): Whether the whole group is disabled, by its `disabled` option or a disabled fieldset around it.
|
|
425
|
+
- `orientation` (`ToggleGroupOrientation`): The group's `orientation` option, or `"horizontal"` when it's unset or invalid.
|
|
426
|
+
|
|
427
|
+
`ToggleIcon`:
|
|
428
|
+
|
|
429
|
+
- `mirror` (`boolean`): Whether it flips in right-to-left text.
|
|
430
|
+
|
|
431
|
+
### Reasons
|
|
432
|
+
|
|
433
|
+
`details.reason` says why the toggle was pressed or unpressed.
|
|
434
|
+
|
|
435
|
+
- `press` (`MouseEvent | KeyboardEvent`): A single-element control was pressed: a checkbox, a switch or a toggle.
|
|
436
|
+
- `imperative` (`none`): A controller or `api` call, or a property set on a web component.
|
|
437
|
+
|
|
438
|
+
For a group, it says why the pressed toggles changed.
|
|
439
|
+
|
|
440
|
+
- `item-press` (`MouseEvent | KeyboardEvent`): A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
|
|
441
|
+
- `imperative` (`none`): A controller or `api` call, or a property set on a web component.
|
|
442
|
+
|
|
443
|
+
### Toggle Controller
|
|
444
|
+
|
|
445
|
+
Create one with `createToggleController()` and pass it as `controller`.
|
|
446
|
+
|
|
447
|
+
Every write method takes an optional `{ reason, event }`, runs the change callback like a press, and returns `false` when the change is canceled.
|
|
448
|
+
|
|
449
|
+
- `isPressed()` (`() => boolean`): Whether it's pressed. Inside a group, it follows the group's value.
|
|
450
|
+
- `setPressed(pressed, options?)` (`(pressed: boolean, options?: ToggleChangeOptions | undefined) => boolean`): Presses or unpresses it, as a user would. A disabled toggle ignores it. Returns false when canceled.
|
|
451
|
+
- `toggle(options?)` (`(options?: ToggleChangeOptions | undefined) => boolean`): Presses it, or unpresses it.
|
|
452
|
+
- `focus()` (`() => void`): Moves focus to it.
|
|
453
|
+
|
|
454
|
+
### ToggleGroup Controller
|
|
455
|
+
|
|
456
|
+
Create one with `createToggleGroupController()` and pass it as `controller`.
|
|
457
|
+
|
|
458
|
+
Every write method takes an optional `{ reason, event }`, runs `onValueChange` like a press, and returns `false` when the change is canceled.
|
|
459
|
+
|
|
460
|
+
- `getValue()` (`() => T[]`): The pressed toggles' values, in the order they appear.
|
|
461
|
+
- `setValue(value, options?)` (`(value: T[], options?: ToggleGroupChangeOptions | undefined) => boolean`): Sets the pressed toggles. Only the first counts unless `multiple` is set. Returns false when canceled.
|
|
462
|
+
- `isPressed(value)` (`(value: T) => boolean`): Whether a toggle is pressed.
|
|
463
|
+
- `toggle(value, options?)` (`(value: T, options?: ToggleGroupChangeOptions | undefined) => boolean`): Presses a toggle, or unpresses a pressed one, as a user would. Disabled toggles and `deselectable={false}` refuse it.
|
|
464
|
+
- `focus(value?)` (`(value?: T | undefined) => void`): Moves focus to a toggle, or to the one that holds the group's tab stop.
|
|
465
|
+
|
|
466
|
+
### Events
|
|
467
|
+
|
|
468
|
+
Callbacks get the new state or value first, then the details. `onValueChange` never runs on mount.
|
|
469
|
+
|
|
470
|
+
| Prop | On | Receives |
|
|
471
|
+
| ----------------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------- |
|
|
472
|
+
| `onPressedChange` | `Toggle` | `(pressed, details)`: the new state, and the [reason](#reasons). In a group, it runs first and can cancel the group's change |
|
|
473
|
+
| `onValueChange` | `ToggleGroup` | `(value, details)`: the pressed toggles' values, and the [reason](#reasons) |
|
|
474
|
+
|
|
475
|
+
## Accessibility
|
|
476
|
+
|
|
477
|
+
A toggle follows the toggle button in the [WAI-ARIA button pattern](https://www.w3.org/WAI/ARIA/apg/patterns/button/): a native button with `aria-pressed`. A group follows the keyboard of the [WAI-ARIA toolbar pattern](https://www.w3.org/WAI/ARIA/apg/patterns/toolbar/), with one tab stop and arrow keys between toggles. It has the `group` role, not `toolbar`, so it can sit inside a [Toolbar](./toolbar.md), and not `radiogroup`, since its toggles are buttons.
|
|
478
|
+
|
|
479
|
+
### Keyboard
|
|
480
|
+
|
|
481
|
+
A group is one tab stop. Tab first enters on its first enabled pressed toggle, or its first enabled toggle, and later returns to the last focused one.
|
|
482
|
+
|
|
483
|
+
| Key | Does |
|
|
484
|
+
| ------------ | ------------------------------------------------------- |
|
|
485
|
+
| Enter, Space | Presses or unpresses the focused toggle |
|
|
486
|
+
| Tab | Moves into or out of a group |
|
|
487
|
+
| Left, Right | Move focus in a horizontal group, swapped right to left |
|
|
488
|
+
| Up, Down | Move focus in a vertical group |
|
|
489
|
+
| Home, End | Move focus to the first or last toggle |
|
|
490
|
+
|
|
491
|
+
### Tested Behavior
|
|
492
|
+
|
|
493
|
+
Every line below is an automated test.
|
|
494
|
+
|
|
495
|
+
- A toggle starts unpressed.
|
|
496
|
+
- Clicking a toggle presses it, and clicking again unpresses it.
|
|
497
|
+
- Space presses a focused toggle.
|
|
498
|
+
- Enter presses a focused toggle.
|
|
499
|
+
- A toggle can start pressed.
|
|
500
|
+
- A change can be cancelled.
|
|
501
|
+
- A toggle keeps its name when it's pressed.
|
|
502
|
+
|
|
503
|
+
Runs in React, Vue and web components.
|
|
504
|
+
|
|
505
|
+
In a group:
|
|
506
|
+
|
|
507
|
+
**One pressed at a time**
|
|
508
|
+
|
|
509
|
+
- The group is named and its default toggle is pressed.
|
|
510
|
+
- Pressing a toggle unpresses the others.
|
|
511
|
+
- Pressing the pressed toggle unpresses it.
|
|
512
|
+
- A change can be cancelled.
|
|
513
|
+
|
|
514
|
+
**Several pressed at once**
|
|
515
|
+
|
|
516
|
+
- Pressing toggles adds them to the value.
|
|
517
|
+
- A disabled toggle can't be pressed.
|
|
518
|
+
- A disabled group presses nothing.
|
|
519
|
+
|
|
520
|
+
**Keyboard**
|
|
521
|
+
|
|
522
|
+
- Tab moves into the group once, and out of it.
|
|
523
|
+
- Only one toggle in a group is a tab stop.
|
|
524
|
+
- ArrowRight and ArrowLeft move focus without pressing.
|
|
525
|
+
- Focus wraps at both ends.
|
|
526
|
+
- Home and End move to the first and last toggles.
|
|
527
|
+
- ArrowUp and ArrowDown do nothing in a horizontal group.
|
|
528
|
+
- Space presses the focused toggle.
|
|
529
|
+
- Enter presses the focused toggle.
|
|
530
|
+
- Tab returns to the toggle focused last.
|
|
531
|
+
- The group itself is never a tab stop.
|
|
532
|
+
- Keys pressed without waiting still land.
|
|
533
|
+
- Arrow keys with a modifier are left to the browser.
|
|
534
|
+
|
|
535
|
+
**Options**
|
|
536
|
+
|
|
537
|
+
- With loop off, arrows stop at the ends.
|
|
538
|
+
- A vertical group moves with ArrowDown and ArrowUp.
|
|
539
|
+
- The group carries its orientation.
|
|
540
|
+
|
|
541
|
+
Runs in React, Vue and web components.
|