@pfern/elements 0.1.11 → 0.2.0

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/LICENSE CHANGED
@@ -5,18 +5,18 @@ Copyright (c) 2025 Paul Fernandez
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
7
7
  in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
11
 
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
14
 
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  THE SOFTWARE.
22
22
 
package/README.md CHANGED
@@ -1,168 +1,96 @@
1
- # Elements.js
1
+ # @pfern/elements
2
2
 
3
- Elements.js is a tiny, functional UI toolkit for building DOM trees with plain
4
- functions. Components are just functions; updates are just calling the function
5
- again with new arguments.
3
+ A minimalist, pure functional declarative UI toolkit.
6
4
 
7
- ## Install
8
-
9
- ```bash
10
- npm install @pfern/elements
11
- ```
5
+ Elements.js is JS-first: TypeScript is not required at runtime. This package
6
+ ships `.d.ts` files so editors like VSCode can provide rich inline docs and
7
+ autocomplete.
12
8
 
13
- ## Quick Example: Counter
14
-
15
- ```js
16
- import { button, component, div, output } from '@pfern/elements'
9
+ ## Install
17
10
 
18
- export const counter = component((count = 0) =>
19
- div(
20
- output(count),
21
- button(
22
- { onclick: () => counter(count + 1) },
23
- 'Increment')))
11
+ ```sh
12
+ npm i @pfern/elements
24
13
  ```
25
14
 
26
- ## Example: Todos App
15
+ ## How Updates Work
27
16
 
28
- ```js
29
-
30
- import { button, component, div,
31
- form, input, li, span, ul } from '@pfern/elements'
32
-
33
- export const todos = component(
34
- (items = [{ value: 'Add my first todo', done: true }]) => {
17
+ Most apps call `render()` once on page load. You can force a full remount via `render(vtree, container, { replace: true })`. After that, updates happen when a
18
+ DOM event handler (e.g. `onclick`, `onsubmit`) returns the next vnode: Elements.js
19
+ replaces the closest component boundary.
35
20
 
36
- const add = ({ todo: { value } }) =>
37
- value && todos([...items, { value, done: false }])
38
21
 
39
- const remove = item =>
40
- todos(items.filter(i => i !== item))
22
+ Note: for `<a href>` links, if an `onclick` handler returns a vnode, Elements.js
23
+ calls `event.preventDefault()` for unmodified left-clicks so SPAs can use real
24
+ links without manual boilerplate.
41
25
 
42
- const toggle = item =>
43
- todos(items.map(i => i === item ? { ...i, done: !item.done } : i))
44
26
 
45
- return (
46
- div({ class: 'todos' },
47
27
 
48
- form({ onsubmit: add },
49
- input({ name: 'todo', placeholder: 'What needs doing?' }),
50
- button({ type: 'submit' }, 'Add')),
28
+ ### Routing
51
29
 
52
- ul(...items.map(item =>
53
- li(
54
- { style:
55
- { 'text-decoration': item.done ? 'line-through' : 'none' } },
56
- span({ onclick: () => toggle(item) }, item.value),
57
- button({ onclick: () => remove(item) }, '✕'))))))})
58
-
59
- ```
60
-
61
- ## Root Rendering Shortcut
62
-
63
- If you use `html`, `head`, or `body` as the top-level tag, `render()` will
64
- automatically mount into the corresponding document element—no need to pass a
65
- container.
30
+ For SPAs, register a URL-change handler once:
66
31
 
67
32
  ```js
68
- import {
69
- body, h1, h2, head, header, html,
70
- link, main, meta, render, section, title
71
- } from './elements.js'
72
- import { todos } from './components/todos.js'
33
+ import { onNavigate } from '@pfern/elements'
73
34
 
