Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185import { hDom as h, } from '@monochromatic-dev/module-hyperscript/ts';import { css, } from '../css.ts';
/** Shadow DOM styles for the `\<toggle-switch\>` component. */const STYLES = css(` :host { display: inline-flex; cursor: pointer; inline-size: 3rem; block-size: 2rem; } .track { inline-size: 100%; block-size: 100%; border-width: calc(1 / 16 * 1rem); border-style: solid; border-color: var(--fg); @apply --border-radius-full; background-color: var(--bg); position: relative; overflow: hidden; } .thumb { position: absolute; inset-block-start: 50%; transform: translateY(-50%); inline-size: 2rem; block-size: 2rem; @apply --border-radius-full; border-width: calc(1 / 16 * 1rem); border-style: solid; border-color: var(--fg); background-color: var(--bg-stronger); @apply --flex-center; font-size: 1rem; transition: inset-inline-start 0.15s, inset-inline-end 0.15s; } .thumb.on { inset-inline-end: calc(-1 / 16 * 1rem); inset-inline-start: auto; } .thumb.off { inset-inline-start: calc(-1 / 16 * 1rem); inset-inline-end: auto; }`,);
/** `\<toggle-switch\>`: boolean toggle with animated thumb. Reflects state via the `on` attribute and dispatches a `change` event on toggle. */class ToggleSwitch extends HTMLElement { /** Attributes to observe for re-rendering. */ static observedAttributes = ['on',];
/** Shadow root for encapsulated rendering. */ readonly #shadow: ShadowRoot;
/** Click handler pre-bound to this instance for stable add/removeEventListener references. */ readonly #boundHandleClick = this.#handleClick .bind(this,);
/** Initializes the shadow root. */ constructor() { super(); this.#shadow = this.attachShadow({ mode: 'open', },); }
/** Whether the toggle is currently in the "on" state. @returns True when the `on` attribute is present */ get on(): boolean { return this.hasAttribute('on',); }
/** Sets or removes the `on` attribute to reflect toggle state. @param value - New toggle state */ set on(value: boolean,) { if (value) { this.setAttribute( 'on', '', ); } else { this.removeAttribute('on',); } }
/** Renders initial content and attaches the click handler. */ connectedCallback(): void { this.#render(); this.addEventListener( 'click', this.#boundHandleClick, ); }
/** Removes the click handler on disconnect. */ disconnectedCallback(): void { this.removeEventListener( 'click', this.#boundHandleClick, ); }
/** Re-renders when observed attributes change. */ attributeChangedCallback(): void { this.#render(); }
/** Toggles state and dispatches a change event. Registered directly as the click handler on the host element, so `this` resolves to this component (the listener's `currentTarget`) without binding. */ #handleClick(): void { this.on = !this.on; this.dispatchEvent( new CustomEvent( 'change', { detail: { on: this.on, }, bubbles: true, }, ), ); }
/** Renders the track and thumb into the shadow root. */ #render(): void { /** Current toggle state, used to pick the thumb position class and glyph. */ const isOn = this.on; this.#shadow .replaceChildren( h({ tag: 'style', text: STYLES, },), h({ tag: 'div', class: 'track', children: [ h({ tag: 'span', class: `thumb ${isOn ? 'on' : 'off'}`, text: isOn ? '\u2713' : '\u2717', },), ], },), ); }}
customElements.define( 'toggle-switch', ToggleSwitch,);