ToggleSwitch
A toggle switch for binary settings, feature flags and compact on-off decisions.
import { ToggleSwitch } from "@schukai/monster/source/components/form/toggle-switch.mjs";Introduction
The Monster ToggleSwitch is a compact on-off control for binary settings. Use it when users need to enable or disable a feature, preference or mode and the current state should be visible at a glance.
When to use ToggleSwitch
- Use it for immediate binary choices: Feature flags, notification settings and compact preferences fit well.
- Use it when the current state matters: The control communicates enabled or disabled status directly.
- Do not use it for multi-step confirmation: For risky actions, a switch is too easy to trigger accidentally.
Typical mistakes
Label the meaning, not just the state. Users should know what happens when the switch is on, not only that it is currently on or off.
Simple Example
The Toggle Switch component is a simple way to toggle between two states. It is a stylized version of the native checkbox input.
Javascript
import '@schukai/monster/source/components/form/toggle-switch.mjs';<script type="module">import '@schukai/monster/source/components/form/toggle-switch.mjs';</script>HTML
<monster-toggle-switch></monster-toggle-switch>Stylesheet
/** no additional stylesheet is defined **/Custom Values And Labels
This example maps the switch to business values and updates the surrounding UI through actions.on and actions.off.
Javascript
import "@schukai/monster/source/components/form/toggle-switch.mjs";
const control = document.getElementById("toggle-switch-custom-demo");
const state = document.getElementById("toggle-switch-custom-state");
const log = document.getElementById("toggle-switch-custom-log");
control.setOption("values.on", "automatic");
control.setOption("values.off", "manual");
control.setOption("labels.toggleSwitchOn", "A");
control.setOption("labels.toggleSwitchOff", "M");
control.setOption("actions.on", () => {
log.textContent = "Last event: switched to automatic";
});
control.setOption("actions.off", () => {
log.textContent = "Last event: switched to manual";
});
control.value = "automatic";
const render = () => {
state.textContent = "Mode: " + control.value;
};
control.addEventListener("monster-change", render);
render();<script type="module">import "@schukai/monster/source/components/form/toggle-switch.mjs";
const control = document.getElementById("toggle-switch-custom-demo-run");
const state = document.getElementById("toggle-switch-custom-state-run");
const log = document.getElementById("toggle-switch-custom-log-run");
control.setOption("values.on", "automatic");
control.setOption("values.off", "manual");
control.setOption("labels.toggleSwitchOn", "A");
control.setOption("labels.toggleSwitchOff", "M");
control.setOption("actions.on", () => {
log.textContent = "Last event: switched to automatic";
});
control.setOption("actions.off", () => {
log.textContent = "Last event: switched to manual";
});
control.value = "automatic";
const render = () => {
state.textContent = "Mode: " + control.value;
};
control.addEventListener("monster-change", render);
render();</script>HTML
<div style="display:grid;gap:1rem;max-inline-size:28rem;">
<monster-toggle-switch id="toggle-switch-custom-demo"></monster-toggle-switch>
<div style="display:grid;gap:0.5rem;">
<strong id="toggle-switch-custom-state">Mode: automatic</strong>
<div id="toggle-switch-custom-log">Last event: initialized</div>
</div>
</div>Stylesheet
/** no additional stylesheet is defined **/Component Design
The ToggleSwitch component is implemented using the Shadow DOM to encapsulate its internal structure and styling. By leveraging the shadow root, the component ensures isolation, protecting its layout and behavior from external styles and scripts while maintaining a clean and modular design.
Shadow DOM and Accessibility
Encapsulation through the Shadow DOM ensures that external interference does not alter the component's internal structure. The component is designed to be accessible, supporting semantic structure, keyboard interaction (e.g., space key to toggle), and assistive technologies. This makes the ToggleSwitch user-friendly for all users.
Customizing Through Exported Parts
The ToggleSwitch exposes specific internal elements using exported parts that developers can style using CSS. This allows targeted customization of the switch without compromising its encapsulation.
Available Part Attributes
control: Represents the container wrapping the entire toggle switch.switch: Represents the main switch area, which toggles between states.handle: Represents the sliding handle of the switch.
Below is an example of how to use CSS part attributes to customize the ToggleSwitch component:
monster-toggle-switch::part(control) {
display: inline-block;
padding: 5px;
background-color: #f8f8f8;
border-radius: 6px;
}
monster-toggle-switch::part(switch) {
width: 50px;
height: 24px;
background-color: #d6d6d6;
border-radius: 12px;
position: relative;
cursor: pointer;
transition: background-color 0.3s ease;
}
monster-toggle-switch::part(handle) {
position: absolute;
top: 2px;
left: 2px;
width: 20px;
height: 20px;
background-color: #ffffff;
border-radius: 50%;
transition: transform 0.3s ease;
}
monster-toggle-switch::part(switch)[data-monster-state="on"] monster-toggle-switch::part(handle) {
transform: translateX(26px);
}
Explanation of the Example
monster-toggle-switch::part(control): Styles the main container with padding and a background.monster-toggle-switch::part(switch): Customizes the toggle switch background and dimensions.monster-toggle-switch::part(switch)[data-monster-state="on"]: Changes the background color when the switch is toggled on.monster-toggle-switch::part(handle): Styles the sliding handle and animates its position when toggled.
Accessibility
The ToggleSwitch component is accessible via keyboard navigation, allowing users to toggle the switch using the Space key. Screen readers can detect the switch's state, providing appropriate feedback. The aria-disabled attribute is automatically managed when the switch is disabled, ensuring compliance with accessibility standards.
HTML Structure
<monster-toggle-switch></monster-toggle-switch>JavaScript Initialization
const element = document.createElement('monster-toggle-switch');
document.body.appendChild(element);Exported
ToggleSwitchDerived from
CustomControlOptions
The Options listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the CustomControl.
- since
- deprecated
Properties and Attributes
The Properties and Attributes listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits Properties and Attributes from these parent classes. If you cannot find a specific Properties and Attributes in this list, we recommend consulting the documentation of the CustomControl.
data-monster-options: Sets the configuration options for the collapse component when used as an HTML attribute.data-monster-option-[name]: Sets the value of the configuration option[name]for the collapse component when used as an HTML attribute.
Methods
The methods listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the CustomControl.
Behavioral methods
toggle()- {ToggleSwitch}
e = document.querySelector('monster-toggle-switch'); e.toggle()``toggleOff()- {ToggleSwitch}
e = document.querySelector('monster-toggle-switch'); e.toggleOff()``toggleOn()- {ToggleSwitch}
e = document.querySelector('monster-toggle-switch'); e.toggleOn()``State query methods
value()- {string}
e = document.querySelector('monster-toggle-switch'); console.log(e.value) // ↦ on``value(value)value
e = document.querySelector('monster-toggle-switch'); e.value="on"``Static methods
[instanceSymbol]()- {symbol}
instanceof operator.getCSSStyleSheet()- [CSSStyleSheet]
getTag()Lifecycle methods
Lifecycle methods are called by the environment and are usually not intended to be called directly.
[assembleMethodSymbol]()- {void}
Other methods
[updaterTransformerMethodsSymbol]()- {function}
click()e = document.querySelector('monster-toggle-switch'); e.click()``state()- {string}
e = document.querySelector('monster-toggle-switch'); console.log(e.state) // ↦ off``Events
The component emits the following events:
monster-options-setmonster-selectedmonster-changemonster-changed
For more information on how to handle events, see the mdn documentation.