@solidjs/h 2.0.0-experimental.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 +21 -0
- package/README.md +99 -0
- package/dist/h.cjs +115 -0
- package/dist/h.js +144 -0
- package/jsx-dev-runtime/package.json +8 -0
- package/jsx-runtime/dist/jsx.cjs +15 -0
- package/jsx-runtime/dist/jsx.js +10 -0
- package/jsx-runtime/package.json +8 -0
- package/jsx-runtime/types/index.d.ts +14 -0
- package/jsx-runtime/types/jsx.d.ts +4080 -0
- package/package.json +66 -0
- package/types/hyperscript.d.ts +20 -0
- package/types/index.d.ts +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016-2023 Ryan Carniato
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# Solid HyperScript
|
|
2
|
+
|
|
3
|
+
This sub module provides a HyperScript method for Solid. This is useful to use Solid in non-compiled environments or in some environments where you can only use a standard JSX transform. This method can be used as the JSX factory function.
|
|
4
|
+
|
|
5
|
+
HyperScript function takes a few forms. The 2nd props argument is optional. Children may be passed as either an array to the 2nd/3rd argument or as every argument past the 2nd.
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
// create an element with a title attribute
|
|
9
|
+
h("button", { title: "My button" }, "Click Me")
|
|
10
|
+
|
|
11
|
+
// create a component with a title prop
|
|
12
|
+
h(Button, { title: "My button" }, "Click Me")
|
|
13
|
+
|
|
14
|
+
// create an element with many children
|
|
15
|
+
h("div", { title: "My button" }, h("span", "1"), h("span", "2"), h("span", "3"))
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
This is the least efficient way to use Solid as it requires a slightly larger runtime that isn't treeshakeable, and cannot leverage anything in the way of analysis, so it requires manual wrapping of expressions and has a few other caveats (see below).
|
|
19
|
+
|
|
20
|
+
## Example
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import { render } from "solid-js/web";
|
|
24
|
+
import h from "solid-js/h";
|
|
25
|
+
import { createSignal } from "solid-js";
|
|
26
|
+
|
|
27
|
+
function Button(props) {
|
|
28
|
+
return h("button.btn-primary", props)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function Counter() {
|
|
32
|
+
const [count, setCount] = createSignal(0);
|
|
33
|
+
const increment = (e) => setCount(c => c + 1);
|
|
34
|
+
|
|
35
|
+
return h(Button, { type: "button", onClick: increment }, count);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
render(Counter, document.getElementById("app"));
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Differences from JSX
|
|
42
|
+
|
|
43
|
+
There are a few differences from Solid's JSX that are important to note. And also apply when attempting use any transformation that would compile to HyperScript.
|
|
44
|
+
|
|
45
|
+
1. Reactive expression must be manually wrapped in functions to be reactive.
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
// jsx
|
|
49
|
+
<div id={props.id}>{firstName() + lastName()}</div>
|
|
50
|
+
|
|
51
|
+
// hyperscript
|
|
52
|
+
h("div", { id: () => props.id }, () => firstName() + lastName())
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
2. Merging spreads requires using the merge props helper to keep reactivity
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
// jsx
|
|
59
|
+
<div class={selectedClass()} {...props} />
|
|
60
|
+
|
|
61
|
+
// hyperscript
|
|
62
|
+
import { mergeProps } from "solid-js"
|
|
63
|
+
|
|
64
|
+
h("div", mergeProps({ class: selectedClass }, props))
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
3. Events on components require explicit event in the arguments
|
|
68
|
+
|
|
69
|
+
Solid's HyperScript automatically wraps functions passed to props of components with no arguments in getters so you need to provide one to prevent this. The same applies to render props like in the `<For>` component.
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
// good
|
|
73
|
+
h(Button, { onClick: (e) => console.log("Hi")});
|
|
74
|
+
|
|
75
|
+
// bad
|
|
76
|
+
h(Button, { onClick: () => console.log("Hi")})
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
4. All refs are callback form
|
|
80
|
+
|
|
81
|
+
We can't do the compiled assignment trick so only the callback form is supported.
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
let myEl;
|
|
85
|
+
|
|
86
|
+
h(div, { ref: (el) => myEl = el });
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
5. There is a shorthand for static id and classes
|
|
90
|
+
|
|
91
|
+
```js
|
|
92
|
+
h("div#some-id.my-class")
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
6. Fragments are just arrays
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
[h("span", "1"), h("span", "2")]
|
|
99
|
+
```
|
package/dist/h.cjs
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var web = require('@solidjs/web');
|
|
4
|
+
|
|
5
|
+
const $ELEMENT = Symbol("hyper-element");
|
|
6
|
+
function createHyperScript(r) {
|
|
7
|
+
function h() {
|
|
8
|
+
let args = [].slice.call(arguments),
|
|
9
|
+
e,
|
|
10
|
+
classes = [],
|
|
11
|
+
multiExpression = false;
|
|
12
|
+
while (Array.isArray(args[0])) args = args[0];
|
|
13
|
+
if (args[0][$ELEMENT]) args.unshift(h.Fragment);
|
|
14
|
+
typeof args[0] === "string" && detectMultiExpression(args);
|
|
15
|
+
const ret = () => {
|
|
16
|
+
while (args.length) item(args.shift());
|
|
17
|
+
if (e instanceof Element && classes.length) e.classList.add(...classes);
|
|
18
|
+
return e;
|
|
19
|
+
};
|
|
20
|
+
ret[$ELEMENT] = true;
|
|
21
|
+
return ret;
|
|
22
|
+
function item(l) {
|
|
23
|
+
const type = typeof l;
|
|
24
|
+
if (l == null) ;else if ("string" === type) {
|
|
25
|
+
if (!e) parseClass(l);else e.appendChild(document.createTextNode(l));
|
|
26
|
+
} else if ("number" === type || "boolean" === type || "bigint" === type || "symbol" === type || l instanceof Date || l instanceof RegExp) {
|
|
27
|
+
e.appendChild(document.createTextNode(l.toString()));
|
|
28
|
+
} else if (Array.isArray(l)) {
|
|
29
|
+
for (let i = 0; i < l.length; i++) item(l[i]);
|
|
30
|
+
} else if (l instanceof Element) {
|
|
31
|
+
r.insert(e, l, multiExpression ? null : undefined);
|
|
32
|
+
} else if ("object" === type) {
|
|
33
|
+
let dynamic = false;
|
|
34
|
+
const d = Object.getOwnPropertyDescriptors(l);
|
|
35
|
+
for (const k in d) {
|
|
36
|
+
if (k === "class" && classes.length !== 0) {
|
|
37
|
+
const fixedClasses = classes.join(" "),
|
|
38
|
+
value = typeof d["class"].value === "function" ? () => fixedClasses + " " + d["class"].value() : fixedClasses + " " + l["class"];
|
|
39
|
+
Object.defineProperty(l, "class", {
|
|
40
|
+
...d[k],
|
|
41
|
+
value
|
|
42
|
+
});
|
|
43
|
+
classes = [];
|
|
44
|
+
}
|
|
45
|
+
if (k !== "ref" && k.slice(0, 2) !== "on" && typeof d[k].value === "function") {
|
|
46
|
+
r.dynamicProperty(l, k);
|
|
47
|
+
dynamic = true;
|
|
48
|
+
} else if (d[k].get) dynamic = true;
|
|
49
|
+
}
|
|
50
|
+
dynamic ? r.spread(e, l, e instanceof SVGElement, !!args.length) : r.assign(e, l, e instanceof SVGElement, !!args.length);
|
|
51
|
+
} else if ("function" === type) {
|
|
52
|
+
if (!e) {
|
|
53
|
+
let props,
|
|
54
|
+
next = args[0];
|
|
55
|
+
if (next == null || typeof next === "object" && !Array.isArray(next) && !(next instanceof Element)) props = args.shift();
|
|
56
|
+
props || (props = {});
|
|
57
|
+
if (args.length) {
|
|
58
|
+
props.children = args.length > 1 ? args : args[0];
|
|
59
|
+
}
|
|
60
|
+
const d = Object.getOwnPropertyDescriptors(props);
|
|
61
|
+
for (const k in d) {
|
|
62
|
+
if (Array.isArray(d[k].value)) {
|
|
63
|
+
const list = d[k].value;
|
|
64
|
+
props[k] = () => {
|
|
65
|
+
for (let i = 0; i < list.length; i++) {
|
|
66
|
+
while (list[i][$ELEMENT]) list[i] = list[i]();
|
|
67
|
+
}
|
|
68
|
+
return list;
|
|
69
|
+
};
|
|
70
|
+
r.dynamicProperty(props, k);
|
|
71
|
+
} else if (typeof d[k].value === "function" && !d[k].value.length) r.dynamicProperty(props, k);
|
|
72
|
+
}
|
|
73
|
+
e = r.createComponent(l, props);
|
|
74
|
+
args = [];
|
|
75
|
+
} else {
|
|
76
|
+
while (l[$ELEMENT]) l = l();
|
|
77
|
+
r.insert(e, l, multiExpression ? null : undefined);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function parseClass(string) {
|
|
82
|
+
const m = string.split(/([\.#]?[^\s#.]+)/);
|
|
83
|
+
if (/^\.|#/.test(m[1])) e = document.createElement("div");
|
|
84
|
+
for (let i = 0; i < m.length; i++) {
|
|
85
|
+
const v = m[i],
|
|
86
|
+
s = v.substring(1, v.length);
|
|
87
|
+
if (!v) continue;
|
|
88
|
+
if (!e) e = r.SVGElements.has(v) ? document.createElementNS("http://www.w3.org/2000/svg", v) : document.createElement(v);else if (v[0] === ".") classes.push(s);else if (v[0] === "#") e.setAttribute("id", s);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function detectMultiExpression(list) {
|
|
92
|
+
for (let i = 1; i < list.length; i++) {
|
|
93
|
+
if (typeof list[i] === "function") {
|
|
94
|
+
multiExpression = true;
|
|
95
|
+
return;
|
|
96
|
+
} else if (Array.isArray(list[i])) {
|
|
97
|
+
detectMultiExpression(list[i]);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
h.Fragment = props => props.children;
|
|
103
|
+
return h;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const h = createHyperScript({
|
|
107
|
+
spread: web.spread,
|
|
108
|
+
assign: web.assign,
|
|
109
|
+
insert: web.insert,
|
|
110
|
+
createComponent: web.createComponent,
|
|
111
|
+
dynamicProperty: web.dynamicProperty,
|
|
112
|
+
SVGElements: web.SVGElements
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
module.exports = h;
|
package/dist/h.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import {
|
|
2
|
+
spread,
|
|
3
|
+
assign,
|
|
4
|
+
insert,
|
|
5
|
+
createComponent,
|
|
6
|
+
dynamicProperty,
|
|
7
|
+
SVGElements
|
|
8
|
+
} from "@solidjs/web";
|
|
9
|
+
|
|
10
|
+
const $ELEMENT = Symbol("hyper-element");
|
|
11
|
+
function createHyperScript(r) {
|
|
12
|
+
function h() {
|
|
13
|
+
let args = [].slice.call(arguments),
|
|
14
|
+
e,
|
|
15
|
+
classes = [],
|
|
16
|
+
multiExpression = false;
|
|
17
|
+
while (Array.isArray(args[0])) args = args[0];
|
|
18
|
+
if (args[0][$ELEMENT]) args.unshift(h.Fragment);
|
|
19
|
+
typeof args[0] === "string" && detectMultiExpression(args);
|
|
20
|
+
const ret = () => {
|
|
21
|
+
while (args.length) item(args.shift());
|
|
22
|
+
if (e instanceof Element && classes.length) e.classList.add(...classes);
|
|
23
|
+
return e;
|
|
24
|
+
};
|
|
25
|
+
ret[$ELEMENT] = true;
|
|
26
|
+
return ret;
|
|
27
|
+
function item(l) {
|
|
28
|
+
const type = typeof l;
|
|
29
|
+
if (l == null);
|
|
30
|
+
else if ("string" === type) {
|
|
31
|
+
if (!e) parseClass(l);
|
|
32
|
+
else e.appendChild(document.createTextNode(l));
|
|
33
|
+
} else if (
|
|
34
|
+
"number" === type ||
|
|
35
|
+
"boolean" === type ||
|
|
36
|
+
"bigint" === type ||
|
|
37
|
+
"symbol" === type ||
|
|
38
|
+
l instanceof Date ||
|
|
39
|
+
l instanceof RegExp
|
|
40
|
+
) {
|
|
41
|
+
e.appendChild(document.createTextNode(l.toString()));
|
|
42
|
+
} else if (Array.isArray(l)) {
|
|
43
|
+
for (let i = 0; i < l.length; i++) item(l[i]);
|
|
44
|
+
} else if (l instanceof Element) {
|
|
45
|
+
r.insert(e, l, multiExpression ? null : undefined);
|
|
46
|
+
} else if ("object" === type) {
|
|
47
|
+
let dynamic = false;
|
|
48
|
+
const d = Object.getOwnPropertyDescriptors(l);
|
|
49
|
+
for (const k in d) {
|
|
50
|
+
if (k === "class" && classes.length !== 0) {
|
|
51
|
+
const fixedClasses = classes.join(" "),
|
|
52
|
+
value =
|
|
53
|
+
typeof d["class"].value === "function"
|
|
54
|
+
? () => fixedClasses + " " + d["class"].value()
|
|
55
|
+
: fixedClasses + " " + l["class"];
|
|
56
|
+
Object.defineProperty(l, "class", {
|
|
57
|
+
...d[k],
|
|
58
|
+
value
|
|
59
|
+
});
|
|
60
|
+
classes = [];
|
|
61
|
+
}
|
|
62
|
+
if (k !== "ref" && k.slice(0, 2) !== "on" && typeof d[k].value === "function") {
|
|
63
|
+
r.dynamicProperty(l, k);
|
|
64
|
+
dynamic = true;
|
|
65
|
+
} else if (d[k].get) dynamic = true;
|
|
66
|
+
}
|
|
67
|
+
dynamic
|
|
68
|
+
? r.spread(e, l, e instanceof SVGElement, !!args.length)
|
|
69
|
+
: r.assign(e, l, e instanceof SVGElement, !!args.length);
|
|
70
|
+
} else if ("function" === type) {
|
|
71
|
+
if (!e) {
|
|
72
|
+
let props,
|
|
73
|
+
next = args[0];
|
|
74
|
+
if (
|
|
75
|
+
next == null ||
|
|
76
|
+
(typeof next === "object" && !Array.isArray(next) && !(next instanceof Element))
|
|
77
|
+
)
|
|
78
|
+
props = args.shift();
|
|
79
|
+
props || (props = {});
|
|
80
|
+
if (args.length) {
|
|
81
|
+
props.children = args.length > 1 ? args : args[0];
|
|
82
|
+
}
|
|
83
|
+
const d = Object.getOwnPropertyDescriptors(props);
|
|
84
|
+
for (const k in d) {
|
|
85
|
+
if (Array.isArray(d[k].value)) {
|
|
86
|
+
const list = d[k].value;
|
|
87
|
+
props[k] = () => {
|
|
88
|
+
for (let i = 0; i < list.length; i++) {
|
|
89
|
+
while (list[i][$ELEMENT]) list[i] = list[i]();
|
|
90
|
+
}
|
|
91
|
+
return list;
|
|
92
|
+
};
|
|
93
|
+
r.dynamicProperty(props, k);
|
|
94
|
+
} else if (typeof d[k].value === "function" && !d[k].value.length)
|
|
95
|
+
r.dynamicProperty(props, k);
|
|
96
|
+
}
|
|
97
|
+
e = r.createComponent(l, props);
|
|
98
|
+
args = [];
|
|
99
|
+
} else {
|
|
100
|
+
while (l[$ELEMENT]) l = l();
|
|
101
|
+
r.insert(e, l, multiExpression ? null : undefined);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function parseClass(string) {
|
|
106
|
+
const m = string.split(/([\.#]?[^\s#.]+)/);
|
|
107
|
+
if (/^\.|#/.test(m[1])) e = document.createElement("div");
|
|
108
|
+
for (let i = 0; i < m.length; i++) {
|
|
109
|
+
const v = m[i],
|
|
110
|
+
s = v.substring(1, v.length);
|
|
111
|
+
if (!v) continue;
|
|
112
|
+
if (!e)
|
|
113
|
+
e = r.SVGElements.has(v)
|
|
114
|
+
? document.createElementNS("http://www.w3.org/2000/svg", v)
|
|
115
|
+
: document.createElement(v);
|
|
116
|
+
else if (v[0] === ".") classes.push(s);
|
|
117
|
+
else if (v[0] === "#") e.setAttribute("id", s);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function detectMultiExpression(list) {
|
|
121
|
+
for (let i = 1; i < list.length; i++) {
|
|
122
|
+
if (typeof list[i] === "function") {
|
|
123
|
+
multiExpression = true;
|
|
124
|
+
return;
|
|
125
|
+
} else if (Array.isArray(list[i])) {
|
|
126
|
+
detectMultiExpression(list[i]);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
h.Fragment = props => props.children;
|
|
132
|
+
return h;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const h = createHyperScript({
|
|
136
|
+
spread,
|
|
137
|
+
assign,
|
|
138
|
+
insert,
|
|
139
|
+
createComponent,
|
|
140
|
+
dynamicProperty,
|
|
141
|
+
SVGElements
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
export { h as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var h = require('@solidjs/h');
|
|
4
|
+
|
|
5
|
+
function Fragment(props) {
|
|
6
|
+
return props.children;
|
|
7
|
+
}
|
|
8
|
+
function jsx(type, props) {
|
|
9
|
+
return h(type, props);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
exports.Fragment = Fragment;
|
|
13
|
+
exports.jsx = jsx;
|
|
14
|
+
exports.jsxDEV = jsx;
|
|
15
|
+
exports.jsxs = jsx;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type { JSX } from "./jsx.d.ts";
|
|
2
|
+
import type { JSX } from "./jsx.d.ts";
|
|
3
|
+
declare function Fragment(props: { children: JSX.Element }): JSX.Element;
|
|
4
|
+
declare function jsx(
|
|
5
|
+
type: any,
|
|
6
|
+
props: any
|
|
7
|
+
): () =>
|
|
8
|
+
| (Node & {
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
})
|
|
11
|
+
| (Node & {
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
})[];
|
|
14
|
+
export { jsx, jsx as jsxs, jsx as jsxDEV, Fragment };
|