# Switch Toggle component allows users to switch between two states typically representing on/off, enabled/disabled, or active/inactive settings. Toggles provide a quick and intuitive way to modify system or feature preferences with a single action. **Category:** Components **Storybook:** https://doc.ux4g.gov.in/web/?path=/story/ux4g-components-switch--introduction ## Framework setup ### HTML / CDN ```html ``` ### React ```bash npm install ux4g-web-components ``` ```jsx import 'ux4g-web-components/styles.css'; import 'ux4g-web-components/design-system'; ``` ### Angular Update `angular.json`: ```json { "styles": [ "node_modules/ux4g-web-components/styles/ux4g.css", "src/styles.css" ], "scripts": [ "node_modules/ux4g-web-components/dist/runtime/design-system.js" ] } ``` ## Code examples The snippets below are extracted from the Storybook canvas for Switch. Each example shows the rendered HTML, the equivalent React JSX, and the Angular template markup. ### 1. Description Variants — Description Variants HTML: ```html ``` React: ```jsx import React from 'react'; const SwitchExample1Demo = () => ( <> {/* Description: Helper */} {/* Description: Error */} {/* Description: Warning */} {/* Description: Success */} ); export default SwitchExample1Demo; ``` Angular: ```html ``` ### 2. Introduction — Introduction HTML: ```html
Components

Switch

Toggle component allows users to switch between two states typically representing on/off, enabled/disabled, or active/inactive settings. Toggles provide a quick and intuitive way to modify system or feature preferences with a single action.

State Showcase

Live previews taken directly from the switch states matrix in src/index.html.

OFF
ON
Default
Hover
Active
Focused
Disabled

Sizes

Large, medium, and small switch sizes from the source component section.

Large
Medium
Small

Label Variants

Full, required, label-only, and control-only variants from the switch showcase.

Full (all elements)
Label + required
Label only
Control only

Description Variants

Helper, error, warning, and success descriptions using source-backed switch description classes.

Helper
Error
Warning
Success

List Interaction Patterns

Whole-row toggle and switch-only toggle patterns from the list-switch source block.

Default (whole list item toggles switch)
Only switch toggles (list item not clickable)

CSS Class Reference

Quickly copy the classes used by the showcased Switch variants.

Structure

Switch wrapper ux4g-switch
Native checkbox input ux4g-switch-input
Control wrapper ux4g-switch-control
Track element ux4g-switch-track
Thumb element ux4g-switch-thumb
Content wrapper ux4g-switch-content
Header row ux4g-switch-header
Label text ux4g-switch-label
Description text row ux4g-switch-description

Variants

Large size ux4g-switch-lg
Medium size ux4g-switch-md
Small size ux4g-switch-sm
Required marker ux4g-switch-required
Icon treatment ux4g-switch-icon
Helper description ux4g-switch-desc-helper
Error description ux4g-switch-desc-error
Warning description ux4g-switch-desc-warning
Success description ux4g-switch-desc-success
List switch layout ux4g-list-switch
Whole-row switch pattern ux4g-list-switch-select
``` React: ```jsx import React from 'react'; const SwitchExample2Demo = () => ( <>
Components

Switch

Toggle component allows users to switch between two states typically representing on/off, enabled/disabled, or active/inactive settings. Toggles provide a quick and intuitive way to modify system or feature preferences with a single action.

State Showcase

Live previews taken directly from the switch states matrix in src/index.html .

OFF
ON
Default
Hover
Active
Focused
Disabled

Sizes

Large, medium, and small switch sizes from the source component section.

Large
Medium
Small

Label Variants

Full, required, label-only, and control-only variants from the switch showcase.

Full (all elements)
Label + required
Label only
Control only

Description Variants

Helper, error, warning, and success descriptions using source-backed switch description classes.

Helper
Error
Warning
Success

List Interaction Patterns

Whole-row toggle and switch-only toggle patterns from the list-switch source block.

Default (whole list item toggles switch)
Only switch toggles (list item not clickable)
  • Label list switch
  • Label list switch
  • Label list switch
  • Label list switch

CSS Class Reference

Quickly copy the classes used by the showcased Switch variants.

Structure

Switch wrapper ux4g-switch
Native checkbox input ux4g-switch-input
Control wrapper ux4g-switch-control
Track element ux4g-switch-track
Thumb element ux4g-switch-thumb
Content wrapper ux4g-switch-content
Header row ux4g-switch-header
Label text ux4g-switch-label
Description text row ux4g-switch-description

Variants

Large size ux4g-switch-lg
Medium size ux4g-switch-md
Small size ux4g-switch-sm
Required marker ux4g-switch-required
Icon treatment ux4g-switch-icon
Helper description ux4g-switch-desc-helper
Error description ux4g-switch-desc-error
Warning description ux4g-switch-desc-warning
Success description ux4g-switch-desc-success
List switch layout ux4g-list-switch
Whole-row switch pattern ux4g-list-switch-select
); export default SwitchExample2Demo; ``` Angular: ```html
Components

