main 2023.02.03
idylls 1 year ago
parent a10a1192af
commit 3dfd606aff
Signed by: idylls
GPG Key ID: 8A7167CBC2CC9F0F

@ -1,20 +1,24 @@
/// <reference lib="dom" />
type BasicProperties<T> = {
[K in keyof T as T[K] extends (string | number | boolean) ? K : never]: T[K]
}
[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,
};
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;
style: Record<string, string | boolean | number> | null;
};
interface Swel {
<T extends keyof HTMLElementTagNameMap>(
@ -71,10 +75,12 @@ export const swel: Swel = <T extends keyof HTMLElementTagNameMap>(
} else {
o = opts;
}
} else {
o = {};
}
let o_ = o!;
if (o_.children) {
o_.children = o_.children.filter(c => !!c) as any;
o_.children = o_.children.filter((c) => !!c) as any;
}
return swel_(tag, o);
@ -82,11 +88,11 @@ export const swel: Swel = <T extends keyof HTMLElementTagNameMap>(
const swel_ = <T extends keyof HTMLElementTagNameMap>(
tag: T,
opts: Partial<SwelOpts<T>> | null
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;
const { children, on, attributes, style, ...rest } = opts;
if (children) {
el.append(...children);
@ -101,6 +107,11 @@ const swel_ = <T extends keyof HTMLElementTagNameMap>(
el.setAttribute(k, v as unknown as string);
});
}
if (style) {
Object.entries(style).map(([k, v]) => {
Reflect.set(el.style, k, v);
});
}
Object.assign(el, rest);
}

@ -0,0 +1,9 @@
{
"name": "swel",
"version": "0.0.0",
"description": "",
"author": "",
"main": "mod.ts",
"module": "mod.ts",
"license": "UNLICENSED"
}

@ -0,0 +1,11 @@
module.exports = {
printWidth: 80,
useTabs: true,
semi: true,
singleQuote: false,
quoteProps: "as-needed",
trailingComma: "all",
bracketSpacing: true,
arrowParens: "always",
parser: "typescript",
};
Loading…
Cancel
Save