Skip to content
loseta

Components / Forms

Switch

A control that toggles between on and off.

Status stableWorks with field, label
<label class="label" for="demo-switch">
  <input class="switch" type="checkbox" role="switch" id="demo-switch" checked>
  Airplane mode
</label>

Installation

pnpm dlx loseta add switch
npx loseta add switch

Examples

With description

Focus is shared across devices, and turns off when you leave the app.

<div class="field" data-orientation="horizontal" style="max-width: 24rem">
  <input class="switch" type="checkbox" role="switch" id="demo-switch-description" checked>
  <section>
    <label class="label" for="demo-switch-description">Share across devices</label>
    <p>Focus is shared across devices, and turns off when you leave the app.</p>
  </section>
</div>

Choice card

<label class="label" for="demo-switch-focus" style="max-width: 24rem">
  <div class="field" data-orientation="horizontal">
    <section>
      <h3>Share across devices</h3>
      <p>Focus is shared across devices, and turns off when you leave the app.</p>
    </section>
    <input class="switch" type="checkbox" role="switch" id="demo-switch-focus" checked>
  </div>
</label>

Disabled

<div style="display: flex; flex-direction: column; gap: 1rem">
  <div class="field">
    <label class="label" for="demo-switch-disabled-unchecked">
      <input class="switch" type="checkbox" role="switch" id="demo-switch-disabled-unchecked" disabled>
      Disabled (unchecked)
    </label>
  </div>
  <div class="field">
    <label class="label" for="demo-switch-disabled-checked">
      <input class="switch" type="checkbox" role="switch" id="demo-switch-disabled-checked" checked disabled>
      Disabled (checked)
    </label>
  </div>
</div>

Invalid

You must accept the terms and conditions to continue.

<div class="field" data-orientation="horizontal" aria-invalid="true" style="max-width: 24rem">
  <input class="switch" type="checkbox" role="switch" id="demo-switch-invalid" aria-invalid="true">
  <section>
    <label class="label" for="demo-switch-invalid">Accept terms and conditions</label>
    <p role="alert">You must accept the terms and conditions to continue.</p>
  </section>
</div>

Size

<div style="display: flex; flex-direction: column; gap: 1rem">
  <div class="field" data-orientation="horizontal">
    <input class="switch" type="checkbox" role="switch" id="demo-switch-sm" data-size="sm" checked>
    <label class="label" for="demo-switch-sm">Small</label>
  </div>
  <div class="field" data-orientation="horizontal">
    <input class="switch" type="checkbox" role="switch" id="demo-switch-default" data-size="default" checked>
    <label class="label" for="demo-switch-default">Default</label>
  </div>
</div>

Right-to-left

يتم مشاركة التركيز عبر الأجهزة، ويتوقف عند مغادرة التطبيق.

<div dir="rtl" lang="ar" style="max-width: 24rem">
  <div class="field" data-orientation="horizontal">
    <input class="switch" type="checkbox" role="switch" id="demo-switch-rtl" checked>
    <section>
      <label class="label" for="demo-switch-rtl">المشاركة عبر الأجهزة</label>
      <p>يتم مشاركة التركيز عبر الأجهزة، ويتوقف عند مغادرة التطبيق.</p>
    </section>
  </div>
</div>

Accessibility and behavior

  • Use input[type="checkbox"] with role="switch" and the .switch class. Compose rows with .field[data-orientation="horizontal"] and a label; a label wrapping a .field becomes a selectable card. data-size="sm" shrinks the control and aria-invalid="true" marks it invalid. Native checked and disabled states need no JavaScript.

Files

components/switch.cssbase
styles/nova/switch.cssstyle

Install contract

The CLI writes the base file under components/, the visual file for the selected style under styles/<name>/, and any primitive dependency underprimitives/. The generated index.css imports them in the right layer order.

← Back to components

loseta is MIT licensed. Built with native CSS, no runtime. Inspired byshadcn/ui.