@wardic/foldkit 0.0.0-stage → 1.3.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 (154) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +59 -2
  3. package/THIRD_PARTY_NOTICES.md +145 -0
  4. package/dist/AnimationControls.d.ts +37 -0
  5. package/dist/AnimationControls.d.ts.map +1 -0
  6. package/dist/AnimationControls.js +37 -0
  7. package/dist/AnimationControls.js.map +1 -0
  8. package/dist/AnimationScope.d.ts +20 -0
  9. package/dist/AnimationScope.d.ts.map +1 -0
  10. package/dist/AnimationScope.js +20 -0
  11. package/dist/AnimationScope.js.map +1 -0
  12. package/dist/DragControls.d.ts +25 -0
  13. package/dist/DragControls.d.ts.map +1 -0
  14. package/dist/DragControls.js +21 -0
  15. package/dist/DragControls.js.map +1 -0
  16. package/dist/Features.d.ts +36 -0
  17. package/dist/Features.d.ts.map +1 -0
  18. package/dist/Features.js +44 -0
  19. package/dist/Features.js.map +1 -0
  20. package/dist/Ward.d.ts +301 -0
  21. package/dist/Ward.d.ts.map +1 -0
  22. package/dist/Ward.js +6 -0
  23. package/dist/Ward.js.map +1 -0
  24. package/dist/Wardic.d.ts +363 -0
  25. package/dist/Wardic.d.ts.map +1 -0
  26. package/dist/Wardic.js +85 -0
  27. package/dist/Wardic.js.map +1 -0
  28. package/dist/index.d.ts +15 -0
  29. package/dist/index.d.ts.map +1 -0
  30. package/dist/index.js +20 -0
  31. package/dist/index.js.map +1 -0
  32. package/dist/internal/builder.d.ts +8 -0
  33. package/dist/internal/builder.d.ts.map +1 -0
  34. package/dist/internal/builder.js +242 -0
  35. package/dist/internal/builder.js.map +1 -0
  36. package/dist/internal/commands.d.ts +143 -0
  37. package/dist/internal/commands.d.ts.map +1 -0
  38. package/dist/internal/commands.js +242 -0
  39. package/dist/internal/commands.js.map +1 -0
  40. package/dist/internal/delivery.d.ts +44 -0
  41. package/dist/internal/delivery.d.ts.map +1 -0
  42. package/dist/internal/delivery.js +85 -0
  43. package/dist/internal/delivery.js.map +1 -0
  44. package/dist/internal/dev.d.ts +10 -0
  45. package/dist/internal/dev.d.ts.map +1 -0
  46. package/dist/internal/dev.js +10 -0
  47. package/dist/internal/dev.js.map +1 -0
  48. package/dist/internal/dev.production.d.ts +8 -0
  49. package/dist/internal/dev.production.d.ts.map +1 -0
  50. package/dist/internal/dev.production.js +8 -0
  51. package/dist/internal/dev.production.js.map +1 -0
  52. package/dist/internal/drag.d.ts +66 -0
  53. package/dist/internal/drag.d.ts.map +1 -0
  54. package/dist/internal/drag.js +71 -0
  55. package/dist/internal/drag.js.map +1 -0
  56. package/dist/internal/element.d.ts +59 -0
  57. package/dist/internal/element.d.ts.map +1 -0
  58. package/dist/internal/element.js +582 -0
  59. package/dist/internal/element.js.map +1 -0
  60. package/dist/internal/gestures.d.ts +62 -0
  61. package/dist/internal/gestures.d.ts.map +1 -0
  62. package/dist/internal/gestures.js +109 -0
  63. package/dist/internal/gestures.js.map +1 -0
  64. package/dist/internal/hydrate.d.ts +5 -0
  65. package/dist/internal/hydrate.d.ts.map +1 -0
  66. package/dist/internal/hydrate.js +27 -0
  67. package/dist/internal/hydrate.js.map +1 -0
  68. package/dist/internal/keys.d.ts +21 -0
  69. package/dist/internal/keys.d.ts.map +1 -0
  70. package/dist/internal/keys.js +12 -0
  71. package/dist/internal/keys.js.map +1 -0
  72. package/dist/internal/layout.d.ts +29 -0
  73. package/dist/internal/layout.d.ts.map +1 -0
  74. package/dist/internal/layout.js +88 -0
  75. package/dist/internal/layout.js.map +1 -0
  76. package/dist/internal/mount.d.ts +22 -0
  77. package/dist/internal/mount.d.ts.map +1 -0
  78. package/dist/internal/mount.js +152 -0
  79. package/dist/internal/mount.js.map +1 -0
  80. package/dist/internal/presence.d.ts +33 -0
  81. package/dist/internal/presence.d.ts.map +1 -0
  82. package/dist/internal/presence.js +338 -0
  83. package/dist/internal/presence.js.map +1 -0
  84. package/dist/internal/reactivity.d.ts +15 -0
  85. package/dist/internal/reactivity.d.ts.map +1 -0
  86. package/dist/internal/reactivity.js +26 -0
  87. package/dist/internal/reactivity.js.map +1 -0
  88. package/dist/internal/reorder.d.ts +47 -0
  89. package/dist/internal/reorder.d.ts.map +1 -0
  90. package/dist/internal/reorder.js +250 -0
  91. package/dist/internal/reorder.js.map +1 -0
  92. package/dist/internal/scope.d.ts +28 -0
  93. package/dist/internal/scope.d.ts.map +1 -0
  94. package/dist/internal/scope.js +44 -0
  95. package/dist/internal/scope.js.map +1 -0
  96. package/dist/internal/server.d.ts +13 -0
  97. package/dist/internal/server.d.ts.map +1 -0
  98. package/dist/internal/server.js +13 -0
  99. package/dist/internal/server.js.map +1 -0
  100. package/dist/internal/server.node.d.ts +8 -0
  101. package/dist/internal/server.node.d.ts.map +1 -0
  102. package/dist/internal/server.node.js +8 -0
  103. package/dist/internal/server.node.js.map +1 -0
  104. package/dist/internal/services.d.ts +231 -0
  105. package/dist/internal/services.d.ts.map +1 -0
  106. package/dist/internal/services.js +350 -0
  107. package/dist/internal/services.js.map +1 -0
  108. package/dist/internal/tree.d.ts +71 -0
  109. package/dist/internal/tree.d.ts.map +1 -0
  110. package/dist/internal/tree.js +207 -0
  111. package/dist/internal/tree.js.map +1 -0
  112. package/dist/internal/variant-node.d.ts +62 -0
  113. package/dist/internal/variant-node.d.ts.map +1 -0
  114. package/dist/internal/variant-node.js +279 -0
  115. package/dist/internal/variant-node.js.map +1 -0
  116. package/dist/internal/view-transition.d.ts +10 -0
  117. package/dist/internal/view-transition.d.ts.map +1 -0
  118. package/dist/internal/view-transition.js +36 -0
  119. package/dist/internal/view-transition.js.map +1 -0
  120. package/dist/internal/walk.d.ts +26 -0
  121. package/dist/internal/walk.d.ts.map +1 -0
  122. package/dist/internal/walk.js +162 -0
  123. package/dist/internal/walk.js.map +1 -0
  124. package/package.json +99 -4
  125. package/src/AnimationControls.ts +46 -0
  126. package/src/AnimationScope.ts +19 -0
  127. package/src/DragControls.ts +27 -0
  128. package/src/Features.ts +45 -0
  129. package/src/Ward.ts +434 -0
  130. package/src/Wardic.ts +410 -0
  131. package/src/index.ts +20 -0
  132. package/src/internal/builder.ts +262 -0
  133. package/src/internal/commands.ts +359 -0
  134. package/src/internal/delivery.ts +116 -0
  135. package/src/internal/dev.production.ts +7 -0
  136. package/src/internal/dev.ts +9 -0
  137. package/src/internal/drag.ts +136 -0
  138. package/src/internal/element.ts +668 -0
  139. package/src/internal/gestures.ts +184 -0
  140. package/src/internal/hydrate.ts +29 -0
  141. package/src/internal/keys.ts +19 -0
  142. package/src/internal/layout.ts +109 -0
  143. package/src/internal/mount.ts +181 -0
  144. package/src/internal/presence.ts +362 -0
  145. package/src/internal/reactivity.ts +45 -0
  146. package/src/internal/reorder.ts +313 -0
  147. package/src/internal/scope.ts +60 -0
  148. package/src/internal/server.node.ts +7 -0
  149. package/src/internal/server.ts +12 -0
  150. package/src/internal/services.ts +508 -0
  151. package/src/internal/tree.ts +230 -0
  152. package/src/internal/variant-node.ts +352 -0
  153. package/src/internal/view-transition.ts +43 -0
  154. package/src/internal/walk.ts +211 -0