74
- render(
75
- html(
76
- head(
77
- title('Elements.js'),
78
- meta({ name: 'viewport',
79
- content: 'width=device-width, initial-scale=1.0' }),
80
- link({ rel: 'stylesheet', href: 'css/style.css' })
81
- ),
82
- body(
83
- header(h1('Elements.js Demo')),
84
- main(
85
- section(
86
- h2('Todos'),
87
- todos())))))
35
+ onNavigate(() => App())
88
36
  ```
89
37
 
90
- ## Declarative Events
91
-
92
- ### General Behavior
38
+ With a handler registered, `a({ href: '/path' }, ...)` intercepts unmodified
39
+ left-clicks for same-origin links and uses the History API instead of reloading
40
+ the page.
93
41
 
94
- * Any event handler (e.g. `onclick`, `onsubmit`, `oninput`) may return a new
95
- vnode to trigger a subtree replacement.
96
- * If the handler returns `undefined`, the event is treated as passive (no update
97
- occurs).
98
- * Returned vnodes are applied at the closest component boundary.
42
+ ### SSG / SSR
99
43
 
100
- ### Form Events
101
-
102
- For `onsubmit`, `oninput`, and `onchange`, Elements.js provides a special
103
- signature:
44
+ For build-time prerendering (static site generation) or server-side rendering,
45
+ Elements.js can serialize vnodes to HTML:
104
46
 
105
47
  ```js
106
- (event.target.elements, event)
107
- ```
108
-
109
- That is, your handler receives:
48
+ import { div, html, head, body, title, toHtmlString } from '@pfern/elements'
110
49
 
111
- 1. `elements`: the HTML form’s named inputs
112
- 2. `event`: the original DOM event object
50
+ toHtmlString(div('Hello')) // => <div>Hello</div>
113
51
 
114
- Elements.js will automatically call `event.preventDefault()` *only if* your
115
- handler returns a vnode.
52
+ const doc =
53
+ html(
54
+ head(title('My page')),
55
+ body(div('Hello')))
116
56
 
117
- ```js
118
- form({
119
- onsubmit: ({ todo: { value } }, e) =>
120
- value && todos([...items, { value, done: false }])
121
- })
57
+ const htmlText = toHtmlString(doc, { doctype: true })
122
58
  ```
123
59
 
124
- If the handler returns nothing, `preventDefault()` is skipped and the form
125
- submits natively.
126
-
127
- ## API
128
-
129
- ### `component(fn)`
130
-
131
- Wrap a recursive pure function that returns a vnode.
132
-
133
- ### `render(vnode[, container])`
60
+ Notes:
61
+ - Event handlers (function props like `onclick`) are dropped during
62
+ serialization.
63
+ - `innerHTML` is treated as an explicit escape hatch and is inserted verbatim.
134
64
 
135
- Render a vnode into the DOM. If `vnode[0]` is `html`, `head`, or `body`, no
136
- `container` is required.
65
+ ## Usage
137
66
 
138
- ### DOM Elements
67
+ ```js
68
+ import { button, component, div, output, render } from '@pfern/elements'
139
69
 
140
- Every HTML and SVG tag is available as a function:
70
+ export const counter = component((count = 0) =>
71
+ div(
72
+ output(count),
73
+ button({ onclick: () => counter(count + 1) }, 'Increment')))
141
74
 
142
- ```js
143
- div({ id: 'box' }, 'hello')
144
- svg({ width: 100 }, circle({ r: 10 }))
75
+ render(counter(), document.body)
145
76
  ```
146
77
 
147
- ### TypeScript & JSDoc
78
+ ## MathML (curated)
148
79
 
149
- Each tag function (e.g. `div`, `button`, `svg`) includes a `@typedef` and
150
- MDN-sourced description to:
80
+ The runtime supports rendering MathML tags natively (via the MathML namespace).
81
+ A small curated helper set is available as a separate entrypoint:
151
82
 
152
- * Provide editor hints
153
- * Encourage accessibility and semantic markup
154
- * Enable intelligent autocomplete
83
+ ```js
84
+ import { apply, ci, csymbol, math } from '@pfern/elements/mathml'
155
85
 
156
- ### Testing Philosophy
86
+ math(
87
+ apply(csymbol({ cd: 'ski' }, 'app'), ci('f'), ci('x')))
88
+ ```
157
89
 
158
- Elements are data-in, data-out only, so mocking and headless browsers like
159
- `jsdom` are unnecessary out of the box. See the tests [in this
160
- repository](test/README.md) for some examples.
90
+ ## Optional 3D
161
91
 
162
- ## Notes
92
+ X3DOM / X3D helpers live in `@pfern/elements-x3dom` to keep this package small:
163
93
 
164
- - Elements.js is intended to be small and easy to reason about.
165
- - For a starter app template, use `@pfern/create-elements`:
166
- - https://github.com/pfernandez/create-elements
167
- - `npx @pfern/create-elements my-app`
168
- - More examples live in `examples/`.
94
+ ```sh
95
+ npm i @pfern/elements @pfern/elements-x3dom
96
+ ```