-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.ts
249 lines (226 loc) · 7.66 KB
/
switch.ts
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// eslint-disable-next-line max-classes-per-file
import { WUPCssIconHover, WUPcssHidden } from "../styles";
import WUPBaseControl, { SetValueReasons } from "./baseControl";
const tagName = "wup-switch";
declare global {
namespace WUP.Switch {
interface EventMap extends WUP.BaseControl.EventMap {}
interface ValidityMap extends WUP.BaseControl.ValidityMap {}
interface NewOptions {
/** Reversed-style (switch+label for true vs label+switch)
* @defaultValue false */
reverse: boolean;
}
interface Options<T = boolean, VM = ValidityMap> extends WUP.BaseControl.Options<T, VM>, NewOptions {}
interface JSXProps<C = WUPSwitchControl> extends WUP.BaseControl.JSXProps<C>, WUP.Base.OnlyNames<NewOptions> {
"w-reverse"?: boolean | "";
/** @deprecated use `initValue` instead */
defaultChecked?: boolean;
}
}
interface HTMLElementTagNameMap {
[tagName]: WUPSwitchControl; // add element to document.createElement
}
}
declare module "react" {
namespace JSX {
interface IntrinsicElements {
/** Form-control with toggle button
* @see {@link WUPSwitchControl} */
[tagName]: WUP.Base.ReactHTML<WUPSwitchControl> & WUP.Switch.JSXProps; // add element to tsx/jsx intellisense (react)
}
}
}
// @ts-ignore - because Preact & React can't work together
declare module "preact/jsx-runtime" {
namespace JSX {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface HTMLAttributes<RefType> {}
interface IntrinsicElements {
/** Form-control with toggle button
* @see {@link WUPSwitchControl} */
[tagName]: HTMLAttributes<WUPSwitchControl> & WUP.Switch.JSXProps; // add element to tsx/jsx intellisense (preact)
}
}
}
/** Form-control with toggle button
* @see demo {@link https://yegorich555.github.io/web-ui-pack/control/switch}
* @example
const el = document.createElement("wup-switch");
el.$options.name = "isDarkMode";
el.$options.label = "Dark Mode";
el.$initValue = false;
const form = document.body.appendChild(document.createElement("wup-form"));
form.appendChild(el);
// or HTML
<wup-form>
<wup-switch w-name="isDarkMode" w-label="Dark Mode" w-initvalue="false"/>
</wup-form>;
* @tutorial innerHTML @example
* <label>
* <input type='checkbox'/>
* <strong>{$options.label}</strong>
* <span bar>
* <span thumb></span>
* </span>
* </label> */
export default class WUPSwitchControl<
TOptions extends WUP.Switch.Options = WUP.Switch.Options,
EventMap extends WUP.Switch.EventMap = WUP.Switch.EventMap
> extends WUPBaseControl<boolean, TOptions, EventMap> {
#ctr = this.constructor as typeof WUPSwitchControl;
static get $styleRoot(): string {
return `:root {
--ctrl-switch-padding: 1em;
--ctrl-switch-on: #fff;
--ctrl-switch-off: #fff;
--ctrl-switch-off-bg: #9f9f9f;
--ctrl-switch-on-bg: #00778d;
--ctrl-switch-shadow: #0003;
--ctrl-switch-h: var(--ctrl-icon-size);
--ctrl-switch-w: calc(var(--ctrl-icon-size) * 2.8);
--ctrl-switch-r: calc(var(--ctrl-icon-size) * 1.4);
}
[wupdark] {
--ctrl-switch-on: #e7e7e7;
--ctrl-switch-off: #e7e7e7;
--ctrl-switch-off-bg: #707070;
--ctrl-switch-shadow: #000;
}`;
}
static get $style(): string {
return `${super.$style}
:host {
background: none;
cursor: pointer;
}
:host[readonly] {
cursor: initial;
}
:host label {
display: flex;
gap: 0.5em;
padding: var(--ctrl-switch-padding);
}
:host strong {
box-sizing: border-box;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
font-weight: normal;
text-decoration: none;
color: inherit;
}
:host [bar] {
display: inline-flex;
align-items: center;
overflow: visible;
width: var(--ctrl-switch-w);
min-width: var(--ctrl-switch-w);
height: var(--ctrl-switch-h);
border-radius: 999px;
color: whitesmoke;
background: var(--ctrl-switch-off-bg);
}
${WUPCssIconHover(":host", "[thumb]")}
:host [thumb] {
z-index: 2;
display: inline-block;
height: var(--ctrl-switch-r);
width: var(--ctrl-switch-r);
background: var(--ctrl-switch-off);
box-shadow: 0 1px 4px 0 var(--ctrl-switch-shadow);
border-radius: 50%;
transform: translateX(-1px);
}
:host input { ${WUPcssHidden} }
:host[checked] [bar] {
background-color: var(--ctrl-switch-on-bg);
}
:host[checked] [thumb] {
background: var(--ctrl-switch-on);
transform: translateX(var(--ctrl-switch-w)) translateX(calc(-100% + 1px));
}
:host[w-reverse] label {
flex-direction: row-reverse;
}
:host[w-reverse] strong {
margin-right: auto;
}
@media not all and (prefers-reduced-motion) {
:host [bar] { transition: background-color var(--anim); }
:host [thumb] { transition: transform var(--anim); }
}`;
}
static $isEqual(v1: boolean | undefined, v2: boolean | undefined): boolean {
return !!v1 === !!v2;
}
static get observedAttributes(): Array<string> {
const arr = super.observedAttributes;
arr.push("defaultchecked");
return arr;
}
static $defaults: WUP.Switch.Options = {
...WUPBaseControl.$defaults,
validationRules: { ...WUPBaseControl.$defaults.validationRules },
reverse: false,
};
get $value(): boolean {
return !!super.$value as boolean;
}
set $value(v: boolean) {
super.$value = !!v;
}
override parse(text: string): boolean | undefined {
return text === "" || text === "1" || text.toLowerCase() === "true";
}
override valueToStorage(v: boolean): string | null {
return v ? "1" : null;
}
protected override renderControl(): void {
this.$refInput.id = this.#ctr.$uniqueId;
this.$refLabel.setAttribute("for", this.$refInput.id);
this.$refInput.type = "checkbox";
this.$refLabel.appendChild(this.$refInput);
this.$refLabel.appendChild(this.$refTitle);
const sp = document.createElement("span");
sp.setAttribute("bar", "");
sp.appendChild(document.createElement("span")).setAttribute("thumb", "");
this.$refLabel.appendChild(sp);
this.appendChild(this.$refLabel);
}
protected override gotReady(): void {
super.gotReady();
this.$refInput.oninput = (e) => this.gotInput(e);
}
/** Called when user changes value via click or keyboard */
protected gotInput(e: Event): void {
const el = e.target as HTMLInputElement;
if (this.$isReadOnly) {
el.checked = !el.checked;
} else {
this.setValue(el.checked, SetValueReasons.userInput);
}
}
protected override setValue(v: boolean, reason: SetValueReasons): boolean | null {
const r = super.setValue(v, reason);
this.$refInput.checked = !!v;
this.setAttr("checked", this.$refInput.checked, true);
return r;
}
protected override gotChanges(propsChanged: Array<keyof WUP.Switch.Options | any> | null): void {
super.gotChanges(propsChanged as any);
this.setAttr("w-reverse", this._opts.reverse, true);
}
override gotFormChanges(propsChanged: Array<keyof WUP.Form.Options> | null): void {
super.gotFormChanges(propsChanged);
this.setAttr.call(this.$refInput, "aria-readonly", this.$isReadOnly);
}
protected override attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void {
if (name === "defaultchecked") {
name = "initvalue";
}
super.attributeChangedCallback(name, oldValue, newValue);
}
}
customElements.define(tagName, WUPSwitchControl);