Switch

A switch turns a setting on or off, and the change applies at once, with nothing to confirm. Where a choice only takes effect once a form is submitted, use a checkbox instead.

Usage

<script lang="ts">
	import { Switch } from 'noph-ui'
</script>

<Switch />

Disable

<script lang="ts">
	import { Switch } from 'noph-ui'
</script>

<Switch disabled />

Icons

<script lang="ts">
	import { Switch } from 'noph-ui'
</script>

<Switch icons="selected" />
<Switch icons="both" />

Theming

Tokens

TokenDefault value
--np-switch-selected-track-color--np-color-primary
--np-switch-selected-handle-color--np-color-on-primary
--np-switch-selected-icon-color--np-color-primary
--np-switch-unselected-track-color--np-color-surface-container-highest
--np-switch-unselected-track-outline-color--np-color-outline
--np-switch-unselected-handle-color--np-color-outline
--np-switch-handle-shape--np-shape-corner-full
--np-switch-state-layer-shape--np-shape-corner-full
--np-switch-state-layer-size2.5rem
--np-switch-disabled-unselected-track-color--np-color-surface-container-highest
--np-switch-disabled-selected-track-color--np-color-on-surface
--np-switch-disabled-track-opacity0.12
--np-switch-disabled-unselected-handle-opacity0.38
--np-switch-disabled-selected-handle-opacity1

--np-switch-selected-icon-color colors the icon inside the handle, which only shows with icons="selected" or icons="both". The disabled tokens take a colour and an opacity separately, because the disabled switch is drawn by fading the same colours rather than by swapping in pre-faded ones.

Example

<script lang="ts">
	import { Switch } from 'noph-ui'
</script>

<Switch
	selected
	icons="both"
	--np-switch-selected-track-color="var(--np-color-tertiary)"
	--np-switch-selected-handle-color="var(--np-color-on-tertiary)"
	--np-switch-selected-icon-color="var(--np-color-tertiary)"
/>

Accessibility

The switch is a native checkbox reporting role="switch", so it is announced as on or off rather than checked. Space toggles it, and Enter does too, which is what people expect from a setting that applies at once.

It carries no label of its own: wrap it in a <label> or pass aria-label. Write that name for the setting, Auto refresh, not for the current state. The optional icons on the handle are decoration, and a screen reader never reads them.

API

Switch

Attributes

AttributeTypeDescription
disabledbooleanDisables the switch.
icons'selected' | 'both' | undefinedControls the visibility of icons. Determines if only one icon is shown when selected, if both are shown (which also increases the size of the indicator when unchecked) or none for never showing an icon.

Bindables

AttributeTypeDescription
selectedbooleanThe state of the switch. True indicates the switch is on.
elementHTMLDivElementA reference to the root DOM element of the component. This variable is bound using bind:this, allowing direct access to the underlying HTML element for manipulation or querying within the component's logic.
inputElementHTMLInputElement | undefinedAllows access to the underlying native checkbox input element.