@pfern/elements 0.1.10 → 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,279 +1,96 @@
1
- # Elements.js
1
+ # @pfern/elements
2
2
 
3
- Elements.js is a minimalist declarative UI toolkit designed around purity,
4
- immutability, and HTML semantics.
3
+ A minimalist, pure functional declarative UI toolkit.
5
4
 
6
- ## Features
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.
7
8
 
8
- * Zero-dependency functional UI engine
9
- * Stateless components defined as pure functions
10
- * Fully declarative, deeply composable view trees
11
- * HTML element functions with JSDoc and TypeScript-friendly signatures
12
- * No hooks, no classes, no virtual DOM heuristics
9
+ ## Install
13
10
 
14
- ---
15
-
16
- ## Why Elements.js?
17
-
18
- Modern frameworks introduced declarative UI—but buried it beneath lifecycle
19
- hooks, mutable state, and complex diffing algorithms.
20
-
21
- **Elements.js goes further:**
22
-
23
- * Pure functions represent both logic and view
24
- * The DOM *is* your state model
25
- * Re-rendering is *recursion*, not reconciliation
26
-
27
- > Can UI be defined as a tree of pure function calls—nothing more?
28
-
29
- Yes. Elements.js proves it.
30
-
31
- ---
32
-
33
- ## Philosophy
34
-
35
- ### Declarative from top to bottom
36
-
37
- * No internal component state
38
- * No lifecycle methods or effects
39
- * Every component is a function
11
+ ```sh
12
+ npm i @pfern/elements
13
+ ```
40
14
 
41
- To update a view: just **call the function again** with new arguments. The DOM
42
- subtree is replaced in place.
15
+ ## How Updates Work
43
16
 
44
- ### State lives in the DOM
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.
45
20
 
46
- There is no observer graph, no `useState`, and no memory of previous renders.
47
- The DOM node *is the history*. Input state is passed as an argument.
48
21
 
49
- ### Minimal abstraction
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.
50
25
 
51
- * No keys, refs, proxies, or context systems
52
- * No transpilation step
53
- * No reactive graph to debug
54
26
 
55
- Elements.js embraces the full truth of each function call as the only valid
56
- state.
57
27
 
58
- ---
28
+ ### Routing
59
29
 
60
- ## Example: Counter
30
+ For SPAs, register a URL-change handler once:
61
31
 
62
32
  ```js
63
- import { button, component, div, output } from '@pfern/elements'
33
+ import { onNavigate } from '@pfern/elements'
64
34
 
65
- export const counter = component((count = 0) =>
66
- div(
67
- output(count),
68
- button(
69
- { onclick: () => counter(count + 1) },
70
- 'Increment')))
35
+ onNavigate(() => App())
71
36
  ```
72
37
 
73
- * Each click returns a new call to `counter(count + 1)`
74
- * The old DOM node is replaced with the new one
75
- * No virtual DOM, no diffing
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.
76
41
 
77
- ---
42
+ ### SSG / SSR
78
43
 
79
- ## Form Example: Todos App
44
+ For build-time prerendering (static site generation) or server-side rendering,
45
+ Elements.js can serialize vnodes to HTML:
80
46
 
81
47
  ```js
48
+ import { div, html, head, body, title, toHtmlString } from '@pfern/elements'
82
49
 
83
- import { button, component, div,
84
- form, input, li, span, ul } from '@pfern/elements'
85
-
86
- export const todos = component(
87
- (items = [{ value: 'Add my first todo', done: true }]) => {
88
-
89
- const add = ({ todo: { value } }) =>
90
- value && todos([...items, { value, done: false }])
91
-
92
- const remove = item =>
93
- todos(items.filter(i => i !== item))
94
-
95
- const toggle = item =>
96
- todos(items.map(i => i === item ? { ...i, done: !item.done } : i))
97
-
98
- return (
99
- div({ class: 'todos' },
50
+ toHtmlString(div('Hello')) // => <div>Hello</div>
100
51
 
101
- form({ onsubmit: add },
102
- input({ name: 'todo', placeholder: 'What needs doing?' }),
103
- button({ type: 'submit' }, 'Add')),
104
-
105
- ul(...items.map(item =>
106
- li(
107
- { style:
108
- { 'text-decoration': item.done ? 'line-through' : 'none' } },
109
- span({ onclick: () => toggle(item) }, item.value),
110
- button({ onclick: () => remove(item) }, '✕'))))))})
52
+ const doc =
53
+ html(
54
+ head(title('My page')),
55
+ body(div('Hello')))
111
56
 
57
+ const htmlText = toHtmlString(doc, { doctype: true })
112
58
  ```
113
59
 
