@ui-sdk/vue 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 +561 -0
- package/dist/accordion.js +24 -0
- package/dist/agent.d.ts +2 -0
- package/dist/agent.js +7 -0
- package/dist/alert-dialog.d.ts +231 -0
- package/dist/alert-dialog.js +33 -0
- package/dist/async-options.d.ts +18 -0
- package/dist/async-options.js +40 -0
- package/dist/autocomplete.d.ts +384 -0
- package/dist/autocomplete.js +344 -0
- package/dist/avatar.d.ts +40 -0
- package/dist/avatar.js +28 -0
- package/dist/button.d.ts +52 -0
- package/dist/button.js +43 -0
- package/dist/calendar-context.d.ts +10 -0
- package/dist/calendar-context.js +7 -0
- package/dist/calendar.d.ts +311 -0
- package/dist/calendar.js +349 -0
- package/dist/carousel.d.ts +236 -0
- package/dist/carousel.js +204 -0
- package/dist/checkbox.d.ts +211 -0
- package/dist/checkbox.js +151 -0
- package/dist/children.d.ts +41 -0
- package/dist/children.js +135 -0
- package/dist/collapsible.d.ts +54 -0
- package/dist/collapsible.js +18 -0
- package/dist/color-picker.d.ts +187 -0
- package/dist/color-picker.js +158 -0
- package/dist/color.d.ts +2 -0
- package/dist/color.js +21 -0
- package/dist/combobox.d.ts +517 -0
- package/dist/combobox.js +531 -0
- package/dist/command.d.ts +322 -0
- package/dist/command.js +345 -0
- package/dist/controller-target.d.ts +49 -0
- package/dist/controller-target.js +112 -0
- package/dist/create-parts.d.ts +126 -0
- package/dist/create-parts.js +206 -0
- package/dist/date-input.d.ts +133 -0
- package/dist/date-input.js +31 -0
- package/dist/date-picker.d.ts +328 -0
- package/dist/date-picker.js +354 -0
- package/dist/date.d.ts +2 -0
- package/dist/date.js +59 -0
- package/dist/dialog-parts.d.ts +259 -0
- package/dist/dialog-parts.js +300 -0
- package/dist/dialog.d.ts +251 -0
- package/dist/dialog.js +25 -0
- package/dist/drawer.d.ts +304 -0
- package/dist/drawer.js +373 -0
- package/dist/editor-context.d.ts +4 -0
- package/dist/editor-context.js +5 -0
- package/dist/field-context.d.ts +26 -0
- package/dist/field-context.js +44 -0
- package/dist/field.d.ts +154 -0
- package/dist/field.js +240 -0
- package/dist/file-input.d.ts +212 -0
- package/dist/file-input.js +255 -0
- package/dist/form-inputs.d.ts +16 -0
- package/dist/form-inputs.js +25 -0
- package/dist/gesture.d.ts +1 -0
- package/dist/gesture.js +15 -0
- package/dist/history.d.ts +2 -0
- package/dist/history.js +11 -0
- package/dist/hover-card.d.ts +137 -0
- package/dist/hover-card.js +153 -0
- package/dist/image-cropper.d.ts +74 -0
- package/dist/image-cropper.js +36 -0
- package/dist/image.d.ts +2 -0
- package/dist/image.js +5 -0
- package/dist/index.d.ts +59 -0
- package/dist/index.js +60 -0
- package/dist/input.d.ts +120 -0
- package/dist/input.js +73 -0
- package/dist/item-icon.d.ts +34 -0
- package/dist/item-icon.js +63 -0
- package/dist/label.d.ts +16 -0
- package/dist/label.js +8 -0
- package/dist/lifecycle.d.ts +11 -0
- package/dist/lifecycle.js +91 -0
- package/dist/list-items.d.ts +38 -0
- package/dist/list-items.js +59 -0
- package/dist/list-text.d.ts +7 -0
- package/dist/list-text.js +15 -0
- package/dist/listbox.d.ts +273 -0
- package/dist/listbox.js +126 -0
- package/dist/menu.d.ts +678 -0
- package/dist/menu.js +607 -0
- package/dist/menubar-context.d.ts +5 -0
- package/dist/menubar-context.js +5 -0
- package/dist/menubar.d.ts +61 -0
- package/dist/menubar.js +48 -0
- package/dist/meter.d.ts +41 -0
- package/dist/meter.js +37 -0
- package/dist/navigation-menu.d.ts +228 -0
- package/dist/navigation-menu.js +195 -0
- package/dist/number-input.d.ts +176 -0
- package/dist/number-input.js +231 -0
- package/dist/number.d.ts +2 -0
- package/dist/number.js +39 -0
- package/dist/nuxt/entries.json +977 -0
- package/dist/nuxt/module.d.ts +18 -0
- package/dist/nuxt/module.js +31 -0
- package/dist/nuxt/registrations.d.ts +22 -0
- package/dist/nuxt/registrations.js +22 -0
- package/dist/observe.d.ts +2 -0
- package/dist/observe.js +7 -0
- package/dist/otp-input.d.ts +142 -0
- package/dist/otp-input.js +130 -0
- package/dist/overlays.d.ts +40 -0
- package/dist/overlays.js +118 -0
- package/dist/pagination.d.ts +201 -0
- package/dist/pagination.js +146 -0
- package/dist/password-input.d.ts +144 -0
- package/dist/password-input.js +134 -0
- package/dist/popover.d.ts +211 -0
- package/dist/popover.js +258 -0
- package/dist/popup.d.ts +60 -0
- package/dist/popup.js +89 -0
- package/dist/preferences.d.ts +44 -0
- package/dist/preferences.js +102 -0
- package/dist/primitive.d.ts +2 -0
- package/dist/primitive.js +11 -0
- package/dist/progress.d.ts +41 -0
- package/dist/progress.js +37 -0
- package/dist/radio.d.ts +164 -0
- package/dist/radio.js +145 -0
- package/dist/rating.d.ts +149 -0
- package/dist/rating.js +146 -0
- package/dist/renderless.d.ts +4 -0
- package/dist/renderless.js +28 -0
- package/dist/resizable.d.ts +54 -0
- package/dist/resizable.js +22 -0
- package/dist/scroll-area.d.ts +72 -0
- package/dist/scroll-area.js +77 -0
- package/dist/segmented-control.d.ts +151 -0
- package/dist/segmented-control.js +110 -0
- package/dist/segmented-input.d.ts +76 -0
- package/dist/segmented-input.js +115 -0
- package/dist/select.d.ts +458 -0
- package/dist/select.js +468 -0
- package/dist/separator.d.ts +14 -0
- package/dist/separator.js +8 -0
- package/dist/shortcuts.d.ts +32 -0
- package/dist/shortcuts.js +56 -0
- package/dist/slider.d.ts +65 -0
- package/dist/slider.js +194 -0
- package/dist/switch.d.ts +103 -0
- package/dist/switch.js +71 -0
- package/dist/tabs.d.ts +338 -0
- package/dist/tabs.js +27 -0
- package/dist/text-control.d.ts +89 -0
- package/dist/text-control.js +51 -0
- package/dist/text-match.d.ts +2 -0
- package/dist/text-match.js +7 -0
- package/dist/textarea.d.ts +106 -0
- package/dist/textarea.js +68 -0
- package/dist/time-input.d.ts +131 -0
- package/dist/time-input.js +32 -0
- package/dist/timeline.d.ts +81 -0
- package/dist/timeline.js +30 -0
- package/dist/toast-parts.d.ts +203 -0
- package/dist/toast-parts.js +270 -0
- package/dist/toast.d.ts +2 -0
- package/dist/toast.js +21 -0
- package/dist/toggle-props.d.ts +22 -0
- package/dist/toggle-props.js +14 -0
- package/dist/toggle.d.ts +199 -0
- package/dist/toggle.js +129 -0
- package/dist/token-input.d.ts +136 -0
- package/dist/token-input.js +214 -0
- package/dist/toolbar-context.d.ts +7 -0
- package/dist/toolbar-context.js +20 -0
- package/dist/toolbar.d.ts +120 -0
- package/dist/toolbar.js +77 -0
- package/dist/tooltip.d.ts +160 -0
- package/dist/tooltip.js +224 -0
- package/dist/tree.d.ts +300 -0
- package/dist/tree.js +209 -0
- package/dist/trigger-menu.d.ts +452 -0
- package/dist/trigger-menu.js +254 -0
- package/dist/use-primitive.d.ts +132 -0
- package/dist/use-primitive.js +462 -0
- package/dist/waterfall.d.ts +201 -0
- package/dist/waterfall.js +171 -0
- package/docs/README.md +89 -0
- package/docs/agents.md +1523 -0
- package/docs/components/accordion.md +1369 -0
- package/docs/components/alert-dialog.md +799 -0
- package/docs/components/autocomplete.md +1857 -0
- package/docs/components/avatar.md +272 -0
- package/docs/components/button.md +435 -0
- package/docs/components/calendar.md +1589 -0
- package/docs/components/carousel.md +1171 -0
- package/docs/components/checkbox.md +1008 -0
- package/docs/components/collapsible.md +536 -0
- package/docs/components/color-picker.md +706 -0
- package/docs/components/combobox.md +3504 -0
- package/docs/components/command.md +1481 -0
- package/docs/components/date-input.md +536 -0
- package/docs/components/date-picker.md +1244 -0
- package/docs/components/dialog.md +1397 -0
- package/docs/components/drawer.md +1166 -0
- package/docs/components/field.md +538 -0
- package/docs/components/file-input.md +611 -0
- package/docs/components/hover-card.md +505 -0
- package/docs/components/image-cropper.md +556 -0
- package/docs/components/input.md +306 -0
- package/docs/components/label.md +203 -0
- package/docs/components/listbox.md +1004 -0
- package/docs/components/menu.md +1229 -0
- package/docs/components/menubar.md +443 -0
- package/docs/components/meter.md +315 -0
- package/docs/components/navigation-menu.md +1523 -0
- package/docs/components/number-input.md +1029 -0
- package/docs/components/otp-input.md +512 -0
- package/docs/components/pagination.md +613 -0
- package/docs/components/password-input.md +649 -0
- package/docs/components/popover.md +1448 -0
- package/docs/components/preferences.md +208 -0
- package/docs/components/progress.md +423 -0
- package/docs/components/radio.md +785 -0
- package/docs/components/rating.md +416 -0
- package/docs/components/resizable.md +910 -0
- package/docs/components/scroll-area.md +915 -0
- package/docs/components/segmented-control.md +435 -0
- package/docs/components/select.md +2015 -0
- package/docs/components/separator.md +229 -0
- package/docs/components/slider.md +1084 -0
- package/docs/components/switch.md +565 -0
- package/docs/components/tabs.md +944 -0
- package/docs/components/textarea.md +299 -0
- package/docs/components/time-input.md +224 -0
- package/docs/components/timeline.md +654 -0
- package/docs/components/toast.md +1224 -0
- package/docs/components/toggle.md +500 -0
- package/docs/components/token-input.md +953 -0
- package/docs/components/toolbar.md +739 -0
- package/docs/components/tooltip.md +818 -0
- package/docs/components/tree.md +1306 -0
- package/docs/components/trigger-menu.md +1223 -0
- package/docs/components/waterfall.md +717 -0
- package/docs/controllers.md +71 -0
- package/docs/core.md +8027 -0
- package/docs/custom-parts.md +89 -0
- package/docs/first-component.md +83 -0
- package/docs/forms.md +122 -0
- package/docs/installation.md +72 -0
- package/docs/introduction.md +32 -0
- package/docs/lists-and-menus.md +122 -0
- package/docs/llms.txt +1502 -0
- package/docs/parts-and-composition.md +85 -0
- package/docs/popups.md +108 -0
- package/docs/rendering.md +56 -0
- package/docs/server-rendering.md +180 -0
- package/docs/state-and-events.md +100 -0
- package/docs/styling-and-animation.md +148 -0
- package/docs/utilities/async-options.md +348 -0
- package/docs/utilities/shortcuts.md +159 -0
- package/package.json +352 -3
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
# Switch
|
|
2
|
+
|
|
3
|
+
Turn a setting on or off.
|
|
4
|
+
|
|
5
|
+
Use a switch for a setting that takes effect as soon as it changes, like Airplane mode or a notification preference. Keep its label the same in both states: the label says what the setting is, and the switch shows whether it's on.
|
|
6
|
+
|
|
7
|
+
```vue title="basic.vue"
|
|
8
|
+
<script setup lang="ts">
|
|
9
|
+
import { Switch, SwitchThumb } from "@ui-sdk/vue/switch"
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<template>
|
|
13
|
+
<label data-slot="label">
|
|
14
|
+
Airplane mode
|
|
15
|
+
<Switch>
|
|
16
|
+
<SwitchThumb />
|
|
17
|
+
</Switch>
|
|
18
|
+
</label>
|
|
19
|
+
</template>
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Click Airplane mode, or focus the switch and press Space, to turn it on and off.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install @ui-sdk/vue @ui-sdk/core
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Composition
|
|
31
|
+
|
|
32
|
+
Use the following composition to build a `Switch`:
|
|
33
|
+
|
|
34
|
+
```text
|
|
35
|
+
Switch
|
|
36
|
+
└── SwitchThumb
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Wrap the switch and its text in a label. The thumb stays mounted and gets the switch's state, so CSS can slide it from one end to the other.
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
Import the parts and put them together:
|
|
44
|
+
|
|
45
|
+
```vue
|
|
46
|
+
<script setup lang="ts">
|
|
47
|
+
import { Switch, SwitchThumb } from "@ui-sdk/vue/switch"
|
|
48
|
+
</script>
|
|
49
|
+
|
|
50
|
+
<template>
|
|
51
|
+
<label>
|
|
52
|
+
Airplane mode
|
|
53
|
+
<Switch>
|
|
54
|
+
<SwitchThumb />
|
|
55
|
+
</Switch>
|
|
56
|
+
</label>
|
|
57
|
+
</template>
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Description
|
|
61
|
+
|
|
62
|
+
Use `FieldLabel` and `FieldDescription` when a setting needs more explanation. The field connects both to the switch.
|
|
63
|
+
|
|
64
|
+
```vue title="description.vue"
|
|
65
|
+
<script setup lang="ts">
|
|
66
|
+
import { Field, FieldDescription, FieldLabel } from "@ui-sdk/vue/field"
|
|
67
|
+
import { Switch, SwitchThumb } from "@ui-sdk/vue/switch"
|
|
68
|
+
</script>
|
|
69
|
+
|
|
70
|
+
<template>
|
|
71
|
+
<Field>
|
|
72
|
+
<div data-slot="stack">
|
|
73
|
+
<FieldLabel>Weekly digest</FieldLabel>
|
|
74
|
+
<FieldDescription>A summary of your week, every Monday.</FieldDescription>
|
|
75
|
+
</div>
|
|
76
|
+
<Switch>
|
|
77
|
+
<SwitchThumb />
|
|
78
|
+
</Switch>
|
|
79
|
+
</Field>
|
|
80
|
+
</template>
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Click Weekly digest. The field connects the label and description to the switch.
|
|
84
|
+
|
|
85
|
+
## Choice Card
|
|
86
|
+
|
|
87
|
+
A setting can fill a whole card. Here the label's `::after` covers the card, so a click anywhere on it toggles the switch, and `:has([data-checked])` marks the cards that are on.
|
|
88
|
+
|
|
89
|
+
```vue title="choice-card.vue"
|
|
90
|
+
<script setup lang="ts">
|
|
91
|
+
import { Field, FieldDescription, FieldLabel } from "@ui-sdk/vue/field"
|
|
92
|
+
import { Switch, SwitchThumb } from "@ui-sdk/vue/switch"
|
|
93
|
+
|
|
94
|
+
const settings = [
|
|
95
|
+
{
|
|
96
|
+
label: "Public profile",
|
|
97
|
+
description: "Anyone can see your name and photo.",
|
|
98
|
+
defaultChecked: false,
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
label: "Show activity status",
|
|
102
|
+
description: "Teammates can see when you're online.",
|
|
103
|
+
defaultChecked: true,
|
|
104
|
+
},
|
|
105
|
+
]
|
|
106
|
+
</script>
|
|
107
|
+
|
|
108
|
+
<template>
|
|
109
|
+
<div data-slot="stack">
|
|
110
|
+
<Field v-for="setting in settings" :key="setting.label" data-variant="card">
|
|
111
|
+
<div data-slot="stack">
|
|
112
|
+
<FieldLabel>{{ setting.label }}</FieldLabel>
|
|
113
|
+
<FieldDescription>{{ setting.description }}</FieldDescription>
|
|
114
|
+
</div>
|
|
115
|
+
<Switch :default-checked="setting.defaultChecked">
|
|
116
|
+
<SwitchThumb />
|
|
117
|
+
</Switch>
|
|
118
|
+
</Field>
|
|
119
|
+
</div>
|
|
120
|
+
</template>
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Click anywhere on the Public profile card to turn it on. Each card's switch keeps its own value.
|
|
124
|
+
|
|
125
|
+
## Disabled
|
|
126
|
+
|
|
127
|
+
`disabled` prevents focus and changes, and leaves the switch out of form submissions. A disabled `fieldset` does the same.
|
|
128
|
+
|
|
129
|
+
```vue title="disabled.vue"
|
|
130
|
+
<script setup lang="ts">
|
|
131
|
+
import { Switch, SwitchThumb } from "@ui-sdk/vue/switch"
|
|
132
|
+
</script>
|
|
133
|
+
|
|
134
|
+
<template>
|
|
135
|
+
<div data-slot="stack">
|
|
136
|
+
<label data-slot="label">
|
|
137
|
+
Product updates
|
|
138
|
+
<Switch>
|
|
139
|
+
<SwitchThumb />
|
|
140
|
+
</Switch>
|
|
141
|
+
</label>
|
|
142
|
+
<label data-slot="label">
|
|
143
|
+
Security alerts
|
|
144
|
+
<Switch default-checked disabled>
|
|
145
|
+
<SwitchThumb />
|
|
146
|
+
</Switch>
|
|
147
|
+
</label>
|
|
148
|
+
</div>
|
|
149
|
+
</template>
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Security alerts are always on, so that switch is disabled. Clicking it leaves it on, and Tab skips it.
|
|
153
|
+
|
|
154
|
+
## Read-Only
|
|
155
|
+
|
|
156
|
+
`read-only` keeps the switch focusable and included in form submissions, while preventing the user from changing it. Use it for a setting managed somewhere else.
|
|
157
|
+
|
|
158
|
+
```vue title="read-only.vue"
|
|
159
|
+
<script setup lang="ts">
|
|
160
|
+
import { Field, FieldDescription, FieldLabel } from "@ui-sdk/vue/field"
|
|
161
|
+
import { Switch, SwitchThumb } from "@ui-sdk/vue/switch"
|
|
162
|
+
</script>
|
|
163
|
+
|
|
164
|
+
<template>
|
|
165
|
+
<Field>
|
|
166
|
+
<div data-slot="stack">
|
|
167
|
+
<FieldLabel>Encrypt backups</FieldLabel>
|
|
168
|
+
<FieldDescription
|
|
169
|
+
>Your organization manages this setting.</FieldDescription
|
|
170
|
+
>
|
|
171
|
+
</div>
|
|
172
|
+
<Switch name="encrypt" default-checked read-only>
|
|
173
|
+
<SwitchThumb />
|
|
174
|
+
</Switch>
|
|
175
|
+
</Field>
|
|
176
|
+
</template>
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Tab to Encrypt backups. Space and clicks leave it on.
|
|
180
|
+
|
|
181
|
+
## Controlled
|
|
182
|
+
|
|
183
|
+
Bind `v-model:checked`, or pass `checked` and handle `@checked-change`, to own the value. A [controller](#controller) from `createSwitchController()` changes it from anywhere, like the Toggle button below. See [State and Events](../state-and-events.md) for the shared change callback.
|
|
184
|
+
|
|
185
|
+
```vue title="controlled.vue"
|
|
186
|
+
<script setup lang="ts">
|
|
187
|
+
import {
|
|
188
|
+
createSwitchController,
|
|
189
|
+
type SwitchCheckedChangeReason,
|
|
190
|
+
} from "@ui-sdk/core/switch"
|
|
191
|
+
import { Switch, SwitchThumb } from "@ui-sdk/vue/switch"
|
|
192
|
+
import { ref } from "vue"
|
|
193
|
+
import { useLog } from "./log"
|
|
194
|
+
|
|
195
|
+
const log = useLog()
|
|
196
|
+
const wifi = createSwitchController()
|
|
197
|
+
const checked = ref(true)
|
|
198
|
+
|
|
199
|
+
function onCheckedChange(
|
|
200
|
+
next: boolean,
|
|
201
|
+
details: { reason: SwitchCheckedChangeReason }
|
|
202
|
+
) {
|
|
203
|
+
log(`${next ? "On" : "Off"} (${details.reason})`)
|
|
204
|
+
}
|
|
205
|
+
</script>
|
|
206
|
+
|
|
207
|
+
<template>
|
|
208
|
+
<div data-slot="stack">
|
|
209
|
+
<div data-slot="controls">
|
|
210
|
+
<button type="button" @click="wifi.toggle()">Toggle</button>
|
|
211
|
+
</div>
|
|
212
|
+
<label data-slot="label">
|
|
213
|
+
Wi-Fi
|
|
214
|
+
<Switch v-model:checked="checked" :controller="wifi" @checked-change="onCheckedChange">
|
|
215
|
+
<SwitchThumb />
|
|
216
|
+
</Switch>
|
|
217
|
+
</label>
|
|
218
|
+
</div>
|
|
219
|
+
</template>
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Turn Wi-Fi off, or press Toggle. The console logs each change and its reason.
|
|
223
|
+
|
|
224
|
+
## Form
|
|
225
|
+
|
|
226
|
+
Set `name` to include the switch in a native form. A switch that's on submits its `value`, which defaults to `"on"`; a switch that's off submits nothing. Reset returns it to `default-checked`. Set `form` to a form's id to join it from elsewhere on the page.
|
|
227
|
+
|
|
228
|
+
```vue title="form.vue"
|
|
229
|
+
<script setup lang="ts">
|
|
230
|
+
import { FieldLegend, FieldSet } from "@ui-sdk/vue/field"
|
|
231
|
+
import { Switch, SwitchThumb } from "@ui-sdk/vue/switch"
|
|
232
|
+
import { useLog } from "./log"
|
|
233
|
+
|
|
234
|
+
const log = useLog()
|
|
235
|
+
|
|
236
|
+
function onSubmit(event: Event) {
|
|
237
|
+
const entries = [...new FormData(event.currentTarget as HTMLFormElement)].map(
|
|
238
|
+
([name, value]) => `${name}=${value}`
|
|
239
|
+
)
|
|
240
|
+
log(entries.length ? `Submitted ${entries.join("&")}` : "Submitted nothing")
|
|
241
|
+
}
|
|
242
|
+
</script>
|
|
243
|
+
|
|
244
|
+
<template>
|
|
245
|
+
<form data-slot="stack" @submit.prevent="onSubmit">
|
|
246
|
+
<FieldSet>
|
|
247
|
+
<FieldLegend>Email notifications</FieldLegend>
|
|
248
|
+
<label data-slot="label">
|
|
249
|
+
Product updates
|
|
250
|
+
<Switch name="updates">
|
|
251
|
+
<SwitchThumb />
|
|
252
|
+
</Switch>
|
|
253
|
+
</label>
|
|
254
|
+
<label data-slot="label">
|
|
255
|
+
Security alerts
|
|
256
|
+
<Switch name="security" default-checked>
|
|
257
|
+
<SwitchThumb />
|
|
258
|
+
</Switch>
|
|
259
|
+
</label>
|
|
260
|
+
<label data-slot="label">
|
|
261
|
+
Weekly digest
|
|
262
|
+
<Switch name="digest" value="weekly" default-checked>
|
|
263
|
+
<SwitchThumb />
|
|
264
|
+
</Switch>
|
|
265
|
+
</label>
|
|
266
|
+
</FieldSet>
|
|
267
|
+
<div data-slot="row">
|
|
268
|
+
<button type="submit" data-variant="solid">Save</button>
|
|
269
|
+
<button type="reset">Reset</button>
|
|
270
|
+
</div>
|
|
271
|
+
</form>
|
|
272
|
+
</template>
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Turn on Product updates and press Save. The console logs the submitted values, and Reset restores the defaults.
|
|
276
|
+
|
|
277
|
+
## Validation
|
|
278
|
+
|
|
279
|
+
`required` blocks submission until the switch is on. In a [Field](./field.md), `FieldError` shows the message under the switch and connects it to the switch. [Forms and Validation](../forms.md) covers the shared field behavior.
|
|
280
|
+
|
|
281
|
+
```vue title="invalid.vue"
|
|
282
|
+
<script setup lang="ts">
|
|
283
|
+
import {
|
|
284
|
+
Field,
|
|
285
|
+
FieldDescription,
|
|
286
|
+
FieldError,
|
|
287
|
+
FieldLabel,
|
|
288
|
+
} from "@ui-sdk/vue/field"
|
|
289
|
+
import { Switch, SwitchThumb } from "@ui-sdk/vue/switch"
|
|
290
|
+
import { useLog } from "./log"
|
|
291
|
+
|
|
292
|
+
const log = useLog()
|
|
293
|
+
|
|
294
|
+
function onSubmit(event: Event) {
|
|
295
|
+
log(
|
|
296
|
+
`Submitted two-factor=${new FormData(event.currentTarget as HTMLFormElement).get("two-factor")}`
|
|
297
|
+
)
|
|
298
|
+
}
|
|
299
|
+
</script>
|
|
300
|
+
|
|
301
|
+
<template>
|
|
302
|
+
<form data-slot="stack" @submit.prevent="onSubmit">
|
|
303
|
+
<Field>
|
|
304
|
+
<div data-slot="row">
|
|
305
|
+
<div data-slot="stack">
|
|
306
|
+
<FieldLabel>Two-factor authentication</FieldLabel>
|
|
307
|
+
<FieldDescription
|
|
308
|
+
>Required for admins of this workspace.</FieldDescription
|
|
309
|
+
>
|
|
310
|
+
</div>
|
|
311
|
+
<Switch name="two-factor" required>
|
|
312
|
+
<SwitchThumb />
|
|
313
|
+
</Switch>
|
|
314
|
+
</div>
|
|
315
|
+
<FieldError match="valueMissing">Turn this on to continue.</FieldError>
|
|
316
|
+
</Field>
|
|
317
|
+
<div data-slot="row">
|
|
318
|
+
<button type="submit" data-variant="solid">Continue</button>
|
|
319
|
+
</div>
|
|
320
|
+
</form>
|
|
321
|
+
</template>
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
Press Continue with the switch off to see the error. Turn on Two-factor authentication and continue; the console logs the submitted value.
|
|
325
|
+
|
|
326
|
+
## Saving on Toggle
|
|
327
|
+
|
|
328
|
+
To save a change before it shows, call `details.cancel()` in `@checked-change` to keep the current value while the save runs. When it finishes, call `controller.setChecked(next, { reason: "imperative" })`, and let that reason through in the handler so the saved value commits.
|
|
329
|
+
|
|
330
|
+
The example ignores presses while a save is running, and shows a spinner in the thumb. `aria-busy` tells assistive technology the switch is busy; the handler is what blocks another change.
|
|
331
|
+
|
|
332
|
+
```vue title="async.vue"
|
|
333
|
+
<script setup lang="ts">
|
|
334
|
+
import { LoaderCircleIcon } from "@lucide/vue"
|
|
335
|
+
import {
|
|
336
|
+
createSwitchController,
|
|
337
|
+
type SwitchCheckedChangeReason,
|
|
338
|
+
} from "@ui-sdk/core/switch"
|
|
339
|
+
import { Switch, SwitchThumb } from "@ui-sdk/vue/switch"
|
|
340
|
+
import { ref } from "vue"
|
|
341
|
+
import { useLog } from "./log"
|
|
342
|
+
|
|
343
|
+
const save = (_checked: boolean) =>
|
|
344
|
+
new Promise((resolve) => setTimeout(resolve, 800))
|
|
345
|
+
|
|
346
|
+
const log = useLog()
|
|
347
|
+
const backups = createSwitchController()
|
|
348
|
+
const saving = ref(false)
|
|
349
|
+
|
|
350
|
+
function onCheckedChange(
|
|
351
|
+
next: boolean,
|
|
352
|
+
details: { reason: SwitchCheckedChangeReason; cancel: () => void }
|
|
353
|
+
) {
|
|
354
|
+
if (details.reason === "imperative") {
|
|
355
|
+
return
|
|
356
|
+
}
|
|
357
|
+
details.cancel()
|
|
358
|
+
if (saving.value) {
|
|
359
|
+
return
|
|
360
|
+
}
|
|
361
|
+
saving.value = true
|
|
362
|
+
log("Saving…")
|
|
363
|
+
save(next).then(() => {
|
|
364
|
+
backups.setChecked(next, { reason: "imperative" })
|
|
365
|
+
saving.value = false
|
|
366
|
+
log(`Saved: ${next ? "on" : "off"}`)
|
|
367
|
+
})
|
|
368
|
+
}
|
|
369
|
+
</script>
|
|
370
|
+
|
|
371
|
+
<template>
|
|
372
|
+
<label data-slot="label">
|
|
373
|
+
Automatic backups
|
|
374
|
+
<Switch :controller="backups" :aria-busy="saving" @checked-change="onCheckedChange">
|
|
375
|
+
<SwitchThumb>
|
|
376
|
+
<LoaderCircleIcon v-if="saving" data-slot="spinner" aria-hidden="true" />
|
|
377
|
+
</SwitchThumb>
|
|
378
|
+
</Switch>
|
|
379
|
+
</label>
|
|
380
|
+
</template>
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
Turn on Automatic backups. The thumb shows a spinner while it saves, then the switch turns on.
|
|
384
|
+
|
|
385
|
+
## Custom Rendering
|
|
386
|
+
|
|
387
|
+
The switch and thumb take a `render` slot, which receives the props to bind and the part's state. Keep the part's props on the element so its role, label and keyboard behavior stay connected. See [Parts and Composition](../parts-and-composition.md) for the rendering contract.
|
|
388
|
+
|
|
389
|
+
```vue title="render.vue"
|
|
390
|
+
<script setup lang="ts">
|
|
391
|
+
import { MoonIcon, SunIcon } from "@lucide/vue"
|
|
392
|
+
import { Switch, SwitchThumb } from "@ui-sdk/vue/switch"
|
|
393
|
+
</script>
|
|
394
|
+
|
|
395
|
+
<template>
|
|
396
|
+
<label data-slot="label">
|
|
397
|
+
Dark mode
|
|
398
|
+
<Switch>
|
|
399
|
+
<SwitchThumb>
|
|
400
|
+
<template #render="{ props, state }">
|
|
401
|
+
<span v-bind="props">
|
|
402
|
+
<MoonIcon v-if="state.checked" aria-hidden="true" />
|
|
403
|
+
<SunIcon v-else aria-hidden="true" />
|
|
404
|
+
</span>
|
|
405
|
+
</template>
|
|
406
|
+
</SwitchThumb>
|
|
407
|
+
</Switch>
|
|
408
|
+
</label>
|
|
409
|
+
</template>
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
Turn on Dark mode. The thumb renders a sun or a moon from its state.
|
|
413
|
+
|
|
414
|
+
## Styling
|
|
415
|
+
|
|
416
|
+
Style the switch and its thumb with `data-checked` and `data-unchecked`, and use `data-disabled`, `data-readonly` and `data-required` for the rest. In an invalid field, the switch also gets `data-invalid` and `aria-invalid`; the thumb doesn't. Slide the thumb with `translate` on `data-checked`, and reverse it under `dir="rtl"`.
|
|
417
|
+
|
|
418
|
+
Each part has a `data-slot`, such as `switch`, and its state attributes are listed under [Data Attributes](#data-attributes).
|
|
419
|
+
|
|
420
|
+
## Data Attributes
|
|
421
|
+
|
|
422
|
+
### Switch
|
|
423
|
+
`[data-slot="switch"]`
|
|
424
|
+
|
|
425
|
+
- `data-checked`: Present while on.
|
|
426
|
+
- `data-disabled`: Present when disabled, by its own prop or a `<fieldset>`.
|
|
427
|
+
- `data-readonly`: Present when read-only.
|
|
428
|
+
- `data-required`: Present when required.
|
|
429
|
+
- `data-unchecked`: Present while off.
|
|
430
|
+
- `data-invalid`: Present while a `Field` around it shows as invalid. The thumb doesn't get it.
|
|
431
|
+
|
|
432
|
+
### SwitchThumb
|
|
433
|
+
`[data-slot="switch-thumb"]`
|
|
434
|
+
|
|
435
|
+
- `data-checked`: Present while on.
|
|
436
|
+
- `data-disabled`: Present when disabled, by its own prop or a `<fieldset>`.
|
|
437
|
+
- `data-readonly`: Present when read-only.
|
|
438
|
+
- `data-required`: Present when required.
|
|
439
|
+
- `data-unchecked`: Present while off.
|
|
440
|
+
|
|
441
|
+
## API Reference
|
|
442
|
+
|
|
443
|
+
Props are the same as below, and callbacks are events.
|
|
444
|
+
|
|
445
|
+
### Switch
|
|
446
|
+
|
|
447
|
+
- `checked?` (`boolean`): Whether it's checked. Pass it to control the checkbox.
|
|
448
|
+
- `defaultChecked?` (`boolean`): Whether it's checked at first, when uncontrolled. A form reset returns to it.
|
|
449
|
+
- `@checked-change` (`((checked: boolean, details: ChangeDetails<CheckboxCheckedChangeReason>) => void)`): Runs when a user or a form reset wants to change it. `details.reason` says why, and `details.cancel()` stops it.
|
|
450
|
+
- `disabled?` (`boolean`): Disables it like a native control: it can't be focused or changed, and isn't submitted.
|
|
451
|
+
- `readOnly?` (`boolean`): Keeps it focusable and submitted, but stops changes.
|
|
452
|
+
- `required?` (`boolean`): Blocks form submission until it's checked, with the browser's own message.
|
|
453
|
+
- `name?` (`string`): The name it submits under. Without one, it submits nothing.
|
|
454
|
+
- `value?` (`string`): What it submits while checked.
|
|
455
|
+
- `form?` (`string`): The id of a form to belong to, when it isn't inside that form.
|
|
456
|
+
- `controller?` (`{ toggle: (details?: Details) => boolean; getChecked: () => boolean; setChecked: (checked: boolean, details?: Details) => boolean; focus: () => void; }`): Toggles it from anywhere. Create one with `createSwitchController()`.
|
|
457
|
+
- `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.
|
|
458
|
+
|
|
459
|
+
### State
|
|
460
|
+
|
|
461
|
+
What render slots and the default slot receive. The thumb's state, `SwitchThumbState`, is the same.
|
|
462
|
+
|
|
463
|
+
- `checked` (`boolean`): Whether it's checked. False while indeterminate.
|
|
464
|
+
- `disabled` (`boolean`): Whether it's disabled, by itself or by a `<fieldset>`.
|
|
465
|
+
- `readOnly` (`boolean`): Whether it's read-only: it stays focusable and submitted, but presses don't change it.
|
|
466
|
+
- `required` (`boolean`): Whether a form won't submit until it's checked. Indeterminate doesn't count.
|
|
467
|
+
|
|
468
|
+
### Reasons
|
|
469
|
+
|
|
470
|
+
`details.reason` says why the switch turned on or off.
|
|
471
|
+
|
|
472
|
+
- `press` (`MouseEvent | KeyboardEvent`): A single-element control was pressed: a checkbox, a switch or a toggle.
|
|
473
|
+
- `reset` (`none`): A form reset.
|
|
474
|
+
- `restore` (`none`): The browser restored state, like after going back, or a saved value was read back from storage.
|
|
475
|
+
- `imperative` (`none`): A controller or `api` call, or a property set on a web component.
|
|
476
|
+
|
|
477
|
+
### Controller
|
|
478
|
+
|
|
479
|
+
Create one with `createSwitchController()` and pass it as `controller`.
|
|
480
|
+
|
|
481
|
+
Every write method takes an optional `{ reason, event }`, runs the change callback like a press, and returns `false` when the change is canceled.
|
|
482
|
+
|
|
483
|
+
- `toggle(details?)` (`(details?: CheckboxChangeOptions | undefined) => boolean`): Turns it on, or off.
|
|
484
|
+
- `getChecked()` (`() => boolean`): Whether it's on.
|
|
485
|
+
- `setChecked(checked, details?)` (`(checked: boolean, details?: CheckboxChangeOptions | undefined) => boolean`): Turns it on or off, as a user would.
|
|
486
|
+
- `focus()` (`() => void`): Moves focus to it.
|
|
487
|
+
|
|
488
|
+
### Events
|
|
489
|
+
|
|
490
|
+
Events get the new state first, then the details. `v-model` only updates when the change event wasn't canceled.
|
|
491
|
+
|
|
492
|
+
| Binding | On | Receives |
|
|
493
|
+
| ----------------- | -------- | --------------------------------------------------------------- |
|
|
494
|
+
| `v-model:checked` | `Switch` | Whether the switch is on |
|
|
495
|
+
| `@checked-change` | `Switch` | `(checked, details)`: the new state, and the [reason](#reasons) |
|
|
496
|
+
|
|
497
|
+
## Accessibility
|
|
498
|
+
|
|
499
|
+
The switch follows the [WAI-ARIA switch pattern](https://www.w3.org/WAI/ARIA/apg/patterns/switch/). `Switch` renders a button with the `switch` role, and a hidden native checkbox that takes part in forms.
|
|
500
|
+
|
|
501
|
+
### Keyboard
|
|
502
|
+
|
|
503
|
+
| Key | Does |
|
|
504
|
+
| ----- | -------------------------------------- |
|
|
505
|
+
| Tab | Moves to the next switch |
|
|
506
|
+
| Space | Turns it on or off |
|
|
507
|
+
| Enter | Submits its form, and never toggles it |
|
|
508
|
+
|
|
509
|
+
### Tested Behavior
|
|
510
|
+
|
|
511
|
+
Every line below is an automated test.
|
|
512
|
+
|
|
513
|
+
- Starts in its default state.
|
|
514
|
+
- Clicking toggles it.
|
|
515
|
+
- Clicking its label toggles it.
|
|
516
|
+
- Space toggles it.
|
|
517
|
+
- Enter submits the form and never toggles it.
|
|
518
|
+
- The thumb follows the state.
|
|
519
|
+
|
|
520
|
+
**The first input on a page**
|
|
521
|
+
|
|
522
|
+
- The first Space reports keyboard.
|
|
523
|
+
|
|
524
|
+
**On a touch screen**
|
|
525
|
+
|
|
526
|
+
- The first tap reports touch.
|
|
527
|
+
|
|
528
|
+
Runs in React, Vue and web components.
|
|
529
|
+
|
|
530
|
+
In forms:
|
|
531
|
+
|
|
532
|
+
- The form submits every checked control's name and value.
|
|
533
|
+
- Unchecked controls submit nothing.
|
|
534
|
+
- A checkbox outside the form joins it with the form attribute.
|
|
535
|
+
- Required controls block submission.
|
|
536
|
+
- A form reset returns every control to its default.
|
|
537
|
+
- A reset returns to the default value, not to nothing.
|
|
538
|
+
- A disabled fieldset disables the controls inside it.
|
|
539
|
+
- A read-only control still submits.
|
|
540
|
+
- A user's change fires native input and change events on the form.
|
|
541
|
+
|
|
542
|
+
Runs in React, Vue and web components.
|
|
543
|
+
|
|
544
|
+
Beyond the pattern's baseline:
|
|
545
|
+
|
|
546
|
+
**Enhancements**
|
|
547
|
+
|
|
548
|
+
- The first invalid control takes focus in document order.
|
|
549
|
+
- A later required field without an error part doesn't take focus from the first.
|
|
550
|
+
|
|
551
|
+
**Form-associated custom elements**
|
|
552
|
+
|
|
553
|
+
- Take part in the form with no hidden input. Web components only.
|
|
554
|
+
- Report validity like native controls. Web components only.
|
|
555
|
+
- Have live checked and value properties. Web components only.
|
|
556
|
+
- Are named by their labels. Web components only.
|
|
557
|
+
|
|
558
|
+
Runs in React, Vue and web components.
|
|
559
|
+
|
|
560
|
+
Screen readers hear the same thing in each tested framework, recorded by a virtual screen reader:
|
|
561
|
+
|
|
562
|
+
- Announcements while checking boxes with a keyboard.
|
|
563
|
+
- Announcements while choosing a radio with a keyboard.
|
|
564
|
+
|
|
565
|
+
Runs in React, Vue and web components.
|