Skip to content

Commit 7696e16

Browse files
committed
feat(slide-switch): add slide-switch element
1 parent 396da85 commit 7696e16

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ export { ArduinoMegaElement } from './arduino-mega-element';
2121
export { ArduinoNanoElement } from './arduino-nano-element';
2222
export { Ds1307Element } from './ds1307-element';
2323
export { LEDRingElement } from './led-ring-element';
24+
export { SlideSwitchElement } from './slide-switch-element';

src/react-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { ArduinoMegaElement } from './arduino-mega-element';
2020
import { ArduinoNanoElement } from './arduino-nano-element';
2121
import { Ds1307Element } from './ds1307-element';
2222
import { LEDRingElement } from './led-ring-element';
23+
import { SlideSwitchElement } from './slide-switch-element';
2324

2425
declare global {
2526
namespace JSX {
@@ -43,6 +44,7 @@ declare global {
4344
'wokwi-arduino-nano': Partial<ArduinoNanoElement>;
4445
'wokwi-ds1307': Partial<Ds1307Element>;
4546
'wokwi-neopixel-ring': Partial<LEDRingElement>;
47+
'wokwi-slide-switch': Partial<SlideSwitchElement>;
4648
}
4749
}
4850
}

src/slide-switch-element.stories.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { html } from 'lit-html';
2+
import './slide-switch-element';
3+
import { action } from '@storybook/addon-actions';
4+
5+
export default {
6+
title: 'Slide Switch',
7+
component: 'wokwi-slide-switch',
8+
};
9+
10+
export const slideSwitch = () =>
11+
html`<wokwi-slide-switch @input=${action('input')}></wokwi-slide-switch>`;

src/slide-switch-element.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)