@solidjs/h 2.0.0-beta.7 → 2.0.0-beta.9

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/README.md CHANGED
@@ -17,11 +17,17 @@ h("div", { title: "My button" }, h("span", "1"), h("span", "2"), h("span", "3"))
17
17
 
18
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
19
 
20
+ > `h(...)` returns a tagged zero-arity thunk rather than a DOM node directly.
21
+ > Pass a function reference (or `() => h(App)`) to `render(...)` so the thunk
22
+ > is invoked inside the root. Nested `h(...)` thunks auto-invoke when consumed,
23
+ > so composition with control-flow components like `<For>` and `<Show>` works
24
+ > without extra wrapping.
25
+
20
26
  ## Example
21
27
 
22
28
  ```js
23
- import { render } from "solid-js/web";
24
- import h from "solid-js/h";
29
+ import { render } from "@solidjs/web";
30
+ import h from "@solidjs/h";
25
31
  import { createSignal } from "solid-js";
26
32
 
27
33
  function Button(props) {
package/dist/h.cjs CHANGED
@@ -2,15 +2,54 @@
2
2
 
3
3
  var web = require('@solidjs/web');
4
4
 
5
+ const $ELEMENT = Symbol("hyper-element");
6
+ const $WRAPPED = Symbol("hyper-wrapped");
7
+ function resolveThunks(value) {
8
+ if (typeof value === "function" && value[$ELEMENT]) return resolveThunks(value());
9
+ if (Array.isArray(value)) {
10
+ const out = new Array(value.length);
11
+ for (let i = 0; i < value.length; i++) out[i] = resolveThunks(value[i]);
12
+ return out;
13
+ }
14
+ return value;
15
+ }
16
+ function wrapCallback(orig) {
17
+ let w;
18
+ if (orig.length === 1) {
19
+ w = function (a) {
20
+ return resolveThunks(orig.call(this, a));
21
+ };
22
+ } else if (orig.length === 2) {
23
+ w = function (a, b) {
24
+ return resolveThunks(orig.call(this, a, b));
25
+ };
26
+ } else {
27
+ w = function (...args) {
28
+ return resolveThunks(orig.apply(this, args));
29
+ };
30
+ Object.defineProperty(w, "length", {
31
+ value: orig.length
32
+ });
33
+ }
34
+ w[$WRAPPED] = true;
35
+ return w;
36
+ }
5
37
  function createHyperScript(r) {
6
- function h() {
7
- let args = [].slice.call(arguments),
8
- e,
9
- classes = [],
10
- multiExpression = false;
38
+ function h(...rawArgs) {
39
+ if (rawArgs.length === 1 && Array.isArray(rawArgs[0])) return rawArgs[0];
40
+ const thunk = () => r.untrack(() => materialize(rawArgs));
41
+ thunk[$ELEMENT] = true;
42
+ return thunk;
43
+ }
44
+ function materialize(args) {
45
+ let e;
46
+ let classes = [];
47
+ let multiExpression = false;
48
+ args = args.slice();
11
49
  function item(l) {
50
+ if (l == null) return;
12
51
  const type = typeof l;
13
- if (l == null) ;else if ("string" === type) {
52
+ if ("string" === type) {
14
53
  if (!e) parseClass(l);else e.appendChild(document.createTextNode(l));
15
54
  } else if ("number" === type || "boolean" === type || "bigint" === type || "symbol" === type || l instanceof Date || l instanceof RegExp) {
16
55
  e.appendChild(document.createTextNode(l.toString()));
@@ -38,26 +77,24 @@ function createHyperScript(r) {
38
77
  dynamic ? r.spread(e, l, !!args.length) : r.assign(e, l, !!args.length);
39
78
  } else if ("function" === type) {
40
79
  if (!e) {
41
- let props,
42
- next = args[0];
43
- if (next == null || typeof next === "object" && !Array.isArray(next) && !(next instanceof Element)) props = args.shift();
44
- props || (props = {});
45
- if (args.length) {
46
- props.children = args.length > 1 ? args : args[0];
47
- }
48
- const d = Object.getOwnPropertyDescriptors(props);
49
- for (const k in d) {
50
- if (typeof d[k].value === "function" && !d[k].value.length) r.dynamicProperty(props, k);
80
+ const first = args[0];
81
+ const props = first == null || typeof first === "object" && !Array.isArray(first) && !(first instanceof Element) ? args.shift() || {} : {};
82
+ if (args.length) props.children = args.length > 1 ? args : args[0];
83
+ for (const k in props) {
84
+ const v = props[k];
85
+ if (typeof v === "function") {
86
+ if (!v.length) r.dynamicProperty(props, k);else if (!v[$ELEMENT] && !v[$WRAPPED]) {
87
+ props[k] = wrapCallback(v);
88
+ }
89
+ }
51
90
  }
52
91
  e = r.createComponent(l, props);
92
+ while (typeof e === "function" && e[$ELEMENT]) e = e();
53
93
  args = [];
54
- } else {
55
- r.insert(e, l, multiExpression ? null : undefined);
56
- }
94
+ } else if (l[$ELEMENT]) item(l());else r.insert(e, l, multiExpression ? null : undefined);
57
95
  }
58
96
  }
59
- if (args.length === 1 && Array.isArray(args[0])) return args[0];
60
- typeof args[0] === "string" && detectMultiExpression(args);
97
+ if (typeof args[0] === "string") detectMultiExpression(args);
61
98
  while (args.length) item(args.shift());
62
99
  if (e instanceof Element && classes.length) e.classList.add(...classes);
63
100
  return e;
@@ -92,6 +129,7 @@ const h = createHyperScript({
92
129
  insert: web.insert,
93
130
  createComponent: web.createComponent,
94
131
  dynamicProperty: web.dynamicProperty,
132
+ untrack: web.untrack,
95
133
  SVGElements: web.SVGElements,
96
134
  MathMLElements: web.MathMLElements,
97
135
  Namespaces: web.Namespaces
package/dist/h.js CHANGED
@@ -1,14 +1,53 @@
1
- import { Namespaces, MathMLElements, SVGElements, dynamicProperty, createComponent, insert, assign, spread } from '@solidjs/web';
1
+ import { Namespaces, MathMLElements, SVGElements, untrack, dynamicProperty, createComponent, insert, assign, spread } from '@solidjs/web';
2
2
 
3
+ const $ELEMENT = Symbol("hyper-element");
4
+ const $WRAPPED = Symbol("hyper-wrapped");
5
+ function resolveThunks(value) {
6
+ if (typeof value === "function" && value[$ELEMENT]) return resolveThunks(value());
7
+ if (Array.isArray(value)) {
8
+ const out = new Array(value.length);
9
+ for (let i = 0; i < value.length; i++) out[i] = resolveThunks(value[i]);
10
+ return out;
11
+ }
12
+ return value;
13
+ }
14
+ function wrapCallback(orig) {
15
+ let w;
16
+ if (orig.length === 1) {
17
+ w = function (a) {
18
+ return resolveThunks(orig.call(this, a));
19
+ };
20
+ } else if (orig.length === 2) {
21
+ w = function (a, b) {
22
+ return resolveThunks(orig.call(this, a, b));
23
+ };
24
+ } else {
25
+ w = function (...args) {
26
+ return resolveThunks(orig.apply(this, args));
27
+ };
28
+ Object.defineProperty(w, "length", {
29
+ value: orig.length
30
+ });
31
+ }
32
+ w[$WRAPPED] = true;
33
+ return w;
34
+ }
3
35
  function createHyperScript(r) {
4
- function h() {
5
- let args = [].slice.call(arguments),
6
- e,
7
- classes = [],
8
- multiExpression = false;
36
+ function h(...rawArgs) {
37
+ if (rawArgs.length === 1 && Array.isArray(rawArgs[0])) return rawArgs[0];
38
+ const thunk = () => r.untrack(() => materialize(rawArgs));
39
+ thunk[$ELEMENT] = true;
40
+ return thunk;
41
+ }
42
+ function materialize(args) {
43
+ let e;
44
+ let classes = [];
45
+ let multiExpression = false;
46
+ args = args.slice();
9
47
  function item(l) {
48
+ if (l == null) return;
10
49
  const type = typeof l;
11
- if (l == null) ;else if ("string" === type) {
50
+ if ("string" === type) {
12
51
  if (!e) parseClass(l);else e.appendChild(document.createTextNode(l));
13
52
  } else if ("number" === type || "boolean" === type || "bigint" === type || "symbol" === type || l instanceof Date || l instanceof RegExp) {
14
53
  e.appendChild(document.createTextNode(l.toString()));
@@ -36,26 +75,24 @@ function createHyperScript(r) {
36
75
  dynamic ? r.spread(e, l, !!args.length) : r.assign(e, l, !!args.length);
37
76
  } else if ("function" === type) {
38
77
  if (!e) {
39
- let props,
40
- next = args[0];
41
- if (next == null || typeof next === "object" && !Array.isArray(next) && !(next instanceof Element)) props = args.shift();
42
- props || (props = {});
43
- if (args.length) {
44
- props.children = args.length > 1 ? args : args[0];
45
- }
46
- const d = Object.getOwnPropertyDescriptors(props);
47
- for (const k in d) {
48
- if (typeof d[k].value === "function" && !d[k].value.length) r.dynamicProperty(props, k);
78
+ const first = args[0];
79
+ const props = first == null || typeof first === "object" && !Array.isArray(first) && !(first instanceof Element) ? args.shift() || {} : {};
80
+ if (args.length) props.children = args.length > 1 ? args : args[0];
81
+ for (const k in props) {
82
+ const v = props[k];
83
+ if (typeof v === "function") {
84
+ if (!v.length) r.dynamicProperty(props, k);else if (!v[$ELEMENT] && !v[$WRAPPED]) {
85
+ props[k] = wrapCallback(v);
86
+ }
87
+ }
49
88
  }
50
89
  e = r.createComponent(l, props);
90
+ while (typeof e === "function" && e[$ELEMENT]) e = e();
51
91
  args = [];
52
- } else {
53
- r.insert(e, l, multiExpression ? null : undefined);
54
- }
92
+ } else if (l[$ELEMENT]) item(l());else r.insert(e, l, multiExpression ? null : undefined);
55
93
  }
56
94
  }
57
- if (args.length === 1 && Array.isArray(args[0])) return args[0];
58
- typeof args[0] === "string" && detectMultiExpression(args);
95
+ if (typeof args[0] === "string") detectMultiExpression(args);
59
96
  while (args.length) item(args.shift());
60
97
  if (e instanceof Element && classes.length) e.classList.add(...classes);
61
98
  return e;
@@ -90,6 +127,7 @@ const h = createHyperScript({
90
127
  insert,
91
128
  createComponent,
92
129
  dynamicProperty,
130
+ untrack,
93
131
  SVGElements,
94
132
  MathMLElements,
95
133
  Namespaces
@@ -3,9 +3,5 @@ import type { JSX } from "./jsx.d.ts";
3
3
  declare function Fragment(props: {
4
4
  children: JSX.Element;
5
5
  }): JSX.Element;
6
- declare function jsx(type: any, props: any): (Node & {
7
- [key: string]: any;
8
- }) | (Node & {
9
- [key: string]: any;
10
- })[];
6
+ declare function jsx(type: any, props: any): JSX.Element;
11
7
  export { jsx, jsx as jsxs, jsx as jsxDEV, Fragment };
@@ -41,11 +41,8 @@ export type PropValue =
41
41
  * narrowing
42
42
  * - other types pass through unchanged
43
43
  */
44
- export type WidenPropValue<V> = [V] extends [string]
45
- ? string extends V
46
- ? string | number
47
- : V
48
- : V;
44
+ type WidenString<V> = string extends V ? string | number : V;
45
+ export type WidenPropValue<V> = [V] extends [string] ? WidenString<V> : V;
49
46
 
50
47
  /**
51
48
  * Structurally identical → `Y`; distinct → `N`. Used by `IsReadonlyKey` to detect