elements-kit 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +252 -16
- package/dist/attributes.d.mts +90 -0
- package/dist/attributes.mjs +82 -0
- package/dist/builder/dom.d.mts +213 -0
- package/dist/builder/dom.mjs +208 -0
- package/dist/builder/index.d.mts +89 -0
- package/dist/builder/index.mjs +115 -0
- package/dist/element-B3gwJmBr.mjs +277 -0
- package/dist/index.d.mts +36 -0
- package/dist/index.mjs +130 -0
- package/dist/jsx-runtime/index.d.mts +70 -0
- package/dist/jsx-runtime/index.mjs +4 -0
- package/dist/polyfill-Bvo2e52W.d.mts +14 -0
- package/dist/polyfill-DAalJpCO.mjs +20 -0
- package/dist/signals.d.mts +40 -3
- package/dist/signals.mjs +74 -3
- package/dist/slot.d.mts +74 -0
- package/dist/slot.mjs +143 -0
- package/package.json +40 -14
- package/dist/builder.d.mts +0 -11799
- package/dist/builder.mjs +0 -208
- package/dist/chunk-CFhWmker.mjs +0 -36
- package/dist/core.d.mts +0 -14
- package/dist/core.mjs +0 -74
- package/dist/signals-BGUPt0zl.mjs +0 -13
- package/dist/signals-BzhB4Ch2.d.mts +0 -10
package/dist/slot.d.mts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import "./polyfill-Bvo2e52W.mjs";
|
|
2
|
+
import { ElementBuilder } from "./builder/index.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/slot.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* A lightweight slot that reserves a region in the DOM using comment markers.
|
|
7
|
+
* Content between the markers can be replaced dynamically without wrapper elements.
|
|
8
|
+
*/
|
|
9
|
+
declare class Slot {
|
|
10
|
+
private readonly start;
|
|
11
|
+
private readonly end;
|
|
12
|
+
/**
|
|
13
|
+
* Render the slot as a DocumentFragment.
|
|
14
|
+
* If not yet mounted, inserts the comment markers and optional default content.
|
|
15
|
+
* If already mounted, extracts and returns the current content.
|
|
16
|
+
*/
|
|
17
|
+
slot(defaultContent?: string | Node | ElementBuilder): DocumentFragment;
|
|
18
|
+
/**
|
|
19
|
+
* Replace the slot's content with the given element.
|
|
20
|
+
* No-op if the slot is not mounted or the content is identical.
|
|
21
|
+
*/
|
|
22
|
+
set(element: Node): void;
|
|
23
|
+
get(): DocumentFragment | null;
|
|
24
|
+
/** Returns the parent node if the slot is mounted, otherwise `null`. */
|
|
25
|
+
parent(): ParentNode;
|
|
26
|
+
/** Whether the slot's comment markers are attached to the DOM. */
|
|
27
|
+
isMounted(): boolean;
|
|
28
|
+
private isSame;
|
|
29
|
+
/**
|
|
30
|
+
* Create a callable slot instance.
|
|
31
|
+
*
|
|
32
|
+
* The returned value is both a function and an object:
|
|
33
|
+
* - Call it to render the slot with optional default content.
|
|
34
|
+
* - Access `.set()`, `.parent()`, `.isMounted()` for slot management.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const slot = createSlot();
|
|
39
|
+
* el.append(slot("default text")); // mount with default
|
|
40
|
+
* slot.set(newElement); // replace content
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
static new(): Slot & ((defaultContent?: string | Node | ElementBuilder) => DocumentFragment);
|
|
44
|
+
}
|
|
45
|
+
type SlotInstance = ReturnType<typeof Slot.new>;
|
|
46
|
+
/** A callable slot — invoke to render, or access `.set()` / `.isMounted()` / `.parent()` for management. */
|
|
47
|
+
/**
|
|
48
|
+
* Symbol key for attaching a `Slots` instance to a custom element instance.
|
|
49
|
+
* This prevent collisions with Element properties and are not meant to be treated as JSX children.
|
|
50
|
+
*/
|
|
51
|
+
declare const $slots: unique symbol;
|
|
52
|
+
declare const $map: unique symbol;
|
|
53
|
+
declare const $keys: unique symbol;
|
|
54
|
+
declare const $has: unique symbol;
|
|
55
|
+
/**
|
|
56
|
+
* A keyed collection of slot instances.
|
|
57
|
+
* Slots are pre-created from the provided keys and lazily created on first access for unknown keys.
|
|
58
|
+
*/
|
|
59
|
+
declare class Slots<K extends string> implements Iterable<[K, SlotInstance]> {
|
|
60
|
+
readonly [$map]: Map<K, Slot & ((defaultContent?: string | Node | ElementBuilder) => DocumentFragment)>;
|
|
61
|
+
private constructor();
|
|
62
|
+
[Symbol.iterator](): MapIterator<[K, Slot & ((defaultContent?: string | Node | ElementBuilder) => DocumentFragment)]>;
|
|
63
|
+
[Symbol.toStringTag](): string;
|
|
64
|
+
[Symbol.hasInstance](instance: unknown): instance is Slots<any>;
|
|
65
|
+
[$has](key: K): boolean;
|
|
66
|
+
/** Check whether a slot with the given key exists. */
|
|
67
|
+
static has<K extends string>(slots: Slots<K>, key: K): boolean;
|
|
68
|
+
[$keys](): MapIterator<K>;
|
|
69
|
+
/** Iterate over all registered slot keys. */
|
|
70
|
+
static keys<K extends string>(slots: Slots<K>): MapIterator<K>;
|
|
71
|
+
static new<K extends string>(keys: K[]): Slots<K> & { readonly [P in K]: SlotInstance };
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
export { $slots, Slot, SlotInstance, Slots };
|
package/dist/slot.mjs
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
//#region src/slot.ts
|
|
2
|
+
/**
|
|
3
|
+
* A lightweight slot that reserves a region in the DOM using comment markers.
|
|
4
|
+
* Content between the markers can be replaced dynamically without wrapper elements.
|
|
5
|
+
*/
|
|
6
|
+
var Slot = class Slot {
|
|
7
|
+
start = document.createComment("{");
|
|
8
|
+
end = document.createComment("}");
|
|
9
|
+
/**
|
|
10
|
+
* Render the slot as a DocumentFragment.
|
|
11
|
+
* If not yet mounted, inserts the comment markers and optional default content.
|
|
12
|
+
* If already mounted, extracts and returns the current content.
|
|
13
|
+
*/
|
|
14
|
+
slot(defaultContent) {
|
|
15
|
+
const fragment = document.createDocumentFragment();
|
|
16
|
+
if (this.isMounted()) {
|
|
17
|
+
const range = document.createRange();
|
|
18
|
+
range.setStartAfter(this.start);
|
|
19
|
+
range.setEndBefore(this.end);
|
|
20
|
+
fragment.appendChild(range.extractContents());
|
|
21
|
+
return fragment;
|
|
22
|
+
}
|
|
23
|
+
fragment.appendChild(this.start);
|
|
24
|
+
fragment.appendChild(this.end);
|
|
25
|
+
if (defaultContent) {
|
|
26
|
+
const defaultNode = typeof defaultContent === "string" ? document.createTextNode(defaultContent) : defaultContent instanceof Node ? defaultContent : defaultContent.ref();
|
|
27
|
+
fragment.insertBefore(defaultNode, this.end);
|
|
28
|
+
}
|
|
29
|
+
return fragment;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Replace the slot's content with the given element.
|
|
33
|
+
* No-op if the slot is not mounted or the content is identical.
|
|
34
|
+
*/
|
|
35
|
+
set(element) {
|
|
36
|
+
const parent = this.parent();
|
|
37
|
+
if (!parent) return;
|
|
38
|
+
if (this.isSame(element)) return;
|
|
39
|
+
const range = document.createRange();
|
|
40
|
+
range.setStartAfter(this.start);
|
|
41
|
+
range.setEndBefore(this.end);
|
|
42
|
+
range.deleteContents();
|
|
43
|
+
parent.insertBefore(element, this.end);
|
|
44
|
+
}
|
|
45
|
+
get() {
|
|
46
|
+
if (!this.isMounted()) return null;
|
|
47
|
+
const range = document.createRange();
|
|
48
|
+
range.setStartAfter(this.start);
|
|
49
|
+
range.setEndBefore(this.end);
|
|
50
|
+
return range.extractContents();
|
|
51
|
+
}
|
|
52
|
+
/** Returns the parent node if the slot is mounted, otherwise `null`. */
|
|
53
|
+
parent() {
|
|
54
|
+
return this.isMounted() ? this.start.parentNode : null;
|
|
55
|
+
}
|
|
56
|
+
/** Whether the slot's comment markers are attached to the DOM. */
|
|
57
|
+
isMounted() {
|
|
58
|
+
return this.start.parentNode === this.end.parentNode && !!this.start.parentNode;
|
|
59
|
+
}
|
|
60
|
+
isSame(element) {
|
|
61
|
+
return this.start.nextSibling === element && this.end === element.nextSibling;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Create a callable slot instance.
|
|
65
|
+
*
|
|
66
|
+
* The returned value is both a function and an object:
|
|
67
|
+
* - Call it to render the slot with optional default content.
|
|
68
|
+
* - Access `.set()`, `.parent()`, `.isMounted()` for slot management.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* const slot = createSlot();
|
|
73
|
+
* el.append(slot("default text")); // mount with default
|
|
74
|
+
* slot.set(newElement); // replace content
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
static new() {
|
|
78
|
+
const instance = new Slot();
|
|
79
|
+
return new Proxy(instance.slot.bind(instance), {
|
|
80
|
+
apply(target, _thisArg, argArray) {
|
|
81
|
+
return target(...argArray);
|
|
82
|
+
},
|
|
83
|
+
get(_target, prop) {
|
|
84
|
+
return instance[prop];
|
|
85
|
+
},
|
|
86
|
+
getPrototypeOf() {
|
|
87
|
+
return Slot.prototype;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
/** A callable slot — invoke to render, or access `.set()` / `.isMounted()` / `.parent()` for management. */
|
|
93
|
+
/**
|
|
94
|
+
* Symbol key for attaching a `Slots` instance to a custom element instance.
|
|
95
|
+
* This prevent collisions with Element properties and are not meant to be treated as JSX children.
|
|
96
|
+
*/
|
|
97
|
+
const $slots = Symbol("slots");
|
|
98
|
+
const $map = Symbol("map");
|
|
99
|
+
const $keys = Symbol("keys");
|
|
100
|
+
const $has = Symbol("has");
|
|
101
|
+
/**
|
|
102
|
+
* A keyed collection of slot instances.
|
|
103
|
+
* Slots are pre-created from the provided keys and lazily created on first access for unknown keys.
|
|
104
|
+
*/
|
|
105
|
+
var Slots = class Slots {
|
|
106
|
+
[$map] = /* @__PURE__ */ new Map();
|
|
107
|
+
constructor(keys = []) {
|
|
108
|
+
for (const key of keys) this[$map].set(key, Slot.new());
|
|
109
|
+
}
|
|
110
|
+
[Symbol.iterator]() {
|
|
111
|
+
return this[$map][Symbol.iterator]();
|
|
112
|
+
}
|
|
113
|
+
[Symbol.toStringTag]() {
|
|
114
|
+
return "Slots";
|
|
115
|
+
}
|
|
116
|
+
[Symbol.hasInstance](instance) {
|
|
117
|
+
return instance instanceof Slots;
|
|
118
|
+
}
|
|
119
|
+
[$has](key) {
|
|
120
|
+
return this[$map].has(key);
|
|
121
|
+
}
|
|
122
|
+
/** Check whether a slot with the given key exists. */
|
|
123
|
+
static has(slots, key) {
|
|
124
|
+
return slots[$has](key);
|
|
125
|
+
}
|
|
126
|
+
[$keys]() {
|
|
127
|
+
return this[$map].keys();
|
|
128
|
+
}
|
|
129
|
+
/** Iterate over all registered slot keys. */
|
|
130
|
+
static keys(slots) {
|
|
131
|
+
return slots[$keys]();
|
|
132
|
+
}
|
|
133
|
+
static new(keys) {
|
|
134
|
+
const instance = new Slots(keys);
|
|
135
|
+
return new Proxy(instance, { get(target, prop, receiver) {
|
|
136
|
+
if (typeof prop === "string" && target[$map].has(prop)) return target[$map].get(prop);
|
|
137
|
+
return Reflect.get(target, prop, receiver);
|
|
138
|
+
} });
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
//#endregion
|
|
143
|
+
export { $slots, Slot, Slots };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "elements-kit",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.2",
|
|
5
5
|
"description": "A lightweight reactive UI library that transforms native HTMLElements into reactive components with signals. Ideal for framework-agnostic applications and web components.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"webcomponents",
|
|
@@ -17,35 +17,61 @@
|
|
|
17
17
|
],
|
|
18
18
|
"author": "@waelbettayeb",
|
|
19
19
|
"license": "MIT",
|
|
20
|
-
"main": "./dist/
|
|
21
|
-
"types": "./dist/
|
|
20
|
+
"main": "./dist/index.mjs",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
22
|
"files": [
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
25
|
"exports": {
|
|
26
26
|
".": {
|
|
27
|
-
"import": "./dist/
|
|
28
|
-
"types": "./dist/
|
|
27
|
+
"import": "./dist/index.mjs",
|
|
28
|
+
"types": "./dist/index.d.ts"
|
|
29
29
|
},
|
|
30
30
|
"./signals": {
|
|
31
|
-
"import": "./dist/signals.
|
|
31
|
+
"import": "./dist/signals.mjs",
|
|
32
32
|
"types": "./dist/signals.d.ts"
|
|
33
33
|
},
|
|
34
|
+
"./slot": {
|
|
35
|
+
"import": "./dist/slot.mjs",
|
|
36
|
+
"types": "./dist/slot.d.ts"
|
|
37
|
+
},
|
|
38
|
+
"./attributes": {
|
|
39
|
+
"import": "./dist/attributes.mjs",
|
|
40
|
+
"types": "./dist/attributes.d.ts"
|
|
41
|
+
},
|
|
34
42
|
"./builder": {
|
|
35
|
-
"import": "./dist/builder.
|
|
36
|
-
"types": "./dist/builder.d.ts"
|
|
43
|
+
"import": "./dist/builder/index.mjs",
|
|
44
|
+
"types": "./dist/builder/index.d.ts"
|
|
45
|
+
},
|
|
46
|
+
"./builder/dom": {
|
|
47
|
+
"import": "./dist/builder/dom.mjs",
|
|
48
|
+
"types": "./dist/builder/dom.d.ts"
|
|
49
|
+
},
|
|
50
|
+
"./jsx-runtime": {
|
|
51
|
+
"import": "./dist/jsx-runtime/index.mjs",
|
|
52
|
+
"types": "./dist/jsx-runtime/index.d.ts"
|
|
53
|
+
},
|
|
54
|
+
"./jsx-dev-runtime": {
|
|
55
|
+
"import": "./dist/jsx-runtime/index.mjs",
|
|
56
|
+
"types": "./dist/jsx-runtime/index.d.ts"
|
|
37
57
|
}
|
|
38
58
|
},
|
|
39
59
|
"scripts": {
|
|
40
|
-
"demo": "pnpx vite ./demo",
|
|
41
|
-
"build": "tsdown"
|
|
60
|
+
"demo": "pnpx vite ./demo -c demo/vite.config.ts",
|
|
61
|
+
"build": "tsdown",
|
|
62
|
+
"watch": "tsdown -w",
|
|
63
|
+
"test": "vitest run",
|
|
64
|
+
"test:watch": "vitest"
|
|
42
65
|
},
|
|
43
|
-
"packageManager": "pnpm@10.
|
|
66
|
+
"packageManager": "pnpm@10.33.0",
|
|
44
67
|
"devDependencies": {
|
|
45
|
-
"
|
|
46
|
-
"
|
|
68
|
+
"happy-dom": "^20.8.9",
|
|
69
|
+
"tsdown": "0.21.7",
|
|
70
|
+
"typescript": "^6.0.2",
|
|
71
|
+
"vitest": "^4.1.2"
|
|
47
72
|
},
|
|
48
73
|
"dependencies": {
|
|
49
|
-
"alien-signals": "^3.1.2"
|
|
74
|
+
"alien-signals": "^3.1.2",
|
|
75
|
+
"dom-expressions": "^0.40.6"
|
|
50
76
|
}
|
|
51
77
|
}
|