-
Notifications
You must be signed in to change notification settings - Fork 2
/
base.ts
212 lines (171 loc) · 4.86 KB
/
base.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
import { LitElement, html, nothing } from 'lit';
import {
property,
query,
queryAssignedElements,
state,
} from 'lit/decorators.js';
import { watch } from '../common/decorators/watch.js';
import { asPercent, clamp, format } from '../common/util.js';
export abstract class IgcProgressBaseComponent extends LitElement {
private __internals: ElementInternals;
private _ticker!: number;
@queryAssignedElements()
protected assignedElements!: Array<HTMLElement>;
@query('[part~="fill"]', true)
protected progressIndicator!: Element;
@state()
protected percentage = 0;
@state()
protected progress = 0;
/**
* Maximum value of the control.
* @attr
*/
@property({ type: Number })
public max = 100;
/**
* The value of the control.
* @attr
*/
@property({ type: Number })
public value = 0;
/**
* The variant of the control.
* @attr
*/
@property({ reflect: true })
public variant: 'primary' | 'info' | 'success' | 'warning' | 'danger' =
'primary';
/**
* Animation duration in milliseconds.
* @attr animation-duration
*/
@property({ type: Number, attribute: 'animation-duration' })
public animationDuration = 500;
/**
* The indeterminate state of the control.
* @attr
*/
@property({ type: Boolean, reflect: false })
public indeterminate = false;
/**
* Shows/hides the label of the control.
* @attr hide-label
*/
@property({ type: Boolean, attribute: 'hide-label', reflect: false })
public hideLabel = false;
/**
* Format string for the default label of the control.
* Placeholders:
* {0} - current value of the control.
* {1} - max value of the control.
* @attr label-format
*/
@property({ attribute: 'label-format' })
public labelFormat!: string;
@watch('indeterminate', { waitUntilFirstUpdate: true })
protected indeterminateChange() {
this.cancelAnimations();
if (!this.indeterminate) {
this._setProgress();
this.animateLabelTo(0, this.value);
}
}
@watch('max', { waitUntilFirstUpdate: true })
protected maxChange() {
this.max = Math.max(0, this.max);
if (this.value > this.max) {
this.value = this.max;
}
this._setProgress();
if (!this.indeterminate) {
cancelAnimationFrame(this._ticker);
this.animateLabelTo(this.max, this.value);
}
}
@watch('value', { waitUntilFirstUpdate: true })
protected valueChange(previous: number) {
this.value = clamp(this.value, 0, this.max);
this._setProgress();
if (!this.indeterminate) {
cancelAnimationFrame(this._ticker);
this.animateLabelTo(previous, this.value);
}
}
protected slotChanged() {
this.requestUpdate();
}
constructor() {
super();
this.__internals = this.attachInternals();
this.__internals.role = 'progressbar';
this.__internals.ariaValueMin = '0';
}
protected override updated() {
this._updateARIA();
}
private _updateARIA() {
const internals = this.__internals;
const text = this.labelFormat
? this.renderLabelFormat()
: `${this.percentage}%`;
internals.ariaValueMax = `${this.max}`;
internals.ariaValueNow = this.indeterminate ? null : `${this.value}`;
internals.ariaValueText = this.indeterminate ? null : text;
}
private _setProgress() {
this.progress = this.value / this.max;
}
public override async connectedCallback() {
super.connectedCallback();
await this.updateComplete;
if (!this.indeterminate) {
requestAnimationFrame(() => {
this._setProgress();
this.animateLabelTo(0, this.value);
});
}
}
protected cancelAnimations() {
cancelAnimationFrame(this._ticker);
this.progressIndicator?.getAnimations().forEach((animation) => {
if (animation instanceof CSSTransition) {
animation.cancel();
}
});
}
protected animateLabelTo(start: number, end: number) {
let t0: number;
const tick = (t1: number) => {
t0 = t0 ?? t1;
const delta = Math.min(
(t1 - t0) / Math.max(this.animationDuration, 1),
1
);
this.percentage = Math.floor(
asPercent(delta * (end - start) + start, this.max)
);
delta < 1
? (this._ticker = requestAnimationFrame(tick))
: cancelAnimationFrame(this._ticker);
};
requestAnimationFrame(tick);
}
protected renderLabelFormat() {
return format(this.labelFormat, `${this.value}`, `${this.max}`);
}
protected renderDefaultSlot() {
const hasNoLabel =
this.indeterminate || this.hideLabel || this.assignedElements.length;
return html`
<slot part="label" @slotchange=${this.slotChanged}></slot>
${hasNoLabel
? nothing
: html`<span part="label value">${this.renderLabelText()}</span>`}
`;
}
protected renderLabelText() {
return this.labelFormat ? this.renderLabelFormat() : `${this.percentage}%`;
}
}