@pfern/elements 0.1.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.
Files changed (4) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +255 -0
  3. package/elements.js +1931 -0
  4. package/package.json +38 -0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Paul Fernandez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
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:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
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
21
+ THE SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,255 @@
1
+ # Elements.js
2
+
3
+ A minimalist declarative UI toolkit designed around purity, immutability, and HTML semantics.
4
+
5
+ ## Features
6
+
7
+ * Zero-dependency functional UI engine
8
+ * Stateless components defined as pure functions
9
+ * Fully declarative, deeply composable view trees
10
+ * HTML element functions with JSDoc and TypeScript-friendly signatures
11
+ * No hooks, no classes, no virtual DOM heuristics
12
+
13
+ ---
14
+
15
+ ## Why Elements.js?
16
+
17
+ Modern frameworks introduced declarative UI—but buried it beneath lifecycle hooks, mutable state, and complex diffing algorithms.
18
+
19
+ **Elements.js goes further:**
20
+
21
+ * Pure functions represent both logic and view
22
+ * The DOM *is* your state model
23
+ * Re-rendering is *recursion*, not reconciliation
24
+
25
+ > Can UI be defined as a tree of pure function calls—nothing more?
26
+
27
+ Yes. Elements.js proves it.
28
+
29
+ ---
30
+
31
+ ## Philosophy
32
+
33
+ ### Declarative from top to bottom
34
+
35
+ * No internal component state
36
+ * No lifecycle methods or effects
37
+ * Every component is a function
38
+
39
+ To update a view: just **call the function again** with new arguments. The DOM subtree is replaced in place.
40
+
41
+ ### State lives in the DOM
42
+
43
+ There is no observer graph, no `useState`, and no memory of previous renders.
44
+ The DOM node *is the history*. Input state is passed as an argument.
45
+
46
+ ### Minimal abstraction
47
+
48
+ * No keys, refs, proxies, or context systems
49
+ * No transpilation step
50
+ * No reactive graph to debug
51
+
52
+ Elements.js embraces the full truth of each function call as the only valid state.
53
+
54
+ ---
55
+
56
+ ## Example: Counter
57
+
58
+ ```js
59
+ import { div, pre, button, component, render } from './elements.js';
60
+
61
+ const counter = component((count = 0) =>
62
+ div(
63
+ pre(count),
64
+ button({ onclick: () => counter(count + 1) }, 'Increment')
65
+ )
66
+ )
67
+
68
+ render(counter(), document.body);
69
+ ```
70
+
71
+ * Each click returns a new call to `counter(count + 1)`
72
+ * The old DOM node is replaced with the new one
73
+ * No virtual DOM, no diffing
74
+
75
+ ---
76
+
77
+ ## Form Example: Todos App
78
+
79
+ ```js
80
+ import { button, div, component, form, input, li, span, ul } from './elements.js';
81
+
82
+ export const todos = component((items = []) => {
83
+ const add = ({ todo: { value } }) =>
84
+ value && todos([...items, { value, done: false }])
85
+
86
+ const remove = item =>
87
+ todos(items.filter(i => i !== item))
88
+
89
+ const toggle = item =>
90
+ todos(items.map(i => i === item ? { ...i, done: !item.done } : i))
91
+
92
+ return div({ class: 'todos' },
93
+ form({ onsubmit: add },
94
+ input({ name: 'todo', placeholder: 'What needs doing?' }),
95
+ button({ type: 'submit' }, 'Add')),
96
+ ul(...items.map(item =>
97
+ li({ style: { 'text-decoration': item.done ? 'line-through' : 'none' } },
98
+ span({ onclick: () => toggle(item) }, item.value),
99
+ button({ onclick: () => remove(item) }, '✕')
100
+ ))
101
+ )
102
+ )
103
+ })
104
+ ```
105
+
106
+ This is a complete MVC-style app:
107
+
108
+ * Stateless
109
+ * Immutable
110
+ * Pure
111
+
112
+ ---
113
+
114
+ ## Root Rendering Shortcut
115
+
116
+ If you use `html`, `head`, or `body` as the top-level tag, `render()` will automatically mount into the corresponding document element—no need to pass a container.
117
+
118
+ ```js
119
+ import {
120
+ body, h1, h2, head, header, html,
121
+ link, main, meta, render, section, title
122
+ } from './elements.js'
123
+ import { todos } from './components/todos.js'
124
+
125
+ render(
126
+ html(
127
+ head(
128
+ title('Elements.js'),
129
+ meta({ name: 'viewport', content: 'width=device-width, initial-scale=1.0' }),
130
+ link({ rel: 'stylesheet', href: 'css/style.css' })
131
+ ),
132
+ body(
133
+ header(h1('Elements.js Demo')),
134
+ main(
135
+ section(
136
+ h2('Todos'),
137
+ todos()
138
+ )
139
+ )
140
+ )
141
+ )
142
+ )
143
+ ```
144
+
145
+ ---
146
+
147
+ ## Declarative Events
148
+
149
+ All event listeners in Elements.js are pure functions. You can return a vnode from a listener to declaratively update the component tree—no mutation or imperative logic required.
150
+
151
+ ### General Behavior
152
+
153
+ * Any event handler (e.g. `onclick`, `onsubmit`, `oninput`) may return a new vnode to trigger a subtree replacement.
154
+ * If the handler returns `undefined`, the event is treated as passive (no update occurs).
155
+ * Returned vnodes are passed to `component()` to re-render declaratively.
156
+
157
+ ### Form Events
158
+
159
+ For `onsubmit`, `oninput`, and `onchange`, Elements.js provides a special signature:
160
+
161
+ ```js
162
+ (event.target.elements, event)
163
+ ```
164
+
165
+ That is, your handler receives:
166
+
167
+ 1. `elements`: the HTML form’s named inputs
168
+ 2. `event`: the original DOM event object
169
+
170
+ Elements.js will automatically call `event.preventDefault()` *only if* your handler returns a vnode.
171
+
172
+ ```js
173
+ form({
174
+ onsubmit: ({ todo: { value } }, e) =>
175
+ value && todos([...items, { value, done: false }])
176
+ })
177
+ ```
178
+
179
+ If the handler returns nothing, `preventDefault()` is skipped and the form submits natively.
180
+
181
+ ---
182
+
183
+ ## API
184
+
185
+ ### `component(fn)`
186
+
187
+ Wrap a recursive pure function that returns a vnode.
188
+
189
+ ### `render(vnode[, container])`
190
+
191
+ Render a vnode into the DOM. If `vnode[0]` is `html`, `head`, or `body`, no `container` is required.
192
+
193
+ ### DOM Elements
194
+
195
+ Every HTML and SVG tag is available as a function:
196
+
197
+ ```js
198
+ div({ id: 'box' }, 'hello')
199
+ svg({ width: 100 }, circle({ r: 10 }))
200
+ ```
201
+
202
+ ### TypeScript & JSDoc
203
+
204
+ Each tag function (e.g. `div`, `button`, `svg`) includes a `@typedef` and MDN-sourced description to:
205
+
206
+ * Provide editor hints
207
+ * Encourage accessibility and semantic markup
208
+ * Enable intelligent autocomplete
209
+
210
+ ---
211
+
212
+ ## Status
213
+
214
+ * ✅ Production-ready core
215
+ * 🧪 Fully tested (data-in/data-out behavior)
216
+ * ⚡ Under 2kB min+gzip
217
+ * ✅ Node and browser compatible
218
+
219
+ ---
220
+
221
+ ## Installation
222
+
223
+ ```bash
224
+ npm install elements-js
225
+ ```
226
+
227
+ Or clone the repo and use as an ES module:
228
+
229
+ ```js
230
+ import { render, div, component, ... } from './elements.js';
231
+ ```
232
+
233
+ ---
234
+
235
+ ## Summary
236
+
237
+ Elements.js is a thought experiment turned practical:
238
+
239
+ > Can UI be nothing but functions?
240
+
241
+ Turns out, yes.
242
+
243
+ * No diffing
244
+ * No state hooks
245
+ * No lifecycle
246
+ * No reconciliation heuristics
247
+
248
+ Just pure declarative HTML—rewritten in JavaScript.
249
+
250
+ ---
251
+
252
+ **Lightweight. Immutable. Composable.**
253
+
254
+ Give it a try. You might never go back.
255
+