Files
ladder/lib/Rung.ts
2022-05-26 15:11:17 +02:00

38 lines
915 B
TypeScript

import {SubNode} from "./helpers";
export type RungOptions = {};
export default abstract class Rung {
protected el: HTMLElement | null = null;
protected constructor(options: RungOptions) {}
render(): HTMLElement {
if (!this.el) {
this.el = this.build();
}
return this.el;
}
protected getEl(): HTMLElement {
return this.render();
}
redraw(): void {
const oldNode = this.el;
if (!oldNode || !this.el) {
return;
}
const parent = this.el.parentElement;
if (parent) {
this.el = this.build();
parent.replaceChild(this.el, oldNode);
} else {
this.render();
}
}
protected abstract build(): HTMLElement;
}
export type FunctionalRung<Props extends Record<string, any>, N extends Node> = (attributes: Props, subNodes: SubNode[]) => N;