Switch
shadcn-lit-registry/switchFreeComponent
A control that allows the user to toggle between checked and not checked. Built with Lit and styled using Tailwind CSS. Supports controlled and uncontrolled modes with form integration.
Install it
npx shadcn@latest add https://lit-registry.lloydrichards.dev/r/switch.jsonSource
1import { html, LitElement, nothing, type PropertyValues } from "lit";2import { customElement, property, state } from "lit/decorators.js";3import { TW } from "@/registry/lib/tailwindMixin";45export interface SwitchChangeEvent extends CustomEvent {6 detail: { checked: boolean };7}89export interface SwitchProperties {10 disabled?: boolean;11 required?: boolean;12 name?: string;13 value?: string;14 checked?: boolean;15 defaultChecked?: boolean;16}1718@customElement("ui-switch")19export class Switch extends TW(LitElement) implements SwitchProperties {20 static formAssociated = true;21 private internals: ElementInternals;2223 @property({ type: Boolean }) disabled = false;24 @property({ type: Boolean }) required = false;25 @property({ type: String }) name = "";26 @property({ type: String }) value = "on";2728 @property({ type: Boolean }) checked: boolean | undefined = undefined;29 @property({ type: Boolean, attribute: "default-checked" })30 defaultChecked = false;3132 @property({ type: String, attribute: "aria-label" }) accessor ariaLabel:33 | string34 | null = null;35 @property({ type: String, attribute: "aria-labelledby" })36 accessor ariaLabelledby: string | null = null;37 @property({ type: String, attribute: "aria-describedby" })38 accessor ariaDescribedby: string | null = null;3940 @state() private _checked = false;4142 private _labelClickHandler = (e: Event) => {43 const label = e.currentTarget as HTMLLabelElement;44 if (label.htmlFor === this.id && !this.disabled) {45 e.preventDefault();46 this.handleClick();47 }48 };4950 constructor() {51 super();52 this.internals = this.attachInternals();53 }5455 override connectedCallback() {56 super.connectedCallback();57 if (this.checked === undefined) {58 this._checked = this.defaultChecked;59 }60 this._setupLabelDelegation();61 }6263 override disconnectedCallback() {64 super.disconnectedCallback();65 this._cleanupLabelDelegation();66 }6768 private _setupLabelDelegation() {69 if (!this.id) return;70 const root = this.getRootNode() as Document | ShadowRoot;71 const labels = root.querySelectorAll(72 `label[for="${this.id}"]`,73 ) as NodeListOf<HTMLLabelElement>;74 labels.forEach((label) => {75 label.addEventListener("click", this._labelClickHandler);76 });77 }7879 private _cleanupLabelDelegation() {80 if (!this.id) return;81 const root = this.getRootNode() as Document | ShadowRoot;82 const labels = root.querySelectorAll(83 `label[for="${this.id}"]`,84// … truncated
Files it writes
More from shadcn-lit-registry
All 26 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | shadcn-lit-registry | Components | Free | 1 dep · 2 files | |
| Avataravatar | shadcn-lit-registry | Components | Free | no deps · 2 files | |
| Badgebadge | shadcn-lit-registry | Components | Free | no deps · 2 files | |
| Buttonbutton | shadcn-lit-registry | Components | Free | 1 dep · 2 files | |
| Cardcard | shadcn-lit-registry | Components | Free | no deps · 2 files | |
| Checkboxcheckbox | shadcn-lit-registry | Components | Free | 1 dep · 2 files | |
| Collapsiblecollapsible | shadcn-lit-registry | Components | Free | 1 dep · 2 files | |
| Commandcommand | shadcn-lit-registry | Components | Free | 1 dep · 2 files |
