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
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
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
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
<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
<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>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>
);
}<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('-') } } }) --><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>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
| Property | Type | Default |
|---|---|---|
checked | boolean | false |
disabled | boolean | false |
label | string | "" |
description | string | "" |
extra | string | "" |
| Event | When |
|---|---|
checkedchange | It 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.