Loading...
Skip to main content

Switch

FormSwitch wraps a native checkbox input in a customizable toggle component.

Basic switch

Switches work by layering an invisible checkbox over the custom switch indicator. This preserves interactivity while giving you a completely customizable layer underneath. FormSwitch supplies role="switch" on the checkbox. Pass native input attributes such as id, name, disabled, and defaultChecked directly to FormSwitch.

<FormSwitch
defaultValue=""
id="switch"
/>

With form field

Wrap FormSwitch in a FormField layout wrapper and add your label. The grid layout places the switch and label side by side.

<div className="d-flex flex-column gap-3">
<FormField>
<FormSwitch
defaultValue=""
id="switchMdLabel"
/>
<FormLabel
htmlFor="switchMdLabel"
plain
>
Default switch
</FormLabel>
</FormField>
</div>

Theme colors

Modify the appearance of switches by adding the theme prop to the .switch element. This will set the checked background and border color to the theme color.

<div className="d-flex flex-column gap-2">
<FormField>
<FormSwitch
defaultChecked
defaultValue=""
id="switchprimary"
theme="primary"
/>
<FormLabel
htmlFor="switchprimary"
plain
>
Example primary switch
</FormLabel>
</FormField>
<FormField>
<FormSwitch
defaultChecked
defaultValue=""
id="switchaccent"
theme="accent"
/>
<FormLabel
htmlFor="switchaccent"
plain
>
Example accent switch
</FormLabel>
</FormField>
<FormField>
<FormSwitch
defaultChecked
defaultValue=""
id="switchsecondary"
theme="secondary"
/>
<FormLabel
htmlFor="switchsecondary"
plain
>
Example secondary switch
</FormLabel>
</FormField>
<FormField>
<FormSwitch
defaultChecked
defaultValue=""
id="switchsuccess"
theme="success"
/>
<FormLabel
htmlFor="switchsuccess"
plain
>
Example success switch
</FormLabel>
</FormField>
<FormField>
<FormSwitch
defaultChecked
defaultValue=""
id="switchdanger"
theme="danger"
/>
<FormLabel
htmlFor="switchdanger"
plain
>
Example danger switch
</FormLabel>
</FormField>
<FormField>
<FormSwitch
defaultChecked
defaultValue=""
id="switchwarning"
theme="warning"
/>
<FormLabel
htmlFor="switchwarning"
plain
>
Example warning switch
</FormLabel>
</FormField>
<FormField>
<FormSwitch
defaultChecked
defaultValue=""
id="switchinfo"
theme="info"
/>
<FormLabel
htmlFor="switchinfo"
plain
>
Example info switch
</FormLabel>
</FormField>
<FormField>
<FormSwitch
defaultChecked
defaultValue=""
id="switchdark"
theme="dark"
/>
<FormLabel
htmlFor="switchdark"
plain
>
Example dark switch
</FormLabel>
</FormField>
<FormField>
<FormSwitch
defaultChecked
defaultValue=""
id="switchlight"
theme="light"
/>
<FormLabel
htmlFor="switchlight"
plain
>
Example light switch
</FormLabel>
</FormField>
<FormField>
<FormSwitch
defaultChecked
defaultValue=""
id="switchinverse"
theme="inverse"
/>
<FormLabel
htmlFor="switchinverse"
plain
>
Example inverse switch
</FormLabel>
</FormField>
</div>

Disabled

Set the disabled prop on FormSwitch; its label is automatically styled in a lighter color to indicate the input's state.

<Stack gap={2}>
<FormField>
<FormSwitch
defaultValue=""
disabled
id="switchDisabledLabel"
/>
<FormLabel
htmlFor="switchDisabledLabel"
plain
>
Disabled switch
</FormLabel>
</FormField>
<FormField>
<FormSwitch
defaultChecked
defaultValue=""
disabled
id="switchCheckedDisabledLabel"
/>
<FormLabel
htmlFor="switchCheckedDisabledLabel"
plain
>
Disabled checked switch
</FormLabel>
</FormField>
</Stack>

Sizes

Add a size modifier class to make your switch appear smaller or larger.

<div className="d-flex flex-column gap-3">
<FormField>
<FormSwitch
defaultValue=""
id="switchSizeSm"
size="sm"
/>
<FormLabel
htmlFor="switchSizeSm"
plain
>
Small switch
</FormLabel>
</FormField>
<FormField>
<FormSwitch
defaultValue=""
id="switchSizeMd"
/>
<FormLabel
htmlFor="switchSizeMd"
plain
>
Default switch
</FormLabel>
</FormField>
<FormField>
<FormSwitch
defaultValue=""
id="switchSizeLg"
size="lg"
/>
<FormLabel
htmlFor="switchSizeLg"
plain
>
Large switch
</FormLabel>
</FormField>
</div>

Custom width

Customize your switch for any width and indicator size you need by overriding the CSS variables. For example, for a more modern look, you could widen the switch and indicator.

<div className="d-flex flex-column gap-3">
<FormField>
<FormSwitch
defaultValue=""
id="switchCustomWidth"
indicatorWidth="1.5rem"
width="2.75rem"
/>
<FormLabel
htmlFor="switchCustomWidth"
plain
>
Custom width switch
</FormLabel>
</FormField>
</div>

CSS

Variables

.example {
--bs-switch-height: 1.25rem;
--bs-switch-width: calc(var(--bs-switch-height) * 1.75);
--bs-switch-padding: 0.0625rem;
--bs-switch-margin-block: 0.125rem;
--bs-switch-transition-duration: var(--bs-control-transition-duration);
--bs-switch-transition-timing: var(--bs-control-transition-timing);
--bs-switch-bg: var(--bs-bg-3);
--bs-switch-border-width: var(--bs-border-width);
--bs-switch-border-color: var(--bs-border-color);
--bs-switch-indicator-bg: var(--bs-white);
--bs-switch-indicator-width: calc(
var(--bs-switch-height) - var(--bs-switch-padding) * 2 - var(--bs-switch-border-width) * 2
);
--bs-switch-indicator-height: calc(
var(--bs-switch-height) - var(--bs-switch-padding) * 2 - var(--bs-switch-border-width) * 2
);
--bs-switch-checked-bg: var(--bs-control-checked-bg);
--bs-switch-checked-border-color: var(--bs-switch-checked-bg);
--bs-switch-checked-indicator-bg: var(--bs-white);
--bs-switch-disabled-bg: var(--bs-control-disabled-bg);
--bs-switch-disabled-indicator-bg: var(--bs-fg-3);
}
Documentation navigation
On this page
On this page