Skip to content

Toggle

A button that can be toggled between the on and off states.

Controls

Pressed: false

Features

  • Full keyboard navigation.

Usage

Importing Toggle gives you access the components.

MyComponent.svelte
<script lang="ts">
import { Toggle } from "@niklasburggraaff/svelte-runia";
</script>
<Toggle aria-label="...">
...
</Toggle>
  • The Toggle must be passed a labelledByContent, aria-label, or aria-labelledby prop
    • labelledByContent should be passed if the toggle contains text describing what the toggle does
    • Otherwise, the aria-label should describe what the toggle does when pressed
    • If the description is already in an element, use aria-labelledby to reference the id of that element

API

Toggle

button

The Toggle component the entire toggle.

Props

  • pressed:boolean = false (bindable)
    • Whether the toggle is pressed.
  • disabled:boolean = false
    • When true, prevents the user from interacting with the toggle.
  • labelledByContent:true (2)
    • Indicates the toggle contains text describing what the toggle does when pressed.
  • aria-label:string (1)
    • Description of what the toggle does when pressed.
  • aria-labelledby:string (1)
    • ID of element that describes what the toggle does when pressed.
  • element:HTMLButtonElement (bindable)
    • The rendered button element.
PropTypeDefaultDescription
pressed
boolean

(bindable)

false Whether the toggle is pressed.
disabled
boolean false When true, prevents the user from interacting with the toggle.
labelledByContent (1)
true -Indicates the toggle contains text describing what the toggle does when pressed.
aria-label (1)
string -Description of what the toggle does when pressed.
aria-labelledby (1)
string -ID of element that describes what the toggle does when pressed.
element
HTMLButtonElement

(bindable)

-The rendered button element.

(1)- One of labelledByContent aria-label aria-labelledby is required.

Data attributes

  • data-state: "on" | "off"
    • The state of the toggle.
  • data-disabled
    • Present when the toggle is disabled.
AttributeValuesDescription
[data-state] "on" | "off" The state of the toggle.
[data-disabled] -Present when the toggle is disabled.

Accessibility

Adheres to the Toggle button in WAI-ARIA Button Pattern.

Keyboard interactions

  • Enter/Space :

    Attempts to toggle the state of the toggle.

KeysConditionAction
Enter / SpaceIf focus on a TriggerAttempts to toggle the state of the toggle.

Other events

  • Toggle. onclick :

    Attempts to toggle the state of the toggle.

ComponentEventAction
ToggleonclickAttempts to toggle the state of the toggle.

ARIA attributes

Toggle

  • aria-pressed: "on" | "off"
    • "on" when the toggle is pressed and "off" when the toggle is not pressed.
  • aria-label: string
    • Description of what the toggle does when pressed.
  • aria-labelledby: string
    • ID of element that describes what the toggle does when pressed.
AttributeTypeDescription
aria-pressed "on" | "off" "on" when the toggle is pressed and "off" when the toggle is not pressed.
aria-label string Description of what the toggle does when pressed.
aria-labelledby string ID of element that describes what the toggle does when pressed.

Examples

Pressed by default

Use the pressed prop to set the initial state of the toggle to be pressed.

<Toggle pressed ...>
...
</Toggle>

Control the pressed state

Add label

Set the aria-label prop to the description of what the toggle does when pressed.

<Toggle aria-label="arm axe">...</Toggle>

Label by element

Set the aria-labelledby prop to the id of an element that describes what the toggle does when pressed.

<label id="axe-label">Arm axe</label>
<Toggle aria-labelledby="axe-label">...</Toggle>

Label by content

Set the labelledByContent prop to true to indicate the toggle contains text describing what the toggle does when pressed.

<Toggle labelledByContent>Arm axe</Toggle>