Toggle
shadcn-lit-registry/toggleFreeComponent
A two-state button that can be either on or off. Built with Lit and styled using Tailwind CSS. Supports multiple variants and sizes with form integration.
Install it
npx shadcn@latest add https://lit-registry.lloydrichards.dev/r/toggle.jsonSource
1import { cva, type VariantProps } from "class-variance-authority";2import { html, LitElement, nothing, type PropertyValues } from "lit";3import { customElement, property, state } from "lit/decorators.js";4import { TW } from "@/registry/lib/tailwindMixin";56export const toggleVariants = cva(7 "inline-flex items-center justify-center gap-2 rounded-md text-sm font-medium hover:bg-muted hover:text-muted-foreground disabled:pointer-events-none disabled:opacity-50 data-[state=on]:bg-accent data-[state=on]:text-accent-foreground [&>svg]:pointer-events-none [&>svg]:size-4 [&>svg]:shrink-0 focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] outline-none transition-[color,box-shadow] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive whitespace-nowrap",8 {9 variants: {10 variant: {11 default: "bg-transparent",12 outline:13 "border border-input bg-transparent shadow-xs hover:bg-accent hover:text-accent-foreground",14 },15 size: {16 default: "h-9 px-2 min-w-9",17 sm: "h-8 px-1.5 min-w-8",18 lg: "h-10 px-2.5 min-w-10",19 },20 },21 defaultVariants: {22 variant: "default",23 size: "default",24 },25 },26);2728type ToggleVariants = VariantProps<typeof toggleVariants>;2930export interface ToggleChangeEvent extends CustomEvent {31 detail: { pressed: boolean };32}3334export interface ToggleProperties {35 variant?: ToggleVariants["variant"];36 size?: ToggleVariants["size"];37 disabled?: boolean;38 pressed?: boolean;39 defaultPressed?: boolean;40}4142@customElement("ui-toggle")43export class Toggle extends TW(LitElement) implements ToggleProperties {44 static formAssociated = true;45 private internals: ElementInternals;4647 @property({ type: String }) variant: ToggleVariants["variant"] = "default";48 @property({ type: String }) size: ToggleVariants["size"] = "default";49 @property({ type: Boolean }) disabled = false;5051 @property({ type: Boolean }) pressed: boolean | undefined = undefined;52 @property({ type: Boolean, attribute: "default-pressed" })53 defaultPressed = false;5455 @property({ type: String, attribute: "aria-label" }) accessor ariaLabel:56 | string57 | null = null;5859 @state() private _pressed = false;6061 constructor() {62 super();63 this.internals = this.attachInternals();64 }6566 override connectedCallback() {67 super.connectedCallback();68 if (this.pressed === undefined) {69 this._pressed = this.defaultPressed;70// … truncated
What it pulls in
npm packages
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 |
