Files
rio/frontend/code/components/progressCircle.ts
2025-03-11 19:14:15 +01:00

56 lines
1.7 KiB
TypeScript

import { applySwitcheroo } from "../designApplication";
import { ColorSet } from "../dataModels";
import { ComponentBase, ComponentState, DeltaState } from "./componentBase";
export type ProgressCircleState = ComponentState & {
_type_: "ProgressCircle-builtin";
progress: number | null;
color: ColorSet;
};
export class ProgressCircleComponent extends ComponentBase<ProgressCircleState> {
createElement(): HTMLElement {
let element = document.createElement("div");
element.innerHTML = `
<svg viewBox="25 25 50 50">
<circle class="progress" cx="50" cy="50" r="20"></circle>
</svg>
`;
element.classList.add("rio-progress-circle");
return element;
}
updateElement(
deltaState: DeltaState<ProgressCircleState>,
latentComponents: Set<ComponentBase>
): void {
super.updateElement(deltaState, latentComponents);
// Apply the progress
if (deltaState.progress !== undefined) {
if (deltaState.progress === null) {
this.element.classList.add("spinning");
} else {
this.element.classList.remove("spinning");
let fullCircle = 40 * Math.PI;
this.element.style.setProperty(
"--dasharray",
`${deltaState.progress * fullCircle}, ${
(1 - deltaState.progress) * fullCircle
}`
);
}
}
// Apply the color
if (deltaState.color !== undefined) {
applySwitcheroo(
this.element,
deltaState.color === "keep" ? "bump" : deltaState.color
);
}
}
}