id-dom 0.0.2 → 0.0.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 +296 -0
- package/dist/index.cjs +176 -75
- package/dist/index.cjs.map +2 -2
- package/dist/index.js +176 -75
- package/dist/index.js.map +2 -2
- package/dist/index.min.js +1 -1
- package/package.json +1 -1
- package/Readme.md +0 -249
- /package/{LICENSE.md → LICENSE} +0 -0
package/Readme.md
DELETED
|
@@ -1,249 +0,0 @@
|
|
|
1
|
-
# id-dom
|
|
2
|
-
|
|
3
|
-
**Deterministic DOM element getters by ID (typed, tiny, modern).**
|
|
4
|
-
|
|
5
|
-
`id-dom` is a small utility for grabbing DOM references safely **by `id`**, with predictable behavior:
|
|
6
|
-
|
|
7
|
-
* ✅ **Typed getters** (`button('saveBtn')`, `input('name')`, etc.)
|
|
8
|
-
* ✅ **Strict or optional** mode (`throw` vs `null`)
|
|
9
|
-
* ✅ **Short optional alias** (`.opt`)
|
|
10
|
-
* ✅ **Scopable** to a root (`document`, `ShadowRoot`, or an `Element`)
|
|
11
|
-
* ✅ **Centralized error handling** (`onError`, optional `warn`)
|
|
12
|
-
* ✅ **Zero deps**
|
|
13
|
-
|
|
14
|
-
This is deliberately **not** a selector framework — it’s a tiny “ID-first” primitive for clean, safe DOM wiring.
|
|
15
|
-
|
|
16
|
-
---
|
|
17
|
-
|
|
18
|
-
## Install
|
|
19
|
-
|
|
20
|
-
```bash
|
|
21
|
-
npm install id-dom
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
---
|
|
25
|
-
|
|
26
|
-
## Quick Start
|
|
27
|
-
|
|
28
|
-
```js
|
|
29
|
-
import dom from 'id-dom'
|
|
30
|
-
|
|
31
|
-
const saveBtn = dom.button('saveBtn') // throws if missing or wrong type
|
|
32
|
-
saveBtn.addEventListener('click', save)
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
Optional access (never throws for missing/wrong-type):
|
|
36
|
-
|
|
37
|
-
```js
|
|
38
|
-
const debug = dom.div.optional('debugPanel')
|
|
39
|
-
debug?.append('hello')
|
|
40
|
-
|
|
41
|
-
// short alias
|
|
42
|
-
const maybeCanvas = dom.canvas.opt('game')
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
---
|
|
46
|
-
|
|
47
|
-
## Why ID-first?
|
|
48
|
-
|
|
49
|
-
Using `getElementById` is:
|
|
50
|
-
|
|
51
|
-
* fast
|
|
52
|
-
* unambiguous
|
|
53
|
-
* easy to reason about
|
|
54
|
-
|
|
55
|
-
…and with typed getters, you immediately know whether you have a `HTMLButtonElement`, `HTMLInputElement`, etc.
|
|
56
|
-
|
|
57
|
-
Scoped lookups safely escape IDs when using `querySelector`, ensuring stability even for edge-case IDs (e.g. starting with digits).
|
|
58
|
-
|
|
59
|
-
---
|
|
60
|
-
|
|
61
|
-
## API
|
|
62
|
-
|
|
63
|
-
### Default export: `dom`
|
|
64
|
-
|
|
65
|
-
The default export is a scoped instance using `document` (when available) with **strict** behavior:
|
|
66
|
-
|
|
67
|
-
* missing element → **throws**
|
|
68
|
-
* wrong type/tag → **throws**
|
|
69
|
-
|
|
70
|
-
```js
|
|
71
|
-
import dom from 'id-dom'
|
|
72
|
-
|
|
73
|
-
const name = dom.input('nameInput')
|
|
74
|
-
const submit = dom.button('submitBtn')
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
---
|
|
78
|
-
|
|
79
|
-
### `createDom(root, config?)`
|
|
80
|
-
|
|
81
|
-
Create a scoped instance that searches within a root:
|
|
82
|
-
|
|
83
|
-
* `document` (uses `getElementById`)
|
|
84
|
-
* `ShadowRoot` / `Element` (uses `querySelector(#id)` fallback)
|
|
85
|
-
|
|
86
|
-
```js
|
|
87
|
-
import { createDom } from 'id-dom'
|
|
88
|
-
|
|
89
|
-
const d = createDom(document, { mode: 'null', warn: true })
|
|
90
|
-
|
|
91
|
-
const sidebar = d.div('sidebar') // null if missing
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
#### Config
|
|
95
|
-
|
|
96
|
-
```ts
|
|
97
|
-
type DomMode = 'throw' | 'null'
|
|
98
|
-
|
|
99
|
-
{
|
|
100
|
-
mode?: DomMode // default: 'throw'
|
|
101
|
-
warn?: boolean // default: false
|
|
102
|
-
onError?: (err: Error, ctx: any) => void
|
|
103
|
-
}
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
---
|
|
107
|
-
|
|
108
|
-
### `byId(id, Type, config?)`
|
|
109
|
-
|
|
110
|
-
Generic typed lookup:
|
|
111
|
-
|
|
112
|
-
```js
|
|
113
|
-
import { byId } from 'id-dom'
|
|
114
|
-
|
|
115
|
-
const btn = byId('saveBtn', HTMLButtonElement)
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
Optional variants (never throw for missing/wrong-type):
|
|
119
|
-
|
|
120
|
-
```js
|
|
121
|
-
const maybeBtn = byId.optional('saveBtn', HTMLButtonElement)
|
|
122
|
-
const maybeBtn2 = byId.opt('saveBtn', HTMLButtonElement)
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
---
|
|
126
|
-
|
|
127
|
-
### `tag(id, tagName, config?)`
|
|
128
|
-
|
|
129
|
-
Tag-based validation for semantic elements:
|
|
130
|
-
|
|
131
|
-
```js
|
|
132
|
-
import { tag } from 'id-dom'
|
|
133
|
-
|
|
134
|
-
const main = tag('appMain', 'main')
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
Optional variants:
|
|
138
|
-
|
|
139
|
-
```js
|
|
140
|
-
const maybeMain = tag.optional('appMain', 'main')
|
|
141
|
-
const maybeMain2 = tag.opt('appMain', 'main')
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
---
|
|
145
|
-
|
|
146
|
-
## Built-in Getters
|
|
147
|
-
|
|
148
|
-
### Typed getters
|
|
149
|
-
|
|
150
|
-
From `dom` (and any `createDom()` instance):
|
|
151
|
-
|
|
152
|
-
* `el(id)` → `HTMLElement`
|
|
153
|
-
* `input(id)` → `HTMLInputElement`
|
|
154
|
-
* `button(id)` → `HTMLButtonElement`
|
|
155
|
-
* `textarea(id)` → `HTMLTextAreaElement`
|
|
156
|
-
* `select(id)` → `HTMLSelectElement`
|
|
157
|
-
* `form(id)` → `HTMLFormElement`
|
|
158
|
-
* `div(id)` → `HTMLDivElement`
|
|
159
|
-
* `span(id)` → `HTMLSpanElement`
|
|
160
|
-
* `label(id)` → `HTMLLabelElement`
|
|
161
|
-
* `canvas(id)` → `HTMLCanvasElement`
|
|
162
|
-
* `template(id)` → `HTMLTemplateElement`
|
|
163
|
-
* `svg(id)` → `SVGSVGElement`
|
|
164
|
-
|
|
165
|
-
Each also has:
|
|
166
|
-
|
|
167
|
-
```js
|
|
168
|
-
dom.canvas.optional('game')
|
|
169
|
-
dom.canvas.opt('game')
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
### Common semantic tags
|
|
173
|
-
|
|
174
|
-
* `main(id)` → `<main>`
|
|
175
|
-
* `section(id)` → `<section>`
|
|
176
|
-
* `small(id)` → `<small>`
|
|
177
|
-
|
|
178
|
-
(Also with `.optional` and `.opt`.)
|
|
179
|
-
|
|
180
|
-
---
|
|
181
|
-
|
|
182
|
-
## Error Handling
|
|
183
|
-
|
|
184
|
-
### Throwing (default)
|
|
185
|
-
|
|
186
|
-
```js
|
|
187
|
-
import dom from 'id-dom'
|
|
188
|
-
|
|
189
|
-
dom.button('missing') // throws
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
### Null-returning mode
|
|
193
|
-
|
|
194
|
-
```js
|
|
195
|
-
import { createDom } from 'id-dom'
|
|
196
|
-
|
|
197
|
-
const d = createDom(document, { mode: 'null' })
|
|
198
|
-
|
|
199
|
-
d.button('missing') // null
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
### Central reporting
|
|
203
|
-
|
|
204
|
-
```js
|
|
205
|
-
const d = createDom(document, {
|
|
206
|
-
mode: 'null',
|
|
207
|
-
onError: (err, ctx) => {
|
|
208
|
-
// sendToSentry({ err, ctx })
|
|
209
|
-
},
|
|
210
|
-
})
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
Enable console warnings:
|
|
214
|
-
|
|
215
|
-
```js
|
|
216
|
-
createDom(document, { mode: 'null', warn: true })
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
---
|
|
220
|
-
|
|
221
|
-
## Shadow DOM / Scoped Roots
|
|
222
|
-
|
|
223
|
-
```js
|
|
224
|
-
import { createDom } from 'id-dom'
|
|
225
|
-
|
|
226
|
-
const host = document.querySelector('#widget')
|
|
227
|
-
const shadow = host.attachShadow({ mode: 'open' })
|
|
228
|
-
shadow.innerHTML = `<button id="shadowBtn">Click</button>`
|
|
229
|
-
|
|
230
|
-
const d = createDom(shadow)
|
|
231
|
-
const btn = d.button('shadowBtn')
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
---
|
|
235
|
-
|
|
236
|
-
## Browser Support
|
|
237
|
-
|
|
238
|
-
Modern browsers supporting:
|
|
239
|
-
|
|
240
|
-
* `getElementById`
|
|
241
|
-
* `querySelector`
|
|
242
|
-
|
|
243
|
-
`CSS.escape` is used when available. A safe internal fallback is provided for environments (e.g. some jsdom builds) where it is missing.
|
|
244
|
-
|
|
245
|
-
---
|
|
246
|
-
|
|
247
|
-
## License
|
|
248
|
-
|
|
249
|
-
See `LICENSE`.
|
/package/{LICENSE.md → LICENSE}
RENAMED
|
File without changes
|