Toggle a setting on or off. Suitable for binary options like enabling or disabling features.
import { Field, Switch } from '@fluxui-dev/react';<Field variant="inline"> <Field.Label>Enable notifications</Field.Label> <Switch name="notifications" /> <Field.Error name="notifications" /></Field>
Group related switches within a fieldset.
import { Separator, Switch } from '@fluxui-dev/react';<fieldset> <legend className="mb-4 text-base font-medium text-zinc-800 dark:text-white">Email notifications</legend> <div className="space-y-4"> <Switch name="communication" label="Communication emails" description="Receive emails about your account activity." /> <Separator variant="subtle" /> <Switch name="marketing" label="Marketing emails" description="Receive emails about new products, features, and more." /> <Separator variant="subtle" /> <Switch name="social" label="Social emails" description="Receive emails for friend requests, follows, and more." /> <Separator variant="subtle" /> <Switch name="security" label="Security emails" description="Receive emails about your account activity and security." /> </div></fieldset>
Left align switches for more compact layouts using the align prop.
<fieldset> <legend className="mb-4 text-base font-medium text-zinc-800 dark:text-white">Email notifications</legend> <div className="space-y-3"> <Switch label="Communication emails" align="start" /> <Switch label="Marketing emails" align="start" /> <Switch label="Social emails" align="start" /> <Switch label="Security emails" align="start" /> </div></fieldset>
|
Prop
|
Description
|
|---|---|
| label |
Label text displayed next to the switch. When provided, wraps the switch in a Field component with an adjacent Field.Label component. See the field component.
|
| description |
Help text displayed below the label. When provided alongside label, appears between the label and switch within the Field wrapper. See the field component.
|
| error |
Error message displayed below the switch, marking it invalid and defaulting to the validation error for the switch's name.
|
| align |
Alignment of the switch relative to its label. Options: end (default), start.
|
| name |
Form field name. The switch submits its value (or on) while on, and nothing while off unless uncheckedValue is set.
|
| value |
Value submitted while the switch is on. Defaults to on.
|
| uncheckedValue |
Value submitted while the switch is off.
|
| checked |
Controlled on/off state of the switch, paired with onCheckedChange.
|
| defaultChecked |
Initial state when uncontrolled. A form reset returns to it.
|
| onCheckedChange |
Callback fired when the switch is turned on or off, receiving the new state.
|
| disabled |
Prevents user interaction with the switch.
|
| invalid |
Sets aria-invalid and data-invalid on the switch.
|
| className |
CSS classes applied to the switch. With label set, they stay on the switch, not the field around it; to space the whole field, write the Field yourself and pass it className.
|
|
Attribute
|
Description
|
|---|---|
| data-flux-switch |
Applied to the root element for styling and identification.
|
| data-checked |
Applied when the switch is in the "on" state.
|
| data-disabled |
Applied when the switch is disabled.
|