From 1586763706d8491373e30c8b1c2cca3bf33a4e5f Mon Sep 17 00:00:00 2001 From: Shiba <44804845+DeepDoge@users.noreply.github.com> Date: Fri, 18 Oct 2024 11:14:56 +0000 Subject: [PATCH] some refactoring --- jsr.json | 2 +- lib/tags.ts | 53 ++++++++++++++++++++++++++--------------------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/jsr.json b/jsr.json index 0bf0e04..fd1becd 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@purifyjs/core", - "version": "0.0.309", + "version": "0.0.310", "exports": "./lib/all.ts", "publish": { "include": ["lib", "LICENSE", "README.md"] diff --git a/lib/tags.ts b/lib/tags.ts index 24dce1c..454ef0a 100644 --- a/lib/tags.ts +++ b/lib/tags.ts @@ -74,13 +74,16 @@ export let toAppendable = ( } if (instancesOf(value, Signal)) { + /* I would make a custom element for this but we have to stay under 1.0kB */ return toAppendable( - tags["div"]({ style: "display:contents" }).use((element) => - value.follow( - (value) => element.replaceChildren(toAppendable(value)), - true + tags + .div({ style: "display:contents" }) + .use((element) => + value.follow( + (value) => element.replaceChildren(toAppendable(value)), + true + ) ) - ) ) } @@ -129,7 +132,7 @@ export let tags = new Proxy( (_: never, tag: T) => ( attributes: Builder.Attributes< - HTMLElementTagNameMap[T] & HTMLElementWithLifecycle + WithLifecycle > = {} ) => Builder.Proxy(withLifecycle(tag)).attributes(attributes) @@ -138,21 +141,17 @@ export let tags = new Proxy( export type Tags = { [K in keyof HTMLElementTagNameMap]: ( - attributes?: Builder.Attributes< - HTMLElementTagNameMap[K] & HTMLElementWithLifecycle - > - ) => Builder.Proxy + attributes?: Builder.Attributes> + ) => Builder.Proxy> } +export type WithLifecycle = T & HTMLElementWithLifecycle export interface HTMLElementWithLifecycle extends HTMLElement { onConnect(callback: Lifecycle.OnConnected): Lifecycle.OffConnected } - export namespace Lifecycle { export type OnDisconnected = () => void - export type OnConnected = ( - element: T - ) => void | OnDisconnected + export type OnConnected = (element: T) => void | OnDisconnected export type OffConnected = () => void } @@ -216,13 +215,13 @@ let withLifecycle = ( ) } - return new constructor() as HTMLElementTagNameMap[T] & HTMLElementWithLifecycle + return new constructor() as WithLifecycle } /** * Builder class to construct a builder to populate an element with attributes and children. */ -export class Builder { +export class Builder> { public readonly element: T /** @@ -246,8 +245,7 @@ export class Builder { } public children(...members: MemberOf[]): this { - let element = this.element - element.append(...members.map(toAppendable)) + this.element.append(...members.map(toAppendable)) return this } @@ -291,9 +289,9 @@ export declare namespace Builder { * .ariaLabel("Hello, World!"); * ``` */ - function Proxy(element: T): Builder.Proxy + function Proxy>(element: T): Builder.Proxy } -Builder.Proxy = (element: T) => +Builder.Proxy = >(element: T) => new Proxy(new Builder(element), { get: (target: Builder, name: PropertyKey, proxy) => target[name as never] ?? @@ -316,13 +314,13 @@ Builder.Proxy = (element: T) => export namespace Builder { export type Attributes = { - class?: T extends HTMLElementWithLifecycle ? string | Signal : string + class?: T extends WithLifecycle ? string | Signal : string id?: string | Signal - style?: T extends HTMLElementWithLifecycle ? string | Signal : string + style?: T extends WithLifecycle ? string | Signal : string title?: string | Signal - form?: T extends HTMLElementWithLifecycle ? string | Signal : string + form?: T extends WithLifecycle ? string | Signal : string } & { - [K in keyof ARIAMixin as ToKebabCase]?: T extends HTMLElementWithLifecycle ? + [K in keyof ARIAMixin as ToKebabCase]?: T extends WithLifecycle ? ARIAMixin[K] | Signal : ARIAMixin[K] } & { @@ -335,9 +333,10 @@ export namespace Builder { | boolean | bigint | null - | (T extends HTMLElementWithLifecycle ? Signal> : never) + | (T extends WithLifecycle ? Signal> + : never) - export type Proxy = Builder & { + export type Proxy> = Builder & { [K in keyof T as true extends ( [IsEventHandler, Not> & Not>][number] ) ? @@ -381,6 +380,6 @@ export type MemberOf = | bigint | null | ChildNodeOf - | (HTMLElement extends ChildNodeOf ? Builder : never) + | (HTMLElement extends ChildNodeOf ? Builder> : never) | MemberOf[] | Signal>