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" /> /// <reference lib="dom" />
type BasicProperties<T> = { 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 Child = Node | string | null;
type SwelOpts<T extends keyof HTMLElementTagNameMap> = type SwelOpts<T extends keyof HTMLElementTagNameMap> = Partial<
Partial<BasicProperties<HTMLElementTagNameMap[T]>> & { BasicProperties<HTMLElementTagNameMap[T]>
on: Partial<{ > & {
[Ev in keyof HTMLElementEventMap]: on: Partial<{
(this: HTMLElement, ev: HTMLElementEventMap[Ev]) => any [Ev in keyof HTMLElementEventMap]: (
}>, this: HTMLElement,
children: (Node | string)[] | null, ev: HTMLElementEventMap[Ev],
attributes: Record<string, string | boolean | number>, ) => any;
className: string, }>;
}; children: (Node | string)[] | null;
attributes: Record<string, string | boolean | number>;
className: string;
style: Record<string, string | boolean | number> | null;
};
interface Swel { interface Swel {
<T extends keyof HTMLElementTagNameMap>( <T extends keyof HTMLElementTagNameMap>(
@ -71,10 +75,12 @@ export const swel: Swel = <T extends keyof HTMLElementTagNameMap>(
} else { } else {
o = opts; o = opts;
} }
} else {
o = {};
} }
let o_ = o!; let o_ = o!;
if (o_.children) { 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); return swel_(tag, o);
@ -82,11 +88,11 @@ export const swel: Swel = <T extends keyof HTMLElementTagNameMap>(
const swel_ = <T extends keyof HTMLElementTagNameMap>( const swel_ = <T extends keyof HTMLElementTagNameMap>(
tag: T, tag: T,
opts: Partial<SwelOpts<T>> | null opts: Partial<SwelOpts<T>> | null,
): HTMLElementTagNameMap[T] => { ): HTMLElementTagNameMap[T] => {
const el = document.createElement(tag) as unknown as HTMLElementTagNameMap[T]; const el = document.createElement(tag) as unknown as HTMLElementTagNameMap[T];
if (opts) { if (opts) {
const { children, on, attributes, ...rest } = opts; const { children, on, attributes, style, ...rest } = opts;
if (children) { if (children) {
el.append(...children); el.append(...children);
@ -101,6 +107,11 @@ const swel_ = <T extends keyof HTMLElementTagNameMap>(
el.setAttribute(k, v as unknown as string); 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); 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