Download ave-switch.js from Avenislo/ave: direct link, hf CLI and curl.
- Browser
- Download file 6.55 kB
-
https://huggingface.co/spaces/Avenislo/ave/resolve/main/ave-switch.js
- Command line
-
hf download hf://spaces/Avenislo/ave/ave-switch.js
-
curl -L -o ave-switch.js https://huggingface.co/spaces/Avenislo/ave/resolve/main/ave-switch.js
6.55 kB
| import { BaseComponent } from "./ave-base-component.js"; | |
| class AveSwitch extends BaseComponent { | |
| static get observedAttributes() { | |
| return ['checked', 'disabled', 'name', 'value', 'id']; | |
| } | |
| constructor() { | |
| super(); | |
| this._handleChange = this._handleChange.bind(this); | |
| } | |
| // Property getters/setters | |
| get checked() { | |
| const switchElement = this.shadowRoot?.querySelector('.switch-input'); | |
| return switchElement ? switchElement.checked : (this._currentChecked !== undefined ? this._currentChecked : this.hasAttribute('checked')); | |
| } | |
| set checked(value) { | |
| const isChecked = value === true || value === 'true' || value === ''; | |
| this._currentChecked = isChecked; | |
| if (isChecked) { | |
| this.setAttribute('checked', ''); | |
| } else { | |
| this.removeAttribute('checked'); | |
| } | |
| const switchElement = this.shadowRoot?.querySelector('.switch-input'); | |
| const trackElement = this.shadowRoot?.querySelector('.switch-track'); | |
| if (switchElement) { | |
| switchElement.checked = isChecked; | |
| } | |
| if (trackElement) { | |
| trackElement.setAttribute('data-state', isChecked ? 'checked' : 'unchecked'); | |
| } | |
| } | |
| get disabled() { return this.hasAttribute('disabled'); } | |
| set disabled(value) { | |
| if (value === true || value === 'true' || value === '') { | |
| this.setAttribute('disabled', ''); | |
| } else { | |
| this.removeAttribute('disabled'); | |
| } | |
| } | |
| get name() { return this.getAttribute('name') || ''; } | |
| set name(value) { this.setAttribute('name', value); } | |
| get value() { return this.getAttribute('value') || 'on'; } | |
| set value(value) { this.setAttribute('value', value); } | |
| get switchId() { return this.getAttribute('id') || ''; } | |
| set switchId(value) { this.setAttribute('id', value); } | |
| setupEventListeners() { | |
| this._registerEvent('.switch-input', 'change', this._handleChange); | |
| this._registerEvent('.switch-track', 'click', this._handleTrackClick.bind(this)); | |
| } | |
| _handleTrackClick(e) { | |
| if (this.disabled) return; | |
| const input = this.shadowRoot.querySelector('.switch-input'); | |
| if (input) { | |
| input.checked = !input.checked; | |
| this._handleChange({ target: input }); | |
| // Update the visual state | |
| const track = this.shadowRoot.querySelector('.switch-track'); | |
| if (track) { | |
| track.setAttribute('data-state', input.checked ? 'checked' : 'unchecked'); | |
| } | |
| } | |
| } | |
| _handleChange(e) { | |
| // Don't update the attribute during user interaction to prevent re-rendering | |
| // Just store the state internally and update visual state | |
| this._currentChecked = e.target.checked; | |
| // Update visual state | |
| const track = this.shadowRoot.querySelector('.switch-track'); | |
| if (track) { | |
| track.setAttribute('data-state', e.target.checked ? 'checked' : 'unchecked'); | |
| } | |
| this._dispatch('ave-switch-change', { | |
| checked: e.target.checked, | |
| value: this.value, | |
| name: this.name | |
| }); | |
| } | |
| _getSwitchClasses() { | |
| return [ | |
| 'peer', | |
| 'inline-flex', | |
| 'h-6', | |
| 'w-11', | |
| 'shrink-0', | |
| 'cursor-pointer', | |
| 'items-center', | |
| 'rounded-full', | |
| 'border-2', | |
| 'border-transparent', | |
| 'transition-colors', | |
| 'focus-visible:outline-none', | |
| 'focus-visible:ring-2', | |
| 'focus-visible:ring-offset-2', | |
| 'disabled:cursor-not-allowed', | |
| 'disabled:opacity-50' | |
| ].join(' '); | |
| } | |
| render() { | |
| const switchClasses = this._getSwitchClasses(); | |
| const isChecked = this.hasAttribute('checked'); | |
| this.shadowRoot.innerHTML = ` | |
| <style> | |
| :host { | |
| display: inline-block; | |
| } | |
| .switch-container { | |
| position: relative; | |
| display: inline-flex; | |
| align-items: center; | |
| } | |
| .switch-track { | |
| position: relative; | |
| display: inline-flex; | |
| height: 1.5rem; | |
| width: 2.75rem; | |
| shrink: 0; | |
| cursor: pointer; | |
| align-items: center; | |
| border-radius: 9999px; | |
| border: 2px solid transparent; | |
| background-color: var(--input); | |
| transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); | |
| ring-offset-background: var(--background); | |
| } | |
| .switch-track:focus-visible { | |
| outline: none; | |
| box-shadow: 0 0 0 2px var(--background), 0 0 0 4px var(--ring); | |
| } | |
| .switch-track[data-state="checked"] { | |
| background-color: var(--primary); | |
| } | |
| .switch-track:disabled { | |
| cursor: not-allowed; | |
| opacity: 0.5; | |
| } | |
| .switch-thumb { | |
| pointer-events: none; | |
| display: block; | |
| height: 1.25rem; | |
| width: 1.25rem; | |
| border-radius: 50%; | |
| background-color: var(--background); | |
| box-shadow: var(--shadow-lg); | |
| transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1); | |
| transform: translateX(0); | |
| border: 1px solid var(--border); | |
| } | |
| .switch-track[data-state="checked"] .switch-thumb { | |
| transform: translateX(1.25rem); | |
| } | |
| /* Enhanced hover states */ | |
| .switch-track:hover:not(:disabled) { | |
| opacity: 0.9; | |
| } | |
| .switch-track[data-state="checked"]:hover:not(:disabled) { | |
| background-color: color-mix(in srgb, var(--primary) 90%, transparent); | |
| } | |
| .switch-input { | |
| position: absolute; | |
| opacity: 0; | |
| pointer-events: none; | |
| } | |
| </style> | |
| <div class="switch-container"> | |
| <input | |
| type="checkbox" | |
| role="switch" | |
| class="switch-input" | |
| ${isChecked ? 'checked' : ''} | |
| ${this.disabled ? 'disabled' : ''} | |
| ${this.name ? `name="${this.name}"` : ''} | |
| ${this.switchId ? `id="${this.switchId}"` : ''} | |
| value="${this.value}" | |
| > | |
| <div class="switch-track" data-state="${isChecked ? 'checked' : 'unchecked'}" ${this.disabled ? 'disabled' : ''}> | |
| <div class="switch-thumb"></div> | |
| </div> | |
| </div> | |
| `; | |
| this.setupEventListeners(); | |
| // Ensure the switch element has the correct checked state after rendering | |
| const switchElement = this.shadowRoot.querySelector('.switch-input'); | |
| const trackElement = this.shadowRoot.querySelector('.switch-track'); | |
| if (switchElement) { | |
| switchElement.checked = isChecked; | |
| } | |
| if (trackElement) { | |
| trackElement.setAttribute('data-state', isChecked ? 'checked' : 'unchecked'); | |
| } | |
| } | |
| } | |
| customElements.define('ave-switch', AveSwitch); | |