@twobirds/microcomponents 0.2.2 → 0.2.4
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 +45 -20
- package/dist/MC.min.js +1 -1
- package/dist/autoload.min.js +1 -1
- package/dist/core/MC.d.ts +288 -237
- package/dist/core/MC.d.ts.map +1 -1
- package/dist/core/MC.js +438 -182
- package/dist/core/elements.d.ts +58 -231
- package/dist/core/elements.d.ts.map +1 -1
- package/dist/core/elements.js +99 -214
- package/dist/core/index.d.ts +75 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +72 -0
- package/dist/core/mcGuards.d.ts +7 -7
- package/dist/core/mcGuards.d.ts.map +1 -1
- package/dist/core/mcGuards.js +1 -1
- package/dist/elements.min.js +1 -1
- package/dist/formValues.min.js +1 -1
- package/dist/mcGuards.min.js +1 -1
- package/dist/microcomponents.min.js +1 -1
- package/dist/observables.min.js +1 -1
- package/dist/state/formValues.d.ts +3 -1
- package/dist/state/formValues.d.ts.map +1 -1
- package/dist/state/formValues.js +3 -1
- package/dist/state/index.d.ts +7 -0
- package/dist/state/index.d.ts.map +1 -0
- package/dist/state/index.js +5 -0
- package/dist/state/observables.d.ts +7 -59
- package/dist/state/observables.d.ts.map +1 -1
- package/dist/state/observables.js +63 -1
- package/dist/state/placeholders.d.ts +1 -0
- package/dist/state/placeholders.d.ts.map +1 -1
- package/dist/state/placeholders.js +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/mcTypes.d.ts +3 -3
- package/dist/types/mcTypes.js +1 -1
- package/dist/utils/autoload.d.ts +6 -2
- package/dist/utils/autoload.d.ts.map +1 -1
- package/dist/utils/autoload.js +7 -5
- package/dist/utils/events.d.ts +22 -79
- package/dist/utils/events.d.ts.map +1 -1
- package/dist/utils/events.js +63 -1
- package/dist/utils/helpers.d.ts +3 -2
- package/dist/utils/helpers.d.ts.map +1 -1
- package/dist/utils/helpers.js +3 -2
- package/dist/utils/index.d.ts +17 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +12 -0
- package/package.json +9 -11
package/dist/core/MC.d.ts
CHANGED
|
@@ -1,219 +1,319 @@
|
|
|
1
|
+
import { BubbleController } from '../utils/events.js';
|
|
2
|
+
import type { BubbleDirections, McController, McEntriesOf, McEventHandler, McHost, McHostOf, McMerged, McPayloadOf, McRemoved } from '../types/mcTypes.js';
|
|
3
|
+
export type { McBubbleController, McController, McDetail, McEntriesOf, McEvent, McEventHandler, McHost, McHostOf, McMerged, McNativeEventHandlers, McPayloadOf, McRemoved, } from '../types/mcTypes.js';
|
|
4
|
+
export { isMcHost } from './mcGuards.js';
|
|
1
5
|
/**
|
|
2
|
-
@
|
|
3
|
-
## Overview
|
|
6
|
+
The base class for all microcomponents — shared root of both {@link MC} and {@link DC}.
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
Holds a `WeakRef` to the target (a DOM element for `DC`, or any plain object for `MC`)
|
|
9
|
+
to avoid circular references. For `DC` subclasses it also bootstraps two automatic
|
|
10
|
+
behaviours:
|
|
6
11
|
|
|
7
|
-
|
|
12
|
+
1. **Event listener auto-attachment** — methods whose names start with `on` or `one`
|
|
13
|
+
(e.g. `onClick`, `oneInit`) are wired to the target element via {@link on} / {@link one}.
|
|
14
|
+
2. **"Init" lifecycle event** — dispatched asynchronously in a microtask so that all
|
|
15
|
+
synchronous constructor work finishes first.
|
|
8
16
|
|
|
9
|
-
|
|
17
|
+
Both mechanisms run only when the instance is a `DC` (checked with `instanceof DC`).
|
|
18
|
+
Plain `MC` instances skip them entirely, keeping server-side usage lightweight.
|
|
10
19
|
|
|
11
|
-
|
|
20
|
+
### Plugin extension points
|
|
12
21
|
|
|
13
|
-
The
|
|
22
|
+
The prototype of `McBase` is the attachment point for optional plugins that curry
|
|
23
|
+
additional instance methods onto every microcomponent. Two built-in plugins modify
|
|
24
|
+
this class at runtime:
|
|
14
25
|
|
|
15
|
-
- **
|
|
26
|
+
- **{@link State.observePlugin}** — adds `notify()`, `subscribe()`, and `bind()` so that
|
|
27
|
+
observable properties can be controlled directly on the component instance with
|
|
28
|
+
fluent chaining. Call `observePlugin(DC)` once at module level to activate.
|
|
16
29
|
|
|
17
|
-
- **
|
|
30
|
+
- **{@link State.formValuePlugin}** — adds `formValues()` for two-way binding between DOM
|
|
31
|
+
form controls and an observable property. Also pulls in all methods from
|
|
32
|
+
{@link State.observePlugin}, so you can chain `.subscribe()` and `.bind()` after
|
|
33
|
+
`.formValues()`. Call `formValuePlugin(DC)` once at module level to activate.
|
|
18
34
|
|
|
19
|
-
|
|
35
|
+
Both plugins are idempotent — calling them multiple times has no side effects. They
|
|
36
|
+
work by assigning methods onto `McBase.prototype` (reached via
|
|
37
|
+
`Object.getPrototypeOf(Class.prototype)`) and use TypeScript module augmentation
|
|
38
|
+
(`declare module`) so the added signatures appear in type-checking automatically.
|
|
20
39
|
|
|
21
|
-
|
|
40
|
+
@template TTarget - The concrete type of the wrapped target; preserved by the
|
|
41
|
+
{@link McBase.target} getter.
|
|
22
42
|
|
|
23
|
-
|
|
24
|
-
|----------|-------------------|
|
|
25
|
-
| Server-side rendering or logic | MC |
|
|
26
|
-
| Pure data processing without DOM | MC |
|
|
27
|
-
| Interactive UI elements in the browser | DC |
|
|
28
|
-
| Event-driven components attached to HTML nodes | DC |
|
|
43
|
+
@example Extending with plugins at runtime
|
|
29
44
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
## The `target` Property: Backreference Concept
|
|
45
|
+
```ts
|
|
46
|
+
import { DC, observePlugin, formValuePlugin } from '@twobirds/microcomponents';
|
|
33
47
|
|
|
34
|
-
|
|
48
|
+
observePlugin(DC); // adds notify(), subscribe(), bind() to McBase.prototype
|
|
49
|
+
formValuePlugin(DC); // adds formValues() (and re-runs observePlugin)
|
|
35
50
|
|
|
36
|
-
|
|
37
|
-
|
|
51
|
+
class MyForm extends DC {
|
|
52
|
+
data = { name: '' };
|
|
38
53
|
|
|
39
|
-
|
|
54
|
+
oneInit() { // runs once after the constructor
|
|
55
|
+
this
|
|
56
|
+
.formValues(this.target, 'data')
|
|
57
|
+
.subscribe('data', (v) => console.log(v))
|
|
58
|
+
.bind('data', this.target);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
@group Micro Components
|
|
63
|
+
*/
|
|
64
|
+
declare class McBase<TTarget extends object = object> {
|
|
65
|
+
private _target;
|
|
66
|
+
/**
|
|
67
|
+
The constructor for the base `McBase` class.
|
|
68
|
+
|
|
69
|
+
Initializes a weak reference to the target and triggers auto-attachment
|
|
70
|
+
of listeners and initialization lifecycle events.
|
|
71
|
+
|
|
72
|
+
@param target - The object or element this microcomponent represents.
|
|
73
|
+
*/
|
|
74
|
+
constructor(target: TTarget);
|
|
75
|
+
/**
|
|
76
|
+
Returns the current target object (DOM node or other data) referenced by this microcomponent.
|
|
77
|
+
|
|
78
|
+
Uses a `WeakRef` internally to avoid circular references, and safely dereferences it here.
|
|
79
|
+
|
|
80
|
+
The dereference is asserted non-null: the controller is stored on its target
|
|
81
|
+
(`element._mc`), so any code that can reach the controller through the element is
|
|
82
|
+
holding the target alive. Only a controller retained independently after its element
|
|
83
|
+
was garbage-collected could observe a stale reference — do not cache controllers
|
|
84
|
+
detached from their elements.
|
|
40
85
|
|
|
41
|
-
|
|
86
|
+
@returns The target object.
|
|
87
|
+
*/
|
|
88
|
+
get target(): TTarget;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
A general-purpose base microcomponent that does not require a DOM target.
|
|
42
92
|
|
|
43
|
-
|
|
93
|
+
Inherits from `McBase`, but is primarily used as a lightweight container or
|
|
94
|
+
for logic not directly tied to DOM elements.
|
|
44
95
|
|
|
45
|
-
|
|
96
|
+
@template TTarget - The concrete type of the wrapped target data; preserved by
|
|
97
|
+
the `target` getter, so `new MC({ some: 'data' }).target.some` stays typed.
|
|
98
|
+
@group Micro Components
|
|
99
|
+
*/
|
|
100
|
+
declare class MC<TTarget extends object = object> extends McBase<TTarget> {
|
|
101
|
+
/**
|
|
102
|
+
Constructs an instance of the general-purpose microcomponent.
|
|
103
|
+
|
|
104
|
+
@param target - Target object or data.
|
|
105
|
+
*/
|
|
106
|
+
constructor(target: TTarget);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
@class DC
|
|
110
|
+
---
|
|
46
111
|
|
|
47
|
-
|
|
48
|
-
2. The element receives a `data-mc` attribute, signaling that it hosts at least one microcomponent. This attribute is used internally for DOM queries and selectors.
|
|
112
|
+
A DOM-centric microcomponent that operates on a specific HTML element.
|
|
49
113
|
|
|
50
|
-
|
|
114
|
+
Extends {@link McBase} and associates itself with an actual DOM node via the
|
|
115
|
+
`_target` reference. Provides methods for event handling, DOM traversal,
|
|
116
|
+
and interaction with sibling or ancestor microcomponents.
|
|
51
117
|
|
|
52
118
|
---
|
|
53
119
|
|
|
54
|
-
##
|
|
120
|
+
## Static Configuration Properties
|
|
55
121
|
|
|
56
|
-
DC
|
|
122
|
+
DC classes support three static properties that configure custom elements
|
|
123
|
+
before any instance is constructed:
|
|
57
124
|
|
|
58
|
-
|
|
125
|
+
### `static tag?: string`
|
|
59
126
|
|
|
127
|
+
Replaces the default target element with a semantic HTML element, or removes it entirely.
|
|
60
128
|
|
|
61
|
-
|
|
129
|
+
- **Non-empty string** (e.g., `'header'`) — the original custom element is replaced
|
|
130
|
+
with the specified built-in element. The original tag name is added as a CSS class.
|
|
131
|
+
- **Empty string `''`** — the target element is removed from the DOM entirely.
|
|
132
|
+
Useful for components that only inject global styles or act as style containers.
|
|
62
133
|
|
|
63
|
-
|
|
64
|
-
```javascript
|
|
65
|
-
import { DC } from '@twobirds/microcomponents';
|
|
134
|
+
@example static tag — replace with semantic element
|
|
66
135
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
logTarget() {
|
|
73
|
-
// this.target is the <button> element itself
|
|
74
|
-
console.log(this.target.tagName); // "BUTTON"
|
|
75
|
-
}
|
|
136
|
+
```ts
|
|
137
|
+
class ShopHeader extends DC {
|
|
138
|
+
static tag = 'header';
|
|
76
139
|
}
|
|
140
|
+
defineCE('shop-header', ShopHeader);
|
|
141
|
+
```
|
|
77
142
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
143
|
+
Produces `<header class="shop-header">` instead of `<shop-header>`.
|
|
144
|
+
|
|
145
|
+
@example static tag — remove element entirely
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
class GlobalStyles extends DC {
|
|
149
|
+
static tag = '';
|
|
82
150
|
}
|
|
151
|
+
defineCE('global-styles', GlobalStyles);
|
|
83
152
|
```
|
|
84
153
|
|
|
85
|
-
|
|
154
|
+
The element self-destructs; only side effects (e.g., injected styles) remain.
|
|
86
155
|
|
|
156
|
+
### `static assets: string[]`
|
|
87
157
|
|
|
88
|
-
|
|
89
|
-
|
|
158
|
+
Declares companion files for auto-loading by convention. Asset names are camelCase
|
|
159
|
+
and translated into file extensions: `'targetHtml'` → `.target.html`, `'json'` → `.json`.
|
|
90
160
|
|
|
91
|
-
|
|
92
|
-
|
|
161
|
+
The path is derived from the custom element tag segments, and the basename is the
|
|
162
|
+
last segment. For `<cart-container-checkout>`, assets load from `/cart/container/checkout.*`.
|
|
93
163
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
164
|
+
When assets finish loading, an `'assets'` event fires so that `oneAssets(ev)` can
|
|
165
|
+
process them. Everything ending in `Json` arrives parsed; all other files arrive as text.
|
|
97
166
|
|
|
98
|
-
|
|
99
|
-
this.count++;
|
|
100
|
-
}
|
|
167
|
+
@example static assets with autoload
|
|
101
168
|
|
|
102
|
-
|
|
103
|
-
|
|
169
|
+
```ts
|
|
170
|
+
class CartCheckout extends DC {
|
|
171
|
+
static assets = ['targetHtml', 'targetCss', 'configJson'];
|
|
172
|
+
|
|
173
|
+
oneAssets(ev) {
|
|
174
|
+
const { configJson } = ev.detail.data;
|
|
175
|
+
console.log(configJson); // parsed JSON object
|
|
104
176
|
}
|
|
105
177
|
}
|
|
178
|
+
defineCE('cart-checkout', CartCheckout);
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### `static attributes?: object`
|
|
106
182
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
183
|
+
Sets HTML attributes on every instance automatically.
|
|
184
|
+
|
|
185
|
+
@example static attributes
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
class ProductCard extends DC {
|
|
189
|
+
static attributes = { role: 'listitem' };
|
|
190
|
+
}
|
|
191
|
+
defineCE('product-card', ProductCard);
|
|
111
192
|
```
|
|
112
193
|
|
|
113
|
-
|
|
194
|
+
Every `<product-card>` carries `role="listitem"`.
|
|
195
|
+
|
|
196
|
+
### Combined usage
|
|
114
197
|
|
|
115
|
-
|
|
198
|
+
All three can be combined in one class:
|
|
116
199
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
200
|
+
@example All static properties together
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
class ShopCatalog extends DC {
|
|
204
|
+
static tag = 'section';
|
|
205
|
+
static attributes = { role: 'list', 'aria-label': 'products' };
|
|
206
|
+
static assets = ['targetHtml', 'targetCss'];
|
|
207
|
+
}
|
|
208
|
+
defineCE('shop-catalog', ShopCatalog);
|
|
120
209
|
```
|
|
121
210
|
|
|
122
|
-
|
|
123
|
-
```javascript
|
|
124
|
-
import { DC } from '@twobirds/microcomponents';
|
|
211
|
+
Produces `<section role="list" aria-label="products">`.
|
|
125
212
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
## Event Handler Conventions
|
|
216
|
+
|
|
217
|
+
Methods whose names start with `on` or `one` are automatically wired as event listeners
|
|
218
|
+
on the target element. The prefix determines persistence:
|
|
219
|
+
|
|
220
|
+
- **`on<EventName>`** — persistent listener (fires every time)
|
|
221
|
+
- **`one<EventName>`** — one-time listener (fires once, then detaches)
|
|
222
|
+
|
|
223
|
+
### Web Component Lifecycle Events
|
|
224
|
+
|
|
225
|
+
When a DC is registered via {@link defineCE}, the custom element dispatches these
|
|
226
|
+
internal events that can be caught with handler methods:
|
|
227
|
+
|
|
228
|
+
| Method | When it fires |
|
|
229
|
+
|--------|---------------|
|
|
230
|
+
| `onConnected()` | Element was inserted into the DOM (`connectedCallback`) |
|
|
231
|
+
| `onDisconnected()` | Element was removed from the DOM (`disconnectedCallback`) |
|
|
232
|
+
| `onAdopted()` | Element was moved to a new document (`adoptedCallback`) |
|
|
233
|
+
| `onAttributeChanged()` | An observed attribute changed (`attributeChangedCallback`) |
|
|
234
|
+
| `oneInit()` | Constructor finished; all synchronous setup complete (microtask) |
|
|
235
|
+
|
|
236
|
+
@example Event handlers in action
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
class MyElement extends DC {
|
|
240
|
+
onConnected() {
|
|
241
|
+
console.log('entered the DOM');
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
onDisconnected() {
|
|
245
|
+
console.log('left the DOM');
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
onClick() {
|
|
249
|
+
this.trigger('something', 'payload');
|
|
130
250
|
}
|
|
131
251
|
|
|
132
|
-
|
|
133
|
-
console.log('
|
|
252
|
+
oneSomething(ev) {
|
|
253
|
+
console.log('first time only:', ev.detail.data);
|
|
134
254
|
}
|
|
135
255
|
}
|
|
256
|
+
defineCE('my-element', MyElement);
|
|
257
|
+
```
|
|
136
258
|
|
|
137
|
-
|
|
138
|
-
const element = document.getElementById('container')!;
|
|
259
|
+
---
|
|
139
260
|
|
|
140
|
-
|
|
141
|
-
DC.add(element, 'helloWorld', new HelloWorld(element));
|
|
261
|
+
## Construction Patterns
|
|
142
262
|
|
|
143
|
-
|
|
144
|
-
element._mc.helloWorld.greet(); // "Hello World"
|
|
145
|
-
```
|
|
263
|
+
### Template usage (declarative)
|
|
146
264
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
265
|
+
```ts
|
|
266
|
+
class MyElement extends DC {}
|
|
267
|
+
defineCE('my-element', MyElement);
|
|
268
|
+
// Use in HTML: <my-element></my-element>
|
|
150
269
|
```
|
|
151
270
|
|
|
152
|
-
|
|
271
|
+
### Programmatic construction with data
|
|
153
272
|
|
|
154
|
-
|
|
273
|
+
When constructing programmatically via {@link getCE}, pass only the `data` payload —
|
|
274
|
+
the target element is created automatically by the custom element constructor.
|
|
155
275
|
|
|
156
|
-
|
|
157
|
-
import { BubbleController } from '../utils/events.js';
|
|
158
|
-
import type { BubbleDirections, McController, McEntriesOf, McEventHandler, McHost, McHostOf, McMerged, McPayloadOf, McRemoved } from '../types/mcTypes.js';
|
|
159
|
-
export type { McBubbleController, McController, McDetail, McEntriesOf, McEvent, McEventHandler, McHost, McHostOf, McMerged, McNativeEventHandlers, McPayloadOf, McRemoved, } from '../types/mcTypes.js';
|
|
160
|
-
export { isMcDOM } from './mcGuards.js';
|
|
161
|
-
/**
|
|
162
|
-
The base class for all microcomponents.
|
|
276
|
+
@example Programmatic construction
|
|
163
277
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
*/
|
|
168
|
-
declare class McBase<TTarget extends object = object> {
|
|
169
|
-
private _target;
|
|
170
|
-
/**
|
|
171
|
-
The constructor for the base `McBase` class.
|
|
172
|
-
|
|
173
|
-
Initializes a weak reference to the target and triggers auto-attachment
|
|
174
|
-
of listeners and initialization lifecycle events.
|
|
175
|
-
|
|
176
|
-
@param target - The object or element this microcomponent represents.
|
|
177
|
-
*/
|
|
178
|
-
constructor(target: TTarget);
|
|
179
|
-
/**
|
|
180
|
-
Returns the current target object (DOM node or other data) referenced by this microcomponent.
|
|
181
|
-
|
|
182
|
-
Uses a `WeakRef` internally to avoid circular references, and safely dereferences it here.
|
|
183
|
-
|
|
184
|
-
The dereference is asserted non-null: the controller is stored on its target
|
|
185
|
-
(`element._mc`), so any code that can reach the controller through the element is
|
|
186
|
-
holding the target alive. Only a controller retained independently after its element
|
|
187
|
-
was garbage-collected could observe a stale reference — do not cache controllers
|
|
188
|
-
detached from their elements.
|
|
278
|
+
```ts
|
|
279
|
+
class MyElement extends DC {
|
|
280
|
+
data = {};
|
|
189
281
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
282
|
+
constructor(target, data) {
|
|
283
|
+
super(target);
|
|
284
|
+
this.data = data;
|
|
285
|
+
}
|
|
193
286
|
}
|
|
194
|
-
|
|
195
|
-
A general-purpose base microcomponent that does not require a DOM target.
|
|
287
|
+
defineCE('my-element', MyElement);
|
|
196
288
|
|
|
197
|
-
|
|
198
|
-
|
|
289
|
+
const Ctor = getCE('my-element');
|
|
290
|
+
document.body.append(new Ctor({ name: 'John' }));
|
|
291
|
+
```
|
|
199
292
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
293
|
+
### Data from HTML attributes
|
|
294
|
+
|
|
295
|
+
@example Reading data from an attribute
|
|
296
|
+
|
|
297
|
+
```ts
|
|
298
|
+
class MyElement extends DC {
|
|
299
|
+
data = {};
|
|
300
|
+
|
|
301
|
+
constructor(target) {
|
|
302
|
+
super(target);
|
|
303
|
+
this.data = JSON.parse(target.getAttribute('my-data'));
|
|
304
|
+
}
|
|
210
305
|
}
|
|
211
|
-
|
|
212
|
-
|
|
306
|
+
defineCE('my-element', MyElement);
|
|
307
|
+
// Use in HTML: <my-element my-data='{"name":"John"}'></my-element>
|
|
308
|
+
```
|
|
213
309
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
310
|
+
---
|
|
311
|
+
|
|
312
|
+
## Plugin Extension Points
|
|
313
|
+
|
|
314
|
+
The prototype of `DC` (inherited from {@link McBase}) is extended at runtime by optional
|
|
315
|
+
plugins. See the {@link McBase} documentation for details on {@link State.observePlugin} and
|
|
316
|
+
{@link State.formValuePlugin}.
|
|
217
317
|
|
|
218
318
|
@template T - The concrete element type of the host element.
|
|
219
319
|
@template EM - The component's custom event map (`event name → payload type`).
|
|
@@ -221,7 +321,9 @@ and interaction with sibling or ancestor microcomponents.
|
|
|
221
321
|
payload for registered names, and `on()`/`one()` handlers receive
|
|
222
322
|
`CustomEvent<McDetail<Payload>>` with no annotation needed. Unregistered
|
|
223
323
|
names keep the open `unknown`-payload behavior.
|
|
224
|
-
|
|
324
|
+
|
|
325
|
+
@example Typed event map
|
|
326
|
+
|
|
225
327
|
```ts
|
|
226
328
|
class Cart extends DC<HTMLFormElement, { CartUpdate: { count: number } }> {
|
|
227
329
|
checkout() {
|
|
@@ -230,25 +332,52 @@ class Cart extends DC<HTMLFormElement, { CartUpdate: { count: number } }> {
|
|
|
230
332
|
}
|
|
231
333
|
}
|
|
232
334
|
```
|
|
335
|
+
@group Micro Components
|
|
233
336
|
*/
|
|
234
337
|
declare class DC<T extends HTMLElement = HTMLElement, EM extends Record<string, unknown> = Record<never, never>> extends McBase<T> implements McController<T> {
|
|
338
|
+
/**
|
|
339
|
+
* Replaces the target element with a semantic HTML tag, or removes it entirely.
|
|
340
|
+
* Set to `'header'` to swap `<my-element>` for `<header>`, or `''` to self-destruct.
|
|
341
|
+
*/
|
|
235
342
|
static tag?: string;
|
|
343
|
+
/**
|
|
344
|
+
* Result container for assets (e.g., `['targetHtml', 'json']`).
|
|
345
|
+
* Asset names (keys) are camelCase and translated into file extensions: `'targetHtml'` → `.target.html`.
|
|
346
|
+
*/
|
|
347
|
+
static _assets: Record<string, any>;
|
|
348
|
+
/**
|
|
349
|
+
* Declares companion files for auto-loading by convention (e.g., `['targetHtml', 'json']`).
|
|
350
|
+
* Asset names are camelCase and translated into file extensions: `'targetHtml'` → `.target.html`.
|
|
351
|
+
*/
|
|
236
352
|
static assets: string[];
|
|
353
|
+
/**
|
|
354
|
+
* Sets HTML attributes on every instance automatically.
|
|
355
|
+
* Example: `{ role: 'listitem', 'aria-label': 'nav' }`.
|
|
356
|
+
*/
|
|
237
357
|
static attributes?: object;
|
|
358
|
+
/**
|
|
359
|
+
* Constructs a new DC instance and applies static configuration.
|
|
360
|
+
*
|
|
361
|
+
* The constructor performs these steps in order:
|
|
362
|
+
* 1. Calls {@link McBase} to establish the target reference and auto-attach listeners.
|
|
363
|
+
* 2. If `static tag` is set and differs from the current element, replaces the target
|
|
364
|
+
* with a new built-in element (or removes it if `tag === ''`).
|
|
365
|
+
* 3. If `static assets` has entries, initiates asynchronous loading, fires an `'assets'` event when complete handled by `oneAssets(ev)`.
|
|
366
|
+
* 4. Applies any `static attributes` to the target element.
|
|
367
|
+
*
|
|
368
|
+
* @param target - The host DOM element this component operates on.
|
|
369
|
+
*/
|
|
238
370
|
constructor(target: T);
|
|
239
371
|
/**
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
@
|
|
247
|
-
@template E - The type of the attached element; retrievable as `result._mc[key]`.
|
|
248
|
-
@param target - The DOM element to which to attach the microcomponent.
|
|
249
|
-
@param key - The property name/key under which to store the instance in `_mc`.
|
|
250
|
-
@returns The target DOM element with the attached microcomponent.
|
|
372
|
+
Returns the loaded assets for this component instance.
|
|
373
|
+
|
|
374
|
+
Assets are loaded asynchronously when `static assets` is defined on the class.
|
|
375
|
+
This method returns the resolved asset map keyed by filename, or `undefined`
|
|
376
|
+
if assets have not yet been loaded.
|
|
377
|
+
|
|
378
|
+
@returns A record mapping asset keys to their content strings, or undefined.
|
|
251
379
|
*/
|
|
380
|
+
getAssets(): Record<string, string | Record<string, any>> | {};
|
|
252
381
|
static add<T extends HTMLElement>(target: T, key: string): McHost<McHostOf<T>, McEntriesOf<T>>;
|
|
253
382
|
/** @param element - The microcomponent instance or config object to attach. When
|
|
254
383
|
omitted, a bare `{}` marker is stored and no typed entry is claimed. */
|
|
@@ -288,43 +417,10 @@ declare class DC<T extends HTMLElement = HTMLElement, EM extends Record<string,
|
|
|
288
417
|
```
|
|
289
418
|
*/
|
|
290
419
|
static get<E extends object>(element: unknown, key: string, elementMcClass: abstract new (...args: never[]) => E): E | undefined;
|
|
291
|
-
/**
|
|
292
|
-
Detaches an event listener from the DOM node.
|
|
293
|
-
|
|
294
|
-
This is an instance method wrapper for `off()`, using the internal `_target` DOM reference.
|
|
295
|
-
|
|
296
|
-
@param eventName - The name of the event (e.g., 'click', 'update').
|
|
297
|
-
@param handler - The previously attached callback function or object to remove.
|
|
298
|
-
*/
|
|
299
420
|
off<D = unknown, K extends string = string>(eventName: K, handler: McEventHandler<McPayloadOf<EM, K, D>>): void;
|
|
300
421
|
off(eventName: string, handler: EventListenerOrEventListenerObject): void;
|
|
301
|
-
/**
|
|
302
|
-
Attaches a persistent event listener to the DOM node.
|
|
303
|
-
|
|
304
|
-
This is an instance method wrapper for `on()`, using the internal `_target` DOM reference.
|
|
305
|
-
|
|
306
|
-
The handler receives a `CustomEvent<McDetail<Payload>>`. For event names
|
|
307
|
-
registered in the component's event map `EM` the payload is resolved from the
|
|
308
|
-
map — an unannotated arrow handler is fully typed. For unregistered names the
|
|
309
|
-
payload defaults to `unknown` and can be pinned via the type argument
|
|
310
|
-
(`on<Payload>('Name', handler)`) or the handler's own annotation.
|
|
311
|
-
|
|
312
|
-
@param eventName - The name of the event (e.g., 'click', 'update').
|
|
313
|
-
@param handler - The callback function or object to invoke.
|
|
314
|
-
@returns The attached handler reference, unchanged — usable directly with {@link off}.
|
|
315
|
-
*/
|
|
316
422
|
on<D = unknown, K extends string = string>(eventName: K, handler: McEventHandler<McPayloadOf<EM, K, D>>): McEventHandler<McPayloadOf<EM, K, D>>;
|
|
317
423
|
on(eventName: string, handler: EventListenerOrEventListenerObject): EventListenerOrEventListenerObject;
|
|
318
|
-
/**
|
|
319
|
-
Attaches a one-time event listener to the DOM node.
|
|
320
|
-
|
|
321
|
-
This is an instance method wrapper for `one()`, using the internal `_target` DOM reference.
|
|
322
|
-
Payload typing follows the same rules as {@link DC.on}.
|
|
323
|
-
|
|
324
|
-
@param eventName - The name of the event (e.g., 'click', 'update').
|
|
325
|
-
@param handler - The callback function or object to invoke once.
|
|
326
|
-
@returns The attached one-time handler reference, unchanged — usable directly with {@link off}.
|
|
327
|
-
*/
|
|
328
424
|
one<D = unknown, K extends string = string>(eventName: K, handler: McEventHandler<McPayloadOf<EM, K, D>>): McEventHandler<McPayloadOf<EM, K, D>>;
|
|
329
425
|
one(eventName: string, handler: EventListenerOrEventListenerObject): EventListenerOrEventListenerObject;
|
|
330
426
|
/**
|
|
@@ -342,60 +438,15 @@ declare class DC<T extends HTMLElement = HTMLElement, EM extends Record<string,
|
|
|
342
438
|
@param controller - Optional controller for managing bubbling behavior.
|
|
343
439
|
*/
|
|
344
440
|
trigger<D = unknown, K extends string = keyof EM & string>(eventName: K, data?: McPayloadOf<EM, K, D>, bubble?: BubbleDirections, controller?: BubbleController): void;
|
|
345
|
-
/**
|
|
346
|
-
Returns an array of all ancestor microcomponents up the DOM tree.
|
|
347
|
-
|
|
348
|
-
Traverses upward from this element's target, collecting the `_mc` instances
|
|
349
|
-
of ancestor elements marked with `data-mc`. The keyed form only collects
|
|
350
|
-
hosts whose registry carries `key`, typed from the global {@link McRegistry}
|
|
351
|
-
binding — non-optional, since presence is checked during the walk.
|
|
352
|
-
|
|
353
|
-
@template R - Known registry entries of the results; pass a record to assert
|
|
354
|
-
a site-specific shape: `ancestors<{ cart: Cart }>()`. Defaults to none.
|
|
355
|
-
@returns An array of ancestor microcomponent instances (empty if none found).
|
|
356
|
-
*/
|
|
357
441
|
ancestors<R extends Record<string, unknown> = Record<never, never>>(): (McController & R)[];
|
|
358
442
|
/** @param key - Optional registry key each collected ancestor must carry. */
|
|
359
443
|
ancestors<K extends keyof McRegistry & string>(key: K): (McController & Pick<McRegistry, K>)[];
|
|
360
|
-
/**
|
|
361
|
-
Returns the closest ancestor microcomponent or null if none found.
|
|
362
|
-
|
|
363
|
-
This method is equivalent to calling `ancestors()[0]`, but more concise.
|
|
364
|
-
The keyed form walks up to the nearest host whose registry carries `key`,
|
|
365
|
-
typed from the global {@link McRegistry} binding — non-optional, since
|
|
366
|
-
presence is checked during the walk.
|
|
367
|
-
|
|
368
|
-
@template R - Known registry entries of the result; pass a record to assert
|
|
369
|
-
a site-specific shape: `closest<{ cart: Cart }>()?.cart.addItem(...)`.
|
|
370
|
-
Defaults to none.
|
|
371
|
-
@returns The nearest matching ancestor's `_mc`, or null if not found.
|
|
372
|
-
*/
|
|
373
444
|
closest<R extends Record<string, unknown> = Record<never, never>>(): (McController & R) | null;
|
|
374
445
|
/** @param key - Optional registry key the ancestor must carry. */
|
|
375
446
|
closest<K extends keyof McRegistry & string>(key: K): (McController & Pick<McRegistry, K>) | null;
|
|
376
|
-
/**
|
|
377
|
-
Returns an array of direct child microcomponents.
|
|
378
|
-
|
|
379
|
-
Calls `getMcChildNodes()` internally to find immediate descendants only,
|
|
380
|
-
excluding nested grandchildren and deeper levels. The keyed form filters to
|
|
381
|
-
hosts whose registry carries `key`; see {@link DC.closest}.
|
|
382
|
-
|
|
383
|
-
@template R - Known registry entries of the results; see {@link DC.closest}.
|
|
384
|
-
@returns An array of child microcomponent instances (empty if none found).
|
|
385
|
-
*/
|
|
386
447
|
children<R extends Record<string, unknown> = Record<never, never>>(): (McController & R)[];
|
|
387
448
|
/** @param key - Optional registry key each child must carry. */
|
|
388
449
|
children<K extends keyof McRegistry & string>(key: K): (McController & Pick<McRegistry, K>)[];
|
|
389
|
-
/**
|
|
390
|
-
Returns an array of all descendant microcomponents recursively.
|
|
391
|
-
|
|
392
|
-
Uses a `querySelectorAll` query to collect every nested element marked with
|
|
393
|
-
`data-mc`, returning their associated `_mc` instances. The keyed form
|
|
394
|
-
filters to hosts whose registry carries `key`; see {@link DC.closest}.
|
|
395
|
-
|
|
396
|
-
@template R - Known registry entries of the results; see {@link DC.closest}.
|
|
397
|
-
@returns An array of descendant microcomponent instances (empty if none found).
|
|
398
|
-
*/
|
|
399
450
|
descendants<R extends Record<string, unknown> = Record<never, never>>(): (McController & R)[];
|
|
400
451
|
/** @param key - Optional registry key each descendant must carry. */
|
|
401
452
|
descendants<K extends keyof McRegistry & string>(key: K): (McController & Pick<McRegistry, K>)[];
|