Skip to content
WebJs
Menu

A control that allows the user to toggle between checked and not checked.

import { html } from '@webjsdev/core';
import { labelClass } from '#components/ui/label.ts';

export default function SwitchDemo() {
  return html`
    <div class="flex items-center space-x-2">
      <ui-switch input-id="airplane-mode"></ui-switch>
      <label data-slot="label" for="airplane-mode" class=${labelClass()}>Airplane Mode</label>
    </div>
  `;
}
View Code

Installation

pnpm dlx @webjsdev/ui add switch

Usage

import '#components/ui/switch.ts';
<ui-switch aria-label="Airplane mode"></ui-switch>

Description

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

import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import {
  fieldClass, fieldContentClass, fieldLabelClass, fieldDescriptionClass,
} from '#components/ui/field.ts';

export default function SwitchDescription() {
  return html`
    <div role="group" data-slot="field" data-orientation="horizontal" class=${cn(fieldClass({ orientation: 'horizontal' }), 'max-w-sm')}>
      <div data-slot="field-content" class=${fieldContentClass()}>
        <label data-slot="field-label" for="switch-focus-mode" class=${fieldLabelClass()}>
          Share across devices
        </label>
        <p data-slot="field-description" class=${fieldDescriptionClass()}>
          Focus is shared across devices, and turns off when you leave the app.
        </p>
      </div>
      <ui-switch input-id="switch-focus-mode"></ui-switch>
    </div>
  `;
}
View Code

Choice Card

Card-style selection where the field label wraps the entire field for a clickable card pattern.

import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import {
  fieldGroupClass, fieldClass, fieldContentClass, fieldLabelClass,
  fieldTitleClass, fieldDescriptionClass,
} from '#components/ui/field.ts';

// The whole card is the <label>, so a click anywhere on it flips the switch.
export default function SwitchChoiceCard() {
  return html`
    <div data-slot="field-group" class=${cn(fieldGroupClass(), 'w-full max-w-sm')}>
      <label data-slot="field-label" for="switch-share" class=${fieldLabelClass()}>
        <div role="group" data-slot="field" data-orientation="horizontal" class=${fieldClass({ orientation: 'horizontal' })}>
          <div data-slot="field-content" class=${fieldContentClass()}>
            <div data-slot="field-label" class=${fieldTitleClass()}>Share across devices</div>
            <p data-slot="field-description" class=${fieldDescriptionClass()}>
              Focus is shared across devices, and turns off when you leave the
              app.
            </p>
          </div>
          <ui-switch input-id="switch-share"></ui-switch>
        </div>
      </label>
      <label data-slot="field-label" for="switch-notifications" class=${fieldLabelClass()}>
        <div role="group" data-slot="field" data-orientation="horizontal" class=${fieldClass({ orientation: 'horizontal' })}>
          <div data-slot="field-content" class=${fieldContentClass()}>
            <div data-slot="field-label" class=${fieldTitleClass()}>Enable notifications</div>
            <p data-slot="field-description" class=${fieldDescriptionClass()}>
              Receive notifications when focus mode is enabled or disabled.
            </p>
          </div>
          <ui-switch input-id="switch-notifications" checked></ui-switch>
        </div>
      </label>
    </div>
  `;
}
View Code

Disabled

Add the disabled attribute to <ui-switch> to disable the switch. Add the data-disabled attribute to the field for styling.

import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { fieldClass, fieldLabelClass } from '#components/ui/field.ts';

export default function SwitchDisabled() {
  return html`
    <div role="group" data-slot="field" data-orientation="horizontal" data-disabled="true" class=${cn(fieldClass({ orientation: 'horizontal' }), 'w-fit')}>
      <ui-switch input-id="switch-disabled-unchecked" disabled></ui-switch>
      <label data-slot="field-label" for="switch-disabled-unchecked" class=${fieldLabelClass()}>Disabled</label>
    </div>
  `;
}
View Code

Invalid

Add the aria-invalid attribute to <ui-switch> to indicate an invalid state. Add the data-invalid attribute to the field for styling.

You must accept the terms and conditions to continue.

import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import {
  fieldClass, fieldContentClass, fieldLabelClass, fieldDescriptionClass,
} from '#components/ui/field.ts';

