Skip to content

Commit 5f9fdfa

Browse files
committed
minor fixes; dom_reg registers immediately by default
1 parent f901e1f commit 5f9fdfa

29 files changed

+226
-126
lines changed

doc/dom_reg/_Reg.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
11
Registry for custom DOM element classes. Automatically derives tag name from class name, using salting when necessary to avoid collisions. Supports idempotent registration which can be safely called in an element constructor. Allows immediate registration, deferred registration, or a mix of those.
22

3-
By default, this registry has **no global side effects**. To enable global registration, provide a "definer" to the registry.
3+
By default, the main registry uses `globalThis.customElements`, which exists only in browser environments. In non-browser environments, by default it has no global side effects, but does still modify registered classes by deriving their `.customName`, for rendering to HTML.
4+
5+
For browser-only code, prefer the mixin `MixReg` from the same module which is easier to use. See examples in the {{featLink dom_reg readme}}.
6+
7+
Simple usage:
48

59
```js
610
import * as dr from '{{featUrl dom_reg}}'
711

812
class Btn extends HTMLButtonElement {
9-
// Optional. If omitted, `dr.reg` autogenerates
10-
// this from the name of the class.
11-
static customName = `some-btn`
13+
/*
14+
Optional. If omitted, `dr.reg` autogenerates
15+
this from the name of the class.
16+
17+
static customName = `some-btn`
18+
*/
19+
20+
// Automatically derives the name `a-btn` and registers the class.
21+
static {dr.reg(this)}
22+
}
23+
24+
document.body.append(new Btn())
25+
```
26+
27+
You can unset the default definer to defer registration:
1228

29+
```js
30+
import * as dr from '{{featUrl dom_reg}}'
31+
32+
dr.Reg.main.setDefiner()
33+
34+
class Btn extends HTMLButtonElement {
1335
// Registers `Btn` in `dr.Reg.main`,
14-
// but NOT in `window.customElements`.
36+
// but not in `window.customElements` quite yet.
1537
static {dr.reg(this)}
1638
}
1739

18-
// The element is NOT yet upgraded to our custom class.
40+
// The element is not yet upgraded to our custom class.
1941
document.body.append(document.createElement(`button`, {is: `some-btn`}))
2042

2143
// Registers the class and upgrades the element.

doc/dom_reg_readme.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
* [#Usage](#usage)
1919
* [#API](#api)
20+
* [#SSR](#ssr)
2021
{{toc}}
2122

2223
## Usage
@@ -26,10 +27,6 @@ Example mockup for a pushstate link.
2627
```js
2728
import * as dr from '{{featUrl dom_reg}}'
2829

