Zubra UI
Code for
GitHub

toggle-switch

An on/off switch with a label and a description. A form control (submits "on" when checked), switched off by disabled or a disabled fieldset.

Try it: events appear here (checkedchange).

Source: toggle-switch.zb, and its registry item.

Install

Compiled (any project)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/toggle-switch.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/toggle-switch.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/toggle-switch.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/toggle-switch.json
One file, no dependencies (2.5 KB gzipped)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/toggle-switch.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/toggle-switch.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/toggle-switch.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/toggle-switch.json
.zb source (to edit it with Zubra)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/toggle-switch.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/toggle-switch.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/toggle-switch.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/toggle-switch.json
No build: a script tag, straight from this site
HTML
<link rel="stylesheet" href="https://zubra-js.github.io/zubra-ui/themes/default.css">
<script type="module" src="https://zubra-js.github.io/zubra-ui/cdn/toggle-switch.js"></script>

The compiled install writes src/components/zubra/toggle-switch.js and the theme, and adds @zubra/runtime to your dependencies. The one-file install writes the same path as a plain custom element that needs nothing installed (or download it and load it with a <script type="module">); it's smaller on a page with one to three components, the shared runtime with more.

Usage

index.html
<toggle-switch
  label="Email notifications"
  description="A summary of your week, every Monday."
  name="email"
></toggle-switch>

<script type="module">
  import './components/zubra/toggle-switch.js';

  const element = document.querySelector('toggle-switch');
  element.checked = true;
  element.addEventListener('checkedchange', (event) => console.log('checkedchange', event.detail));
</script>
Example.jsx
import './components/zubra/toggle-switch.js';

const checked = true;

export function ToggleSwitchExample() {
  return (
    <toggle-switch
      label="Email notifications"
      description="A summary of your week, every Monday."
      name="email"
      checked={checked}
      oncheckedchange={(event) => console.log('checkedchange', event.detail)}
    ></toggle-switch>
  );
}
Example.vue
<script setup>
import './components/zubra/toggle-switch.js';
const checked = true;
const onCheckedchange = (event) => console.log('checkedchange', event.detail);
</script>

<template>
  <toggle-switch
    label="Email notifications"
    description="A summary of your week, every Monday."
    name="email"
    :checked.prop="checked"
    @checkedchange="onCheckedchange"
  ></toggle-switch>
</template>

<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) -->
Example.svelte
<script>
  import './components/zubra/toggle-switch.js';
  const checked = true;
</script>

<toggle-switch
  label="Email notifications"
  description="A summary of your week, every Monday."
  name="email"
  {checked}
  oncheckedchange={(event) => console.log('checkedchange', event.detail)}
></toggle-switch>
example.component.ts
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/toggle-switch.js';

@Component({
  selector: 'app-toggle-switch-example',
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  template: `
    <toggle-switch
      label="Email notifications"
      description="A summary of your week, every Monday."
      name="email"
      [checked]="checked"
      (checkedchange)="onCheckedchange($event)"
    ></toggle-switch>
  `,
})
export class ToggleSwitchExample {
  checked = true;

  onCheckedchange(event: Event) {
    console.log('checkedchange', (event as CustomEvent).detail);
  }
}

API

Properties (each is also an HTML attribute of the same name, unless marked)
PropertyTypeDefault
checkedbooleanfalse
disabledbooleanfalse
labelstring""
descriptionstring""
extrastring""
Events
EventWhen
checkedchangeIt was switched; detail is the new state.

Size

1.1 KB gzipped for the component, plus the runtime, which every Zubra component on a page shares (up to 22.1 KB, all of it). On its own, with the parts of the runtime it uses, it's 17.3 KB.

Theme

Every component reads shadcn/ui's CSS variables, so any shadcn theme works. Pick a look or a colour, or paste a theme's CSS.

Look: colours, fonts, corners and motion

Accent colour