@snupo/jmc 1.0.62 → 1.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 (60) hide show
  1. package/LICENSE +21 -0
  2. package/dist/base.d.ts +3 -2
  3. package/dist/base.d.ts.map +1 -1
  4. package/dist/base.js +11 -13
  5. package/dist/base.js.map +1 -1
  6. package/dist/events.js +1 -0
  7. package/dist/events.js.map +1 -1
  8. package/dist/h.d.ts +14 -7
  9. package/dist/h.d.ts.map +1 -1
  10. package/dist/h.js +7 -4
  11. package/dist/h.js.map +1 -1
  12. package/dist/index.d.ts +2 -1
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +2 -1
  15. package/dist/index.js.map +1 -1
  16. package/dist/jmc.d.ts +1 -12
  17. package/dist/jmc.d.ts.map +1 -1
  18. package/dist/jmc.js +80 -119
  19. package/dist/jmc.js.map +1 -1
  20. package/dist/jsx-dev-runtime.d.ts +5 -0
  21. package/dist/jsx-dev-runtime.d.ts.map +1 -0
  22. package/dist/jsx-dev-runtime.js +6 -0
  23. package/dist/jsx-dev-runtime.js.map +1 -0
  24. package/dist/jsx-runtime.d.ts +14 -0
  25. package/dist/jsx-runtime.d.ts.map +1 -0
  26. package/dist/jsx-runtime.js +10 -0
  27. package/dist/jsx-runtime.js.map +1 -0
  28. package/dist/jsx.d.ts +39 -46
  29. package/dist/jsx.d.ts.map +1 -1
  30. package/dist/jsx.js.map +1 -1
  31. package/dist/lib.d.ts +5 -3
  32. package/dist/lib.d.ts.map +1 -1
  33. package/dist/lib.js +4 -5
  34. package/dist/lib.js.map +1 -1
  35. package/dist/props.d.ts +11 -0
  36. package/dist/props.d.ts.map +1 -0
  37. package/dist/props.js +43 -0
  38. package/dist/props.js.map +1 -0
  39. package/dist/update.d.ts +10 -0
  40. package/dist/update.d.ts.map +1 -0
  41. package/dist/update.js +48 -0
  42. package/dist/update.js.map +1 -0
  43. package/package.json +29 -3
  44. package/readme.md +303 -34
  45. package/src/base.ts +11 -15
  46. package/src/events.ts +1 -1
  47. package/src/h.ts +29 -18
  48. package/src/index.ts +2 -1
  49. package/src/jmc.ts +90 -138
  50. package/src/jsx-dev-runtime.ts +8 -0
  51. package/src/jsx-runtime.ts +17 -0
  52. package/src/jsx.ts +18 -30
  53. package/src/lib.ts +10 -8
  54. package/src/props.ts +43 -0
  55. package/src/update.ts +54 -0
  56. package/dist/updateview.d.ts +0 -17
  57. package/dist/updateview.d.ts.map +0 -1
  58. package/dist/updateview.js +0 -43
  59. package/dist/updateview.js.map +0 -1
  60. package/src/updateview.ts +0 -48
package/src/h.ts CHANGED
@@ -1,22 +1,21 @@
1
+ import type { JmcIntrinsicElements } from './jsx.js'
2
+
1
3
  export type J = Record<string, any> // a json record
2
- export type UC = J // update context
3
4
 
4
- // Overload signatures
5
5
  export type Selector = string | Node
6
6
 
7
- export type FComponent = (props: J | undefined, children?: Children) => HU
8
- export type FComponentT<P> = (pcn: P, cn?: Children) => HU
7
+ /** an updateview command: a target (selector or node), a depth (number) or an update context (object) */
8
+ export type Command = Selector | J | number
9
+
10
+ // TBD REMOVE the children parameter once we know it is unused: components get their children in props.children.
11
+ // the second parameter remains for function components written before props.children existed
12
+ export type FComponent = (props: J, children?: Children) => HU
9
13
 
10
14
  type HKindElement = 1
11
15
  type HKindFunctionComponent = 2
12
16
  type HKindClassComponent = 3
13
17
  type HKindFragment = 4
14
18
 