@@ -0,0 +1,211 @@
1
+ /**
2
+ * The walk at the Provider element's build (#794, Q7; ADR-0071 decision 21's builder-time walk, in
3
+ * place rather than copy-on-write): Foldkit builds children before their parent, so when the Provider
4
+ * element is built its whole subtree exists as view nodes, plain objects with their `data.style`. The
5
+ * walk visits them once per render, top-down, carrying the variant context each Wardic element
6
+ * provides (core's server variant node, pure), whether a `w.Presence({ initial: false })` above holds
7
+ * its children at their `Animate`, and the nearest `w.Config`'s value, and writes each Variant child's
8
+ * inherited Starting style, or a held child's, into its node's style in place, so the server's HTML
9
+ * and the client's hydration render carry what its Mount would otherwise write at its insert. A
10
+ * patched node serialises as patched (probe P-A). The same walk runs on the server and the client: a
11
+ * hydration render must reproduce the server's style attribute, or Foldkit reconciles the mismatch and
12
+ * strips what the client didn't reassert (P-C). An element with the bundle (`w.attributes`) is a Wardic
13
+ * element here as it is to the client's tree: both go by the config it carries, not by the stamp. On
14
+ * the client, a presence's `initial: false` holds its first render's children alone: a child the walk
15
+ * holds at a later render is started at its `Initial` by its Mount, inside the same patch.
16
+ *
17
+ * On a server, the walk is the render's **Appear animations** too (ADR-0044; #794, PR 2, Q3 and Q8):
18
+ * each Wardic element enters dom's server Appear as Solid's server ward does, its slot named by its
19
+ * index path under the Provider element (Q9), the same on the server and the hydration render, and its
20
+ * `animation` written into its node's style when it plays; the CSS comes back for the Provider element
21
+ * to render as its first child, the chunk the client moves into the head before it hydrates
22
+ * (`wardic.hydrate`, PR 3). A Provider element under this one rendered a chunk of its own first:
23
+ * dropped here, so the rules are this walk's alone and the paths the view's (the client reports and
24
+ * ignores a second provider).
25
+ *
26
+ * On a client, the hydration render's walk samples the same way (PR 3, Q9 and Q10), with no explainer and
27
+ * no CSS asked for: each element's slot is named in its style as the server named it, so the inline
28
+ * `animation` the CSS plays through survives hydration for its Mount's Handoff (decision 6) rather than
29
+ * being stripped as a value the client didn't reassert (P-C). So does every render while a Handoff is
30
+ * still under way, which a render before an element's first commit would otherwise strip, cancelling
31
+ * the CSS before its ward took it over; once the last has released, no render samples (the Mounts have
32
+ * the animations), and a render that reorders the view can't hand one element another's.
33
+ *
34
+ * @module
35
+ */
36
+ import * as AnimationControls from "@wardic/core/unstable/AnimationControls"
37
+ import * as Spine from "@wardic/core/unstable/Spine"
38
+ import * as VariantTree from "@wardic/core/unstable/VariantTree"
39
+ import * as WardicConfigValue from "@wardic/core/unstable/WardicConfigValue"
40
+ import * as Appear from "@wardic/dom/Appear"
41
+ import * as AppearAnimations from "@wardic/dom/unstable/AppearAnimations"
42
+ import * as ElementSetup from "@wardic/dom/unstable/ElementSetup"
43
+ import * as Arr from "effect/Array"
44
+ import * as Bool from "effect/Boolean"
45
+ import { constFalse, constUndefined, identity } from "effect/Function"
46
+ import * as Predicate from "effect/Predicate"
47
+ import * as Rec from "effect/Record"
48
+ import * as Str from "effect/String"
49
+ import * as UndefinedOr from "effect/UndefinedOr"
50
+ import { carriedBy } from "./delivery.ts"
51
+ import { type Config, configOf, startingStyleFrom } from "./element.ts"
52
+ import { holdsInitial } from "./presence.ts"
53
+ import { reactivity } from "./reactivity.ts"
54
+ import { appearsOn, registryOf, type State } from "./services.ts"
55
+ import { configValueUnder } from "./tree.ts"
56
+ import { IS_SERVER } from "@wardic/foldkit/internal/server"
57
+
58
+ /** A view node's data, as the walk reads and writes it: its style record, its attributes and its props. */
59
+ export interface ViewData {
60
+ style?: Record<string, string> | undefined
61
+ attrs?: Record<string, string> | undefined
62
+ props?: Record<string, unknown> | undefined
63
+ }
64
+
65
+ /**
66
+ * A Foldkit view node as the walk reads it: its tag, its data and its children. Foldkit's `vdom` isn't
67
+ * an export, so the node is read by this shape alone, through `unknown` at the builder.
68
+ */
69
+ export interface ViewNode {
70
+ readonly sel?: string | undefined
71
+ data?: ViewData | undefined
72
+ children?: ReadonlyArray<ViewNode | string> | undefined
73
+ }
74
+
75
+ /** What an element's ancestors hand the nodes under it. */
76
+ interface Above {
77
+ /** The variant context: with the Appear group the nearest variant parent leads, on a server. */
78
+ readonly provided: VariantTree.WardicVariantContextValue | undefined
79
+ /** Whether a presence holds them at `Animate`. */
80
+ readonly held: boolean
81
+ /** The nearest `w.Config`'s value, the Handle's at the top (ADR-0062): an element's reduced motion and skipped animations on a server (#434, #468). */
82
+ readonly config: WardicConfigValue.WardicConfigValue | undefined
83
+ }
84
+
85
+ /** One Wardic element as the walk reads it, for its entry in the render's Appear animations. */
86
+ interface Visit {
87
+ readonly node: ViewNode
88
+ readonly config: Config
89
+ readonly variant: VariantTree.ServerVariantNode
90
+ /** Its start: the values its Starting style is drawn from, and whether its mount is quiet. */
91
+ readonly start: Spine.Start
92
+ readonly configValue: WardicConfigValue.WardicConfigValue | undefined
93
+ /** Its index path under the Provider element, its slot's name (Q9). */
94
+ readonly path: string
95
+ }
96
+
97
+ /**
98
+ * Whether the children of an element with `config` are held at their `Animate`: its own `w.Presence`'s
99
+ * word when it carries one (the nearest presence's, as the Mount reads it: a nested presence replaces
100
+ * the one above), else what's above it.
101
+ */
102
+ const heldUnder = (config: Config, above: boolean): boolean => UndefinedOr.match(config.presence, { onUndefined: () => above, onDefined: holdsInitial })
103
+
104
+ /** A child's index path under the Provider element (Q9): its parent's path and its index among its parent's children, the Provider element's own "". */
105
+ const pathOf = (parent: string, index: number): string => Bool.match(Str.isEmpty(parent), { onTrue: () => String(index), onFalse: () => `${parent}-${index}` })
106
+
107
+ /** Whether a node is a chunk `<style>` a Provider element under this one rendered (the builder's `prependChunk`). */
108
+ const isChunk = (node: ViewNode | string): boolean => {
109
+ if (Predicate.isString(node)) return false
110
+ return Bool.and(node.sel === "style", UndefinedOr.match(UndefinedOr.map(node.data, (data) => data.attrs), { onUndefined: constFalse, onDefined: (attrs) => Object.hasOwn(attrs, Appear.CHUNK_ATTRIBUTE) }))
111
+ }
112
+
113
+ /**
114
+ * The render's Appear animations (ADR-0044, decisions 3 and 5; #794, Q3 and Q9): on a server, dom's, from
115
+ * the Handle's `appear` and `nonce` and its config's reduced motion, none with `appear: false`; on a
116
+ * client, while hydration's window is open (the hydration render, and the renders before the last
117
+ * Handoff has released), the same sampling without the explainer (the server said its words) and
118
+ * without a nonce (no chunk is rendered), so each element's slot is named as the server named it; none
119
+ * at any other render.
120
+ */
121
+ const appearOfRender = (state: State): AppearAnimations.ServerAppear | undefined => {
122
+ const setting = WardicConfigValue.settingOf(state.rootConfig)
123
+ if (IS_SERVER) return AppearAnimations.serverAppearUnder(state.server.appear, setting, state.server.nonce)
124
+ return Bool.match(state.hydrating, { onTrue: () => AppearAnimations.serverAppearUnder(appearsOn(state.server), setting, undefined), onFalse: constUndefined })
125
+ }
126
+
127
+ /** The render's CSS: on a server, for the Provider element's chunk; "" on a client, whose rules the server rendered and `wardic.hydrate` moved into the head. */
128
+ const cssOf = (render: AppearAnimations.ServerAppear): string => {
129
+ if (IS_SERVER) return render.css()
130
+ return ""
131
+ }
132
+
133
+ /**
134
+ * Walk the Provider element's subtree from `root`, the Provider element itself: each Wardic element
135
+ * (one carrying a config) is read for it, and a Variant child's inherited Starting style, or a held
136
+ * child's, is written into its node's style. On a server, and on a client while hydration's window is
137
+ * open, each element enters the render's Appear animations, its `animation` written when it plays;
138
+ * what comes back is their CSS on a server, "" for none and on a client, which the Provider element
139
+ * renders in its chunk.
140
+ */
141
+ export const walkProvider = (root: ViewNode, state: State): string => {
142
+ const registry = registryOf(state)
143
+ const appear = appearOfRender(state)
144
+ const visitChildren = (parent: ViewNode, above: Above, path: string): void => {
145
+ if (Predicate.isUndefined(parent.children)) return
146
+ // A Provider element under this one rendered its chunk first (the builder's `prependChunk`, Q4): dropped
147
+ // before the children are counted, so their paths are the view's, as the client's hydration walk counts
148
+ // them, and the rules are this walk's alone.
149
+ if (Bool.and(Predicate.isNotUndefined(appear), Arr.some(parent.children, isChunk))) parent.children = Arr.filter(parent.children, (child) => Bool.not(isChunk(child)))
150
+ Arr.forEach(parent.children, (child, index) => visit(child, above, pathOf(path, index)))
151
+ }
152
+ const visit = (node: ViewNode | string, above: Above, path: string): void => {
153
+ if (Predicate.isString(node)) return
154
+ const carried = carriedBy(UndefinedOr.map(node.data, (data) => data.props))
155
+ // A plain element passes its ancestors' context through, as the client's tree walk does (decision 21).
156
+ if (Predicate.isUndefined(carried)) {
157
+ visitChildren(node, above, path)
158
+ return
159
+ }
160
+ // One given Animation controls has no entry yet (#433; #798, Q5): its Starting style, nothing to play.
161
+ const config = AnimationControls.uncontrolledOf(configOf(carried))
162
+ const variant = VariantTree.serverVariantNode(config, above.provided)
163
+ const start = Spine.startOf(config, variant.inherited, above.held, reactivity.untrack)
164
+ if (Bool.or(Predicate.isNotUndefined(variant.inherited), above.held)) writeStyle(node, startingStyleFrom(node.sel, config, registry, start))
165
+ const configValue = UndefinedOr.match(config.config, { onUndefined: () => above.config, onDefined: (own) => configValueUnder(() => own, above.config) })
166
+ const provided = UndefinedOr.match(appear, {
167
+ onUndefined: () => variant.provided,
168
+ onDefined: (render) => enter(render, { node, config, variant, start, configValue, path }),
169
+ })
170
+ visitChildren(node, { provided, held: heldUnder(config, above.held), config: configValue }, path)
171
+ }
172
+ visit(root, { provided: undefined, held: false, config: state.rootConfig }, "")
173
+ return UndefinedOr.match(appear, { onUndefined: () => "", onDefined: cssOf })
174
+ }
175
+
176
+ /**
177
+ * The element's entry in the render's Appear animations (ADR-0044; dom's `appearEntryOf`, as Solid's
178
+ * server ward takes it): its config with its Config transition and no Animation controls, as a server
179
+ * reads it; its `animation` written into its node's style when it plays, a style with its own
180
+ * `animation` vetoing it (decision 4); and what its children inherit, the group it leads among it.
181
+ */
182
+ const enter = (appear: AppearAnimations.ServerAppear, visit: Visit): VariantTree.WardicVariantContextValue | undefined => {
183
+ const read = AnimationControls.uncontrolledOf(WardicConfigValue.withConfigTransition(visit.config, visit.configValue))
184
+ const entry = ElementSetup.appearEntryOf(
185
+ appear,
186
+ read,
187
+ visit.node.sel,
188
+ visit.start.initialValues,
189
+ visit.variant,
190
+ visit.start.quiet,
191
+ WardicConfigValue.settingOf(visit.configValue),
192
+ WardicConfigValue.skipAnimationsOf(visit.configValue)(),
193
+ visit.path,
194
+ )
195
+ Bool.match(AppearAnimations.setsAnimation(visit.config.style), {
196
+ onTrue: () => entry.veto(AppearAnimations.OWN_ANIMATION),
197
+ onFalse: () => UndefinedOr.map(entry.style, (style) => writeStyle(visit.node, style)),
198
+ })
199
+ return entry.provided
200
+ }
201
+
202
+ /** `patch` written into the node's style, over what its own build rendered. */
203
+ const writeStyle = (node: ViewNode, patch: Readonly<Record<string, unknown>>): void => {
204
+ const data = UndefinedOr.match(node.data, { onUndefined: (): ViewData => ({}), onDefined: identity })
205
+ const style = UndefinedOr.match(data.style, { onUndefined: (): Record<string, string> => ({}), onDefined: identity })
206
+ Arr.forEach(Rec.keys(patch), (key) => {
207
+ style[key] = String(patch[key])
208
+ })
209
+ data.style = style
210
+ node.data = data
211
+ }