@kudzujs/core 0.2.3 → 0.4.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.
@@ -16,20 +16,47 @@ export function applyCommands(state, commands, commit, log = console.log) {
16
16
 
17
17
  export const browserState = new Map()
18
18
  const committers = []
19
+ const mountHooks = []
20
+ const unmountHooks = []
19
21
 
20
22
  export function registerCommitter(commit) {
21
23
  committers.push(commit)
22
24
  }
23
25
 
26
+ export function registerMountHook(mount) {
27
+ mountHooks.push(mount)
28
+ }
29
+
30
+ export function registerUnmountHook(unmount) {
31
+ unmountHooks.push(unmount)
32
+ }
33
+
24
34
  export function commitDom(id, value) {
25
35
  for (const node of document.querySelectorAll(`[data-k-text="${id}"]`)) node.textContent = value
26
36
  for (const commit of committers) commit(id)
27
37
  }
28
38
 
39
+ export function mountText(root) {
40
+ for (const node of matching(root, "[data-k-text]")) {
41
+ const id = node.dataset.kText
42
+ if (browserState.has(id)) node.textContent = browserState.get(id)
43
+ else browserState.set(id, JSON.parse(node.dataset.kValue))
44
+ }
45
+ }
46
+
47
+ export function mountDom(root) {
48
+ mountText(root)
49
+ for (const mount of mountHooks) mount(root)
50
+ }
51
+
52
+ export function unmountDom(root) {
53
+ for (const unmount of unmountHooks) unmount(root)
54
+ }
55
+
29
56
  if (typeof document !== "undefined") {
30
57
  const initialState = document.body.dataset.kState
31
58
  if (initialState) for (const [id, value] of JSON.parse(initialState)) browserState.set(id, value)
32
- for (const node of document.querySelectorAll("[data-k-text]")) browserState.set(node.dataset.kText, JSON.parse(node.dataset.kValue))
59
+ mountText(document)
33
60
 
34
61
  const eventNames = ["click", "input", "change"]
35
62
  for (const eventName of eventNames) {
@@ -42,6 +69,10 @@ if (typeof document !== "undefined") {
42
69
  }
43
70
  }
44
71
 
72
+ function matching(root, selector) {
73
+ return [...(root.matches?.(selector) ? [root] : []), ...(root.querySelectorAll?.(selector) ?? [])]
74
+ }
75
+
45
76
  function capitalize(value) {
46
77
  return value[0].toUpperCase() + value.slice(1)
47
78
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kudzujs/core",
3
- "version": "0.2.3",
3
+ "version": "0.4.0",
4
4
  "description": "HTML-first TSX framework with synchronous state semantics and no virtual DOM",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -46,7 +46,7 @@
46
46
  "scripts": {
47
47
  "build": "node ./bin/kudzu.mjs build",
48
48
  "dev": "node ./bin/kudzu.mjs dev",
49
- "check": "tsc --noEmit && node ./bin/kudzu.mjs build",
49
+ "check": "tsc --noEmit && tsc -p test/fixtures/tsconfig.json --noEmit && node ./bin/kudzu.mjs build",
50
50
  "test": "node --test",
51
51
  "prepublishOnly": "npm run check && npm test",
52
52
  "deploy": "wrangler deploy",