@o.z/zui 0.1.0 → 0.1.1
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 +92 -79
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,36 +1,28 @@
|
|
|
1
1
|
# ZUI Framework ⚡
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/@o.z/zui)
|
|
3
|
+
[](https://www.npmjs.com/package/@o.z/zui)
|
|
4
|
+
[](https://www.npmjs.com/package/@o.z/zui)
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://vitejs.dev/)
|
|
4
7
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
**ZUI** is a lightweight, type-safe abstraction layer for building native Web Components. It leverages the power of **TypeScript Stage 3 Decorators** (`accessor`) to remove boilerplate, manage reactivity, and streamline DOM interactions, all while staying close to the metal of the browser.
|
|
8
|
+
**ZUI** is a next-generation, lightweight abstraction layer for building native Web Components. It leverages **TypeScript Stage 3 Decorators** to eliminate boilerplate, manage reactive state, and streamline DOM interactions while staying close to the metal of the browser.
|
|
8
9
|
|
|
9
10
|
---
|
|
10
11
|
|
|
11
12
|
## 🚀 Why ZUI?
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
Standard Web Components require significant boilerplate: managing `observedAttributes`, manually handling `shadowRoot`, and verbose event dispatching. **ZUI** solves this with a declarative, type-safe API:
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
16
|
+
* **⚡ Stage 3 Decorators:** Built for modern TypeScript, utilizing the standard `accessor` pattern.
|
|
17
|
+
* **🔄 Reactive State:** Properties automatically trigger view updates and lifecycle hooks.
|
|
18
|
+
* **🎯 Zero-Boilerplate Refs:** Type-safe DOM element caching without manual `querySelector` calls.
|
|
19
|
+
* **📡 Magic Event Emitters:** Auto-generated, strictly typed event dispatch methods.
|
|
20
|
+
* **🎨 Scoped Styling:** Seamless integration with standard Shadow DOM or scoped styles.
|
|
21
|
+
* **🛠 Customized Built-ins:** First-class support for extending native elements (e.g., `<div is="my-component">`).
|
|
20
22
|
|
|
21
23
|
---
|
|
22
24
|
|
|
23
|
-
## 📦
|
|
24
|
-
|
|
25
|
-
* **Stage 3 Decorators:** Built for the future of TypeScript.
|
|
26
|
-
* **Reactivity System:** Simple `accessor` pattern with lifecycle hooks (e.g., `countUpdate`).
|
|
27
|
-
* **Scoped Styling:** Automatic Shadow DOM or scoped style injection.
|
|
28
|
-
* **Vite Compatible:** Designed to work with `.html?raw` and `.scss?inline` imports.
|
|
29
|
-
* **Type Safety:** First-class TypeScript support for Props and Events.
|
|
30
|
-
|
|
31
|
-
---
|
|
32
|
-
|
|
33
|
-
## Installation
|
|
25
|
+
## 📦 Installation
|
|
34
26
|
|
|
35
27
|
```bash
|
|
36
28
|
npm install @o.z/zui
|
|
@@ -44,9 +36,9 @@ pnpm add @o.z/zui
|
|
|
44
36
|
|
|
45
37
|
## 🛠️ Usage Example
|
|
46
38
|
|
|
47
|
-
|
|
39
|
+
ZUI works best with Vite's string imports for HTML and CSS. Below is a complete example of a **Reactive Counter** that extends a native `div`.
|
|
48
40
|
|
|
49
|
-
### 1.
|
|
41
|
+
### 1. The Component (`counter.ts`)
|
|
50
42
|
|
|
51
43
|
```typescript
|
|
52
44
|
import { defineElement, event, property, ref } from "@o.z/zui";
|
|
@@ -57,75 +49,82 @@ import cssStr from "./counter.scss?inline";
|
|
|
57
49
|
tagName: "my-counter",
|
|
58
50
|
html: htmlStr,
|
|
59
51
|
css: cssStr,
|
|
60
|
-
options: { extends: 'div' } // Extends native
|
|
52
|
+
options: { extends: 'div' } // Extends native HTMLDivElement
|
|
61
53
|
})
|
|
62
54
|
export class Counter extends HTMLDivElement {
|
|
63
55
|
|
|
64
|
-
// 1. Reactive State
|
|
56
|
+
// 1. Reactive State: Auto-observes attributes and updates
|
|
65
57
|
@property()
|
|
66
|
-
accessor count
|
|
58
|
+
accessor count = 0;
|
|
67
59
|
|
|
68
|
-
// 2. DOM References
|
|
69
|
-
@ref(
|
|
60
|
+
// 2. DOM References: Selects elements from the template
|
|
61
|
+
@ref(".counter")
|
|
70
62
|
counterRef!: HTMLDivElement;
|
|
71
63
|
|
|
72
|
-
@ref(
|
|
64
|
+
@ref(".increase")
|
|
73
65
|
increaseRef!: HTMLButtonElement;
|
|
74
66
|
|
|
75
|
-
@ref(
|
|
67
|
+
@ref(".decrease")
|
|
76
68
|
decreaseRef!: HTMLButtonElement;
|
|
77
69
|
|
|
78
|
-
// 3. Event
|
|
70
|
+
// 3. Event Emitter: Generates 'emitCounterClick' method
|
|
79
71
|
@event({ name: "counter-click" })
|
|
80
72
|
counterClick!: CustomEvent<any>;
|
|
81
73
|
|
|
82
74
|
connected() {
|
|
83
|
-
|
|
84
|
-
this.
|
|
75
|
+
// 'connected' is a ZUI shorthand for connectedCallback
|
|
76
|
+
this.increaseRef.addEventListener("click", this.incHandler);
|
|
77
|
+
this.decreaseRef.addEventListener("click", this.decHandler);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
disconnected() {
|
|
81
|
+
this.increaseRef.removeEventListener("click", this.incHandler);
|
|
82
|
+
this.decreaseRef.removeEventListener("click", this.decHandler);
|
|
85
83
|
}
|
|
86
84
|
|
|
87
|
-
|
|
88
|
-
// Magic method generated by @event
|
|
89
|
-
(this as any).emitCounterClick({ count:
|
|
85
|
+
incHandler = (e: MouseEvent) => {
|
|
86
|
+
// Magic method generated by @event
|
|
87
|
+
(this as any).emitCounterClick({ e, count: 1 });
|
|
90
88
|
}
|
|
91
89
|
|
|
92
|
-
|
|
90
|
+
decHandler = (e: MouseEvent) => {
|
|
91
|
+
(this as any).emitCounterClick({ e, count: -1 });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 4. Lifecycle Hook: Called automatically when 'count' changes
|
|
93
95
|
countUpdate(_oldVal: number, newVal: number) {
|
|
94
96
|
this.counterRef.innerHTML = newVal.toString();
|
|
95
97
|
}
|
|
96
98
|
}
|
|
97
99
|
```
|
|
98
100
|
|
|
99
|
-
### 2. The Template
|
|
101
|
+
### 2. The Template (`counter.html`)
|
|
100
102
|
|
|
101
|
-
**Counter.html**
|
|
102
103
|
```html
|
|
103
104
|
<div class="container">
|
|
104
|
-
<div
|
|
105
|
-
<button class="
|
|
106
|
-
<button class="
|
|
105
|
+
<div class="counter">0</div>
|
|
106
|
+
<button class="increase">+</button>
|
|
107
|
+
<button class="decrease">-</button>
|
|
107
108
|
</div>
|
|
108
109
|
```
|
|
109
110
|
|
|
110
|
-
### 3.
|
|
111
|
+
### 3. Usage in HTML
|
|
111
112
|
|
|
112
|
-
Because ZUI supports **Customized Built-in Elements**, you can
|
|
113
|
+
Because ZUI supports **Customized Built-in Elements**, you can enhance standard tags:
|
|
113
114
|
|
|
114
115
|
```html
|
|
115
|
-
<div id="counter" is="my-counter">
|
|
116
|
-
<span slot="increase">+1</span>
|
|
117
|
-
<span slot="decrease">-1</span>
|
|
118
|
-
</div>
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
```typescript
|
|
122
|
-
// main.ts
|
|
123
|
-
const counterEl = document.querySelector<Counter>("#counter")!;
|
|
116
|
+
<div id="counter" is="my-counter"></div>
|
|
124
117
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
118
|
+
<script type="module">
|
|
119
|
+
import "./counter";
|
|
120
|
+
|
|
121
|
+
const el = document.querySelector("#counter");
|
|
122
|
+
|
|
123
|
+
// Listen to the strongly typed custom event
|
|
124
|
+
el.addEventListener("counter-click", (e) => {
|
|
125
|
+
el.count += e.detail.value.count;
|
|
126
|
+
});
|
|
127
|
+
</script>
|
|
129
128
|
```
|
|
130
129
|
|
|
131
130
|
---
|
|
@@ -133,35 +132,49 @@ counterEl.addEventListener("counter-click", (e: any) => {
|
|
|
133
132
|
## 📚 API Reference
|
|
134
133
|
|
|
135
134
|
### `@defineElement(config)`
|
|
136
|
-
Class decorator
|
|
137
|
-
|
|
138
|
-
* `
|
|
139
|
-
* `
|
|
140
|
-
* `
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
*
|
|
135
|
+
Class decorator to register the custom element.
|
|
136
|
+
|
|
137
|
+
* **tagName** `string`: The kebab-case tag name (e.g., `my-counter`).
|
|
138
|
+
* **html** `string`: Raw HTML string for the Shadow DOM.
|
|
139
|
+
* **css** `string`: (Optional) Raw CSS string.
|
|
140
|
+
* **options** `ElementDefinitionOptions`: (Optional) Used for `extends` (e.g., `{ extends: 'div' }`).
|
|
141
|
+
|
|
142
|
+
### `@property(options)`
|
|
143
|
+
Decorator for class accessors. It handles attribute observation and type conversion.
|
|
144
|
+
|
|
145
|
+
* **type**: `"string" | "number" | "boolean"` (Auto-inferred from initial value if omitted).
|
|
146
|
+
* **name**: Attribute name (defaults to kebab-case of property name).
|
|
147
|
+
* **Lifecycle Hook**: When `myProp` changes, ZUI looks for a method named `myPropUpdate(oldVal, newVal)` and calls it.
|
|
147
148
|
|
|
148
149
|
### `@ref(selector)`
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
*
|
|
150
|
+
Field decorator to cache DOM queries.
|
|
151
|
+
|
|
152
|
+
* **selector** `string`: The CSS selector to find the element within the component's Shadow Root.
|
|
153
|
+
* Populates the property automatically after the component connects.
|
|
152
154
|
|
|
153
|
-
### `@event(
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
*
|
|
155
|
+
### `@event(config)`
|
|
156
|
+
Field decorator to generate event emitters.
|
|
157
|
+
|
|
158
|
+
* **name** `string`: The name of the custom event to dispatch.
|
|
159
|
+
* **Generates**: A method on the instance named `emit[PropertyName capitalized]`.
|
|
160
|
+
* **Payload**: Arguments passed to the emit method are available in `event.detail.value`.
|
|
157
161
|
|
|
158
162
|
---
|
|
159
163
|
|
|
160
|
-
## 💻
|
|
164
|
+
## 💻 Development
|
|
165
|
+
|
|
166
|
+
This project uses **Vite** and **TypeScript**.
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
# Install dependencies
|
|
170
|
+
yarn install
|
|
171
|
+
|
|
172
|
+
# Start development server
|
|
173
|
+
yarn dev
|
|
161
174
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
175
|
+
# Build the library
|
|
176
|
+
yarn build
|
|
177
|
+
```
|
|
165
178
|
|
|
166
179
|
---
|
|
167
180
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@o.z/zui",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Next-generation Web Component framework leveraging TypeScript Stage 3 standard decorators.",
|
|
5
5
|
"homepage": "https://github.com/z-npm/zui#readme",
|
|
6
6
|
"docs": "https://github.com/z-npm/zui#readme",
|
|
@@ -56,10 +56,10 @@
|
|
|
56
56
|
"build": "tsc && vite build"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@types/node": "^25.0.
|
|
59
|
+
"@types/node": "^25.0.10",
|
|
60
60
|
"@z-code/vite-plugin-swc": "^0.5.5",
|
|
61
61
|
"glob": "^13.0.0",
|
|
62
|
-
"sass": "^1.97.
|
|
62
|
+
"sass": "^1.97.3",
|
|
63
63
|
"typescript": "^5.9.3",
|
|
64
64
|
"vite": "^7.3.1",
|
|
65
65
|
"vite-plugin-dts": "^4.5.4",
|