|
| 1 | +import { css, customElement, html, LitElement, property } from 'lit-element'; |
| 2 | + |
| 3 | +@customElement('wokwi-slide-switch') |
| 4 | +export class SlideSwitchElement extends LitElement { |
| 5 | + @property() value = 0; |
| 6 | + |
| 7 | + static get styles() { |
| 8 | + return css` |
| 9 | + .hide-input { |
| 10 | + position: absolute; |
| 11 | + clip: rect(0 0 0 0); |
| 12 | + width: 1px; |
| 13 | + height: 1px; |
| 14 | + margin: -1px; |
| 15 | + } |
| 16 | + svg #handle { |
| 17 | + transition: transform 0.2s linear; |
| 18 | + } |
| 19 | + input:checked + svg #handle { |
| 20 | + transform: translate(2px, 0); |
| 21 | + } |
| 22 | + input:focus + svg #handle { |
| 23 | + stroke-width: 0.4px; |
| 24 | + stroke: #8080ff; |
| 25 | + } |
| 26 | + `; |
| 27 | + } |
| 28 | + |
| 29 | + private onClick() { |
| 30 | + const inputEl = this.shadowRoot?.querySelector<HTMLInputElement>('.hide-input'); |
| 31 | + if (inputEl) { |
| 32 | + inputEl.checked = !inputEl.checked; |
| 33 | + this.onValueChange(inputEl); |
| 34 | + inputEl?.focus(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private onValueChange(target: HTMLInputElement) { |
| 39 | + this.value = target.checked ? 1 : 0; |
| 40 | + this.dispatchEvent(new InputEvent('input', { detail: this.value })); |
| 41 | + } |
| 42 | + |
| 43 | + render() { |
| 44 | + const { value } = this; |
| 45 | + return html` |
| 46 | + <input |
| 47 | + tabindex="0" |
| 48 | + type="checkbox" |
| 49 | + class="hide-input" |
| 50 | + .checked=${value} |
| 51 | + @input="${(e: InputEvent) => this.onValueChange(e.target as HTMLInputElement)}" |
| 52 | + /> |
| 53 | + <svg |
| 54 | + width="8.5mm" |
| 55 | + height="9.23mm" |
| 56 | + version="1.1" |
| 57 | + viewBox="0 0 8.5 9.23" |
| 58 | + xmlns="http://www.w3.org/2000/svg" |
| 59 | + xmlns:xlink="http://www.w3.org/1999/xlink" |
| 60 | + @click="${this.onClick}" |
| 61 | + > |
| 62 | + <defs> |
| 63 | + <radialGradient |
| 64 | + id="a" |
| 65 | + cx="9.33" |
| 66 | + cy="122" |
| 67 | + r="4.25" |
| 68 | + gradientTransform="matrix(1.75 -.511 .28 .959 -41.2 8.15)" |
| 69 | + gradientUnits="userSpaceOnUse" |
| 70 | + > |
| 71 | + <stop stop-color="#808080" offset="0" /> |
| 72 | + <stop stop-color="#b5b5b5" offset="1" /> |
| 73 | + </radialGradient> |
| 74 | + </defs> |
| 75 | + <g fill="#aaa" stroke-width=".0673"> |
| 76 | + <rect x="4" y="5" width=".5" height="4.2" rx=".25" ry=".25" /> |
| 77 | + <rect x="1.54" y="5" width=".5" height="4.2" rx=".25" ry=".25" /> |
| 78 | + <rect x="6.5" y="5" width=".5" height="4.2" rx=".25" ry=".25" /> |
| 79 | + </g> |
| 80 | + <path |
| 81 | + id="handle" |
| 82 | + d="m2.74 0.128 0.145-0.128 0.177 0.0725 0.174-0.0725 0.151 0.0725 0.154-0.0725 0.151 0.0725 0.128-0.0725 0.134 0.0725 0.123-0.0725 0.145 0.128 2e-5 2h-1.48z" |
| 83 | + stroke-width=".0623" |
| 84 | + /> |
| 85 | + <rect x="0" y="2.06" width="8.5" height="3.48" fill="url(#a)" stroke-width=".0548" /> |
| 86 | + <rect x=".0322" y="4.74" width="1.55" height=".805" stroke-width=".0637" /> |
| 87 | + <rect x="6.95" y="4.74" width="1.55" height=".805" stroke-width=".0637" /> |
| 88 | + <rect x="2.55" y="4.74" width="3.47" height=".805" stroke-width=".0955" /> |
| 89 | + </svg> |
| 90 | + `; |
| 91 | + } |
| 92 | +} |
0 commit comments