-
Notifications
You must be signed in to change notification settings - Fork 5
/
Switch.js
210 lines (175 loc) · 5.13 KB
/
Switch.js
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
import './SwitchIcon.js';
import CustomElement from '../core/CustomElement.js';
import InputMixin from '../mixins/InputMixin.js';
import StateMixin from '../mixins/StateMixin.js';
import ThemableMixin from '../mixins/ThemableMixin.js';
import TouchTargetMixin from '../mixins/TouchTargetMixin.js';
export default CustomElement
.extend()
.mixin(ThemableMixin)
.mixin(InputMixin) // Label as root
.mixin(StateMixin)
.mixin(TouchTargetMixin)
// Switches have their own pressed animation (No ripple)
.set({
type: 'checkbox',
stateLayer: true,
})
.observe({
icon: 'string',
selectedIcon: 'string',
unselectedIcon: 'string',
src: 'string',
selectedSrc: 'string',
unselectedSrc: 'string',
})
.html`
<mdw-switch-icon id=switch
color={color} ink={ink} selected={checked} hovered={hoveredState}
focused={focusedState} pressed={pressedState} disabled={disabledState}
icon={icon}
selected-icon={selectedIcon}
unselected-icon={unselectedIcon}
src={src}
selected-src={selectedSrc}
unselected-src={unselectedSrc}
>
</mdw-switch-icon>
<slot id=slot></slot>
`
.recompose(({ refs: { switch: switchEl, state, control } }) => {
switchEl.append(state, control);
control.setAttribute('role', 'switch');
})
.methods({
/**
* @param {(MouseEvent|TouchEvent) & {currentTarget:HTMLInputElement}} event
* @return {void}
*/
onControlMouseOrTouch(event) {
const input = event.currentTarget;
if (input.disabled) return;
if (this.disabledState) return;
if (event.type === 'touchend') {
// this._isHoveringThumb = false;
return;
}
let offsetX;
let clientX;
let pageX;
let isActive;
const isTouch = 'touches' in event;
if (isTouch) {
if (event.touches.length) {
const [touch] = event.touches;
isActive = true;
// @ts-ignore Might exist
({ offsetX, clientX, pageX } = touch);
}
} else {
// Ignore mouse drag-over
// Firefox doesn't report `:active`
// eslint-disable-next-line no-bitwise
isActive = (event.buttons & 1) === 1
&& (event.type === 'mousedown' || input.matches(':active'));
({ offsetX, clientX, pageX } = event);
}
if (!isActive) return;
if (offsetX == null) {
clientX ??= pageX - window.scrollX; // Safari
offsetX = clientX - input.getBoundingClientRect().left;
}
const { clientWidth } = input;
let position = (offsetX / clientWidth);
if (position > 1) {
position = 1;
} else if (position < 0) {
position = 0;
}
// this._isHoveringThumb = true;
let currentValue = this.refs.switch.dragValue;
if (currentValue == null) {
currentValue = this.checked ? 1 : 0;
}
const pixels = offsetX - (clientWidth / 2);
const currentPixels = this.refs.switch.clientWidth - (this.refs.switch.clientHeight);
// console.log(pixels, currentPixels, currentPixels * currentValue, pixels / currentPixels);
const newRatio = pixels / currentPixels;
const newValue = Math.max(Math.min(currentValue + newRatio, 1), 0);
// this.refs.switch.dragValue = newValue;
// event.preventDefault();
},
})
.childEvents({
control: {
'~pointermove': 'onControlMouseOrTouch',
},
})
.css`
/* https://m3.material.io/components/switch/specs */
:host {
--mdw-ink: var(--mdw-color__on-primary);
--mdw-bg: var(--mdw-color__primary);
position: relative;
display: inline-flex;
align-items: center;
gap: 12px;
vertical-align: middle;
cursor: pointer;
}
#control {
cursor: inherit;
}
#touch-target {
z-index: 0;
}
#label {
display: contents;
cursor: pointer;
/* border-radius: 50%; */
}
/** Switch **/
#switch {
--mdw-bg: inherit;
--mdw-ink: inherit;
flex:1;
pointer-events: none;
}
/** State **/
#state {
position: absolute;
/* stylelint-disable-next-line liberty/use-logical-spec */
top: 50%;
/* stylelint-disable-next-line liberty/use-logical-spec */
left: 50%;
block-size: 100%;
min-block-size: 40px;
inline-size: 100%;
min-inline-size: 40px;
pointer-events: none;
transform: translateX(-50%) translateY(-50%);
border-radius: 50%;
}
#state[touched] {
/* Pressed state already has an animation */
/* stylelint-disable-next-line rule-selector-property-disallowed-list */
--mdw-state__pressed-opacity: 0;
}
/** Disabled **/
:host(:disabled) {
--mdw-ink: var(--mdw-color__on-surface); /* selected icon */
--mdw-bg: var(--mdw-color__surface);
cursor: not-allowed;
opacity: 0.38;
}
:host([internals-disabled]) {
--mdw-ink: var(--mdw-color__on-surface); /* selected icon */
--mdw-bg: var(--mdw-color__surface);
cursor: not-allowed;
opacity: 0.38;
}
#switch[disabled] {
opacity: 1;
}
`
.autoRegister('mdw-switch');