-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrange-slider-element.js
206 lines (170 loc) · 6.26 KB
/
range-slider-element.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
// import * as style from './styles.css';
const UPDATE_EVENTS = ['input', 'change'];
const REFLECTED_ATTRIBUTES = ['min', 'max', 'step', 'value', 'disabled', 'value-precision'];
const ARIA_ATTRIBUTES = {
value: 'valuenow',
min: 'valuemin',
max: 'valuemax',
};
const TEMPLATE = document.createElement('template');
TEMPLATE.innerHTML = `
<div class="thumb-wrapper">
<div class="thumb"></div>
</div>
`;
class RangeSliderElement extends HTMLElement {
constructor() {
super();
this._ignoreChange = false;
this._isRTL = this.getAttribute('dir') === 'rtl';
this._defaultValue = this.value;
}
static get observedAttributes() {
return REFLECTED_ATTRIBUTES;
}
get _computedValue() {
const min = Number(this.min);
const max = Number(this.max);
return String(max < min ? min : min + (max - min) / 2);
}
get min() { return this.getAttribute('min') || '0'; }
get max() { return this.getAttribute('max') || '100'; }
get step() { return this.getAttribute('step') || '1'; }
get value() { return this.getAttribute('value') || this._computedValue; }
get disabled() { return this.getAttribute('disabled') || false }
get valuePrecision() { return this.getAttribute('value-precision') || ''; }
get defaultValue() { return this._defaultValue; }
set min(min) { this.setAttribute('min', min); }
set max(max) { this.setAttribute('max', max); }
set step(step) { this.setAttribute('step', step); }
set value(value) { this.setAttribute('value', value); }
set disabled(disabled) { this.setAttribute('disabled', disabled); }
set valuePrecision(precision) { this.setAttribute('value-precision', precision); }
set defaultValue(value) { this._defaultValue = value; }
connectedCallback() {
if (!this.firstChild) {
this.append(TEMPLATE.content.cloneNode(true));
}
this.addEventListener('pointerdown', this._startHandler, false);
this.addEventListener('pointerup', this._endHandler, false);
this.addEventListener('keydown', this._keyCodeHandler, false);
this._update();
// Aria attributes
this.setAttribute('tabindex', '0');
this.setAttribute('role', 'slider');
setAriaAttribute(this, 'value', this.value);
setAriaAttribute(this, 'min', this.min);
setAriaAttribute(this, 'max', this.max);
}
disconnectedCallback() {
this.removeEventListener('pointerdown', this._startHandler, false);
this.removeEventListener('pointerup', this._endHandler, false);
this.removeEventListener('keydown', this._keyCodeHandler, false);
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue === newValue || this._ignoreChange) return;
this._update();
setAriaAttribute(this, name, newValue);
}
_startHandler = e => {
this.focus();
this.classList.add('touch-active');
// Click and drag
this.setPointerCapture(e.pointerId);
this.addEventListener('pointermove', this._moveHandler, false);
// Click jump
this._reflectValue(e);
}
_moveHandler = e => {
this._reflectValue(e);
}
_endHandler = e => {
this.classList.remove('touch-active');
this.releasePointerCapture(e.pointerId);
this.removeEventListener('pointermove', this._moveHandler, false);
// TODO: check if value changed
this.dispatchEvent(new Event('change', { bubbles: true }));
}
_keyCodeHandler = e => {
const code = e.code;
const up = ['ArrowUp', 'ArrowRight'].includes(code);
const down = ['ArrowDown', 'ArrowLeft'].includes(code);
if (up) {
e.preventDefault();
this.stepUp();
}
else if (down) {
e.preventDefault();
this.stepDown();
}
}
_reflectValue = e => {
const isRTL = Boolean(this._isRTL);
const min = Number(this.min);
const max = Number(this.max);
const oldValue = this.value;
const fullWidth = e.target.offsetWidth;
const offsetX = Math.min(Math.max(e.offsetX, 0), fullWidth);
const percent = offsetX / fullWidth;
const percentComplete = isRTL ? 1 - percent : percent;
// Fit the percentage complete between the range [min,max]
// by remapping from [0, 1] to [min, min+(max-min)].
const computedValue = min + percentComplete * (max - min);
// Constrain value
const newValue = this._constrainValue(computedValue);
if (oldValue !== newValue) {
this.value = newValue;
this.dispatchEvent(new Event('input', { bubbles: true }));
}
}
_constrainValue(value) {
const min = Number(this.min);
const max = Number(this.max);
const step = Number(this.step);
const valuePrecision = Number(this.valuePrecision) || getPrescision(this.step) || 0;
// min, max constrain
const saveValue = Math.min(Math.max(value, min), max);
// Rounding in steps
const nearestValue = Math.round(saveValue / step) * step;
// Value precision
const newValue = valuePrecision ? nearestValue.toFixed(valuePrecision) : Math.round(nearestValue).toString();
return newValue;
}
_update() {
const isRTL = Boolean(this._isRTL);
const min = Number(this.min);
const max = Number(this.max);
const value = Number(this.value);
const percent = (100 * (value - min)) / (max - min);
const percentComplete = isRTL ? 100 - percent : percent;
this.style.setProperty('--value-percent', percentComplete + '%');
}
stepUp(amount = this.step) {
const oldValue = Number(this.value);
const newValue = this._constrainValue(oldValue + Number(amount));
if (oldValue !== newValue) {
this.value = newValue;
this.dispatchEvent(new Event('input', { bubbles: true }));
this.dispatchEvent(new Event('change', { bubbles: true }));
}
}
stepDown(amount = this.step) {
const oldValue = Number(this.value);
const newValue = this._constrainValue(oldValue - Number(amount));
if (oldValue !== newValue) {
this.value = newValue;
this.dispatchEvent(new Event('input', { bubbles: true }));
this.dispatchEvent(new Event('change', { bubbles: true }));
}
}
}
function getPrescision(value = '') {
const afterDecimal = value.split('.')[1];
return afterDecimal ? afterDecimal.length : 0;
}
function setAriaAttribute(element, name, value) {
const attributeName = ARIA_ATTRIBUTES[name];
if (!attributeName) return;
element.setAttribute(`aria-${attributeName}`, value);
}
export default RangeSliderElement;