-
Notifications
You must be signed in to change notification settings - Fork 192
/
SliderHandle.ts
253 lines (217 loc) · 7.83 KB
/
SliderHandle.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
/*
Copyright 2021 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import { PropertyValues } from '@spectrum-web-components/base';
import { property } from '@spectrum-web-components/base/src/decorators.js';
import { Focusable } from '@spectrum-web-components/shared/src/focusable.js';
import { ProvideLang } from '@spectrum-web-components/theme';
import {
NumberFormatOptions,
NumberFormatter,
} from '@internationalized/number';
export type HandleMin = number | 'previous';
export type HandleMax = number | 'next';
export type HandleValues = {
name: string;
value: number;
}[];
export interface Controller {
inputForHandle(handle: SliderHandle): HTMLInputElement | undefined;
requestUpdate(): void;
setValueFromHandle(handle: SliderHandle): void;
handleHasChanged(handle: SliderHandle): void;
}
export type SliderNormalization = {
toNormalized: (value: number, min: number, max: number) => number;
fromNormalized: (value: number, min: number, max: number) => number;
};
export const defaultNormalization: SliderNormalization = {
toNormalized(value: number, min: number, max: number) {
return (value - min) / (max - min);
},
fromNormalized(value: number, min: number, max: number) {
return value * (max - min) + min;
},
};
const MinConverter = {
fromAttribute: (value: string): number | 'previous' => {
if (value === 'previous') return value;
return parseFloat(value);
},
toAttribute: (value: 'previous' | number): string => {
return value.toString();
},
};
const MaxConverter = {
fromAttribute: (value: string): number | 'next' => {
if (value === 'next') return value;
return parseFloat(value);
},
toAttribute: (value: 'next' | number): string => {
return value.toString();
},
};
/**
* @element sp-slider-handle
*
* @fires input - The value of the element has changed.
* @fires change - An alteration to the value of the element has been committed by the user.
*/
export class SliderHandle extends Focusable {
public handleController?: Controller;
public get handleName(): string {
return this.name;
}
public get focusElement(): HTMLElement {
/* c8 ignore next */
return this.handleController?.inputForHandle(this) ?? this;
}
_forcedUnit = '';
/**
* By default, the value of a Slider Handle will be halfway between its
* `min` and `max` values, or the `min` value when `max` is less than `min`.
*/
@property({ type: Number })
value!: number;
@property({ type: Boolean, reflect: true })
public dragging = false;
@property({ type: Boolean })
public highlight = false;
@property({ type: String })
public name = '';
@property({ reflect: true, converter: MinConverter })
public min?: number | 'previous';
@property({ reflect: true, converter: MaxConverter })
public max?: number | 'next';
@property({ attribute: false })
private resolvedLanguage =
document.documentElement.lang || navigator.language;
@property({ type: Number, reflect: true })
public step?: number;
@property({ type: Object, attribute: 'format-options' })
public formatOptions?: NumberFormatOptions;
@property({ type: String })
public label = '';
@property({ attribute: false })
public getAriaHandleText: (
value: number,
numberFormat: NumberFormatter
) => string = (value, numberFormat) => {
return numberFormat.format(value);
};
protected update(changes: PropertyValues): void {
if (changes.has('formatOptions') || changes.has('resolvedLanguage')) {
delete this._numberFormatCache;
}
super.update(changes);
}
protected firstUpdated(changedProperties: PropertyValues<this>): void {
super.firstUpdated(changedProperties);
const { max, min } = this as { max: number; min: number };
if (this.value == null) {
if (!isNaN(max) && !isNaN(min)) {
this.value = max < min ? min : min + (max - min) / 2;
}
}
}
protected updated(changedProperties: PropertyValues<this>): void {
if (changedProperties.has('value')) {
const oldValue = changedProperties.get('value');
if (oldValue != null) {
this.handleController /* c8 ignore next */
?.setValueFromHandle(this);
}
}
this.handleController?.handleHasChanged(this);
super.updated(changedProperties);
}
protected firstUpdated(changedProperties: PropertyValues<this>): void {
super.firstUpdated(changedProperties);
this.dispatchEvent(new CustomEvent('sp-slider-handle-ready'));
}
@property({ attribute: false })
public normalization: SliderNormalization = defaultNormalization;
public dispatchInputEvent(): void {
const inputEvent = new Event('input', {
bubbles: true,
composed: true,
});
this.dispatchEvent(inputEvent);
}
protected _numberFormatCache:
| { numberFormat: NumberFormatter; language: string }
| undefined;
protected getNumberFormat(): Intl.NumberFormat {
/* c8 ignore next */
if (
!this._numberFormatCache ||
this.resolvedLanguage !== this._numberFormatCache.language
) {
let numberFormatter: NumberFormatter;
try {
numberFormatter = new NumberFormatter(
this.resolvedLanguage,
this.formatOptions
);
this._forcedUnit = '';
// numberFormatter.format(1);
} catch (error) {
const {
style,
unit,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
unitDisplay,
...formatOptionsNoUnit
} = this.formatOptions || {};
if (style === 'unit') {
this._forcedUnit = unit as string;
}
numberFormatter = new NumberFormatter(
this.resolvedLanguage,
formatOptionsNoUnit
);
}
this._numberFormatCache = {
language: this.resolvedLanguage,
numberFormat: numberFormatter,
};
}
/* c8 ignore next */
return this._numberFormatCache?.numberFormat;
}
public get numberFormat(): NumberFormatter | undefined {
if (!this.formatOptions) return;
return this.getNumberFormat();
}
public connectedCallback(): void {
super.connectedCallback();
this.resolveLanguage();
}
public disconnectedCallback(): void {
this.resolveLanguage();
super.disconnectedCallback();
}
private resolveLanguage(): void {
const queryThemeEvent = new CustomEvent<ProvideLang>(
'sp-language-context',
{
bubbles: true,
composed: true,
detail: {
callback: (lang: string) => {
this.resolvedLanguage = lang;
},
},
cancelable: true,
}
);
this.dispatchEvent(queryThemeEvent);
}
}