Skip to content

Switch

The Switch component provides a toggle switch control that can be used as an alternative to checkbox inputs. It offers a more visual way to toggle between two states (on/off).

This component has no additional dependencies beyond the core Metro UI library.

<!-- Basic switch -->
<input type="checkbox" data-role="switch">
<!-- Switch with labels -->
<input type="checkbox" data-role="switch" data-prepend="Off" data-append="On">
<!-- Material design switch -->
<input type="checkbox" data-role="switch" data-material="true">
<!-- Switch with on/off text -->
<input type="checkbox" data-role="switch" data-onoff="true">
<!-- Switch with custom on/off text -->
<input type="checkbox" data-role="switch" data-onoff="true" data-on="YES" data-off="NO">
<!-- Checked switch -->
<input type="checkbox" data-role="switch" checked>
<!-- Disabled switch -->
<input type="checkbox" data-role="switch" disabled>
<!-- Red switch -->
<input type="checkbox" data-role="switch" class="switch-red">
<!-- Teal switch -->
<input type="checkbox" data-role="switch" class="switch-teal">
ParameterTypeDefaultDescription
switchDeferrednumber0Deferred initialization time in milliseconds
materialbooleanfalseUse material design style for the switch
prependstring""Text to display before the switch
appendstring""Text to display after the switch
clsSwitchstring""Additional CSS class for the switch container
clsCheckstring""Additional CSS class for the checkbox element
clsCaptionstring""Additional CSS class for the caption elements
onoffbooleanfalseShow on/off text inside the switch
onstring""Custom text for the “on” state (when onoff is true)
offstring""Custom text for the “off” state (when onoff is true)
const switchComponent = Metro.getPlugin('#mySwitch', 'switch');
  • toggle(v) - Toggle the switch state. If v is provided, sets the switch to checked (v=1) or unchecked (v≠1).
const switchComponent = Metro.getPlugin('#mySwitch', 'switch');
switchComponent.toggle(); // Toggle the switch state
switchComponent.toggle(1); // Set the switch to checked
switchComponent.toggle(0); // Set the switch to unchecked
EventDescription
onSwitchCreateTriggered when the switch component is created
VariableDefault (Light)Dark ModeDescription
--switch-background#e9e9e9#232527Background color of the switch
--switch-background-checked#191919#474748Background color when the switch is checked
--switch-background-disabled#e8e8e8-Background color when the switch is disabled
--switch-toggle-color#fff#fffColor of the toggle button
--switch-toggle-disabled#ccc#3e4145Color of the toggle button when disabled
--switch-text-color#000#efefefColor of the text in the switch
--switch-text-color-checked#fff#fffColor of the text when the switch is checked
--switch-focus-color#e8e8e8#191919Color of the focus outline
/* Custom styling example */
#my-switch {
--switch-background: #3498db;
--switch-background-checked: #2980b9;
--switch-toggle-color: #ecf0f1;
}
  • .switch - The main container class for the switch component
  • .switch.material - Applies material design style to the switch
  • .switch-[color] - Color variants (e.g., .switch-red, .switch-teal, etc.)
  • .caption-prepend - Styling for text before the switch
  • .caption-append - Styling for text after the switch

The Switch component uses standard checkbox inputs with proper styling, ensuring good accessibility:

  • Works with keyboard navigation (Tab to focus, Space to toggle)
  • Supports standard disabled and readonly attributes
  • Maintains proper focus states for keyboard users