29-
// Enables immediate registration.
30-
// By default, registration is deferred for SSR compatibility.
31-
dr.Reg.main.setDefiner(customElements)
32-
3330
// Immediately ready for use. Tag is automatically set to `a-btn`.
3431
// The mixin `MixReg` enables automatic registration on instantiation.
3532
class Btn extends dr.MixReg(HTMLButtonElement) {
@@ -60,6 +57,32 @@ class MyLink extends dr.MixReg(HTMLAnchorElement) {
6057
document.body.append(new MyLink(`click me`, `/some-link`))
6158
```
6259

60+
## SSR
61+
62+
Apps which use server-side rendering and client-side upgrading of custom elements need a slightly different approach. `MixReg` registers an element class at construction time, when the class is invoked with `new`. Custom elements described in HTML markup are initially not associated with any class, and so the browser wouldn't know what to `new`.
63+
64+
Instead, use `dr.reg`, which is also used internally by `MixReg`. This is simply a shortcut for using the {{link dom_reg Reg default registry}} provided by this module.
65+
66+
```js
67+
import * as dr from '{{featUrl dom_reg}}'
68+
69+
class Btn extends HTMLButtonElement {
70+
/*
71+
Optional. If omitted, `dr.reg` autogenerates
72+
this from the name of the class.
73+
74+
static customName = `some-btn`
75+
*/
76+
77+
// Automatically derives the name `a-btn` and registers the class.
78+
static {dr.reg(this)}
79+
}
80+
81+
const elem = new Btn()
82+
console.log(elem.outerHTML)
83+
`<button is="a-btn"></button>`
84+
```
85+
6386
## API
6487

6588
{{api}}

docs/cli_readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
CLI args:
2424

2525
```js
26-
import * as cl from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/cli.mjs'
26+
import * as cl from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/cli.mjs'
2727

2828
const cli = cl.Flag.os()
2929

@@ -34,15 +34,15 @@ console.log(...cli.args)
3434
Console clearing:
3535

3636
```js
37-
import * as cl from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/cli.mjs'
37+
import * as cl from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/cli.mjs'
3838

3939
cl.emptty()
4040
```
4141

4242
Clearing the console only once, before running your code:
4343

4444
```js
45-
import 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/cli_emptty.mjs'
45+
import 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/cli_emptty.mjs'
4646
```
4747

4848
## API

docs/coll_readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Port and rework of https://github.com/mitranim/jol.
2626
## Usage
2727

2828
```js
29-
import * as c from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/coll.mjs'
29+
import * as c from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/coll.mjs'
3030
```
3131

3232
## API
@@ -101,8 +101,8 @@ Links: [source](../coll.mjs#L100); [test/example](../test/coll_test.mjs#L218).
101101
Variant of [#`Bmap`](#class-bmap) with support for key and value checks. Subclasses must override methods `.reqKey` and `.reqVal`. These methods are automatically called by `.set`. Method `.reqKey` must validate and return the given key, and method `.reqVal` must validate and return the given value. Use type assertions provided by [`lang`](lang_readme.md).
102102

103103
```js
104-
import * as l from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/lang.mjs'
105-
import * as c from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/coll.mjs'
104+
import * as l from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/lang.mjs'
105+
import * as c from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/coll.mjs'
106106

107107
class StrNatMap extends c.TypedMap {
108108
reqKey(key) {return l.reqStr(key)}
@@ -242,7 +242,7 @@ Differences and advantages over `Array`:
242242
The overhead of the wrapper is insignificant.
243243

244244
```js
245-
import * as c from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/coll.mjs'
245+
import * as c from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/coll.mjs'
246246

247247
console.log(new c.Vec())
248248
// Vec{Symbol(val): []}

docs/dom_global_native_readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ In code intended only for browsers, simply use DOM globals. In code intended onl
1515
```js
1616
/*
1717
Use a bundler or importmap to alias this import to one of:
18-
https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/dom_global_native.mjs
19-
https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/dom_global_shim.mjs
18+
https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/dom_global_native.mjs
19+
https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/dom_global_shim.mjs
2020
*/
2121
import * as dg from 'dom_global'
2222

docs/dom_global_shim_readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ In code intended only for non-browser environments, simply import [`dom_global_s
1515
```js
1616
/*
1717
Use a bundler or importmap to alias this import to one of:
18-
https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/dom_global_native.mjs
19-
https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/dom_global_shim.mjs
18+
https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/dom_global_native.mjs
19+
https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/dom_global_shim.mjs
2020
*/
2121
import * as dg from 'dom_global'
2222

docs/dom_readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
## Usage
1212

1313
```js
14-
import * as d from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/dom.mjs'
14+
import * as d from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/dom.mjs'
1515
```
1616

1717
## API

docs/dom_reg_readme.md

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
* [#Usage](#usage)
1919
* [#API](#api)
20+
* [#SSR](#ssr)
2021
* [#`function reg`](#function-reg)
2122
* [#`class Reg`](#class-reg)
2223
* [#Undocumented](#undocumented)
@@ -26,11 +27,7 @@
2627
Example mockup for a pushstate link.
2728

2829
```js
29-
import * as dr from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/dom_reg.mjs'
30-
31-
// Enables immediate registration.
32-
// By default, registration is deferred for SSR compatibility.
33-
dr.Reg.main.setDefiner(customElements)
30+
import * as dr from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/dom_reg.mjs'
3431

3532
// Immediately ready for use. Tag is automatically set to `a-btn`.
3633
// The mixin `MixReg` enables automatic registration on instantiation.
@@ -62,6 +59,32 @@ class MyLink extends dr.MixReg(HTMLAnchorElement) {
6259
document.body.append(new MyLink(`click me`, `/some-link`))
6360
```
6461

62+
## SSR
63+
64+
Apps which use server-side rendering and client-side upgrading of custom elements need a slightly different approach. `MixReg` registers an element class at construction time, when the class is invoked with `new`. Custom elements described in HTML markup are initially not associated with any class, and so the browser wouldn't know what to `new`.
65+
66+
Instead, use `dr.reg`, which is also used internally by `MixReg`. This is simply a shortcut for using the [#default](#class-reg) provided by this module.
67+
68+
```js
69+
import * as dr from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/dom_reg.mjs'
70+
71+
class Btn extends HTMLButtonElement {
72+
/*
73+
Optional. If omitted, `dr.reg` autogenerates
74+
this from the name of the class.
75+
76+
static customName = `some-btn`
77+
*/
78+
79+
// Automatically derives the name `a-btn` and registers the class.
80+
static {dr.reg(this)}
81+
}
82+
83+
const elem = new Btn()
84+
console.log(elem.outerHTML)
85+
`<button is="a-btn"></button>`
86+
```
87+
6588
## API
6689

6790
### `function reg`
@@ -76,22 +99,44 @@ Links: [source](../dom_reg.mjs#L146); [test/example](../test/dom_reg_test.mjs#L2
7699

77100
Registry for custom DOM element classes. Automatically derives tag name from class name, using salting when necessary to avoid collisions. Supports idempotent registration which can be safely called in an element constructor. Allows immediate registration, deferred registration, or a mix of those.
78101

79-
By default, this registry has **no global side effects**. To enable global registration, provide a "definer" to the registry.
102+
By default, the main registry uses `globalThis.customElements`, which exists only in browser environments. In non-browser environments, by default it has no global side effects, but does still modify registered classes by deriving their `.customName`, for rendering to HTML.
103+
104+
For browser-only code, prefer the mixin `MixReg` from the same module which is easier to use. See examples in the [readme](dom_reg_readme.md).
105+
106+
Simple usage:
80107

81108
```js
82-
import * as dr from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/dom_reg.mjs'
109+
import * as dr from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/dom_reg.mjs'
83110

84111
class Btn extends HTMLButtonElement {
85-
// Optional. If omitted, `dr.reg` autogenerates
86-
// this from the name of the class.
87-
static customName = `some-btn`
112+
/*
113+
Optional. If omitted, `dr.reg` autogenerates
114+
this from the name of the class.
115+
116+
static customName = `some-btn`
117+
*/
118+
119+
// Automatically derives the name `a-btn` and registers the class.
120+
static {dr.reg(this)}
121+
}
88122

123+
document.body.append(new Btn())
124+
```
125+
126+
You can unset the default definer to defer registration:
127+
128+
```js
129+
import * as dr from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/dom_reg.mjs'
130+
131+
dr.Reg.main.setDefiner()
132+
133+
class Btn extends HTMLButtonElement {
89134
// Registers `Btn` in `dr.Reg.main`,
90-
// but NOT in `window.customElements`.
135+
// but not in `window.customElements` quite yet.
91136
static {dr.reg(this)}
92137
}
93138

94-
// The element is NOT yet upgraded to our custom class.
139+
// The element is not yet upgraded to our custom class.
95140
document.body.append(document.createElement(`button`, {is: `some-btn`}))
96141

97142
// Registers the class and upgrades the element.
@@ -108,7 +153,9 @@ The following APIs are exported but undocumented. Check [dom_reg.mjs](../dom_reg
108153
* [`function MixReg`](../dom_reg.mjs#L129)
109154
* [`class MixinReg`](../dom_reg.mjs#L131)
110155
* [`function setDefiner`](../dom_reg.mjs#L142)
111-
* [`function isDefiner`](../dom_reg.mjs#L252)
112-
* [`function optDefiner`](../dom_reg.mjs#L253)
113-
* [`function isCustomName`](../dom_reg.mjs#L256)
114-
* [`function reqCustomName`](../dom_reg.mjs#L260)
156+
* [`function isDefiner`](../dom_reg.mjs#L254)
157+
* [`function optDefiner`](../dom_reg.mjs#L255)
158+
* [`function reqDefiner`](../dom_reg.mjs#L256)
159+
* [`function onlyDefiner`](../dom_reg.mjs#L257)
160+
* [`function isCustomName`](../dom_reg.mjs#L260)
161+
* [`function reqCustomName`](../dom_reg.mjs#L264)

docs/dom_shim_readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
## Usage
1212

1313
```js
14-
import * as ds from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/dom_shim.mjs'
14+
import * as ds from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/dom_shim.mjs'
1515
```
1616

1717
## API

docs/http_deno_readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Also see [`http`](http_readme.md) for routing and cookies, and [`live_deno`](liv
1919
Simple example of a server that serves files from the current directory, automatically matching URL paths to HTML files:
2020

2121
```js
22-
import * as hd from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.68/http_deno.mjs'
22+
import * as hd from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.69/http_deno.mjs'
2323

2424
// Finds files in the current folder, with no filtering.
2525
const DIRS = hd.Dirs.of(hd.dirRel(`.`))

0 commit comments

Comments
 (0)