export default function SwitchInvalid() {
  return html`
    <div role="group" data-slot="field" data-orientation="horizontal" data-invalid="true" class=${cn(fieldClass({ orientation: 'horizontal' }), 'max-w-sm')}>
      <div data-slot="field-content" class=${fieldContentClass()}>
        <label data-slot="field-label" for="switch-terms" class=${fieldLabelClass()}>
          Accept terms and conditions
        </label>
        <p data-slot="field-description" class=${fieldDescriptionClass()}>
          You must accept the terms and conditions to continue.
        </p>
      </div>
      <ui-switch input-id="switch-terms" aria-invalid="true"></ui-switch>
    </div>
  `;
}
View Code

Size

Use the size attribute to change the size of the switch.

import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { fieldGroupClass, fieldClass, fieldLabelClass } from '#components/ui/field.ts';

export default function SwitchSizes() {
  return html`
    <div data-slot="field-group" class=${cn(fieldGroupClass(), 'w-full max-w-[10rem]')}>
      <div role="group" data-slot="field" data-orientation="horizontal" class=${fieldClass({ orientation: 'horizontal' })}>
        <ui-switch input-id="switch-size-sm" size="sm"></ui-switch>
        <label data-slot="field-label" for="switch-size-sm" class=${fieldLabelClass()}>Small</label>
      </div>
      <div role="group" data-slot="field" data-orientation="horizontal" class=${fieldClass({ orientation: 'horizontal' })}>
        <ui-switch input-id="switch-size-default" size="default"></ui-switch>
        <label data-slot="field-label" for="switch-size-default" class=${fieldLabelClass()}>Default</label>
      </div>
    </div>
  `;
}
View Code

RTL

To enable RTL support, see the RTL configuration guide.

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

import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import {
  fieldClass, fieldContentClass, fieldLabelClass, fieldDescriptionClass,
} from '#components/ui/field.ts';

const translations = {
  en: {
    dir: 'ltr',
    values: {
      label: 'Share across devices',
      description:
        'Focus is shared across devices, and turns off when you leave the app.',
    },
  },
  ar: {
    dir: 'rtl',
    values: {
      label: 'المشاركة عبر الأجهزة',
      description:
        'يتم مشاركة التركيز عبر الأجهزة، ويتم إيقاف تشغيله عند مغادرة التطبيق.',
    },
  },
  he: {
    dir: 'rtl',
    values: {
      label: 'שיתוף בין מכשירים',
      description: 'המיקוד משותף בין מכשירים, וכבה כשאתה עוזב את האפליקציה.',
    },
  },
};

export default function SwitchRtl() {
  const lang = 'ar';
  const { dir, values: t } = translations[lang];

  return html`
    <div role="group" data-slot="field" data-orientation="horizontal" dir=${dir} lang=${lang} class=${cn(fieldClass({ orientation: 'horizontal' }), 'max-w-sm')}>
      <div data-slot="field-content" class=${fieldContentClass()}>
        <label data-slot="field-label" for="switch-focus-mode-rtl" class=${fieldLabelClass()}>
          ${t.label}
        </label>
        <p data-slot="field-description" class=${fieldDescriptionClass()}>${t.description}</p>
      </div>
      <ui-switch input-id="switch-focus-mode-rtl"></ui-switch>
    </div>
  `;
}
View Code

API Reference

ui-switch

<ui-switch> renders a switch, a root and a thumb, around a real <input type="checkbox" role="switch">. The checkbox is rendered on the server, so the switch submits with its form, takes a <label for>, and toggles with JavaScript off.

AttributeTypeDefault
size"default" | "sm""default"
checkedbooleanfalse
disabledbooleanfalse
requiredbooleanfalse
namestring-
valuestring-
input-idstring-

input-id becomes the checkbox's id, for a <label for>. aria-label, aria-labelledby and aria-describedby on the element are passed to the checkbox. A switch with no label pointing at it needs an aria-label.

EventDetail
ui-checked-change{ checked: boolean }

switchInputClass and switchTrackClass

For a switch as plain markup, put switchInputClass() on a hidden <input type="checkbox" role="switch"> and switchTrackClass({ size }) on the <span> after it. size is "default" or "sm".