15
- // export const kindelement : HKindElement = 1
16
- // export const kindfc : HKindFunctionComponent = 2
17
- // export const kindcc : HKindClassComponent = 3
18
- // export const kindfragment : HKindFragment = 4
19
-
20
19
  export type HKind = HKindElement | HKindFunctionComponent | HKindClassComponent | HKindFragment
21
20
 
22
21
  export interface IClassComponent {
@@ -36,7 +35,7 @@ export type Children = HU[]
36
35
  export type HText =
37
36
  | string // text node
38
37
  | number // text node
39
- | boolean // do not allow boolean, that
38
+ | boolean // renders nothing, like null and undefined
40
39
 
41
40
  export type HFragment = {
42
41
  kind: HKindFragment
@@ -46,21 +45,21 @@ export type HFragment = {
46
45
  export type HElement = {
47
46
  kind: HKindElement
48
47
  tag: string
49
- p?: J
48
+ p: J
50
49
  cn?: Children
51
50
  }
52
51
 
53
52
  export type HFunctionComponent = {
54
53
  kind: HKindFunctionComponent
55
54
  tag: FComponent
56
- p?: J
55
+ p: J
57
56
  cn?: Children
58
57
  }
59
58
 
60
59
  export type HClassComponent = {
61
60
  kind: HKindClassComponent
62
61
  tag: CComponent
63
- p?: J
62
+ p: J
64
63
  cn?: Children
65
64
  i?: IClassComponent
66
65
  }
@@ -72,23 +71,35 @@ export type H =
72
71
  | HElement
73
72
  | HFunctionComponent
74
73
  | HClassComponent
75
- | HFragment
74
+ | HFragment
76
75
 
77
76
  export type HU = H | undefined | null
78
77
 
79
78
  let isclass = (f: Function) => /^class\s|^class\{/.test(f.toString())
80
79
 
81
- export function jsx(tag: any, p: any, ...cn: any[]): HElement | HFunctionComponent | HClassComponent | HFragment {
80
+ export function jsx(tag: any, p: any, ...cn: any[]): HElement | HComp | HFragment {
82
81
  if (tag?.kind) return tag // already an H node, flatten tree
83
82
 
84
83
  let kind: HKind = tag == jsxf ? 4 : tag instanceof Function ? (isclass(tag) ? 3 : 2) : 1
85
84
 
86
- let h: any = { kind }
85
+ let h: any = { kind, p: p ?? {} }
87
86
  if (kind != 4) h.tag = tag
88
- if (p) h.p = p
89
- if (cn?.length) h.cn = cn
87
+ if (cn?.length) {
88
+ h.cn = cn
89
+ // components get their children in props.children, as with the automatic runtime (jsx-runtime.ts).
90
+ // elements do not: setprops would write them to the element's read-only children property
91
+ if (kind == 2 || kind == 3) h.p = { ...h.p, children: cn }
92
+ }
90
93
  return h
91
94
  }
92
95
 
96
+ // typescript looks up the JSX types on the factory function first: jsx.JSX instead of a global JSX namespace.
97
+ // so jmc's element types cannot clash with other libraries. add your own elements by augmenting jsx.JSX.IntrinsicElements
98
+ export namespace jsx {
99
+ export namespace JSX {
100
+ export interface IntrinsicElements extends JmcIntrinsicElements { }
101
+ }
102
+ }
103
+
93
104
  // fragment is only used above as a sentinel
94
105
  export let jsxf = Symbol()
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './jmc.js'
2
+ export { jmcconfig } from './props.js'
2
3
  export * from './lib.js'
3
4
  export * from './base.js'
4
5
  export * from './jsx.js'
5
6
  export * from './h.js'
6
- export * from './updateview.js'
7
+ export * from './update.js'
package/src/jmc.ts CHANGED
@@ -1,72 +1,28 @@
1
1
  import type {
2
2
  H,
3
3
  HElement,
4
+ HComp,
4
5
  J,
5
- Selector,
6
+ Command,
6
7
  Children,
7
8
  HClassComponent,
8
9
  HU,
9
- } from './h'
10
+ } from './h.js'
10
11
  import { rebind } from './base.js'
11
- import { setevent } from './events'
12
+ import { jmcconfig, setprops } from './props.js'
12
13
 
13
14
  const enum NodeType {
14
15
  // vaporizes (but for that must be in this file, otherwise not)
15
16
  TextNode = 3,
17
+ Comment = 8,
16
18
  }
17
19
 
18
- declare global {
19
- interface Node {
20
- h?: H & { p?: J }
21
- }
22
- }
23
-
24
- let iswebcomponent = (h: HElement) => h.tag.includes('-')
25
-
26
- let attributeNames = new Set([
27
- 'selected',
28
- 'viewBox',
29
- 'list'
30
- // other HTML attributes where your framework wants attribute semantics
31
- ])
32
-
33
- export let jmcconfig = {
34
- attributeNames
35
- }
36
-
37
- const isproperty = (e: Element, name: string, value: any) =>
38
- !attributeNames.has(name) &&
39
- (value instanceof Object ||
40
- value instanceof Function || (
41
- e.namespaceURI !== 'http://www.w3.org/2000/svg' &&
42
- name in e &&
43
- name !== 'style'))
44
-
45
- let setprops = (e: Element & J, old: J | undefined, now: J | undefined) => {
46
- old ??= {}
47
- now ??= {}
48
-
49
- // removals
50
- for (let p in old) {
51
- if (!(p in now)) {
52
- if (p.startsWith('on')) setevent(e, p.slice(2), null)
53
- else if (isproperty(e, p, old[p])) e[p] = typeof old[p] === 'boolean' ? false : ''
54
- else e.removeAttribute(p)
55
- }
56
- }
57
-
58
- // updates
59
- for (let p in now) {
60
- let v = now[p]
61
- v = v?.thunk ? v() : v
62
-
63
- if (p.startsWith('on')) setevent(e, p.slice(2), v)
64
- else if (v != old[p]) {
65
- if (isproperty(e, p, v)) e[p] = v
66
- else if ((v === false || v == null) && !p.startsWith('aria-')) e.removeAttribute(p)
67
- else e.setAttribute(p, v)
68
- }
69
- }
20
+ /** a node as jmc sees it. kept module-local, so consumers' Node type stays untouched */
21
+ type JNode<N extends Node = ChildNode> = N & {
22
+ /** the H this node updates from: the element's own H, or the outermost component rendering it */
23
+ h?: HElement | HComp
24
+ /** props last applied to this element. kept apart from h, which may be the owning component's H */
25
+ ep?: J
70
26
  }
71
27
 
72
28
  let removeexcesschildren = (n: Element, i: number) => {
@@ -74,11 +30,11 @@ let removeexcesschildren = (n: Element, i: number) => {
74
30
  while ((c = n.childNodes[i])) {
75
31
  try {
76
32
  c.remove()
77
- } catch (_) { } // due to blur-event-handlers, this can fail
33
+ } catch { } // due to blur-event-handlers, this can fail
78
34
  }
79
35
  }
80
36
 
81
- /** synchronizes children starting at the i-th element. returns the index of the last child synchronized */
37
+ /** synchronizes children starting at the i-th node. returns the index after the last child synchronized */
82
38
  let syncchildren = (
83
39
  p: Element,
84
40
  cn: Children,
@@ -86,91 +42,102 @@ let syncchildren = (
86
42
  depth: number,
87
43
  uc: J,
88
44
  ): number => {
89
- cn.flat(999)
90
- .filter((hc) => hc !== null && hc !== undefined) // skip. keep 0 and false renderable, per comment below)
91
- .forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)))
45
+ cn.flat(999).forEach((hc) => (i = sync(p, i, hc, undefined, depth, uc)))
92
46
  return i
93
47
  }
94
48
 
49
+ /** class components must render exactly one root node, they are attached to it and update through it */
50
+ let assertnonfragment = (h: HClassComponent, hv: HU, i: number, j: number) => {
51
+ if (j - i != 1 || hv?.kind == 4) throw new Error(`${h.tag.name}.view() must render exactly one root element`)
52
+ }
53
+
95
54
  function sync(
96
55
  parent: Element,
97
56
  i: number,
98
57
  h: HU,
99
- hfun: H | undefined,
58
+ hfun: HComp | undefined,
100
59
  depth: number,
101
60
  uc: J,
102
61
  ): number {
103
- // console.log('%csync', "background:orange", parent.tagName, i, h, hfun, depth)
104
-
105
- if (h === null || h === undefined) return i // skip. keep 0 and false renderable, per comment below
106
-
107
- let c = parent.childNodes[i]
62
+ let c: JNode | undefined = parent.childNodes[i]
63
+
64
+ // null, undefined and booleans render nothing, like in React, so {cond && <b />} works. 0 is still rendered.
65
+ // nothing still takes its slot, as a placeholder: so the siblings after it keep their position, and with it their state
66
+ if (h == null || typeof h == 'boolean') {
67
+ if (hfun?.kind != 3) h = '' // an empty text node, invisible
68
+ else {
69
+ // a class component rendering nothing is attached to a comment, so it keeps its state and can show itself again
70
+ if (c?.nodeType != NodeType.Comment) {
71
+ let n = new Comment()
72
+ c ? c.replaceWith(n) : parent.append(n)
73
+ c = n
74
+ }
75
+ c.h = hfun
76
+ return i + 1
77
+ }
78
+ }
108
79
 
109
80
  switch (h.kind) {
110
81
  case 1: {
111
- let n: Element
112
- let { tag, cn, p } = h
113
- cn ??= []
114
-
115
- let process = () => {
116
- setprops(n, n.h?.p, p)
117
- n.h = hfun ?? h // sets the new property values
118
- if (
119
- --depth &&
120
- !n.hasAttribute('const') &&
121
- !iswebcomponent(h as HElement)
122
- ) {
123
- const j = syncchildren(n, cn, 0, depth, uc)
124
- removeexcesschildren(n, j)
125
- }
126
- }
82
+ // element
83
+ let n: Element & JNode
84
+ let { tag, cn = [], p } = h
127
85
 
128
- if ((<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()) {
86
+ let fresh = (<Element>c)?.tagName?.toLowerCase() != tag.toLowerCase()
87
+ if (fresh) {
129
88
  let ns =
130
- p?.xmlns ??
89
+ p.xmlns ??
131
90
  (parent.tagName == 'foreignObject'
132
91
  ? 'http://www.w3.org/1999/xhtml'
133
92
  : parent.namespaceURI)
134
93
  n = document.createElementNS(ns, tag)
135
94
  c ? c.replaceWith(n) : parent.appendChild(n)
136
- process()
137
- p?.mounted?.(n)
138
- } else {
139
- n = c as Element
140
- if (p?.update?.(c, uc)) return i + 1
141
- process()
95
+ } else n = c as Element & JNode
96
+
97
+ n.h = hfun ?? h // attach before update(), which may handle the update itself
98
+ if (jmcconfig.debug && hfun) n.setAttribute('comp', hfun.tag.name)
99
+ if (!fresh && p.update?.(n, uc)) return i + 1
100
+
101
+ // a const element is an island: jmc renders it once, then foreign code owns it. update() still runs
102
+ if (fresh || !p.const) {
103
+ n.ep = setprops(n, n.ep, p)
104
+ if (--depth && !tag.includes('-')) { // web components render their own children
105
+ const j = syncchildren(n, cn, 0, depth, uc)
106
+ removeexcesschildren(n, j)
107
+ }
142
108
  }
109
+ if (fresh) p.mounted?.(n)
143
110
 
144
111
  return i + 1
145
112
  }
146
113
 
147
- case 4: {
148
- return syncchildren(parent, h.cn ?? [], i, --depth, uc)
149
- }
150
-
151
114
  case 2: {
115
+ // function component
116
+ // TBD REMOVE the second argument once we know it is unused: the children are in h.p.children
152
117
  let hr = h.tag(h.p, h.cn)
153
118
  return sync(parent, i, hr, hfun ?? h, depth, uc)
154
119
  }
155
120
 
156
121
  case 3: {
157
- if ((c?.h as any)?.tag == h.tag) {
158
- let ci = (c.h as HClassComponent).i!
159
- ci.props = c.h!.p = h.p
160
- c.h!.p = h.p // for repatable update, set attached h.p. alternatively, we could use ci.props in case where h.p is undefined
161
- // update() substitutes view/sync when returning truthy
162
- return ci.update?.(uc)
163
- ? i + 1
164
- : sync(parent, i, ci.view(), c.h, depth, uc) // hfun because outermost function shall be attached
165
- } else {
166
- let ci = (h.i = rebind(new h.tag(h.p)))
167
- let hv = ci.view()
168
- let j = sync(parent, i, hv, h, depth, uc)
169
- let n = parent.childNodes[i]!
170
- ci.element = n
171
- ci.mounted?.()
172
- return j
122
+ // class component. reuses the instance attached to c, if c renders the same class
123
+ let old = c?.h?.tag == h.tag ? c.h as HClassComponent : undefined
124
+ let ci = old ? old.i! : (h.i = rebind(new h.tag(h.p)))
125
+ if (old) {
126
+ ci.props = old.p = h.p // set attached p too, for a repeatable update through c.h
127
+ if (ci.update?.(uc)) return i + 1 // update() substitutes view/sync when returning truthy
173
128
  }
129
+ let hv = ci.view()
130
+ let j = sync(parent, i, hv, old ?? h, depth, uc) // the attached H, so updates find the instance
131
+ assertnonfragment(h, hv, i, j)
132
+ ci.element = parent.childNodes[i]
133
+ if (!old) ci.mounted?.()
134
+ return j
135
+ }
136
+
137
+ case 4: {
138
+ // fragment. hfun is deliberately not passed on: a component returning a fragment is not attached
139
+ // to any node, so it cannot update itself, only its parent can update it
140
+ return syncchildren(parent, h.cn ?? [], i, --depth, uc)
174
141
  }
175
142
 
176
143
  default:
@@ -189,45 +156,30 @@ export function patch(
189
156
  e: Node | null,
190
157
  h: H,
191
158
  depth = 999,
192
- uc = {} as J,
159
+ uc: J = {},
193
160
  ): HTMLElement | undefined {
194
161
  if (!e) return
195
162
  if (depth == 0) {
196
- ; (e as HTMLElement).replaceChildren()
163
+ (e as Element).replaceChildren()
197
164
  depth = 999
198
165
  }
199
- let p = e.parentElement as HTMLElement
200
- let i = [].indexOf.call<any, any, any>(p.childNodes, e)
166
+ let p = e.parentElement!
167
+ let i = [...p.childNodes].indexOf(e as ChildNode)
201
168
  sync(p, i, h, undefined, depth, uc)
202
169
  return p.childNodes[i] as HTMLElement
203
170
  }
204
171
 
205
- export type Command = Selector | J | number
206
-
207
172
  export function updateview(...cmds: Command[]): void {
208
- if (!cmds.length) cmds.push('body')
209
- let uc = {}
173
+ if (!cmds.some(c => typeof c == 'string' || c instanceof Node)) cmds.push('body') // no target given: body
174
+ let uc: J = {}
210
175
  let depth = 999
211
- let patchcount = 0
212
-
213
- function f(cmds: Command[]) {
214
- for (const c of cmds) {
215
- if (typeof c === 'string') {
216
- f([...document.querySelectorAll(c)])
217
- } else if (c instanceof Node) {
218
- if (c.h) {
219
- patch(c, c.h, depth, uc)
220
- patchcount++
221
- }
222
- } else if (typeof c === 'number') {
223
- depth = c
224
- } else {
225
- uc = c
226
- }
227
- }
228
- }
229
176
 
230
- f(cmds)
177
+ // isConnected: a selector's matches are collected upfront, patching one can replace a later one
178
+ let up = (n: JNode<Node>) => { if (n.h && n.isConnected) patch(n, n.h, depth, uc) }
231
179
 
232
- // if (!patchcount) console.warn("updateview did not update anything. cmds=", ...cmds)
180
+ for (const c of cmds)
181
+ if (typeof c == 'string') document.querySelectorAll(c).forEach(up)
182
+ else if (c instanceof Node) up(c)
183
+ else if (typeof c == 'number') depth = c
184
+ else uc = c
233
185
  }
@@ -0,0 +1,8 @@
1
+ // entry point of the automatic JSX runtime in development ("jsx": "react-jsxdev", or Vite in dev mode)
2
+ import { jsx, jsxs } from './jsx-runtime.js'
3
+
4
+ export { Fragment } from './jsx-runtime.js'
5
+ export type { JSX } from './jsx-runtime.js'
6
+
7
+ /** like jsx and jsxs. the extra arguments for debugging (key, source, self) are not used */
8
+ export let jsxDEV = (tag: any, props: any, _key: any, isstaticchildren: boolean) => (isstaticchildren ? jsxs : jsx)(tag, props)
@@ -0,0 +1,17 @@
1
+ // entry point of the automatic JSX runtime, used with "jsx": "react-jsx" and "jsxImportSource": "@snupo/jmc".
2
+ // the compiler then imports jsx, jsxs and Fragment from here by itself, so .tsx files need no jsx import.
3
+ // it passes the children inside the props: jsx(tag, { ...props, children }). this adapts that to jmc's jsx(tag, props, ...children)
4
+ import { jsx as classic, jsxf } from './h.js'
5
+
6
+ export { jsxf as Fragment }
7
+
8
+ /** an element with one child or none */
9
+ export let jsx = (tag: any, { children, ...p }: any) => classic(tag, p, ...(children === undefined ? [] : [children]))
10
+
11
+ /** an element with several children, always an array */
12
+ export let jsxs = (tag: any, { children, ...p }: any) => classic(tag, p, ...children)
13
+
14
+ export namespace JSX {
15
+ // the same elements as in classic mode. the automatic runtime type-checks children as a prop, so every element accepts them
16
+ export type IntrinsicElements = { [K in keyof classic.JSX.IntrinsicElements]: classic.JSX.IntrinsicElements[K] & { children?: any } }
17
+ }
package/src/jsx.ts CHANGED
@@ -650,45 +650,33 @@ interface SVGAttributes<T> extends AriaAttributes, HTMLAttributes {
650
650
 
651
651
  type SVGProps<T extends SVGElement> = SVGAttributes<T>
652
652
 
653
- declare global {
654
- namespace JSX {
655
-
656
- // type Element = H
657
-
658
- interface IntrinsicElements extends IntrinsicElementAttributes {
659
-
660
- "x-autosuggest": HtmlHTMLAttributes & {
661
- suggestions: string[]
662
- value: string
663
- }
664
-
665
- label: LabelHTMLAttributes & {
666
- for?: string
667
- }
668
-
669
- svg: SVGProps<SVGSVGElement> & { class?: string | undefined, xmlns: "http://www.w3.org/2000/svg" }
670
- path: SVGProps<SVGPathElement> & { class?: string | undefined }
671
- textPath: SVGProps<SVGTextPathElement> & { class?: string | undefined, style?: string | undefined }
672
- tspan: SVGProps<SVGTSpanElement> & { class?: string | undefined }
673
- circle: SVGProps<SVGCircleElement> & { class?: string | undefined }
674
- rect: SVGProps<SVGRectElement> & { class?: string | undefined }
675
- line: SVGProps<SVGLineElement> & { class?: string | undefined }
676
- text: SVGProps<SVGTextElement> & { class?: string | undefined, style?: string | undefined }
677
- g: SVGProps<SVGGElement> & { class?: string | undefined }
678
- foreignObject: SVGProps<SVGForeignObjectElement> & { class?: string | undefined }
679
- }
653
+ /** the elements jmc knows. JSX reads them through jsx.JSX.IntrinsicElements, see h.ts */
654
+ export interface JmcIntrinsicElements extends IntrinsicElementAttributes {
655
+ label: LabelHTMLAttributes & {
656
+ for?: string
680
657
  }
658
+
659
+ svg: SVGProps<SVGSVGElement> & { class?: string | undefined, xmlns: "http://www.w3.org/2000/svg" }
660
+ path: SVGProps<SVGPathElement> & { class?: string | undefined }
661
+ textPath: SVGProps<SVGTextPathElement> & { class?: string | undefined, style?: string | undefined }
662
+ tspan: SVGProps<SVGTSpanElement> & { class?: string | undefined }
663
+ circle: SVGProps<SVGCircleElement> & { class?: string | undefined }
664
+ rect: SVGProps<SVGRectElement> & { class?: string | undefined }
665
+ line: SVGProps<SVGLineElement> & { class?: string | undefined }
666
+ text: SVGProps<SVGTextElement> & { class?: string | undefined, style?: string | undefined }
667
+ g: SVGProps<SVGGElement> & { class?: string | undefined }
668
+ foreignObject: SVGProps<SVGForeignObjectElement> & { class?: string | undefined }
681
669
  }
682
670
 
683
671
  // 1. Define the Thunk transformer for a single type
684
672
  type WithThunk<T> = T extends undefined
685
673
  ? undefined
686
- : T | (() => T);
674
+ : T | (() => T | undefined)
687
675
 
688
676
  // 2. Map across all keys of the existing interface
689
677
  export type HTMLAttributes = {
690
- [K in keyof HTMLAttributesBase]?: WithThunk<NonNullable<HTMLAttributesBase[K]>>;
691
- };
678
+ [K in keyof HTMLAttributesBase]?: WithThunk<NonNullable<HTMLAttributesBase[K]>>
679
+ }
692
680
 
693
681
  export interface HTMLAttributesBase extends AdditionalElementAttributes {
694
682
  class?: string
package/src/lib.ts CHANGED
@@ -1,30 +1,32 @@
1
- import type { Children, IClassComponent, H, J, HFragment } from './h';
1
+ import type { Children, IClassComponent, HU, J, HFragment } from './h.js'
2
2
  import { updateview } from './jmc.js'
3
3
 
4
+ export type UC = J // update context
5
+ export type FComponentT<P> = (pcn: P, cn?: Children) => HU // a function component with typed props
6
+
4
7
  export type DeepReadonly<T> = { readonly [K in keyof T]: T[K] extends Record<string, unknown> ? DeepReadonly<T[K]> : T[K]; }
5
8
 
6
9
  export const When = ({ cond }: { cond: boolean }, cn: Children): HFragment | null => cond ? { kind: 4, cn } : null
7
10
 
8
- export abstract class JMXComp<P extends J = {}> implements IClassComponent {
11
+ export abstract class Component<P extends J = {}> implements IClassComponent {
9
12
 
10
- element!: HTMLElement // assigned by jmx framework
13
+ element!: HTMLElement // assigned by jmc. a placeholder Comment while view() renders nothing
11
14
  constructor(public props: P) { }
12
15
 
13
16
  // overrides
14
17
  mounted() { }
15
18
  update(uc?: any): boolean | void { }
16
- abstract view(): H
19
+ abstract view(): HU
17
20
 
18
21
  // utility: updates the component's view
19
22
  updateview() { updateview(this.element) }
20
- get ismounted(): boolean { return this.element as any }
23
+ get ismounted(): boolean { return !!this.element }
21
24
  }
22
25
 
23
26
  export function cc(...namesOrObjects: (string | J | undefined)[]): string {
24
27
  return namesOrObjects
25
- .filter(x => x !== undefined)
26
- .flatMap(n => (n.trim ? n : Object.keys(n).filter(k => (n as J)[k]))) // n.trim distinguishes string from J
27
- .join(' ')
28
+ .flatMap(n => typeof n == 'string' ? n : Object.keys(n ?? {}).filter(k => n?.[k])) // objects contribute their truthy keys
29
+ .join(' ')
28
30
  }
29
31
 
30
32
  export const focuselement = (e: Node) => (e as HTMLElement).focus()
package/src/props.ts ADDED
@@ -0,0 +1,43 @@
1
+ import type { J } from './h.js'
2
+
3
+ /** settings to tweak jmc */
4
+ export const jmcconfig = {
5
+ /** marks component roots with a comp="Name" attribute */
6
+ debug: false,
7
+ /** prop names always set as attributes, never as properties */
8
+ attributes: new Set(['selected', 'viewBox']),
9
+ }
10
+
11
+ const isproperty = (e: Element, name: string, value: any) =>
12
+ !jmcconfig.attributes.has(name) &&
13
+ (value instanceof Object || ( // objects and functions
14
+ e.namespaceURI !== 'http://www.w3.org/2000/svg' &&
15
+ name in e &&
16
+ name !== 'style'))
17
+
18
+ let removeprop = (e: Element & J, p: string, old: any) => {
19
+ if (isproperty(e, p, old)) e[p] = typeof old === 'boolean' ? false : ''
20
+ if (!(old instanceof Object)) e.removeAttribute(p) // reflected properties (title, id, ...) leave an empty attribute otherwise
21
+ }
22
+
23
+ /** applies now over old and returns the applied values, with thunks evaluated, to serve as old on the next call */
24
+ export let setprops = (e: Element & J, old: J = {}, now: J): J => {
25
+ // evaluate thunks into a copy: now is h.p, which gets rendered again and must keep its thunks
26
+ now = Object.fromEntries(Object.entries(now).map(([k, v]) => [k, v?.thunk ? v() : v]))
27
+
28
+ // removals
29
+ for (let p in old)
30
+ if (!(p in now)) removeprop(e, p, old[p])
31
+
32
+ // updates
33
+ for (let p in now) {
34
+ let v = now[p]
35
+ if (v !== old[p] && !(v == null && old[p] == null)) { // strict: 0 vs '' or 1 vs true are changes. null vs undefined is not
36
+ if (v == null) removeprop(e, p, old[p])
37
+ else if (isproperty(e, p, v)) e[p] = v
38
+ else if (v === false && !p.startsWith('aria-')) e.removeAttribute(p) // aria-*="false" is meaningful
39
+ else e.setAttribute(p, v)
40
+ }
41
+ }
42
+ return now
43
+ }
package/src/update.ts ADDED
@@ -0,0 +1,54 @@
1
+ import { updateview } from './jmc.js'
2
+ import type { Command, J, Selector } from './h.js'
3
+
4
+ // one queued update: a target with the depth and update context of the call that queued it
5
+ type Pending = [target: Selector, depth: number, uc: J | undefined]
6
+
7
+ let pendings: Pending[] = []
8
+ let isScheduled = false
9
+
10
+ /** resolves a call's commands into pendings, so depth and uc stay with their own call */
11
+ let enqueue = (cmds: Command[]) => {
12
+ if (!cmds.some(c => typeof c == 'string' || c instanceof Node)) cmds = [...cmds, 'body'] // like updateview()
13
+ let depth = 999
14
+ let uc: J | undefined
15
+ for (const c of cmds)
16
+ if (typeof c == 'number') depth = c
17
+ else if (typeof c == 'string' || c instanceof Node) pendings.push([c, depth, uc])
18
+ else uc = c
19
+ }
20
+
21
+ /** synchronously runs all queued updates. selectors are resolved now, and each node is updated once per depth and uc */
22
+ export const flush = (): void => {
23
+ isScheduled = false
24
+ const ps = pendings
25
+ pendings = []
26
+ const done: Pending[] = []
27
+ for (const [target, depth, uc] of ps)
28
+ for (const n of typeof target == 'string' ? document.querySelectorAll(target) : [target])
29
+ if (!done.some(([t, d, u]) => t == n && d == depth && u == uc)) {
30
+ done.push([n, depth, uc])
31
+ updateview(depth, uc ?? {}, n)
32
+ }
33
+ }
34
+
35
+ /** queues the update and schedules a flush, so that all calls before it collapse into one batch */
36
+ let batch = (schedule: (f: () => void) => void) => (...cmds: Command[]): void => {
37
+ enqueue(cmds)
38
+ if (!isScheduled) {
39
+ isScheduled = true
40
+ schedule(flush)
41
+ }
42
+ }
43
+
44
+ /** batches updates until the next microtask */
45
+ export const updateviewmicro = batch(f => queueMicrotask(f))
46
+
47
+ /** batches updates until the next animation frame */
48
+ export const updateviewaf = batch(f => requestAnimationFrame(f)) // looked up on call, so importing works without a DOM
49
+
50
+ /** updates immediately, together with all queued updates */
51
+ export const updateviewsync = (...cmds: Command[]): void => {
52
+ enqueue(cmds)
53
+ flush()
54
+ }