@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 +11 -11
- package/README.md +57 -129
- package/elements.js +4 -1942
- package/env.d.ts +24 -0
- package/mathml.js +1 -0
- package/package.json +19 -23
- package/src/core/elements.js +513 -0
- package/src/core/events.js +143 -0
- package/src/core/props.js +177 -0
- package/src/core/tags.js +225 -0
- package/src/core/tick.js +116 -0
- package/src/core/types.js +696 -0
- package/src/helpers.js +73 -0
- package/src/html.js +996 -0
- package/src/mathml.js +340 -0
- package/src/router.js +51 -0
- package/src/ssr.js +175 -0
- package/src/svg.js +407 -0
- package/types/elements.d.ts +4 -1394
- package/types/mathml.d.ts +1 -0
- package/types/src/core/elements.d.ts +29 -0
- package/types/src/core/events.d.ts +15 -0
- package/types/src/core/props.d.ts +9 -0
- package/types/src/core/tags.d.ts +4 -0
- package/types/src/core/tick.d.ts +5 -0
- package/types/src/core/types.d.ts +507 -0
- package/types/src/helpers.d.ts +5 -0
- package/types/src/html.d.ts +802 -0
- package/types/src/mathml.d.ts +264 -0
- package/types/src/router.d.ts +4 -0
- package/types/src/ssr.d.ts +3 -0
- package/types/src/svg.d.ts +348 -0
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
|
-
#
|
|
1
|
+
# @pfern/elements
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
##
|
|
14
|
-
|
|
15
|
-
```js
|
|
16
|
-
import { button, component, div, output } from '@pfern/elements'
|
|
9
|
+
## Install
|
|
17
10
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
output(count),
|
|
21
|
-
button(
|
|
22
|
-
{ onclick: () => counter(count + 1) },
|
|
23
|
-
'Increment')))
|
|
11
|
+
```sh
|
|
12
|
+
npm i @pfern/elements
|
|
24
13
|
```
|
|
25
14
|
|
|
26
|
-
##
|
|
15
|
+
## How Updates Work
|
|
27
16
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
40
|
-
|
|
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
|
-
|
|
49
|
-
input({ name: 'todo', placeholder: 'What needs doing?' }),
|
|
50
|
-
button({ type: 'submit' }, 'Add')),
|
|
28
|
+
### Routing
|
|
51
29
|
|
|
52
|
-
|
|
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
|
-
|
|
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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
That is, your handler receives:
|
|
48
|
+
import { div, html, head, body, title, toHtmlString } from '@pfern/elements'
|
|
110
49
|
|
|
111
|
-
|
|
112
|
-
2. `event`: the original DOM event object
|
|
50
|
+
toHtmlString(div('Hello')) // => <div>Hello</div>
|
|
113
51
|
|
|
114
|
-
|
|
115
|
-
|
|
52
|
+
const doc =
|
|
53
|
+
html(
|
|
54
|
+
head(title('My page')),
|
|
55
|
+
body(div('Hello')))
|
|
116
56
|
|
|
117
|
-
|
|
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
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
|
|
136
|
-
`container` is required.
|
|
65
|
+
## Usage
|
|
137
66
|
|
|
138
|
-
|
|
67
|
+
```js
|
|
68
|
+
import { button, component, div, output, render } from '@pfern/elements'
|
|
139
69
|
|
|
140
|
-
|
|
70
|
+
export const counter = component((count = 0) =>
|
|
71
|
+
div(
|
|
72
|
+
output(count),
|
|
73
|
+
button({ onclick: () => counter(count + 1) }, 'Increment')))
|
|
141
74
|
|
|
142
|
-
|
|
143
|
-
div({ id: 'box' }, 'hello')
|
|
144
|
-
svg({ width: 100 }, circle({ r: 10 }))
|
|
75
|
+
render(counter(), document.body)
|
|
145
76
|
```
|
|
146
77
|
|
|
147
|
-
|
|
78
|
+
## MathML (curated)
|
|
148
79
|
|
|
149
|
-
|
|
150
|
-
|
|
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
|
-
|
|
153
|
-
|
|
154
|
-
* Enable intelligent autocomplete
|
|
83
|
+
```js
|
|
84
|
+
import { apply, ci, csymbol, math } from '@pfern/elements/mathml'
|
|
155
85
|
|
|
156
|
-
|
|
86
|
+
math(
|
|
87
|
+
apply(csymbol({ cd: 'ski' }, 'app'), ci('f'), ci('x')))
|
|
88
|
+
```
|
|
157
89
|
|
|
158
|
-
|
|
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
|
-
|
|
92
|
+
X3DOM / X3D helpers live in `@pfern/elements-x3dom` to keep this package small:
|
|
163
93
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
- `npx @pfern/create-elements my-app`
|
|
168
|
-
- More examples live in `examples/`.
|
|
94
|
+
```sh
|
|
95
|
+
npm i @pfern/elements @pfern/elements-x3dom
|
|
96
|
+
```
|