Native Switch

A styled native HTML checkbox that looks like the Switch and works without JavaScript.

Basic
With Description
With Label
Disabled
Invalid
Sizes
<script lang="ts">
  import { Example, ExampleWrapper } from '#lib/components/example';
  import { Field, FieldContent, FieldDescription, FieldLabel, FieldTitle } from '#lib/components/ui/field';

Installation

pnpm dlx @sveltery/ui@latest add native-switch

Usage

<script lang="ts">
  import { NativeSwitch } from '#lib/components/ui/native-switch';
  import { Label } from '#lib/components/ui/label';
</script>
<div class="flex items-center gap-2">
  <NativeSwitch id="airplane-mode" name="airplane" />
  <Label for="airplane-mode">Airplane Mode</Label>
</div>

NativeSwitch is a real <input type="checkbox" role="switch">, and every prop goes on it. Mark the initial state with defaultChecked; checked sets the current state, which is what a remote form field passes. size takes default or sm.

The input is the track, and the thumb is a sibling that follows :checked. Both sit in a <span> that takes class and carries the Switch's data-slot="switch", so a Label or Field styles itself as it does next to a Switch.

When to use it

Use NativeSwitch when the form must work before the page hydrates or with JavaScript off. A click on the switch or its label, or Space, toggles it, and a checked switch submits with its form. Screen readers announce it as a switch.

Switch runs on the Sveltery Base Switch, which also gives you Base UI state attributes such as data-checked, bind:checked and onCheckedChange, and Base's Field behavior.

Remote forms

Spread a boolean remote form field onto the switch with as('checkbox'). See the example on Native Checkbox.

<Field orientation="horizontal">
  <FieldLabel for="digest">Weekly digest</FieldLabel>
  <NativeSwitch id="digest" {...f.fields.digest.as('checkbox')} />
</Field>