Switch

Toggle component allows users to switch between two states typically representing on/off, enabled/disabled, or active/inactive settings. Toggles provide a quick and intuitive way to modify system or feature preferences with a single action.

State Showcase

Live previews taken directly from the switch states matrix in src/index.html.

OFF
ON
Default
Hover
Active
Focused
Disabled

Sizes

Large, medium, and small switch sizes from the source component section.

Large
Medium
Small

Label Variants

Full, required, label-only, and control-only variants from the switch showcase.

Full (all elements)
Label + required
Label only
Control only

Description Variants

Helper, error, warning, and success descriptions using source-backed switch description classes.

Helper
Error
Warning
Success

List Interaction Patterns

Whole-row toggle and switch-only toggle patterns from the list-switch source block.

Default (whole list item toggles switch)
Only switch toggles (list item not clickable)

CSS Class Reference

Quickly copy the classes used by the showcased Switch variants.

Structure

Switch wrapper ux4g-switch
Native checkbox input ux4g-switch-input
Control wrapper ux4g-switch-control
Track element ux4g-switch-track
Thumb element ux4g-switch-thumb
Content wrapper ux4g-switch-content
Header row ux4g-switch-header
Label text ux4g-switch-label
Description text row ux4g-switch-description

Variants

Large size ux4g-switch-lg
Medium size ux4g-switch-md
Small size ux4g-switch-sm
Required marker ux4g-switch-required
Icon treatment ux4g-switch-icon
Helper description ux4g-switch-desc-helper
Error description ux4g-switch-desc-error
Warning description ux4g-switch-desc-warning
Success description ux4g-switch-desc-success
List switch layout ux4g-list-switch
Whole-row switch pattern ux4g-list-switch-select
``` ### 3. Label Variants — Label Variants HTML: ```html ``` React: ```jsx import React from 'react'; const SwitchExample3Demo = () => ( <> {/* Variant: Full */} {/* Variant: Label + required */} {/* Variant: Label Only */} {/* Variant: Control Only */} ); export default SwitchExample3Demo; ``` Angular: ```html ``` ### 4. List Interaction Patterns — List Interaction Patterns HTML: ```html ``` React: ```jsx import React from 'react'; const SwitchExample4Demo = () => ( <> {/* Pattern: default (whole list item toggles switch) */} {/* Pattern: Only switch toggles (list item not clickable) */} ); export default SwitchExample4Demo; ``` Angular: ```html ``` ### 5. Sizes — Sizes HTML: ```html ``` React: ```jsx import React from 'react'; const SwitchExample5Demo = () => ( <> {/* Size: Large */} {/* Size: Medium */} {/* Size: Small */} ); export default SwitchExample5Demo; ``` Angular: ```html ``` ### 6. States Matrix — States Matrix HTML: ```html ``` React: ```jsx import React from 'react'; const SwitchExample6Demo = () => ( <> {/* Default: Off */} {/* Default: On */} {/* Hover: Off */} {/* Hover: On */} {/* Active: Off */} {/* Active: On */} {/* Focused: Off */} {/* Focused: On */} {/* Disabled: Off */} {/* Disabled: On */} ); export default SwitchExample6Demo; ``` Angular: ```html ``` ## Accessibility notes - Use semantic HTML and required ARIA attributes where the Storybook example shows them. - Ensure visible focus indicators and keyboard operability. - Test color contrast against WCAG 2.1 AA.