ToggleSwitch

A toggle switch for binary settings, feature flags and compact on-off decisions.

Import
the javascript logo
import { ToggleSwitch } from "@schukai/monster/source/components/form/toggle-switch.mjs";
Source
the git logo
Package
the npm logo
Since
3.57.0
click me!

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 **/
Open in playground

Custom Values And Labels

This example maps the switch to business values and updates the surrounding UI through actions.on and actions.off.

Mode: automatic
Last event: initialized

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 **/
Open in playground

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

ToggleSwitch

Derived from

CustomControl

Options

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.

Option
Type
Default
Description
value
string
current
lement
disabled
boolean
false
Disabled state
object
classes
classes.on
string
monster-theme-on
specifies the class for the on state.
classes.off
string
monster-theme-off
specifies the class for the off state.
object
values
values.off
string
off
specifies the value of the element if it is not selected
object
labels
labels.on
string
undefined
specifies the label for the on state.
labels.off
string
undefined
specifies the label for the off state.
string
actions
actions.on
string
undefined
specifies the action for the on state.
actions.off
string
undefined
specifies the action for the off state.
object
templates
templates.main
string
undefined
the main template used by the control.

  • 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()
Returns
  • {ToggleSwitch}
toggle switch on/off ``e = document.querySelector('monster-toggle-switch'); e.toggle()``
toggleOff()
Returns
  • {ToggleSwitch}
toggle switch off ``e = document.querySelector('monster-toggle-switch'); e.toggleOff()``
toggleOn()
Returns
  • {ToggleSwitch}
toggle switch on ``e = document.querySelector('monster-toggle-switch'); e.toggleOn()``

State query methods

value()
Returns
  • {string}
The current value of the Switch ``e = document.querySelector('monster-toggle-switch'); console.log(e.value) // ↦ on``
value(value)
Parameters
  • value
Set value ``e = document.querySelector('monster-toggle-switch'); e.value="on"``

Static methods

[instanceSymbol]()
Returns
  • {symbol}
This method is called by the instanceof operator.
getCSSStyleSheet()
Returns
  • [CSSStyleSheet]
getTag()

Lifecycle methods

Lifecycle methods are called by the environment and are usually not intended to be called directly.

[assembleMethodSymbol]()
Returns
  • {void}

Other methods

[updaterTransformerMethodsSymbol]()
Returns
  • {function}
updater transformer methods for pipe
click()
toggle switch ``e = document.querySelector('monster-toggle-switch'); e.click()``
state()
Returns
  • {string}
returns the status of the element ``e = document.querySelector('monster-toggle-switch'); console.log(e.state) // ↦ off``

Events

The component emits the following events:

  • monster-options-set
  • monster-selected
  • monster-change
  • monster-changed

For more information on how to handle events, see the mdn documentation.

The current width of the area is too small to display the content correctly.