Skip to content

Commit

Permalink
feat(elements): ✨ allow Element instances without id
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNZL committed Jul 17, 2022
1 parent b5cf5e8 commit 067bda7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/elements/Button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class Button extends Element {
}

public click() {
if (!(this.element instanceof HTMLElement)) return;
this.element.click();
}
}
14 changes: 9 additions & 5 deletions src/elements/Element.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
export class Element {
protected static instances = new Map<string, Element>();

protected element: HTMLElement;
protected element: globalThis.Element;
private timeouts = new Map<string, ReturnType<typeof setTimeout>>();

private tile?: HTMLElement | false;
private parentHeading?: HTMLHeadingElement | false;

protected constructor({ id, type }: {
protected constructor({ id, type, element }: {
id: string,
type: string;
element?: globalThis.Element | null;
}) {
const element = document.getElementById(id);
element ??= document.getElementById(id);
if (!element) throw new Error(`Invalid ${type} identifier ${id}!`);

element.id = id;

this.element = element;
}

public static getInstance<T extends string>({ id, type }: {
public static getInstance<T extends string>({ id, type, element }: {
id: T,
type: string;
element?: globalThis.Element;
}): Element {
if (!Element.instances.has(id)) {
Element.instances.set(id, new Element({ id, type }));
Element.instances.set(id, new Element({ id, type, element }));
}

return <Element>Element.instances.get(id);
Expand Down

0 comments on commit 067bda7

Please sign in to comment.