igniteui-webcomponents
Version:
Ignite UI for Web Components is a complete library of UI components, giving you the ability to build modern web applications using encapsulation and the concept of reusable components in a dependency-free approach.
163 lines • 6.06 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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, formatString } from '../common/util.js';
export class IgcProgressBaseComponent extends LitElement {
indeterminateChange() {
this.cancelAnimations();
if (!this.indeterminate) {
this._setProgress();
this.animateLabelTo(0, this.value);
}
}
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);
}
}
valueChange(previous) {
this.value = clamp(this.value, 0, this.max);
this._setProgress();
if (!this.indeterminate) {
cancelAnimationFrame(this._ticker);
this.animateLabelTo(previous, this.value);
}
}
constructor() {
super();
this.percentage = 0;
this.progress = 0;
this.max = 100;
this.value = 0;
this.variant = 'primary';
this.animationDuration = 500;
this.indeterminate = false;
this.hideLabel = false;
this.__internals = this.attachInternals();
this.__internals.role = 'progressbar';
this.__internals.ariaValueMin = '0';
}
createRenderRoot() {
const root = super.createRenderRoot();
root.addEventListener('slotchange', () => this.requestUpdate());
return root;
}
updated() {
this._updateARIA();
}
_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;
}
_setProgress() {
this.progress = this.value / this.max;
}
async connectedCallback() {
super.connectedCallback();
await this.updateComplete;
if (!this.indeterminate) {
requestAnimationFrame(() => {
this._setProgress();
this.animateLabelTo(0, this.value);
});
}
}
cancelAnimations() {
cancelAnimationFrame(this._ticker);
this.progressIndicator?.getAnimations().forEach((animation) => {
if (animation instanceof CSSTransition) {
animation.cancel();
}
});
}
animateLabelTo(start, end) {
let t0;
const tick = (t1) => {
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));
if (delta < 1) {
this._ticker = requestAnimationFrame(tick);
}
else {
cancelAnimationFrame(this._ticker);
}
};
requestAnimationFrame(tick);
}
renderLabelFormat() {
return formatString(this.labelFormat, this.value, this.max);
}
renderDefaultSlot() {
const hasNoLabel = this.indeterminate || this.hideLabel || this.assignedElements.length;
return html `
<slot part="label"></slot>
${hasNoLabel
? nothing
: html `<span part="label value">${this.renderLabelText()}</span>`}
`;
}
renderLabelText() {
return this.labelFormat ? this.renderLabelFormat() : `${this.percentage}%`;
}
}
__decorate([
queryAssignedElements()
], IgcProgressBaseComponent.prototype, "assignedElements", void 0);
__decorate([
query('[part~="fill"]', true)
], IgcProgressBaseComponent.prototype, "progressIndicator", void 0);
__decorate([
state()
], IgcProgressBaseComponent.prototype, "percentage", void 0);
__decorate([
state()
], IgcProgressBaseComponent.prototype, "progress", void 0);
__decorate([
property({ type: Number })
], IgcProgressBaseComponent.prototype, "max", void 0);
__decorate([
property({ type: Number })
], IgcProgressBaseComponent.prototype, "value", void 0);
__decorate([
property({ reflect: true })
], IgcProgressBaseComponent.prototype, "variant", void 0);
__decorate([
property({ type: Number, attribute: 'animation-duration' })
], IgcProgressBaseComponent.prototype, "animationDuration", void 0);
__decorate([
property({ type: Boolean, reflect: false })
], IgcProgressBaseComponent.prototype, "indeterminate", void 0);
__decorate([
property({ type: Boolean, attribute: 'hide-label', reflect: false })
], IgcProgressBaseComponent.prototype, "hideLabel", void 0);
__decorate([
property({ attribute: 'label-format' })
], IgcProgressBaseComponent.prototype, "labelFormat", void 0);
__decorate([
watch('indeterminate', { waitUntilFirstUpdate: true })
], IgcProgressBaseComponent.prototype, "indeterminateChange", null);
__decorate([
watch('max', { waitUntilFirstUpdate: true })
], IgcProgressBaseComponent.prototype, "maxChange", null);
__decorate([
watch('value', { waitUntilFirstUpdate: true })
], IgcProgressBaseComponent.prototype, "valueChange", null);
//# sourceMappingURL=base.js.map