parent
73f498ffce
commit
a10a1192af
@ -0,0 +1,109 @@
|
||||
/// <reference lib="dom" />
|
||||
|
||||
type BasicProperties<T> = {
|
||||
[K in keyof T as T[K] extends (string | number | boolean) ? K : never]: T[K]
|
||||
}
|
||||
|
||||
type Child = Node | string | null;
|
||||
type SwelOpts<T extends keyof HTMLElementTagNameMap> =
|
||||
Partial<BasicProperties<HTMLElementTagNameMap[T]>> & {
|
||||
on: Partial<{
|
||||
[Ev in keyof HTMLElementEventMap]:
|
||||
(this: HTMLElement, ev: HTMLElementEventMap[Ev]) => any
|
||||
}>,
|
||||
children: (Node | string)[] | null,
|
||||
attributes: Record<string, string | boolean | number>,
|
||||
className: string,
|
||||
};
|
||||
|
||||
interface Swel {
|
||||
<T extends keyof HTMLElementTagNameMap>(
|
||||
tag: T,
|
||||
opts?: Partial<SwelOpts<T>>,
|
||||
): HTMLElementTagNameMap[T];
|
||||
|
||||
<T extends keyof HTMLElementTagNameMap>(
|
||||
tag: T,
|
||||
children?: Child[],
|
||||
): HTMLElementTagNameMap[T];
|
||||
|
||||
<T extends keyof HTMLElementTagNameMap>(
|
||||
tag: T,
|
||||
child?: Child,
|
||||
): HTMLElementTagNameMap[T];
|
||||
|
||||
<T extends keyof HTMLElementTagNameMap>(
|
||||
tag: T,
|
||||
opts?: Partial<Omit<SwelOpts<T>, "children">>,
|
||||
children?: Child[],
|
||||
): HTMLElementTagNameMap[T];
|
||||
|
||||
<T extends keyof HTMLElementTagNameMap>(
|
||||
tag: T,
|
||||
opts?: Partial<Omit<SwelOpts<T>, "children">>,
|
||||
child?: Child,
|
||||
): HTMLElementTagNameMap[T];
|
||||
}
|
||||
|
||||
export const swel: Swel = <T extends keyof HTMLElementTagNameMap>(
|
||||
tag: T,
|
||||
opts?: Partial<SwelOpts<T>> | Child | Child[],
|
||||
children?: Child | Child[],
|
||||
): HTMLElementTagNameMap[T] => {
|
||||
let o: Partial<SwelOpts<T>> | null = null;
|
||||
if (children) {
|
||||
let c: Child[] | null;
|
||||
if (Array.isArray(children)) {
|
||||
c = children;
|
||||
} else {
|
||||
c = [children];
|
||||
}
|
||||
|
||||
o = {
|
||||
...(opts as any),
|
||||
children: c,
|
||||
};
|
||||
} else if (opts) {
|
||||
if (Array.isArray(opts)) {
|
||||
o = { children: opts } as any;
|
||||
} else if (typeof opts === "string" || opts instanceof Node) {
|
||||
o = { children: [opts] } as any;
|
||||
} else {
|
||||
o = opts;
|
||||
}
|
||||
}
|
||||
let o_ = o!;
|
||||
if (o_.children) {
|
||||
o_.children = o_.children.filter(c => !!c) as any;
|
||||
}
|
||||
|
||||
return swel_(tag, o);
|
||||
};
|
||||
|
||||
const swel_ = <T extends keyof HTMLElementTagNameMap>(
|
||||
tag: T,
|
||||
opts: Partial<SwelOpts<T>> | null
|
||||
): HTMLElementTagNameMap[T] => {
|
||||
const el = document.createElement(tag) as unknown as HTMLElementTagNameMap[T];
|
||||
if (opts) {
|
||||
const { children, on, attributes, ...rest } = opts;
|
||||
|
||||
if (children) {
|
||||
el.append(...children);
|
||||
}
|
||||
if (on) {
|
||||
Object.entries(on).forEach(([ev, h]) => {
|
||||
el.addEventListener(ev, h as any);
|
||||
});
|
||||
}
|
||||
if (attributes) {
|
||||
Object.entries(attributes).map(([k, v]) => {
|
||||
el.setAttribute(k, v as unknown as string);
|
||||
});
|
||||
}
|
||||
|
||||
Object.assign(el, rest);
|
||||
}
|
||||
|
||||
return el;
|
||||
};
|
Loading…
Reference in New Issue