microtags 0.0.9 → 0.0.11
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 +443 -178
- package/dist/builders.d.ts.map +1 -1
- package/dist/{chunk-P4QHFKTJ.js → chunk-FZKCMDSS.js} +4 -4
- package/dist/{chunk-P4QHFKTJ.js.map → chunk-FZKCMDSS.js.map} +2 -2
- package/dist/index.cjs +31 -31
- package/dist/index.cjs.map +2 -2
- package/dist/index.js +31 -31
- package/dist/index.js.map +2 -2
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +3 -3
- package/dist/meta.json +17 -17
- package/dist/setup-context.d.ts.map +1 -1
- package/dist/util.cjs +3 -3
- package/dist/util.cjs.map +2 -2
- package/dist/util.d.ts +2 -2
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +3 -3
- package/dist/util.min.js +1 -1
- package/dist/util.min.js.map +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
[](https://bundlephobia.com/package/microtags)
|
|
10
10
|
[](LICENSE)
|
|
11
11
|
|
|
12
|
-
Web component helper with reactive props, automatic cleanup, and
|
|
13
|
-
[Shadow DOM](https://gomakethings.com/the-shadow-dom-is-an-antipattern/).
|
|
12
|
+
Web component helper with reactive props, automatic cleanup, and
|
|
13
|
+
[no Shadow DOM](https://gomakethings.com/the-shadow-dom-is-an-antipattern/).
|
|
14
14
|
Reactive via [alien-signals](https://github.com/stackblitz/alien-signals).
|
|
15
15
|
|
|
16
|
-
**Size: <!-- size -->3.
|
|
16
|
+
**Size: <!-- size -->3.48 KB<!-- /size -->** with all dependencies, minified and brotlied
|
|
17
17
|
|
|
18
18
|
Inspired by [nanotags](https://nanotags.psdcoder.dev/). Reactive props use
|
|
19
19
|
**alien-signals** instead of nanostores atoms.
|
|
@@ -25,22 +25,31 @@ Inspired by [nanotags](https://nanotags.psdcoder.dev/). Reactive props use
|
|
|
25
25
|
- [Install](#install)
|
|
26
26
|
* [Import map](#import-map)
|
|
27
27
|
* [Pre-bundled](#pre-bundled)
|
|
28
|
-
- [
|
|
28
|
+
- [Get Started](#get-started)
|
|
29
|
+
* [1. Call `.define`](#1-call-define)
|
|
30
|
+
* [2. Call the `.with*` Methods](#2-call-the-with-methods)
|
|
31
|
+
* [3. Call `.setup`](#3-call-setup)
|
|
32
|
+
- [Full Example](#full-example)
|
|
33
|
+
- [Validation](#validation)
|
|
34
|
+
* [Prop validation](#prop-validation)
|
|
35
|
+
+ [`p.schema` vs Standard Schema](#pschema-vs-standard-schema)
|
|
29
36
|
* [Client-side validation](#client-side-validation)
|
|
30
37
|
- [Serverside Example](#serverside-example)
|
|
31
38
|
* [`.TAG`](#tag)
|
|
32
|
-
* [`.refs`](#refs)
|
|
33
39
|
- [API](#api)
|
|
34
|
-
* [Props](#props)
|
|
35
|
-
+ [`withProps`](#withprops)
|
|
36
|
-
* [Refs](#refs)
|
|
37
|
-
+ [`withRefs`](#withrefs)
|
|
38
|
-
+ [Element types](#element-types)
|
|
39
40
|
* [`define`](#define)
|
|
40
|
-
|
|
41
|
-
* [`
|
|
42
|
-
+ [
|
|
43
|
-
|
|
41
|
+
+ [`ComponentBuilder`](#componentbuilder)
|
|
42
|
+
* [`builder.withProps`](#builderwithprops)
|
|
43
|
+
+ [`p.json`](#pjson)
|
|
44
|
+
+ [`p.schema`](#pschema)
|
|
45
|
+
* [`builder.withRefs(fn)`](#builderwithrefsfn)
|
|
46
|
+
* [`builder.withContexts`](#builderwithcontexts)
|
|
47
|
+
+ [`.withContexts` Example](#withcontexts-example)
|
|
48
|
+
* [`builder.setup(ctx)`](#buildersetupctx)
|
|
49
|
+
+ [`ctx`](#ctx)
|
|
50
|
+
- [Missing refs](#missing-refs)
|
|
51
|
+
* [Checking refs in tests](#checking-refs-in-tests)
|
|
52
|
+
- [Typed Events](#typed-events)
|
|
44
53
|
- [Templates](#templates)
|
|
45
54
|
- [Subpath Exports](#subpath-exports)
|
|
46
55
|
* [`microtags/context`](#microtagscontext)
|
|
@@ -49,14 +58,14 @@ Inspired by [nanotags](https://nanotags.psdcoder.dev/). Reactive props use
|
|
|
49
58
|
+ [`render`](#render)
|
|
50
59
|
* [`microtags/util`](#microtagsutil)
|
|
51
60
|
+ [Coercion](#coercion)
|
|
52
|
-
+ [`
|
|
61
|
+
+ [`runAll`](#runall)
|
|
53
62
|
+ [`toAttributes`](#toattributes)
|
|
54
|
-
- [
|
|
63
|
+
- [Differences from `nanotags`](#differences-from-nanotags)
|
|
55
64
|
* [Reactivity primitive](#reactivity-primitive)
|
|
56
65
|
* [Effects + Subscriptions](#effects--subscriptions)
|
|
57
66
|
* [Refs: `r.all` instead of `r.many`](#refs-rall-instead-of-rmany)
|
|
58
67
|
* [Context API](#context-api)
|
|
59
|
-
* [Props](#props
|
|
68
|
+
* [Props](#props)
|
|
60
69
|
* [Not ported](#not-ported)
|
|
61
70
|
* [Additions / New Features](#additions--new-features)
|
|
62
71
|
|
|
@@ -64,9 +73,7 @@ Inspired by [nanotags](https://nanotags.psdcoder.dev/). Reactive props use
|
|
|
64
73
|
|
|
65
74
|
</details>
|
|
66
75
|
|
|
67
|
-
----------------------------------------------------------------------
|
|
68
76
|
## Install
|
|
69
|
-
----------------------------------------------------------------------
|
|
70
77
|
|
|
71
78
|
```sh
|
|
72
79
|
npm i -S microtags
|
|
@@ -97,10 +104,8 @@ Subpaths follow the same URL pattern: `microtags/context`, `microtags/render`.
|
|
|
97
104
|
|
|
98
105
|
### Pre-bundled
|
|
99
106
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
single self-contained file with `alien-signals` inlined, so no other
|
|
103
|
-
mappings are needed:
|
|
107
|
+
This package exposes `microtags/min`, which is a single self-contained file
|
|
108
|
+
with `alien-signals` inlined.
|
|
104
109
|
|
|
105
110
|
```html
|
|
106
111
|
<script type="importmap">
|
|
@@ -112,23 +117,89 @@ mappings are needed:
|
|
|
112
117
|
</script>
|
|
113
118
|
```
|
|
114
119
|
|
|
120
|
+
## Get Started
|
|
115
121
|
|
|
116
|
-
|
|
117
|
-
## Example
|
|
118
|
-
----------------------------------------------------------------------
|
|
122
|
+
### 1. Call `.define`
|
|
119
123
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
DOM inside `.setup`.
|
|
124
|
+
```ts
|
|
125
|
+
import { define } from 'microtags'
|
|
123
126
|
|
|
124
|
-
|
|
125
|
-
|
|
127
|
+
const myCounter = define('my-counter')
|
|
128
|
+
```
|
|
126
129
|
|
|
127
130
|
---
|
|
128
131
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
+
### 2. Call the `.with*` Methods
|
|
133
|
+
|
|
134
|
+
Methods `withProps`, `withRefs`, and `withContexts` are optional and can appear
|
|
135
|
+
in any order. `.setup` ends the chain. It calls `customElements.define` under
|
|
136
|
+
the hood and returns a typed constructor.
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
myCounter
|
|
140
|
+
.withProps(p => ({
|
|
141
|
+
start: p.number(), // the starting count (an attribute)
|
|
142
|
+
}))
|
|
143
|
+
.withRefs(r => ({ // keys in this object are the `data-ref` names in HTML
|
|
144
|
+
display: r.one<HTMLDivElement>(), // typed as HTMLDivElement,
|
|
145
|
+
inc: r.one(), // depends on a child in HTML with `data-ref="inc"`
|
|
146
|
+
dec: r.one(), // depends on a child in HTML with `data-ref="dec"`
|
|
147
|
+
copy: r.one(), // the <copy-btn> element, via `data-ref="copy"`
|
|
148
|
+
}))
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### 3. Call `.setup`
|
|
152
|
+
|
|
153
|
+
Method `.setup` takes a function that gets called with `context` object.
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
myCounter
|
|
157
|
+
.setup(ctx => {
|
|
158
|
+
const count = signal(ctx.props.start())
|
|
159
|
+
// `ctx.refs` are expected to be HTML elements with a matching `data-ref`
|
|
160
|
+
const display = ctx.refs.display
|
|
161
|
+
|
|
162
|
+
// whenever `count` changes
|
|
163
|
+
ctx.effect(() => {
|
|
164
|
+
display.textContent = `Count: ${count()}`
|
|
165
|
+
display.style.color = count() < 0 ? 'red' : 'inherit'
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
// Reflect the live count onto the copy button's `value` attribute.
|
|
169
|
+
// Because `value` is attribute-backed, this flows into <copy-btn>'s
|
|
170
|
+
// own signal, so it always copies the current count.
|
|
171
|
+
ctx.effect(() => {
|
|
172
|
+
const copy = ctx.refs.copy
|
|
173
|
+
copy.setAttribute('value', String(count()))
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Listen for clicks.
|
|
178
|
+
*/
|
|
179
|
+
ctx.on(ctx.refs.inc, 'click', () => {
|
|
180
|
+
count(count() + 1)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
ctx.on(ctx.refs.dec, 'click', () => {
|
|
184
|
+
count(count() - 1)
|
|
185
|
+
})
|
|
186
|
+
})
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Full Example
|
|
190
|
+
|
|
191
|
+
This example expects the full HTML to already be in the DOM.
|
|
192
|
+
Defining the components is about "hydrating" the page, or adding behavior.
|
|
193
|
+
|
|
194
|
+
If you want to client-side render the components, that is possible too.
|
|
195
|
+
You can update the DOM inside `.setup`.
|
|
196
|
+
|
|
197
|
+
>
|
|
198
|
+
> [!NOTE]
|
|
199
|
+
> The server should know how to create the markup that components depend
|
|
200
|
+
> on here. See [the serverside example](#serverside-example).
|
|
201
|
+
>
|
|
202
|
+
|
|
132
203
|
|
|
133
204
|
```ts
|
|
134
205
|
import { define } from 'microtags'
|
|
@@ -136,7 +207,7 @@ import { signal } from 'alien-signals'
|
|
|
136
207
|
|
|
137
208
|
/**
|
|
138
209
|
* - attribute-backed prop (`start`)
|
|
139
|
-
* - data-ref elements
|
|
210
|
+
* - data-ref elements -- each needs a data-ref attribute, eg data-ref="inc"
|
|
140
211
|
* - ctx.effect, ctx.on
|
|
141
212
|
*/
|
|
142
213
|
export const myCounter = define('my-counter')
|
|
@@ -185,25 +256,102 @@ export const myCounter = define('my-counter')
|
|
|
185
256
|
</my-counter>
|
|
186
257
|
```
|
|
187
258
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
See [example/subscribe-form.ts](./example/subscribe-form.ts).
|
|
259
|
+
## Validation
|
|
191
260
|
|
|
192
|
-
There are two separate things people call "validation"
|
|
193
|
-
handles them in different places:
|
|
261
|
+
There are two separate things people call "validation":
|
|
194
262
|
|
|
195
|
-
1. **Prop validation** checks the attributes passed
|
|
263
|
+
1. **Prop validation** checks the attributes passed into a component.
|
|
196
264
|
Pass a [Standard Schema](https://standardschema.dev) (Zod, Valibot,
|
|
197
265
|
ArkType) to [`p.schema`](#withprops); it coerces the attribute value
|
|
198
|
-
and falls back to `undefined` when the value doesn't
|
|
199
|
-
2. **Form input validation** checks live user input
|
|
200
|
-
value that changes on every keystroke
|
|
266
|
+
and falls back to `undefined` when the value doesn't coerce.
|
|
267
|
+
2. **Form input validation** -- client-side validation -- checks live user input
|
|
268
|
+
in a form, for example a value that changes on every keystroke.
|
|
201
269
|
There is no dedicated API for this. Validate the schema directly
|
|
202
|
-
inside `.setup()` and hold the result
|
|
270
|
+
inside `.setup()` and hold the result as a signal.
|
|
271
|
+
|
|
272
|
+
### Prop validation
|
|
203
273
|
|
|
204
|
-
|
|
274
|
+
Pass a [Standard Schema](https://standardschema.dev) validator to `p.schema`.
|
|
275
|
+
The validator runs against the raw attribute and its output becomes the
|
|
276
|
+
signal's value; anything it rejects falls back to `undefined`.
|
|
205
277
|
|
|
206
278
|
```ts
|
|
279
|
+
import { z } from 'zod'
|
|
280
|
+
import { define } from 'microtags'
|
|
281
|
+
|
|
282
|
+
// Any Standard Schema validator works (Zod, Valibot, ArkType). microtags
|
|
283
|
+
// only sees the shared `~standard` interface, so it has no validation
|
|
284
|
+
// library of its own and no dependency on the one you pick.
|
|
285
|
+
const Variant = z.enum(['info', 'success', 'warning', 'danger'])
|
|
286
|
+
|
|
287
|
+
define('status-badge')
|
|
288
|
+
.withProps(p => ({
|
|
289
|
+
// Validates the raw `variant` attribute. A value outside the
|
|
290
|
+
// four options falls back to `undefined`.
|
|
291
|
+
variant: p.schema(Variant),
|
|
292
|
+
}))
|
|
293
|
+
.setup(ctx => {
|
|
294
|
+
ctx.effect(() => {
|
|
295
|
+
const variant = ctx.props.variant() ?? 'info'
|
|
296
|
+
ctx.host.className = `badge badge-${variant}`
|
|
297
|
+
})
|
|
298
|
+
})
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
```html
|
|
302
|
+
<status-badge variant="success">Saved</status-badge>
|
|
303
|
+
|
|
304
|
+
<!-- "purple" is not in the enum, so ctx.props.variant() is undefined -->
|
|
305
|
+
<status-badge variant="purple">Unknown</status-badge>
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
#### `p.schema` vs Standard Schema
|
|
309
|
+
|
|
310
|
+
[Standard Schema](https://standardschema.dev) is a small shared interface
|
|
311
|
+
that Zod, Valibot and ArkType all implement. `p.schema` validates through that
|
|
312
|
+
interface, so microtags ships no validator of its own.
|
|
313
|
+
|
|
314
|
+
A schema prop is attribute-backed and observed, exactly like `p.string()` or
|
|
315
|
+
`p.number()`: editing the attribute re-runs the validator and pushes the result
|
|
316
|
+
to `ctx.props`.
|
|
317
|
+
|
|
318
|
+
The validator's input is the raw attribute string, or `null` when the attribute
|
|
319
|
+
is absent. Because the input is always a string, schemas for non-string values
|
|
320
|
+
must coerce. Use `z.coerce.number()`, not `z.number()`:
|
|
321
|
+
|
|
322
|
+
```ts
|
|
323
|
+
.withProps(p => ({
|
|
324
|
+
// "5" -> 5; "0" and the absent attribute both fall back to undefined
|
|
325
|
+
quantity: p.schema(z.coerce.number().int().min(1)),
|
|
326
|
+
}))
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
**On success the signal holds the validator's output value**; on failure it
|
|
330
|
+
falls back to `undefined`. The inferred signal type is the schema's output,
|
|
331
|
+
so guard for the `undefined` case, or fold a default into the schema so it
|
|
332
|
+
never fails:
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
```ts
|
|
336
|
+
{
|
|
337
|
+
// always 'info' | 'success' | 'warning' | 'danger', never undefined
|
|
338
|
+
variant: p.schema(Variant.catch('info')),
|
|
339
|
+
}
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
Only synchronous schemas are supported; an async schema throws when the
|
|
343
|
+
attribute is validated.
|
|
344
|
+
|
|
345
|
+
-------------------------------------------------------
|
|
346
|
+
|
|
347
|
+
### Client-side validation
|
|
348
|
+
|
|
349
|
+
There is no special API here. Validate input inside `.setup`,
|
|
350
|
+
|
|
351
|
+
See [example/subscribe-form.ts](./example/subscribe-form.ts).
|
|
352
|
+
|
|
353
|
+
```ts
|
|
354
|
+
// form input (client-side) validation
|
|
207
355
|
import { signal, startBatch, endBatch } from 'alien-signals'
|
|
208
356
|
import { z } from 'zod'
|
|
209
357
|
import { define } from 'microtags'
|
|
@@ -220,18 +368,21 @@ define('subscribe-form')
|
|
|
220
368
|
}))
|
|
221
369
|
.setup(ctx => {
|
|
222
370
|
const value = signal('')
|
|
223
|
-
const touched = signal(false)
|
|
371
|
+
const touched = signal<boolean>(false)
|
|
224
372
|
|
|
225
373
|
// capture the live value (two-way; clears the field on reset)
|
|
226
374
|
ctx.bind(value, ctx.refs.input)
|
|
375
|
+
|
|
227
376
|
// only reveal errors after the first blur
|
|
228
377
|
ctx.on(ctx.refs.input, 'blur', () => touched(true))
|
|
229
378
|
|
|
379
|
+
// ctx.effect is alien-signals/effect with automatic/correct disposal
|
|
230
380
|
ctx.effect(() => {
|
|
381
|
+
// subscribe to `value` and `touched`
|
|
231
382
|
const result = Email.safeParse(value())
|
|
232
|
-
const message = result.success ?
|
|
383
|
+
const message = (result.success ?
|
|
233
384
|
'' :
|
|
234
|
-
result.error.issues[0].message
|
|
385
|
+
result.error.issues[0].message)
|
|
235
386
|
const showError = touched() && !result.success
|
|
236
387
|
|
|
237
388
|
ctx.refs.submit.disabled = !result.success
|
|
@@ -240,6 +391,7 @@ define('subscribe-form')
|
|
|
240
391
|
})
|
|
241
392
|
|
|
242
393
|
ctx.on(ctx.refs.form, 'submit', ev => {
|
|
394
|
+
// subscribe to DOM events
|
|
243
395
|
ev.preventDefault()
|
|
244
396
|
if (!Email.safeParse(value()).success) return
|
|
245
397
|
ctx.refs.status.textContent = 'Thanks for subscribing!'
|
|
@@ -251,14 +403,16 @@ define('subscribe-form')
|
|
|
251
403
|
})
|
|
252
404
|
```
|
|
253
405
|
|
|
254
|
-
The error stays hidden until the field is blurred once, then updates live
|
|
255
|
-
the submit button is
|
|
256
|
-
|
|
406
|
+
The error stays hidden until the field is blurred once, then updates live,
|
|
407
|
+
and the submit button is disabled until valid. See `example/subscribe-form.ts`.
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
---------------------------------------------------------------
|
|
257
411
|
|
|
258
412
|
|
|
259
|
-
----------------------------------------------------------------------
|
|
260
413
|
## Serverside Example
|
|
261
|
-
|
|
414
|
+
|
|
415
|
+
See [./example/ssr.ts](./example/ssr.ts).
|
|
262
416
|
|
|
263
417
|
### `.TAG`
|
|
264
418
|
|
|
@@ -266,11 +420,11 @@ Each tag name that you pass to `define` is exposed as a property `.TAG`
|
|
|
266
420
|
on the return value. It is possible to import your web components in Node,
|
|
267
421
|
even though Node does not have browser APIs.
|
|
268
422
|
|
|
269
|
-
|
|
423
|
+
The `data-ref` attribute is a fallback. It's used when you don't pass a
|
|
424
|
+
selector to `r.one()`. If you pass explicit CSS selectors to `withRefs`,
|
|
425
|
+
e.g. `{ copy: r.one('button') }`, then the HTML does not require a `data-ref`
|
|
426
|
+
attribute.
|
|
270
427
|
|
|
271
|
-
All refs are defined per-component, and the ref names are exposed at `.refs`,
|
|
272
|
-
so you can import your web components on the server and read the ref names
|
|
273
|
-
as strings.
|
|
274
428
|
|
|
275
429
|
```ts
|
|
276
430
|
import { MyCounter, CountBtn, CopyBtn } from './index.js'
|
|
@@ -312,11 +466,39 @@ export function render ():string {
|
|
|
312
466
|
}
|
|
313
467
|
```
|
|
314
468
|
|
|
315
|
-
|
|
469
|
+
|
|
470
|
+
---------------------------------------------------------------------
|
|
471
|
+
|
|
472
|
+
|
|
316
473
|
## API
|
|
317
|
-
----------------------------------------------------------------------
|
|
318
474
|
|
|
319
|
-
###
|
|
475
|
+
### `define`
|
|
476
|
+
|
|
477
|
+
```js
|
|
478
|
+
function define (tagName:string):ComponentBuilder
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
Function `define(tagName)` returns a [`ComponentBuilder`](#componentbuilder).
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
#### `ComponentBuilder`
|
|
485
|
+
|
|
486
|
+
The object returned by [`define`](#define). Includes methods:
|
|
487
|
+
|
|
488
|
+
| Method | Returns | Description |
|
|
489
|
+
|--------|---------|-------------|
|
|
490
|
+
| `.TAG` | `string` | The tag name passed to `define`. See [`.TAG`](#tag). |
|
|
491
|
+
| `.withProps(fn)` | `ComponentBuilder` | Declare reactive attributes. See [Props](#props). |
|
|
492
|
+
| `.withRefs(fn)` | `ComponentBuilder` | Declare DOM refs resolved once the element connects. See [Refs](#refs-1). |
|
|
493
|
+
| `.withContexts(fn)` | `ComponentBuilder` | Consume context values published by an ancestor. See [`withContexts`](#withcontexts). |
|
|
494
|
+
| `.setup(fn)` | `MicrotagElementClass` | Registers the custom element and returns its class. See [`setup`](#setup) below. |
|
|
495
|
+
|
|
496
|
+
Methods `withProps`, `withRefs`, and `withContexts` each return a new builder,
|
|
497
|
+
so they chain in any order; `setup` ends the chain by registering the element.
|
|
498
|
+
|
|
499
|
+
---------------------------------------------------------
|
|
500
|
+
|
|
501
|
+
### `builder.withProps`
|
|
320
502
|
|
|
321
503
|
Declare reactive attributes via `withProps`. Each prop becomes:
|
|
322
504
|
|
|
@@ -332,8 +514,6 @@ Declare reactive attributes via `withProps`. Each prop becomes:
|
|
|
332
514
|
| `p.boolean()` | `"true"` / `""` → `true`, `"false"` → `false` | `false` |
|
|
333
515
|
| `p.oneOf(opts)` | Picklist enum, throws on invalid | throws |
|
|
334
516
|
|
|
335
|
-
#### `withProps`
|
|
336
|
-
|
|
337
517
|
```ts
|
|
338
518
|
.withProps(p => ({
|
|
339
519
|
count: p.number(), // attribute -> number (NaN when absent)
|
|
@@ -345,7 +525,15 @@ Declare reactive attributes via `withProps`. Each prop becomes:
|
|
|
345
525
|
}))
|
|
346
526
|
```
|
|
347
527
|
|
|
348
|
-
|
|
528
|
+
Inside `.setup()`, every prop is a callable signal:
|
|
529
|
+
|
|
530
|
+
```ts
|
|
531
|
+
ctx.props.count() // read
|
|
532
|
+
ctx.props.count(42) // write
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
#### `p.json`
|
|
349
537
|
|
|
350
538
|
`p.json<T>()` -- a prop whose attribute value is parsed with
|
|
351
539
|
`JSON.parse`. The type parameter `T` annotates the parsed result; it is a
|
|
@@ -368,6 +556,7 @@ observed, so editing the attribute re-parses and pushes the new value to
|
|
|
368
556
|
attribute is absent, and when the value is not valid JSON. Parse errors are
|
|
369
557
|
swallowed rather than thrown.
|
|
370
558
|
|
|
559
|
+
|
|
371
560
|
```ts
|
|
372
561
|
// check for `undefined` case
|
|
373
562
|
ctx.effect(() => {
|
|
@@ -377,32 +566,46 @@ ctx.effect(() => {
|
|
|
377
566
|
})
|
|
378
567
|
```
|
|
379
568
|
|
|
569
|
+
#### `p.schema`
|
|
570
|
+
|
|
380
571
|
For input you want validated rather than only parsed, pass a Standard Schema
|
|
381
572
|
to [`p.schema`](#withprops) instead.
|
|
382
573
|
|
|
383
|
-
|
|
574
|
+
|
|
575
|
+
---------------------------------------------------------
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
### `builder.withRefs(fn)`
|
|
579
|
+
|
|
580
|
+
Takes a function that gets called with a helpful query selector, `r`.
|
|
581
|
+
|
|
582
|
+
Call `r.one()` / `r.all()` with no argument to match `[data-ref="<key>"]`,
|
|
583
|
+
where `key` is the key name you passed in. Or pass a CSS selector to query by
|
|
584
|
+
it instead. Queries are resolved relative to the host element, during
|
|
585
|
+
[the `connectedCallback`](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements#custom_element_lifecycle_callbacks).
|
|
586
|
+
|
|
587
|
+
All refs returned from this callback are typed as much as possible, and exposed
|
|
588
|
+
on the `context` object passed to the `.setup` function.
|
|
589
|
+
|
|
384
590
|
|
|
385
591
|
```ts
|
|
386
|
-
|
|
387
|
-
|
|
592
|
+
class ComponentBuilder {
|
|
593
|
+
withRefs<R extends Record<string, RefDef>> (
|
|
594
|
+
fn:(r:RefsDSL) => R
|
|
595
|
+
):ComponentBuilder<Props, R, CtxDefs>
|
|
596
|
+
}
|
|
388
597
|
```
|
|
389
598
|
|
|
390
|
-
---
|
|
391
599
|
|
|
392
|
-
|
|
600
|
+
Takes a function that gets called with a [`RefsDSL`](./src/builders.ts#L54)
|
|
601
|
+
object. It should return an object of child elements that you want to keep
|
|
602
|
+
references to.
|
|
393
603
|
|
|
394
604
|
The `data-ref` attribute is a fallback. It's used when you don't pass a
|
|
395
605
|
selector to `r.one()`. If you pass explicit CSS selectors to `withRefs`,
|
|
396
606
|
e.g. `{ copy: r.one('button') }`, then the HTML does not require a `data-ref`
|
|
397
607
|
attribute.
|
|
398
608
|
|
|
399
|
-
#### `withRefs`
|
|
400
|
-
|
|
401
|
-
Call `r.one()` / `r.all()` with no argument to match `[data-ref="<key>"]`,
|
|
402
|
-
where `key` is the key name you pass in, or pass a CSS selector to query by
|
|
403
|
-
it instead. Queries are resolved relative to the host element, during
|
|
404
|
-
[the `connectedCallback`](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements#custom_element_lifecycle_callbacks).
|
|
405
|
-
|
|
406
609
|
|
|
407
610
|
```ts
|
|
408
611
|
.withRefs(r => ({
|
|
@@ -420,67 +623,41 @@ If there are no `data-ref` attribtues in the HTML, use CSS query selectors.
|
|
|
420
623
|
}))
|
|
421
624
|
```
|
|
422
625
|
|
|
423
|
-
See [#refs in serverside-rendering](#refs). Each component created with
|
|
424
|
-
`micortags` exposes a `.refs` property that is an object map
|
|
425
|
-
of ref names, eg `MyComponent.refs.copyButton`.
|
|
426
626
|
|
|
427
|
-
|
|
627
|
+
---------------------------------------------------------------------
|
|
428
628
|
|
|
429
|
-
An un-annotated `r.one()` is typed as `HTMLElement`, so `.style`,
|
|
430
|
-
`.textContent`, and `.dataset` work without a cast. There are two ways to
|
|
431
|
-
get a more specific element type:
|
|
432
629
|
|
|
433
|
-
|
|
434
|
-
.withRefs(r => ({
|
|
435
|
-
// Generic argument: sets the type, keeps the [data-ref] selector.
|
|
436
|
-
display: r.one<HTMLDivElement>(),
|
|
437
|
-
|
|
438
|
-
// Tag string: infers the type AND queries by that tag, exactly like
|
|
439
|
-
// document.querySelector('button'). It does NOT keep [data-ref].
|
|
440
|
-
save: r.one('button'), // SingleRefDef<HTMLButtonElement>
|
|
441
|
-
items: r.all('li'), // MultipleRefDef<HTMLLIElement>
|
|
442
|
-
button: r.one(), // HTMLElement (un-annotated default)
|
|
443
|
-
}))
|
|
444
|
-
```
|
|
630
|
+
### `builder.withContexts`
|
|
445
631
|
|
|
446
|
-
|
|
632
|
+
Context is for the case where a parent component defines some state, e.g. a
|
|
633
|
+
`theme`, and the top-level (application) knows nothing about the theme, but the
|
|
634
|
+
application *does* determine what children the parent renders
|
|
635
|
+
(the app passes in children), and the children need to know the theme.
|
|
447
636
|
|
|
448
637
|
```ts
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
638
|
+
class ComponentBuilder {
|
|
639
|
+
withContexts<C extends Record<string, ContextToken<unknown>>> (
|
|
640
|
+
fn:(c:ContextDSL) => C
|
|
641
|
+
):ComponentBuilder<Props, RefDefs, C>
|
|
642
|
+
}
|
|
453
643
|
```
|
|
454
644
|
|
|
455
|
-
|
|
456
|
-
constrained to `HTMLElement`, so cast at the use site if you need one
|
|
457
|
-
(e.g. `ctx.refs.icon as unknown as SVGSVGElement`).
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
### `define`
|
|
645
|
+
#### `.withContexts` Example
|
|
461
646
|
|
|
462
|
-
```
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
647
|
+
```html
|
|
648
|
+
<!-- The application composes this markup. -->
|
|
649
|
+
<theme-provider>
|
|
650
|
+
<some-child></some-child>
|
|
651
|
+
<another-child></another-child>
|
|
652
|
+
</theme-provider>
|
|
466
653
|
```
|
|
467
654
|
|
|
468
|
-
Returns a fluent builder. Call `.setup()` to register the custom element.
|
|
469
|
-
All builder methods are chainable and fully type-inferred.
|
|
470
|
-
|
|
471
|
-
### `withContexts`
|
|
472
|
-
|
|
473
|
-
Context is for the case where a parent component defines some state, e.g. a
|
|
474
|
-
`theme`, and the top-level (application) knows nothing about the theme. But the
|
|
475
|
-
top level application does determine what children the parent renders
|
|
476
|
-
(it passes in children), and the children need to know the theme.
|
|
477
|
-
|
|
478
655
|
In that case, there is no way for the "parent" component to pass the theme to
|
|
479
|
-
the children
|
|
656
|
+
the children. The root level (application) doesn't know what the theme is.
|
|
480
657
|
|
|
481
|
-
Context gives us a
|
|
482
|
-
publishes the value, and any descendants opt in
|
|
483
|
-
`withContexts
|
|
658
|
+
Context gives us a way to model this. The context-provider
|
|
659
|
+
publishes the value, and any descendants opt in with
|
|
660
|
+
`withContexts`.
|
|
484
661
|
|
|
485
662
|
>
|
|
486
663
|
> [!IMPORTANT]
|
|
@@ -488,7 +665,6 @@ publishes the value, and any descendants opt in (or pull values) with
|
|
|
488
665
|
> Context gives you nothing for first paint or the no-JS case.
|
|
489
666
|
>
|
|
490
667
|
|
|
491
|
-
|
|
492
668
|
```ts
|
|
493
669
|
import { define } from 'microtags'
|
|
494
670
|
import { createContext, provide } from 'microtags/context'
|
|
@@ -499,7 +675,8 @@ const ThemeToken = createContext<string>()
|
|
|
499
675
|
// not reference the children below. The application composes those in.
|
|
500
676
|
define('theme-provider')
|
|
501
677
|
.setup(ctx => {
|
|
502
|
-
|
|
678
|
+
const rmContext = provide(ctx.host, ThemeToken, 'dark')
|
|
679
|
+
ctx.onCleanup(rmContext)
|
|
503
680
|
})
|
|
504
681
|
|
|
505
682
|
// Child: opts in to the theme and reads whichever provider it is
|
|
@@ -516,82 +693,166 @@ define('themed-card')
|
|
|
516
693
|
```
|
|
517
694
|
|
|
518
695
|
```html
|
|
519
|
-
<!-- The application writes this markup. It nests <themed-card> inside
|
|
520
|
-
<theme-provider> but never sets the theme itself. -->
|
|
521
696
|
<theme-provider>
|
|
522
697
|
<themed-card>Card content</themed-card>
|
|
523
698
|
</theme-provider>
|
|
524
699
|
```
|
|
525
700
|
|
|
526
|
-
`setup()` is deferred until all required contexts resolve. See
|
|
527
|
-
[`microtags/context`](#microtagscontext) for the provider API.
|
|
528
701
|
|
|
529
|
-
|
|
702
|
+
---------------------------------------------------------
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
### `builder.setup(ctx)`
|
|
706
|
+
|
|
707
|
+
Registers the custom element via `customElements.define` and returns the
|
|
708
|
+
element class. The returned class exposes `.TAG` and `.refs` as static
|
|
709
|
+
members, for use in [serverside rendering](#serverside-example).
|
|
530
710
|
|
|
531
711
|
```ts
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
712
|
+
class ComponentBuilder {
|
|
713
|
+
setup (
|
|
714
|
+
fn:(ctx:SetupContext<
|
|
715
|
+
Props,
|
|
716
|
+
RefsMap<RefDefs>,
|
|
717
|
+
ContextsMap<CtxDefs>
|
|
718
|
+
>) => void
|
|
719
|
+
):MicrotagElementClass<RefDefs> {
|
|
720
|
+
}
|
|
721
|
+
```
|
|
542
722
|
|
|
543
|
-
|
|
544
|
-
|
|
723
|
+
The callback function runs once per element instance, and is called with the
|
|
724
|
+
following `context` object.
|
|
545
725
|
|
|
546
|
-
// Add an event listener; auto-removed on disconnect.
|
|
547
|
-
ctx.on(el, 'click', handler)
|
|
548
726
|
|
|
549
|
-
|
|
550
|
-
// detail/options) for a bubbling CustomEvent, or a pre-built
|
|
551
|
-
// Event. Returns dispatchEvent's result.
|
|
552
|
-
ctx.emit('change', { value: 42 })
|
|
727
|
+
#### `ctx`
|
|
553
728
|
|
|
554
|
-
|
|
555
|
-
const snap = ctx.peek(ctx.props.count)
|
|
729
|
+
The argument passed to the callback given to `setup`.
|
|
556
730
|
|
|
557
|
-
|
|
558
|
-
|
|
731
|
+
```ts
|
|
732
|
+
ctx:SetupContext<Props, RefsMap<RefDefs>, ContextsMap<CtxDefs>>
|
|
733
|
+
```
|
|
559
734
|
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
735
|
+
##### `ctx` Methods
|
|
736
|
+
|
|
737
|
+
| Method | Returns | Description |
|
|
738
|
+
|--------|---------|-------------|
|
|
739
|
+
| `.peek(signal)` | `T` | Read a signal's current value without subscribing, even inside `ctx.effect`. |
|
|
740
|
+
| `.on(target, type, handler, options?)` | `void` | Add a DOM event listener, auto-removed on disconnect. |
|
|
741
|
+
| `.emit(event)` / `.emit(name, detail?, options?)` | `boolean` | Dispatch a pre-built `Event` as-is, or construct and dispatch a bubbling `CustomEvent` from a name. Returns `dispatchEvent`'s result: `false` if a cancelable event had `preventDefault()` called. |
|
|
742
|
+
| `.effect(fn)` | `void` | Run an alien-signals effect that auto-tracks the signals it reads. Auto-disposed on disconnect. |
|
|
743
|
+
| `.bind(signal, element, options?)` | `void` | Bind a signal to a DOM element property, auto-cleaned on disconnect. |
|
|
744
|
+
| `.onCleanup(fn)` | `void` | Register custom teardown to run on disconnect. |
|
|
745
|
+
| `.consume(token)` | `T \| undefined` | Read an optional context value published by an ancestor. See [`withContexts`](#withcontexts). |
|
|
746
|
+
|
|
747
|
+
See [Typed Events](#typed-events) for more about `.on` and `.emit`.
|
|
748
|
+
|
|
749
|
+
##### `ctx.bind`
|
|
750
|
+
|
|
751
|
+
The `.bind` method treats the signal as the source of truth. The element
|
|
752
|
+
property is set from the signal.
|
|
753
|
+
With no `options`, the control's element property and write-back event are
|
|
754
|
+
auto-detected from its type:
|
|
755
|
+
|
|
756
|
+
```ts
|
|
757
|
+
{
|
|
758
|
+
bind<T> (
|
|
759
|
+
source:Signal<T>,
|
|
760
|
+
control:Element,
|
|
761
|
+
opts?:BindOptions
|
|
762
|
+
)
|
|
763
|
+
}
|
|
563
764
|
```
|
|
564
765
|
|
|
565
|
-
|
|
766
|
+
| Control | `prop` | write-back `event` |
|
|
767
|
+
|---------|--------|---------------------|
|
|
768
|
+
| `<input type="checkbox">` | `checked` | `change` |
|
|
769
|
+
| `<input type="number">` / `type="range"` | `valueAsNumber` | `input` |
|
|
770
|
+
| Other `<input>` types / `<textarea>` | `value` | `input` |
|
|
771
|
+
| Anything else, e.g. `<select>` | `value` | `change` |
|
|
566
772
|
|
|
567
|
-
|
|
568
|
-
|
|
773
|
+
Passing `options` overrides the auto-detected `prop`, and replaces the
|
|
774
|
+
write-back `event` -- omit `options.event` for a one-way binding. A read-only
|
|
775
|
+
getter (`() => T`) is also accepted as the `signal` argument, for binding a
|
|
776
|
+
derived value one-way (pass it with `options` and omit `event`).
|
|
569
777
|
|
|
570
778
|
```ts
|
|
571
|
-
|
|
779
|
+
// two-way, auto-detected (checkbox -> `checked` / `change`)
|
|
780
|
+
ctx.bind(checked, ctx.refs.toggle)
|
|
572
781
|
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
782
|
+
// one-way: push a derived value into a read-only display
|
|
783
|
+
ctx.bind(() => `${count()} items`, ctx.refs.label, { prop: 'textContent' })
|
|
784
|
+
```
|
|
785
|
+
|
|
786
|
+
---
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
## Missing refs
|
|
790
|
+
|
|
791
|
+
Any calls to `r.one` in `.withRefs` will throw if there is not a matching
|
|
792
|
+
child element.
|
|
793
|
+
|
|
794
|
+
```
|
|
795
|
+
Error: MY-COMPONENT: missing refs: button, save
|
|
796
|
+
```
|
|
797
|
+
|
|
798
|
+
The tag name is upper case (it comes from `host.tagName`), and the
|
|
799
|
+
message lists every required ref that was not found.
|
|
800
|
+
|
|
801
|
+
Call to `r.all()` are **never reported**. A missing match resolves to an
|
|
802
|
+
empty array, not an error.
|
|
803
|
+
|
|
804
|
+
This check happens right before the custom element runs your `.setup` function.
|
|
805
|
+
|
|
806
|
+
For a component with no contexts, refs are resolved at the moment the element
|
|
807
|
+
connects to the DOM. For a component that declares
|
|
808
|
+
[`withContexts`](#withcontexts), ref collection is deferred along
|
|
809
|
+
with `setup` until every required context resolves.
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
### Checking refs in tests
|
|
813
|
+
|
|
814
|
+
A missing `r.one` ref throws while the component connects. When the browser
|
|
815
|
+
runs a lifecycle callback (which is what `appendChild` triggers), it reports
|
|
816
|
+
the exception to the global `error` event rather than propagating it to the
|
|
817
|
+
caller, so a try / catch around `appendChild` sees nothing.
|
|
818
|
+
|
|
819
|
+
You need to listen for the global `error` event. When every
|
|
820
|
+
`data-ref` the component declares is present, nothing is reported.
|
|
821
|
+
|
|
822
|
+
```ts
|
|
823
|
+
import { test } from '@substrate-system/tapzero'
|
|
824
|
+
import './my-counter.js' // the component from the example above
|
|
825
|
+
|
|
826
|
+
test('all refs resolve', t => {
|
|
827
|
+
let err:Error|null = null
|
|
828
|
+
const onError = (ev:ErrorEvent) => { err = ev.error }
|
|
829
|
+
window.addEventListener('error', onError)
|
|
830
|
+
|
|
831
|
+
const el = document.createElement('my-counter')
|
|
832
|
+
el.innerHTML = `
|
|
833
|
+
<div data-ref="display"></div>
|
|
834
|
+
<button data-ref="dec">-</button>
|
|
835
|
+
<button data-ref="inc">+</button>`
|
|
836
|
+
document.body.appendChild(el)
|
|
837
|
+
|
|
838
|
+
t.ok(err === null, 'no missing refs')
|
|
839
|
+
|
|
840
|
+
window.removeEventListener('error', onError)
|
|
841
|
+
document.body.removeChild(el)
|
|
578
842
|
})
|
|
579
843
|
```
|
|
580
844
|
|
|
581
|
-
|
|
845
|
+
|
|
846
|
+
## Typed Events
|
|
582
847
|
|
|
583
848
|
`TypedEvent<Target, Detail>` is a type-only helper that narrows
|
|
584
849
|
`CustomEvent` to a specific `target` and `detail`. Combine it with
|
|
585
|
-
`HTMLElementEventMap` augmentation for app-wide type-safe events
|
|
586
|
-
flow through `ctx.emit` and `ctx.on`:
|
|
850
|
+
`HTMLElementEventMap` augmentation for app-wide type-safe events.
|
|
587
851
|
|
|
588
852
|
```ts
|
|
589
853
|
import type { TypedEvent } from 'microtags'
|
|
590
854
|
|
|
591
|
-
type TabsChangedEvent = TypedEvent<
|
|
592
|
-
InstanceType<typeof XTabs>,
|
|
593
|
-
{ index:number }
|
|
594
|
-
>
|
|
855
|
+
type TabsChangedEvent = TypedEvent<InstanceType<typeof XTabs>, { index:number }>
|
|
595
856
|
|
|
596
857
|
declare global {
|
|
597
858
|
interface HTMLElementEventMap {
|
|
@@ -681,6 +942,7 @@ callback function mutates the DOM through properties like `textContent`.
|
|
|
681
942
|
Nothing concatenates strings into markup, so there is no string-injection
|
|
682
943
|
XSS vector.
|
|
683
944
|
|
|
945
|
+
|
|
684
946
|
## Subpath Exports
|
|
685
947
|
|
|
686
948
|
### `microtags/context`
|
|
@@ -751,6 +1013,10 @@ signal imperatively.
|
|
|
751
1013
|
|
|
752
1014
|
### `microtags/render`
|
|
753
1015
|
|
|
1016
|
+
```ts
|
|
1017
|
+
import { render, renderList } from 'microtags/render'
|
|
1018
|
+
```
|
|
1019
|
+
|
|
754
1020
|
A keyed reconciler for rendering lists and single items from a
|
|
755
1021
|
`<template>`, with no virtual DOM and no diffing library.
|
|
756
1022
|
|
|
@@ -878,7 +1144,8 @@ import {
|
|
|
878
1144
|
coerceString,
|
|
879
1145
|
coerceBoolean,
|
|
880
1146
|
coerceJson,
|
|
881
|
-
|
|
1147
|
+
runAll,
|
|
1148
|
+
toAttributes
|
|
882
1149
|
} from 'microtags/util'
|
|
883
1150
|
```
|
|
884
1151
|
|
|
@@ -904,14 +1171,14 @@ function coerceJson<T = unknown> (raw:string | null):T | undefined
|
|
|
904
1171
|
- `coerceJson` -- `JSON.parse(raw)`, or `undefined` when the attribute is
|
|
905
1172
|
absent or not valid JSON. Pass a type parameter to annotate the result.
|
|
906
1173
|
|
|
907
|
-
#### `
|
|
1174
|
+
#### `runAll`
|
|
908
1175
|
|
|
909
1176
|
Runs every function in the array, even if some throw. After all have run
|
|
910
1177
|
it re-throws the first error encountered, so one failing teardown cannot
|
|
911
1178
|
skip the rest. A `Cleanup` is a no-argument teardown function, `() => void`.
|
|
912
1179
|
|
|
913
1180
|
```ts
|
|
914
|
-
function
|
|
1181
|
+
function runAll (fns:Cleanup[]):void
|
|
915
1182
|
```
|
|
916
1183
|
|
|
917
1184
|
#### `toAttributes`
|
|
@@ -939,7 +1206,7 @@ function toAttributes (attrs:Attrs):string
|
|
|
939
1206
|
|
|
940
1207
|
---
|
|
941
1208
|
|
|
942
|
-
##
|
|
1209
|
+
## Differences from `nanotags`
|
|
943
1210
|
|
|
944
1211
|
### Reactivity primitive
|
|
945
1212
|
|
|
@@ -1017,8 +1284,6 @@ These nanotags APIs have no microtags equivalent:
|
|
|
1017
1284
|
|
|
1018
1285
|
- `ctx.getElement` / `ctx.getElements` -- declare refs with `withRefs`, or
|
|
1019
1286
|
call `ctx.host.querySelector` directly.
|
|
1020
|
-
- The `setup` mixin return value -- `setup` returns `void`; it does not
|
|
1021
|
-
assign members back onto the element.
|
|
1022
1287
|
- The `define(name, setupFn)` two-argument shorthand -- always use the
|
|
1023
1288
|
builder chain ending in `.setup()`.
|
|
1024
1289
|
|