Basic Input
A labelled text input with helper text wired to it via aria-describedby.
6 个框架初级
A settings toggle styled over a native checkbox, with `role="switch"` on the input itself.
<!--
A switch is a checkbox with a different picture on top of it - nothing more.
`role="switch"` is allowed on `input[type=checkbox]` and changes the
announcement from "checked" to "on/off"; it rides on the real input, which is
what keeps Space, form submission and the label association intact. Putting
`role="switch"` on a div and rebuilding those by hand is a downgrade, not a
technique.
The state is carried by the thumb's POSITION as well as the track colour, so it
survives greyscale.
-->
<div class="switch-field">
<label class="switch" for="two-factor">
<input
class="switch__input"
type="checkbox"
id="two-factor"
name="two-factor"
role="switch"
aria-describedby="two-factor-hint"
checked
/>
<span class="switch__track" aria-hidden="true"></span>
<span class="switch__label">Two-factor authentication</span>
</label>
<p class="switch__hint" id="two-factor-hint">
Ask for a code from your authenticator app at every sign-in.
</p>
</div>| Prop | 类型 | 默认值 | 说明 |
|---|---|---|---|
disabled | boolean | false | 禁止交互并将控件置灰。 |
className | string | - | 合并到根元素上的额外类名。 |
This is a checkbox wearing a different picture - and it has to be. A `role="switch"` div has to reimplement Space, focus, the label link and form submission, and still cannot be posted; the input here gets all of it for nothing. `role="switch"` is valid on `input[type=checkbox]` and only changes the announcement from "checked" to "on/off". The thumb slides via `peer-checked:after:translate-x-5`, so state is carried by position as well as by the track colour - recolour `peer-checked:bg-blue-600` freely, the greyscale reading survives. The off-state `bg-gray-400` steps to `dark:bg-gray-600` so the track stays visible against a dark page.