Skip to content

Commit d320048

Browse files
committed
docs(Switch): apply review follow-ups from spec audit
Move page intro after DocsPageFeatures per docs.md spec, remove the misleading HiddenInput "override" recipe, replace the broken thumb slide animation recipe with a Track background color transition (Switch.Thumb has inline visibility:hidden when unchecked, so a translate-x transform cannot visibly slide from the off position), and add the Enter key parenthetical to the accessibility bullet for consistency with the checkbox page.
1 parent 7c42732 commit d320048

File tree

1 file changed

+10
-20
lines changed
  • apps/docs/src/pages/components/forms

1 file changed

+10
-20
lines changed

apps/docs/src/pages/components/forms/switch.md

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ related:
1818

1919
# Switch
2020

21-
A switch for on/off state or multi-selection groups with tri-state support.
22-
2321
<DocsPageFeatures :frontmatter />
2422

23+
A switch for on/off state or multi-selection groups with tri-state support.
24+
2525
## Usage
2626

2727
::: example
@@ -131,19 +131,7 @@ Pass the `name` prop on `Switch.Root` and a hidden native `<input type="checkbox
131131
</template>
132132
```
133133

134-
Place `Switch.HiddenInput` explicitly only when you need to override the auto-rendered name, value, or form association:
135-
136-
```vue
137-
<template>
138-
<Switch.Root name="notifications">
139-
<Switch.Track>
140-
<Switch.Thumb />
141-
</Switch.Track>
142-
143-
<Switch.HiddenInput name="notifications_override" value="custom" />
144-
</Switch.Root>
145-
</template>
146-
```
134+
`Switch.HiddenInput` is exported as an internal building block for custom layouts, but auto-rendering via `name` is the only supported form integration path — placing `Switch.HiddenInput` as a child of a `Switch.Root` that already has a `name` will produce two hidden inputs.
147135

148136
### Styling with Data Attributes
149137

@@ -157,13 +145,15 @@ Switch subcomponents expose data attributes for CSS styling without conditional
157145
```vue
158146
<template>
159147
<Switch.Root class="data-[disabled]:opacity-50">
160-
<Switch.Track class="bg-gray-300 data-[state=checked]:bg-primary">
161-
<Switch.Thumb class="translate-x-0.5 data-[state=checked]:translate-x-5.5" />
148+
<Switch.Track class="bg-gray-300 transition-colors data-[state=checked]:bg-primary">
149+
<Switch.Thumb />
162150
</Switch.Track>
163151
</Switch.Root>
164152
</template>
165153
```
166154

155+
`Switch.Thumb` applies an inline `visibility: hidden` when unchecked, so the thumb is not visible until the switch is on. This means a sliding transform on `Switch.Thumb` cannot animate *from* the "off" position. Animate the `Switch.Track` background color instead, as shown above.
156+
167157
## Accessibility
168158

169159
The Switch.Root component renders as a button and handles all ARIA attributes automatically:
@@ -173,7 +163,7 @@ The Switch.Root component renders as a button and handles all ARIA attributes au
173163
- `aria-disabled` when switch is disabled
174164
- `aria-label` from the `label` prop
175165
- `tabindex="0"` for keyboard focus (removed when disabled)
176-
- Space key toggles the switch
166+
- Space key toggles the switch (Enter works when rendered as button)
177167

178168
For custom implementations, use `renderless` mode and bind the `attrs` slot prop to your element:
179169

@@ -197,9 +187,9 @@ Use `Switch` for settings that take immediate effect, like toggling a feature on
197187

198188
`Switch.Root` only renders the hidden native input when a `name` prop is set. Without `name`, the switch is purely visual and won't appear in `FormData`. Add `name="myField"` (and optionally `value`) to participate in form submission.
199189

200-
??? How do I animate the thumb sliding?
190+
??? How do I animate the switch state change?
201191

202-
Apply a CSS `transition` to `Switch.Thumb` (or `Switch.Track`) and use the `data-[state=checked]:` variant to change its transform. For example, `class="transition-transform data-[state=checked]:translate-x-5"` slides the thumb when toggled. No JavaScript event handling is needed — the data attribute flip drives the animation.
192+
`Switch.Thumb` applies an inline `visibility: hidden` when unchecked, so transforms on the thumb cannot animate from the "off" position. Apply a CSS `transition` to `Switch.Track` and use the `data-[state=checked]:` variant to change its background — for example, `class="transition-colors bg-gray-300 data-[state=checked]:bg-primary"`. No JavaScript event handling is needed — the data attribute flip drives the animation.
203193

204194
??? Can I use Switch.Root without the Track and Thumb subcomponents?
205195

0 commit comments

Comments
 (0)