114
- This is a complete MVC-style app:
115
-
116
- * Stateless
117
- * Immutable
118
- * Pure
119
-
120
- You can view these examples live on [Github
121
- Pages](https://pfernandez.github.io/elements/) or by running them locally with
122
- `npm run dev`.
123
-
124
- ---
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.
125
64
 
126
- ## Root Rendering Shortcut
127
-
128
- If you use `html`, `head`, or `body` as the top-level tag, `render()` will
129
- automatically mount into the corresponding document element—no need to pass a
130
- container.
65
+ ## Usage
131
66
 
132
67
  ```js
133
- import {
134
- body, h1, h2, head, header, html,
135
- link, main, meta, render, section, title
136
- } from './elements.js'
137
- import { todos } from './components/todos.js'
138
-
139
- render(
140
- html(
141
- head(
142
- title('Elements.js'),
143
- meta({ name: 'viewport',
144
- content: 'width=device-width, initial-scale=1.0' }),
145
- link({ rel: 'stylesheet', href: 'css/style.css' })
146
- ),
147
- body(
148
- header(h1('Elements.js Demo')),
149
- main(
150
- section(
151
- h2('Todos'),
152
- todos())))))
153
- ```
68
+ import { button, component, div, output, render } from '@pfern/elements'
154
69
 
155
- ---
156
-
157
- ## Declarative Events
158
-
159
- All event listeners in Elements.js are pure functions. You can return a vnode
160
- from a listener to declaratively update the component tree—- no mutation or
161
- imperative logic required.
162
-
163
- ### General Behavior
164
-
165
- * Any event handler (e.g. `onclick`, `onsubmit`, `oninput`) may return a new
166
- vnode to trigger a subtree replacement.
167
- * If the handler returns `undefined`, the event is treated as passive (no update
168
- occurs).
169
- * Returned vnodes are passed to `component()` to re-render declaratively.
170
-
171
- ### Form Events
172
-
173
- For `onsubmit`, `oninput`, and `onchange`, Elements.js provides a special
174
- signature:
70
+ export const counter = component((count = 0) =>
71
+ div(
72
+ output(count),
73
+ button({ onclick: () => counter(count + 1) }, 'Increment')))
175
74
 
176
- ```js
177
- (event.target.elements, event)
75
+ render(counter(), document.body)
178
76
  ```
179
77
 
180
- That is, your handler receives:
181
-
182
- 1. `elements`: the HTML form’s named inputs
183
- 2. `event`: the original DOM event object
78
+ ## MathML (curated)
184
79
 
185
- Elements.js will automatically call `event.preventDefault()` *only if* your
186
- handler returns a vnode.
80
+ The runtime supports rendering MathML tags natively (via the MathML namespace).
81
+ A small curated helper set is available as a separate entrypoint:
187
82
 
188
83
  ```js
189
- form({
190
- onsubmit: ({ todo: { value } }, e) =>
191
- value && todos([...items, { value, done: false }])
192
- })
193
- ```
194
-
195
- If the handler returns nothing, `preventDefault()` is skipped and the form
196
- submits natively.
197
-
198
- ---
199
-
200
- ## API
201
-
202
- ### `component(fn)`
203
-
204
- Wrap a recursive pure function that returns a vnode.
205
-
206
- ### `render(vnode[, container])`
84
+ import { apply, ci, csymbol, math } from '@pfern/elements/mathml'
207
85
 
208
- Render a vnode into the DOM. If `vnode[0]` is `html`, `head`, or `body`, no
209
- `container` is required.
210
-
211
- ### DOM Elements
212
-
213
- Every HTML and SVG tag is available as a function:
214
-
215
- ```js
216
- div({ id: 'box' }, 'hello')
217
- svg({ width: 100 }, circle({ r: 10 }))
86
+ math(
87
+ apply(csymbol({ cd: 'ski' }, 'app'), ci('f'), ci('x')))
218
88
  ```
219
89
 
220
- ### TypeScript & JSDoc
221
-
222
- Each tag function (e.g. `div`, `button`, `svg`) includes a `@typedef` and
223
- MDN-sourced description to:
224
-
225
- * Provide editor hints
226
- * Encourage accessibility and semantic markup
227
- * Enable intelligent autocomplete
228
-
229
- ### Testing Philosophy
230
-
231
- Elements are data-in, data-out only, so mocking and headless browsers like
232
- `jsdom` are unnecessary out of the box. See the tests [in this
233
- repository](test/README.md) for some examples.
234
-
235
- ---
90
+ ## Optional 3D
236
91
 
237
- ## Status
92
+ X3DOM / X3D helpers live in `@pfern/elements-x3dom` to keep this package small:
238
93
 
239
- * 🧪 Fully tested (data-in/data-out behavior)
240
- * Under 2kB min+gzip
241
- * ✅ Node and browser compatible
242
-
243
- ---
244
-
245
- ## Installation
246
-
247
- ```bash
248
- npm install @pfern/elements
94
+ ```sh
95
+ npm i @pfern/elements @pfern/elements-x3dom
249
96
  ```
250
-
251
- Or clone the repo and use as an ES module:
252
-
253
- ```js
254
- import { render, div, component, ... } from './elements.js';
255
- ```
256
-
257
- ---
258
-
259
- ## Summary
260
-
261
- Elements.js is a thought experiment turned practical:
262
-
263
- > Can UI be nothing but functions?
264
-
265
- Turns out, yes.
266
-
267
- * No diffing
268
- * No state hooks
269
- * No lifecycle
270
- * No reconciliation heuristics
271
-
272
- Just pure declarative HTML—rewritten in JavaScript.
273
-
274
- ---
275
-
276
- **Lightweight. Immutable. Composable.**
277
-
278
- Give it a try. You might never go back.
279
-