Repository navigation
Expand file tree
/
Copy pathwired-toggle.ts
More file actions
122 lines (111 loc) · 2.95 KB
/
Copy pathwired-toggle.ts
File metadata and controls
122 lines (111 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { WiredBase, BaseCSS, Point } from './wired-base';
import { rectangle, hachureEllipseFill, ellipse, svgNode } from './wired-lib';
import { css, TemplateResult, html, CSSResultArray } from 'lit';
import { customElement, property, query } from 'lit/decorators.js';
@customElement('wired-toggle')
export class WiredToggle extends WiredBase {
@property({ type: Boolean }) checked = false;
@property({ type: Boolean, reflect: true }) disabled = false;
@query('input') private input?: HTMLInputElement;
private knob?: SVGElement;
static get styles(): CSSResultArray {
return [
BaseCSS,
css`
:host {
display: inline-block;
cursor: pointer;
position: relative;
outline: none;
}
:host([disabled]) {
opacity: 0.4 !important;
cursor: default;
pointer-events: none;
}
:host([disabled]) svg {
background: rgba(0, 0, 0, 0.07);
}
input {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
box-sizing: border-box;
cursor: pointer;
opacity: 0;
}
.knob {
transition: transform 0.3s ease;
}
.knob path {
stroke-width: 0.7;
}
.knob.checked {
transform: translateX(48px);
}
path.knobfill {
stroke-width: 3 !important;
fill: transparent;
}
.knob.unchecked path.knobfill {
stroke: var(--wired-toggle-off-color, gray);
}
.knob.checked path.knobfill {
stroke: var(--wired-toggle-on-color, rgb(63, 81, 181));
}
`
];
}
render(): TemplateResult {
return html`
<div style="position: relative;">
<svg></svg>
<input type="checkbox" .checked="${this.checked}" ?disabled="${this.disabled}" @change="${this.onChange}">
</div>
`;
}
focus() {
if (this.input) {
this.input.focus();
} else {
super.focus();
}
}
wiredRender(force = false) {
super.wiredRender(force);
this.refreshKnob();
}
private onChange() {
this.checked = this.input!.checked;
this.refreshKnob();
this.fire('change', { checked: this.checked });
}
protected canvasSize(): Point {
return [80, 34];
}
protected draw(svg: SVGSVGElement, size: Point) {
const rect = rectangle(svg, 16, 8, size[0] - 32, 18, this.seed);
rect.classList.add('toggle-bar');
this.knob = svgNode('g');
this.knob.classList.add('knob');
svg.appendChild(this.knob);
const knobFill = hachureEllipseFill(16, 16, 32, 32, this.seed);
knobFill.classList.add('knobfill');
this.knob.appendChild(knobFill);
ellipse(this.knob, 16, 16, 32, 32, this.seed);
}
private refreshKnob() {
if (this.knob) {
const cl = this.knob.classList;
if (this.checked) {
cl.remove('unchecked');
cl.add('checked');
} else {
cl.remove('checked');
cl.add('unchecked');
}
}
}
}