Skip to content
Nana UI

Forms & Inputs

Switch

A form-associated on/off switch. Works inside a native <form>, supports keyboard toggling, and mirrors native checkbox semantics for submission.

View .md Download
Enable notifications

Install the package for a versioned dependency:

npm install @nana-tec/ui-components

Or copy the source in and own it:

npx @nana-tec/ui add switch --framework web-components
npx @nana-tec/ui add switch --framework svelte

Lands in src/lib/components/ui/switch/ as NanaSwitch.svelte, styled with Tailwind classes that resolve to the design tokens. Requires Tailwind v4.

Import the autoloader once, in your app's entry point. Every <nana-*> tag then works anywhere, and each component downloads only the first time a page uses it:

import "@nana-tec/ui-components/autoloader";
import "@nana-tec/ui-components/theme";

The second line gives every component the Nana look and the design tokens behind it. Leave it out to style the components entirely yourself.

Import a component directly when it must be ready at once: on screen at first paint, or when you call its methods straight away. The two mix freely:

import "@nana-tec/ui-components/switch";

Copied in with npx @nana-tec/ui add switch? Import your project's own @/lib/nana-ui/autoloader.js once instead; the CLI keeps it up to date.

<nana-switch>Enable notifications</nana-switch>
<script lang="ts">
import { NanaSwitch } from '$lib/components/ui/switch';
let enabled = $state(true);
</script>
<NanaSwitch label="Email notifications" bind:checked={enabled} />

It is a real <input type="checkbox"> under the visual track, so it submits with a form and announces as a switch. checked is $bindable.

Small Medium Large
On Off Disabled on Disabled off

Set invalid to mark it invalid. error-message shows beneath it only while invalid is set, and is linked to the control as its description.

Enable notifications

Toggling the switch clears invalid.

Properties

PropertyAttributeTypeDefaultDescription
checkedcheckedbooleanfalseOn/off state.
descriptiondescriptionstring | undefined—Secondary descriptive text.
disableddisabledbooleanfalseDisable the control.
errorMessageerror-messagestring''Error under the field while it is invalid, in the error part with role="alert".
invalidinvalidbooleanfalseMarks the field invalid and shows error-message in place of the helper text.
labellabelstring | undefined—Convenience label text (slotted content wins).
namenamestring''Form field name.
requiredrequiredbooleanfalseMark as required for form validation.
sizesizeSwitchSize'md'Control size.
valuevaluestring'on'Submitted value when checked (default "on").

Events

EventDescription
nana-changeFired on toggle (detail.checked).

Slots

SlotDescription
(default)The label (overrides the label attribute).
descriptionSecondary descriptive text.

CSS parts

PartDescription
::part(base)The clickable row.
::part(control)The track.
::part(thumb)The sliding thumb.
::part(label)The label text.
::part(text)The text
::part(required)The required
::part(description)The description
::part(error)The error message, shown while invalid is set

CSS custom properties

PropertyDescription
--nana-switch-accentChecked track colour.
--nana-switch-thumbThumb colour.

NanaSwitch

PropTypeDefault
sizeSwitchSize'md'
checkedbind:booleanfalse
labelstring
descriptionstring
invalidbind:booleanfalse
errorMessagestring
childrenSnippet
onCheckedChange(checked: boolean) => void

Also accepts Omit<HTMLInputAttributes, 'class' | 'size' | 'type' | 'checked'>.