Toggle

The <nys-toggle> allows users to toggle a toggle switch "on" or "off".

Copy Code
<nys-toggle 
  label="Dark Mode"
  description="Enable dark mode for a more comfortable viewing experience."
  name="toggle-switch"
  value="access"
></nys-toggle>

Can't use NYS Design System web components in your project? Try using the CSS Variables instead.

Accessibility

The <nys-toggle> component includes the following accessibility-focused features:

  • Proper ARIA roles and attributes to ensure screen readers can interpret the toggle correctly.

  • Keyboard navigation support, allowing users to toggle the toggle switch using the keyboard.

  • Visual focus indicators to help users navigate the component.

  • Include a label property to provide accessible text for screen readers.

    Additional accessibility test and build content is coming soon, in a new format.

Options

Checked

Copy Code
<nys-toggle label="Dark Mode" name="theme" value="dark" checked></nys-toggle>

Help Text

This slot is called 'description' (learn more)


Copy Code
<nys-toggle label="Toggle Switch" name="toggle-switch" value="access">
  <p slot="description">This slot is called 'description' (<a href="https://www.ny.gov/" target="_blank">learn more</a>)</p>
</nys-toggle>
<br/>
<nys-toggle
  label="Toggle Switch"
  description="This description was passed in as a property"
  name="toggle-switch"
  value="access">
</nys-toggle>

Sizes


Copy Code
<nys-toggle size="sm" label='Small (size="sm")' name="toggle-switch" value="access"></nys-toggle><br>
<nys-toggle size="md" label='Medium (size="md")' name="toggle-switch" value="access"></nys-toggle>

Slotted Description

Add help text to the toggle using the label and description props. Descriptions can be provided either through the description prop or via the slot="description".

Note: Use the prop for simple text. Use the slot when you need custom HTML, such as links or formatting.

This slot is called 'description' (learn more)

Copy Code
<nys-toggle label="Toggle Switch" name="toggle-switch" value="access">
  <p slot="description">This slot is called 'description' (<a href="https://www.ny.gov/" target="_blank">learn more</a>)</p>
</nys-toggle>

Disabled Toggle

Copy Code
<nys-toggle label="Disabled Unchecked" name="toggle-switch" value="access" disabled></nys-toggle>

Disable Icon

Copy Code
<nys-toggle noIcon label="No Icon on the toggle" name="toggle-switch" value="access"></nys-toggle>

Inverted

Set the inverted when the component is on a dark background.

Copy Code
<nys-toggle
  label="Toggle Switch"
  description="This description was passed in as a property"
  name="toggle-switch"
  value="access"
  inverted>
</nys-toggle>

Usage

Do

  • Provide a clear label and optional description to explain what the toggle controls.

  • Ensure the page clearly reflects the toggle's on/off state through visible changes.

  • Use when the state change will be implemented immediately.

Don't

  • Don't use for forms where immediate action isn't expected. Use <nys-checkbox> instead.

  • Don't use for selecting one or more options from a list. Use <nys-checkbox> or <nys-radiobutton> instead.

  • Don't use for complex or multi-state choices.

  • Don't overuse for minor settings that don't affect the user experience.

  • Don't hide labels unless an accessible alternative is in place.

  • Properties

    Property Type Default
    id String ""
    label String ""
    name String ""
    checked boolean false
    description String ""
    disabled boolean false
    noIcon boolean false
    size "sm" , "md" "md"
    value String ""
    form String , null null

    Form Prop

    The form property associates this component with a <form> element by ID, even if the component is not a descendant of that form. See Form Patterns for details on form association and ElementInternals.

    Style Overrides

    There are no existing CSS variables for this component. Explore existing options, or propose a new one with a Component Proposal.

    Events

    Some components emit nys- events with a detail object containing the relevant data, instead of relying on native events alone. Stick to one approach per interaction, don't mix native and nys- listeners for the same thing.

    The <nys-toggle> component emits three custom Javascript events:

    Event details

    Name Description Return details
    nys-change Fired when the toggle state changes (checked/unchecked).
    • id (string): The id of the toggle.
    • checked (boolean): Whether the toggle is currently checked.
    nys-focus Fired when the toggle gains focus. —
    nys-blur Fired when the toggle loses focus. —

    You can listen to these events using JavaScript:

    Copy Code
    // Select the toggle component
    const toggle = document.querySelector('nys-toggle');
    // Listen for the 'nys-change' event
    toggle.addEventListener('nys-change', (event) => {
      const { checked } = event.detail;
      console.log('Toggle changed. Checked:', checked);
    });

    Dependencies

    This component automatically imports the following dependencies:

    • <nys-icon>
    • <nys-label>

    Suggest a New Component

    Do you have an idea for a new NYS Design System web component? Look through the existing proposals in our GitHub discussions board to see if someone already proposed something similar. If not, feel free to submit one.