@brandup/ui 1.0.43 → 2.0.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 +477 -33
- package/dist/cjs/index.js +1216 -77
- package/dist/cjs/index.js.map +1 -1
- package/dist/mjs/index.js +1195 -78
- package/dist/mjs/index.js.map +1 -1
- package/dist/types.d.ts +473 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,65 +12,509 @@ npm i @brandup/ui@latest
|
|
|
12
12
|
|
|
13
13
|
## UIElement
|
|
14
14
|
|
|
15
|
-
`UIElement`
|
|
15
|
+
`UIElement` is a wrapper for `HTMLElement` that lets you attach your own business logic to it.
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
-
|
|
19
|
-
-
|
|
17
|
+
Features:
|
|
18
|
+
- Handling of commands declared in the markup of the `HTMLElement` that is bound to the `UIElement`.
|
|
19
|
+
- Subscribing to events through `EventEmitter`, which `UIElement` extends.
|
|
20
20
|
|
|
21
|
-
```
|
|
22
|
-
abstract class UIElement {
|
|
21
|
+
```ts
|
|
22
|
+
abstract class UIElement extends EventEmitter {
|
|
23
23
|
abstract typeName: string;
|
|
24
24
|
readonly element: HTMLElement | undefined;
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
protected defineEvent(eventName: string, eventOptions?: EventInit): void;
|
|
29
|
-
protected raiseEvent(eventName: string, eventArgs?: any): boolean;
|
|
26
|
+
static hasElement(elem: HTMLElement): boolean;
|
|
30
27
|
|
|
31
|
-
|
|
32
|
-
removeEventListener(type: string, listener?: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
|
|
33
|
-
dispatchEvent(event: Event): boolean;
|
|
28
|
+
protected setElement(elem: HTMLElement): void;
|
|
34
29
|
|
|
35
|
-
registerCommand(name: string, execute:
|
|
30
|
+
registerCommand(name: string, execute: CommandExecuteFunction, canExecute?: CommandCanExecuteFunction): this;
|
|
36
31
|
hasCommand(name: string): boolean;
|
|
37
|
-
|
|
38
|
-
protected _onRenderElement(_elem: HTMLElement);
|
|
39
|
-
protected _onCanExecCommand(_name: string, _elem: HTMLElement): boolean;
|
|
40
32
|
|
|
41
|
-
|
|
33
|
+
protected _onRenderElement(elem: HTMLElement): void;
|
|
34
|
+
protected _onCanExecCommand(name: string, elem: HTMLElement): boolean;
|
|
35
|
+
|
|
36
|
+
effectScope(): EffectScope;
|
|
42
37
|
destroy(): void;
|
|
38
|
+
|
|
39
|
+
toString(): string;
|
|
43
40
|
}
|
|
44
41
|
```
|
|
45
42
|
|
|
46
|
-
|
|
43
|
+
`UIElement` is an abstract class. A subclass sets `typeName` and binds a DOM element via `setElement`. An element can be bound only once; binding again (or binding an element that already belongs to another instance) throws an exception.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { UIElement } from "@brandup/ui";
|
|
47
|
+
|
|
48
|
+
class MyWidget extends UIElement {
|
|
49
|
+
typeName = "MyWidget";
|
|
47
50
|
|
|
48
|
-
|
|
51
|
+
constructor(elem: HTMLElement) {
|
|
52
|
+
super();
|
|
53
|
+
this.setElement(elem);
|
|
54
|
+
}
|
|
49
55
|
|
|
56
|
+
protected _onRenderElement(elem: HTMLElement) {
|
|
57
|
+
// initialize the markup
|
|
58
|
+
}
|
|
59
|
+
}
|
|
50
60
|
```
|
|
51
|
-
<button data-command="send">Send</button>
|
|
52
61
|
|
|
53
|
-
|
|
62
|
+
The `HTMLElement.prototype.ui` extension lets you bind a `UIElement` through a factory and returns the element itself:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
document.getElementById("widget")!.ui(elem => new MyWidget(elem));
|
|
54
66
|
```
|
|
55
67
|
|
|
56
|
-
|
|
68
|
+
The bound `UIElement` is available on the node through the `node.uielement` property.
|
|
69
|
+
|
|
70
|
+
### Element bound at construction
|
|
71
|
+
|
|
72
|
+
On the base `UIElement`, `element` is `HTMLElement | undefined` because the element may be bound later (an `Application`, for example, binds its element on run). When a component always receives its element in the constructor, extend `UIElementBound` instead — it binds the element immediately, so `element` is typed `HTMLElement` (never `undefined`):
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { UIElementBound } from "@brandup/ui";
|
|
76
|
+
|
|
77
|
+
class MyWidget extends UIElementBound {
|
|
78
|
+
constructor(elem: HTMLElement) {
|
|
79
|
+
super("MyWidget", elem); // typeName + element
|
|
80
|
+
}
|
|
81
|
+
}
|
|
57
82
|
|
|
83
|
+
const w = new MyWidget(document.createElement("div"));
|
|
84
|
+
w.element.focus(); // element: HTMLElement — no ?./!
|
|
58
85
|
```
|
|
86
|
+
|
|
87
|
+
## UI commands
|
|
88
|
+
|
|
89
|
+
`UIElement` lets you register command handlers, which are declared in the markup through the `data-command` attribute.
|
|
90
|
+
|
|
91
|
+
```html
|
|
92
|
+
<button data-command="send">Send</button>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
this.registerCommand("send", (context: CommandContext) => {
|
|
97
|
+
context.target.innerHTML = "ok";
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
You can register asynchronous command handlers — just return a `Promise`:
|
|
102
|
+
|
|
103
|
+
```ts
|
|
59
104
|
this.registerCommand("command1-async", (context: CommandContext) => {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
105
|
+
return new Promise<void>(resolve => {
|
|
106
|
+
context.target.innerHTML = "Loading...";
|
|
107
|
+
window.setTimeout(() => {
|
|
108
|
+
context.target.innerHTML = "Ok";
|
|
109
|
+
resolve();
|
|
110
|
+
}, 2000);
|
|
111
|
+
});
|
|
67
112
|
});
|
|
68
113
|
```
|
|
69
114
|
|
|
70
|
-
|
|
115
|
+
The third argument, `canExecute`, lets you restrict execution of the command:
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
this.registerCommand(
|
|
119
|
+
"submit",
|
|
120
|
+
(context) => { /* ... */ },
|
|
121
|
+
(context) => context.target.dataset.enabled === "true"
|
|
122
|
+
);
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Commands are triggered by the `click` event. The handler is looked up by walking up the DOM from the element with the `data-command` attribute to the nearest `UIElement` in which that command is registered.
|
|
126
|
+
|
|
127
|
+
While an asynchronous command is running, the **executing** CSS class is added to the target element (and removed once the `Promise` settles).
|
|
128
|
+
|
|
129
|
+
Command type signatures:
|
|
71
130
|
|
|
72
|
-
|
|
131
|
+
```ts
|
|
132
|
+
type CommandExecuteFunction = (context: CommandContext) => void | Promise<void | any>;
|
|
133
|
+
type CommandCanExecuteFunction = (context: CommandContext) => boolean;
|
|
134
|
+
|
|
135
|
+
interface CommandContext {
|
|
136
|
+
/** HTMLElement on which the command is executed. */
|
|
137
|
+
target: HTMLElement;
|
|
138
|
+
/** UIElement in which the command handler is registered. */
|
|
139
|
+
uiElem: UIElement;
|
|
140
|
+
/** Don't stop the click event chain of target. */
|
|
141
|
+
transparent?: boolean;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
interface CommandResult {
|
|
145
|
+
status: CommandExecStatus; // "disallow" | "already" | "success"
|
|
146
|
+
context: CommandContext;
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Before executing a command, `UIElement` triggers the `command` event with `CommandEventArgs` arguments (`{ element, name }`).
|
|
73
151
|
|
|
74
152
|
## UI Events
|
|
75
153
|
|
|
76
|
-
`UIElement` extends
|
|
154
|
+
`UIElement` extends the `EventEmitter` class.
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
class EventEmitter<TEvents = EventMap> {
|
|
158
|
+
on<K extends keyof TEvents & string>(eventName: K, callback: TEvents[K], context?: any): this;
|
|
159
|
+
once<K extends keyof TEvents & string>(eventName: K, callback: TEvents[K], context?: any): this;
|
|
160
|
+
off<K extends keyof TEvents & string>(eventName?: K | "all" | null, callback?: TEvents[K] | EventCallbackFunc | null, context?: any | null): this;
|
|
161
|
+
|
|
162
|
+
protected listenTo(source: EventEmitter<any>, eventName: string, callback: EventCallbackFunc): this;
|
|
163
|
+
protected listenToOnce(source: EventEmitter<any>, eventName: string, callback: EventCallbackFunc): this;
|
|
164
|
+
protected stopListening(source?: EventEmitter<any>, eventName?: string, callback?: EventCallbackFunc): this;
|
|
165
|
+
|
|
166
|
+
protected trigger<K extends keyof TEvents & string>(eventName: K, ...args: Parameters<TEvents[K]>): this;
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Typed events
|
|
171
|
+
|
|
172
|
+
The optional `TEvents` type parameter is an **event map** (`{ eventName: (args) => void }`) that gives a subclass strongly-typed event names, callback signatures and `trigger` arguments. It defaults to a loose map, so untyped usage keeps working.
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
interface CounterEvents {
|
|
176
|
+
increment: (by: number) => void;
|
|
177
|
+
reset: () => void;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
class Counter extends EventEmitter<CounterEvents> {
|
|
181
|
+
add(n: number) {
|
|
182
|
+
this.trigger("increment", n); // ✅ ok
|
|
183
|
+
// this.trigger("increment", "x"); // ❌ string is not number
|
|
184
|
+
// this.trigger("nope"); // ❌ unknown event
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const c = new Counter();
|
|
189
|
+
c.on("increment", by => console.log(by.toFixed(0))); // by: number
|
|
190
|
+
// c.on("unknown", () => {}); // ❌ unknown event
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
`UIElement` is itself generic — `UIElement<TEvents>` merges `TEvents` with the built-in `command`/`rendered`/`destroy` events, so subclasses can add their own typed events:
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
class MyWidget extends UIElement<{ ready: () => void }> {
|
|
197
|
+
// on/trigger accept "command", "destroy" AND "ready"
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Subscribing to and unsubscribing from events:
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
widget.on("command", (args: CommandEventArgs) => {
|
|
205
|
+
console.log("executing", args.name);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
// one-time handler
|
|
209
|
+
widget.once("destroy", () => console.log("destroyed"));
|
|
210
|
+
|
|
211
|
+
// unsubscribe (filters can be omitted — an omitted filter matches anything)
|
|
212
|
+
widget.off("command");
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
The special event name `"all"` receives every triggered event.
|
|
216
|
+
|
|
217
|
+
The protected `listenTo` / `listenToOnce` methods subscribe one emitter to another's events and track the subscription so it can be released via `stopListening`. On `destroy()`, all of a `UIElement`'s subscriptions are removed automatically.
|
|
218
|
+
|
|
219
|
+
## UIElement lifecycle
|
|
220
|
+
|
|
221
|
+
### destroy()
|
|
222
|
+
|
|
223
|
+
`destroy()` cleans up the element completely:
|
|
224
|
+
|
|
225
|
+
- Fires the `"destroy"` event.
|
|
226
|
+
- Stops all event subscriptions.
|
|
227
|
+
- **Cascades** to every nested `UIElement` found in the subtree (deepest descendants first), so you never need to destroy children manually.
|
|
228
|
+
- Stops all reactive `bind`/`bindEach` effects rendered inside the element.
|
|
229
|
+
- Detaches the `UIElement` from its DOM node (clears the `data-uiElement` attribute and the `uielement` property).
|
|
230
|
+
|
|
231
|
+
```ts
|
|
232
|
+
const parent = new ParentWidget(parentElem); // contains child UIElements
|
|
233
|
+
parent.destroy(); // automatically destroys all nested UIElements too
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### Auto-destroy on DOM removal
|
|
237
|
+
|
|
238
|
+
When a bound element is removed from the document **after having been connected to it**, `destroy()` is called automatically. This works for all nested UIElements too — removing a parent node triggers the full destroy cascade.
|
|
239
|
+
|
|
240
|
+
```ts
|
|
241
|
+
const w = new MyWidget(elem);
|
|
242
|
+
document.body.appendChild(elem);
|
|
243
|
+
|
|
244
|
+
elem.remove(); // destroy() fires automatically on next microtask
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
If the element was never connected to the document (e.g. built in memory and then discarded), auto-destroy does **not** fire — only mounted-then-removed elements are watched.
|
|
248
|
+
|
|
249
|
+
### Global click handler cleanup
|
|
250
|
+
|
|
251
|
+
The library registers one global `click` listener on `window` to handle commands. Call `destroyUI()` to remove it on app teardown or during HMR disposal:
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
import { destroyUI } from "@brandup/ui";
|
|
255
|
+
|
|
256
|
+
destroyUI();
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## DOM helpers
|
|
260
|
+
|
|
261
|
+
> Previously published as the separate `@brandup/ui-dom` package, now merged into `@brandup/ui`.
|
|
262
|
+
|
|
263
|
+
All DOM helpers are available through the `DOM` object.
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
import { DOM } from "@brandup/ui";
|
|
267
|
+
|
|
268
|
+
const DOM = {
|
|
269
|
+
// Finding elements
|
|
270
|
+
getById<T extends HTMLElement = HTMLElement>(id: string): T | null;
|
|
271
|
+
getByClass<T extends HTMLElement = HTMLElement>(container: Element, className: string): T | null;
|
|
272
|
+
getByName<T extends HTMLElement = HTMLElement>(name: string): T | null;
|
|
273
|
+
getElementByTagName<T extends HTMLElement = HTMLElement>(container: Element, tagName: string): T | null;
|
|
274
|
+
getElementsByTagName(container: Element, tagName: string): HTMLCollectionOf<Element>;
|
|
275
|
+
queryElement<T extends HTMLElement = HTMLElement>(container: Element, query: string): T | null;
|
|
276
|
+
queryElements<T extends HTMLElement = HTMLElement>(container: Element, query: string): NodeListOf<T>;
|
|
277
|
+
|
|
278
|
+
// Navigating sibling elements
|
|
279
|
+
nextElement<T extends HTMLElement = HTMLElement>(current: Element): T | null;
|
|
280
|
+
prevElement<T extends HTMLElement = HTMLElement>(current: Element): T | null;
|
|
281
|
+
nextElementByClass<T extends HTMLElement = HTMLElement>(current: Element, className: string): T | null;
|
|
282
|
+
prevElementByClass<T extends HTMLElement = HTMLElement>(current: Element, className: string): T | null;
|
|
283
|
+
|
|
284
|
+
// CSS classes
|
|
285
|
+
addClass(container: Element | null | undefined, selectors: string, cssClass: CssClass): void;
|
|
286
|
+
removeClass(container: Element | null | undefined, selectors: string, cssClass: CssClass): void;
|
|
287
|
+
|
|
288
|
+
// Clearing
|
|
289
|
+
empty(container: Element | null | undefined): void;
|
|
290
|
+
|
|
291
|
+
// Creating elements
|
|
292
|
+
tag<T extends keyof HTMLElementTagNameMap>(tagName: T, options?: ElementOptions | null, ...children: TagChildrenLike[]): HTMLElementTagNameMap[T];
|
|
293
|
+
tag<T extends keyof HTMLElementTagNameMap>(tagName: T, firstChild: TagFirstChild, ...children: TagChildrenLike[]): HTMLElementTagNameMap[T];
|
|
294
|
+
};
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### Creating HTML elements
|
|
298
|
+
|
|
299
|
+
`DOM.tag` creates an element from a tag name. The remaining arguments are children or options:
|
|
300
|
+
|
|
301
|
+
- **Options** — pass `null` (no options) or an `ElementOptions` plain object as the second argument.
|
|
302
|
+
- **Children** — any other value in the second position (string, number, `Element`, `Binding`, `BindingEach`, `Promise`, function, array) is treated as the **first child**, so the options argument can be omitted entirely.
|
|
303
|
+
|
|
304
|
+
```ts
|
|
305
|
+
// No options, no children
|
|
306
|
+
DOM.tag("div");
|
|
307
|
+
|
|
308
|
+
// Options object (id, class, dataset, styles, events, arbitrary attributes)
|
|
309
|
+
DOM.tag("div", { class: "box", id: "main" });
|
|
310
|
+
|
|
311
|
+
// null → no options, children follow
|
|
312
|
+
DOM.tag("div", null, "<p>test</p>");
|
|
313
|
+
|
|
314
|
+
// String as second arg → inserted as HTML child (NOT a CSS class)
|
|
315
|
+
DOM.tag("div", "<b>hello</b>");
|
|
316
|
+
DOM.tag("p", "plain text");
|
|
317
|
+
|
|
318
|
+
// Number or boolean as second arg → text child
|
|
319
|
+
DOM.tag("span", 42);
|
|
320
|
+
|
|
321
|
+
// Element/UIElement child — no null needed
|
|
322
|
+
DOM.tag("div", DOM.tag("span", "child"));
|
|
323
|
+
DOM.tag("div", new MyWidget(DOM.tag("span")));
|
|
324
|
+
|
|
325
|
+
// Multiple children
|
|
326
|
+
DOM.tag("ul", null, DOM.tag("li", "1"), DOM.tag("li", "2"));
|
|
327
|
+
|
|
328
|
+
// Children in an array
|
|
329
|
+
DOM.tag("ul", [DOM.tag("li", "1"), DOM.tag("li", "2")]);
|
|
330
|
+
|
|
331
|
+
// Factory function: receives the container element
|
|
332
|
+
DOM.tag("div", (elem) => { elem.id = "x"; });
|
|
333
|
+
DOM.tag("div", () => DOM.tag("span", "child"));
|
|
334
|
+
|
|
335
|
+
// Promise child — appended once it resolves
|
|
336
|
+
DOM.tag("div", fetch("/fragment").then(r => r.text()));
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
The full `ElementOptions` object:
|
|
340
|
+
|
|
341
|
+
```ts
|
|
342
|
+
interface ElementOptions {
|
|
343
|
+
id?: string; // id attribute
|
|
344
|
+
class?: CssClass; // CSS class(es): a string or an array of strings
|
|
345
|
+
command?: string; // data-command
|
|
346
|
+
dataset?: ElementData; // arbitrary data-* attributes
|
|
347
|
+
events?: ElementEvents; // event handlers keyed by lowercase name
|
|
348
|
+
styles?: ElementStyles; // inline styles (Partial<CSSStyleDeclaration>)
|
|
349
|
+
[name: string]: // any other key is a plain attribute:
|
|
350
|
+
| string | number | boolean | object | null | undefined;
|
|
351
|
+
// null → empty attribute, object → JSON.stringify, undefined → ignored
|
|
352
|
+
}
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
## Reactivity
|
|
356
|
+
|
|
357
|
+
A small fine-grained reactivity layer (Vue/MobX-style) with auto-tracking, plus `DOM.tag` bindings that update the DOM in place.
|
|
358
|
+
|
|
359
|
+
```ts
|
|
360
|
+
import { reactive, effect, computed, nextTick, untrack, bind, bindEach, DOM } from "@brandup/ui";
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### reactive / effect / computed
|
|
364
|
+
|
|
365
|
+
`reactive(obj)` returns a deep reactive proxy: reads are tracked and writes notify the effects that read them.
|
|
366
|
+
|
|
367
|
+
```ts
|
|
368
|
+
const state = reactive({ first: "Ada", last: "Lovelace", tags: ["math"] });
|
|
369
|
+
|
|
370
|
+
// effect re-runs when any property it reads changes
|
|
371
|
+
effect(() => console.log(state.first));
|
|
372
|
+
|
|
373
|
+
// computed: lazily cached, recomputes only when its dependencies change
|
|
374
|
+
const full = computed(() => `${state.first} ${state.last}`);
|
|
375
|
+
|
|
376
|
+
state.first = "Augusta"; // schedules the effect and invalidates `full`
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
- **Deep**: nested objects and arrays are reactive (`state.tags.push(...)` is tracked).
|
|
380
|
+
- **Dynamic dependencies**: an effect only depends on the properties it actually reads on its last run (`useA ? a : b` re-subscribes).
|
|
381
|
+
- **Batched**: effect re-runs are coalesced on the microtask queue, so multiple synchronous writes trigger a single run. Await `nextTick()` to observe the result:
|
|
382
|
+
|
|
383
|
+
```ts
|
|
384
|
+
state.first = "A";
|
|
385
|
+
state.last = "B";
|
|
386
|
+
await nextTick(); // effects have now re-run once
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
### untrack
|
|
390
|
+
|
|
391
|
+
`untrack(fn)` runs a function **without** recording any reactive reads as dependencies. Use it when you need to read reactive state inside an effect without creating a dependency on that read:
|
|
392
|
+
|
|
393
|
+
```ts
|
|
394
|
+
import { untrack } from "@brandup/ui";
|
|
395
|
+
|
|
396
|
+
effect(() => {
|
|
397
|
+
const items = state.list; // tracked — effect re-runs when list changes
|
|
398
|
+
const config = untrack(() => state.config); // not tracked — config changes won't re-run this effect
|
|
399
|
+
render(items, config);
|
|
400
|
+
});
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
### Binding DOM with `bind`
|
|
404
|
+
|
|
405
|
+
`bind(() => expr)` is a reactive `tag` child. The expression is tracked and re-rendered in place when its reactive state changes — text values reuse a text node (rendered via `textContent`, so safe from HTML injection), element/`UIElement` values swap the node.
|
|
406
|
+
|
|
407
|
+
```ts
|
|
408
|
+
const state = reactive({ name: "Alice", online: true });
|
|
409
|
+
|
|
410
|
+
const el = DOM.tag("div", null,
|
|
411
|
+
"Hi, ", bind(() => state.name), "! ",
|
|
412
|
+
bind(() => state.online ? DOM.tag("b", null, "online") : "offline")
|
|
413
|
+
);
|
|
414
|
+
|
|
415
|
+
state.name = "Bob"; // the text updates on the next tick
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
### Binding an array property with `bindEach`
|
|
419
|
+
|
|
420
|
+
`bindEach` is a reactive tag child — used exactly like `bind` — that renders a keyed list with minimal DOM updates. Each item is identified by a stable key; when the array changes, only new, removed, or reordered nodes are touched.
|
|
421
|
+
|
|
422
|
+
```ts
|
|
423
|
+
import { reactive, bindEach, bind, nextTick, DOM } from "@brandup/ui";
|
|
424
|
+
|
|
425
|
+
const state = reactive({
|
|
426
|
+
users: [
|
|
427
|
+
{ id: 1, name: "Alice" },
|
|
428
|
+
{ id: 2, name: "Bob" },
|
|
429
|
+
{ id: 3, name: "Charlie" },
|
|
430
|
+
]
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
const list = DOM.tag("ul", "user-list",
|
|
434
|
+
bindEach(
|
|
435
|
+
() => state.users, // reactive item source (tracked)
|
|
436
|
+
user => user.id, // stable key — identifies each node across re-renders
|
|
437
|
+
user => DOM.tag("li", null, // render one item → Element (called once per key)
|
|
438
|
+
bind(() => user.name) // bind() inside render for fine-grained per-item updates
|
|
439
|
+
)
|
|
440
|
+
)
|
|
441
|
+
);
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
Array mutations are tracked — the list reconciles on the next tick:
|
|
445
|
+
|
|
446
|
+
```ts
|
|
447
|
+
state.users.push({ id: 4, name: "Diana" }); // inserts one new <li>
|
|
448
|
+
state.users.splice(1, 1); // removes the <li> for id=2
|
|
449
|
+
state.users.unshift(state.users.pop()!); // moves last node to front, no re-render
|
|
450
|
+
state.users = [{ id: 1, name: "Alice" }]; // full reassignment — removes all but id=1
|
|
451
|
+
|
|
452
|
+
await nextTick(); // DOM is up to date
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
Because `render` is called **once per key** and runs **untracked**, reads inside it do not create dependencies on the list reconciler. Use `bind()` inside `render` so individual property changes update only the affected node — not the whole list:
|
|
456
|
+
|
|
457
|
+
```ts
|
|
458
|
+
// ✅ only the text node re-renders when user.name changes
|
|
459
|
+
user => DOM.tag("li", null, bind(() => user.name))
|
|
460
|
+
|
|
461
|
+
// ⚠️ name changes have no effect — render is untracked and never called again for existing keys
|
|
462
|
+
user => DOM.tag("li", null, user.name)
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
The binding stops automatically when its container is removed from the document (same lifecycle as `bind`).
|
|
466
|
+
|
|
467
|
+
### Disposal
|
|
468
|
+
|
|
469
|
+
Bindings hold a reactive effect that must be stopped to avoid leaks. This is handled automatically in common cases:
|
|
470
|
+
|
|
471
|
+
- **`UIElement.destroy()`** stops every `bind`/`bindEach` effect in the subtree and cascades to nested UIElements — no manual wiring needed.
|
|
472
|
+
- A binding **stops itself** once its node has been mounted into the document and then removed from it.
|
|
473
|
+
- Removing a bound element from the document also calls `destroy()` automatically (see [UIElement lifecycle](#uielement-lifecycle)).
|
|
474
|
+
|
|
475
|
+
```ts
|
|
476
|
+
class Widget extends UIElementBound {
|
|
477
|
+
constructor(elem: HTMLElement) {
|
|
478
|
+
super("widget", elem);
|
|
479
|
+
elem.append(DOM.tag("span", null, bind(() => state.name)));
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
const w = new Widget(document.createElement("div"));
|
|
483
|
+
w.destroy(); // stops bind() effects, cascades to nested UIElements
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
For effects and bindings outside a `UIElement`, or to group them explicitly, use `EffectScope`:
|
|
487
|
+
|
|
488
|
+
```ts
|
|
489
|
+
import { effectScope } from "@brandup/ui";
|
|
490
|
+
|
|
491
|
+
const scope = effectScope();
|
|
492
|
+
const view = scope.run(() => DOM.tag("div", null, bind(() => state.name)));
|
|
493
|
+
scope.stop(); // stops every effect/binding created inside the scope
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
`UIElement.effectScope()` returns a scope that is stopped automatically when the element is destroyed:
|
|
497
|
+
|
|
498
|
+
```ts
|
|
499
|
+
class Widget extends UIElementBound {
|
|
500
|
+
constructor(elem: HTMLElement) {
|
|
501
|
+
super("widget", elem);
|
|
502
|
+
this.effectScope().run(() => {
|
|
503
|
+
elem.append(DOM.tag("span", null, bind(() => state.name)));
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
```
|
|
508
|
+
|
|
509
|
+
## Constants
|
|
510
|
+
|
|
511
|
+
The names of DOM attributes, properties, and CSS classes are exported as the `UICONSTANTS` namespace:
|
|
512
|
+
|
|
513
|
+
```ts
|
|
514
|
+
import { UICONSTANTS } from "@brandup/ui";
|
|
515
|
+
|
|
516
|
+
UICONSTANTS.ElemAttributeName; // "uiElement" — data attribute holding the typeName
|
|
517
|
+
UICONSTANTS.ElemPropertyName; // "uielement" — property on the DOM element referencing the UIElement
|
|
518
|
+
UICONSTANTS.CommandAttributeName; // "command" — data attribute of the command
|
|
519
|
+
UICONSTANTS.CommandExecutingCssClassName; // "executing" — class applied while an async command is running
|
|
520
|
+
```
|