Switch
Source: https://getbootstrap.com/docs/6.0/forms/switch
Custom toggle component built on top of native <input> checkbox elements.
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. Switches also include the switch attribute for browsers that support it, which provides haptic feedback when toggling.
<input type="checkbox" value="" id="switch" role="switch" switch>
</div>`} />
With form field
Wrap the .switch in a .form-field layout wrapper and add your label. The grid layout places the switch and label side by side.
<div class="switch">
<input type="checkbox" value="" id="switchMdLabel" role="switch" switch>
</div>
<label for="switchMdLabel">Default switch</label>
</div>`} />
Theme colors
Modify the appearance of switches by adding the .theme-{color} class to the .switch element. This will set the checked background and border color to the theme color.
<Example class="d-flex flex-column gap-2" code={getData('theme-colors').map((themeColor) => <div class="form-field">
<div class="switch theme-${themeColor.name}">
<input type="checkbox" value="" id="switch${themeColor.name}" role="switch" switch checked>
</div>
<label for="switch${themeColor.name}">Example ${themeColor.name} switch</label>
</div>)} />
Disabled
Add the disabled attribute and the associated <label>s are automatically styled to match with a lighter color to help indicate the input’s state.
<div class="switch">
<input type="checkbox" value="" id="switchDisabledLabel" role="switch" switch disabled>
</div>
<label for="switchDisabledLabel">Disabled switch</label>
</div>
<div class="form-field"> <div class="switch"> <input type="checkbox" value="" id="switchCheckedDisabledLabel" role="switch" switch disabled checked> </div> <label for="switchCheckedDisabledLabel">Disabled checked switch</label> </div>`} />
Sizes
Add a size modifier class to make your switch appear smaller or larger.
<div class="switch switch-sm">
<input type="checkbox" value="" id="switchSizeSm" role="switch" switch>
</div>
<label for="switchSizeSm">Small switch</label>
</div> <div class="form-field"> <div class="switch"> <input type="checkbox" value="" id="switchSizeMd" role="switch" switch> </div> <label for="switchSizeMd">Default switch</label> </div> <div class="form-field"> <div class="switch switch-lg"> <input type="checkbox" value="" id="switchSizeLg" role="switch" switch> </div> <label for="switchSizeLg">Large switch</label> </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 class="switch" style="--bs-switch-width: 2.75rem; --bs-switch-indicator-width: 1.5rem;"><!--[!code highlight]-->
<input type="checkbox" value="" id="switchCustomWidth" role="switch" switch>
</div>
<label for="switchCustomWidth">Custom width switch</label>
</div>`} />
