-
Notifications
You must be signed in to change notification settings - Fork 2
/
range-slider.ts
307 lines (267 loc) · 8.73 KB
/
range-slider.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import { html, nothing } from 'lit';
import { property, query } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { styleMap } from 'lit/directives/style-map.js';
import { registerComponent } from '../common/definitions/register.js';
import type { Constructor } from '../common/mixins/constructor.js';
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
import { asNumber, asPercent } from '../common/util.js';
import { IgcSliderBaseComponent } from './slider-base.js';
import IgcSliderLabelComponent from './slider-label.js';
/* blazorSuppress */
export interface IgcRangeSliderValue {
lower: number;
upper: number;
}
export interface IgcRangeSliderEventMap {
/**
* Emitted when a value is changed via thumb drag or keyboard interaction.
*/
igcInput: CustomEvent<IgcRangeSliderValue>;
/**
* Emitted when a value change is committed on a thumb drag end or keyboard interaction.
*/
igcChange: CustomEvent<IgcRangeSliderValue>;
}
/**
* A range slider component used to select two numeric values within a range.
*
* @element igc-range-slider
*
* @fires igcInput - Emitted when a value is changed via thumb drag or keyboard interaction.
* @fires igcChange - Emitted when a value change is committed on a thumb drag end or keyboard interaction.
*
* @csspart base - The base wrapper of the slider.
* @csspart ticks - The ticks container.
* @csspart tick-group - The tick group container.
* @csspart tick - The tick element.
* @csspart tick-label - The tick label element.
* @csspart tick-label-inner - The inner element of the tick label.
* @csspart thumbs - The thumbs container.
* @csspart thumb - The thumb element.
* @csspart thumb-label - The thumb tooltip label container.
* @csspart thumb-label-inner - The thumb tooltip label inner element.
* @csspart track - The track container.
* @csspart steps - The track steps element.
* @csspart inactive - The inactive element of the track.
* @csspart fill - The filled part of the track.
*/
export default class IgcRangeSliderComponent extends EventEmitterMixin<
IgcRangeSliderEventMap,
Constructor<IgcSliderBaseComponent>
>(IgcSliderBaseComponent) {
public static readonly tagName = 'igc-range-slider';
/* blazorSuppress */
public static register() {
registerComponent(IgcRangeSliderComponent, IgcSliderLabelComponent);
}
@query('#thumbFrom')
private thumbFrom!: HTMLElement;
@query('#thumbTo')
private thumbTo!: HTMLElement;
private _lower = 0;
private _upper = 0;
/**
* The current value of the lower thumb.
* @attr
*/
@property({ type: Number })
public set lower(val: number) {
this._lower = this.validateValue(asNumber(val, this._lower));
}
public get lower(): number {
return this._lower;
}
/**
* The current value of the upper thumb.
* @attr
*/
@property({ type: Number })
public set upper(val: number) {
this._upper = this.validateValue(asNumber(val, this._upper));
}
public get upper(): number {
return this._upper;
}
/**
* The aria label for the lower thumb.
* @attr thumb-label-lower
*/
@property({ attribute: 'thumb-label-lower' })
public thumbLabelLower!: string;
/**
* The aria label for the upper thumb.
* @attr thumb-label-upper
*/
@property({ attribute: 'thumb-label-upper' })
public thumbLabelUpper!: string;
/**
* The aria label of the lower thumb.
* @attr aria-label-lower
*
* @deprecated since v4.8.0. Use the `thumbLabelLower` property instead.
*/
@property({ attribute: 'aria-label-lower' })
public set ariaLabelLower(value: string) {
this.thumbLabelLower = value;
}
public get ariaLabelLower() {
return this.thumbLabelLower;
}
/**
* The aria label of the upper thumb.
* @attr aria-label-upper
*
* @deprecated since v4.8.0. Use the `thumbLabelUpper` property instead.
*/
@property({ attribute: 'aria-label-upper' })
public set ariaLabelUpper(value: string) {
this.thumbLabelUpper = value;
}
public get ariaLabelUpper() {
return this.thumbLabelUpper;
}
protected override get activeValue(): number {
return this.activeThumb === this.thumbFrom ? this.lower : this.upper;
}
protected override normalizeValue(): void {
this._lower = this.validateValue(this._lower);
this._upper = this.validateValue(this._upper);
}
protected override getTrackStyle() {
const start = asPercent(this.lower - this.min, this.distance);
return {
insetInlineStart: `${start}%`,
width: `${asPercent(this.upper - this.min, this.distance) - start}%`,
};
}
private closestTo(goal: number, positions: number[]): number {
return positions.reduce((previous, current) =>
Math.abs(goal - current) < Math.abs(goal - previous) ? current : previous
);
}
protected override closestHandle(event: PointerEvent): HTMLElement {
const fromOffset =
this.thumbFrom.offsetLeft + this.thumbFrom.offsetWidth / 2;
const toOffset = this.thumbTo.offsetLeft + this.thumbTo.offsetWidth / 2;
const xPointer = event.clientX - this.getBoundingClientRect().left;
const match = this.closestTo(xPointer, [fromOffset, toOffset]);
if (fromOffset === toOffset && toOffset < xPointer) {
return this.thumbTo;
}
if (fromOffset === toOffset && toOffset > xPointer) {
return this.thumbFrom;
}
if (match === fromOffset) {
return this.thumbFrom;
}
return this.thumbTo;
}
protected override updateValue(increment: number) {
const oldValue = this.activeValue;
let [lower, upper] = [this.lower, this.upper];
if (this.activeThumb === this.thumbFrom) {
lower += increment;
} else {
upper += increment;
}
if (lower >= upper) {
[this.lower, this.upper] = [upper, lower];
this.toggleActiveThumb();
} else {
[this.lower, this.upper] = [lower, upper];
}
if (oldValue === this.activeValue) {
return false;
}
this.emitInputEvent();
return true;
}
protected override emitInputEvent() {
this.emitEvent('igcInput', {
detail: { lower: this.lower, upper: this.upper },
});
}
protected override emitChangeEvent() {
this.emitEvent('igcChange', {
detail: { lower: this.lower, upper: this.upper },
});
}
private toggleActiveThumb() {
const thumb =
this.activeThumb === this.thumbFrom ? this.thumbTo : this.thumbFrom;
thumb.focus();
}
private handleFocus(event: FocusEvent) {
this.activeThumb = event.target as HTMLElement;
const id = this.activeThumb.id;
for (const thumb of [this.thumbFrom, this.thumbTo]) {
if (thumb.id === id) continue;
const [activeValue, thumbVal] = [
asNumber(this.activeThumb.ariaValueNow),
asNumber(thumb.ariaValueNow),
];
this.activeThumb.ariaValueText = `${this.formatValue(Math.min(activeValue, thumbVal))} - ${this.formatValue(Math.max(activeValue, thumbVal))}`;
}
}
protected override renderThumb(
value: number,
ariaLabel?: string,
thumbId?: string
) {
const percent = `${asPercent(value - this.min, this.distance)}%`;
const thumbStyles = { insetInlineStart: percent };
const tooltipStyles = {
insetInlineStart: percent,
opacity: this.thumbLabelsVisible ? 1 : 0,
};
const ariaValueText =
thumbId === 'thumbFrom' ? `min ${this.lower}` : `max ${this.upper}`;
const textValue = this.hasLabels
? this.labels[value]
: this.valueFormat || this.valueFormatOptions
? this.formatValue(value)
: ariaValueText;
return html`
<div
part="thumb"
id=${ifDefined(thumbId)}
tabindex=${this.disabled ? -1 : 0}
style=${styleMap(thumbStyles)}
role="slider"
aria-valuemin=${this.lowerBound}
aria-valuemax=${this.upperBound}
aria-valuenow=${value}
aria-valuetext=${ifDefined(textValue)}
aria-label=${ifDefined(ariaLabel)}
aria-disabled=${this.disabled ? 'true' : 'false'}
@pointerenter=${this.showThumbLabels}
@pointerleave=${this.hideThumbLabels}
@focus=${this.handleFocus}
@blur=${this.handleThumbBlur}
></div>
${this.hideTooltip
? nothing
: html`
<div part="thumb-label" style=${styleMap(tooltipStyles)}>
<div part="thumb-label-inner">
${this.hasLabels ? this.labels[value] : this.formatValue(value)}
</div>
</div>
`}
`;
}
protected override renderThumbs() {
return html`${this.renderThumb(
this.lower,
this.thumbLabelLower,
'thumbFrom'
)}
${this.renderThumb(this.upper, this.thumbLabelUpper, 'thumbTo')}`;
}
}
declare global {
interface HTMLElementTagNameMap {
'igc-range-slider': IgcRangeSliderComponent;
}
}