@yaks/react 0.0.0-stage → 0.2.36

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/mod.ts ADDED
@@ -0,0 +1,255 @@
1
+ /** React bindings for portable and component renderers. @module */
2
+
3
+ /* @ts-types="npm:@types/react@19.3.0" */ import {
4
+ type Context as Provided,
5
+ createContext,
6
+ createElement as h,
7
+ type FunctionComponent,
8
+ type ReactElement,
9
+ useContext,
10
+ useLayoutEffect,
11
+ useState,
12
+ } from 'react'
13
+ import {
14
+ type Bundle,
15
+ type Child,
16
+ type Context,
17
+ type H,
18
+ type Registration,
19
+ type RenderContext,
20
+ type Renderer,
21
+ rendering,
22
+ resolve,
23
+ type Selection,
24
+ viewIn,
25
+ } from '@yaks/render'
26
+ import {
27
+ committed,
28
+ type EntityProps,
29
+ type Events,
30
+ type Store,
31
+ type Subscribe,
32
+ } from '@yaks/render/component'
33
+ export type {
34
+ EntityProps,
35
+ Events,
36
+ Store,
37
+ Subscribe,
38
+ } from '@yaks/render/component'
39
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
40
+
41
+ /** A native component; the host mounts it so React owns its hooks and identity. */
42
+ export type ComponentRenderer<E = Bundle> = Registration & {
43
+ Render: FunctionComponent<Context & Events & { e: E }>
44
+ }
45
+
46
+ /** Portable trees and native components share the same selection machinery. */
47
+ export type HostRegistry<E = Bundle> = Selection<
48
+ Renderer | ComponentRenderer<E>
49
+ >
50
+ type Node<E = Bundle> =
51
+ | ReactElement<Record<string, unknown>>
52
+ | ReactElement<Context & Events & { e: E }>
53
+
54
+ /** The values bound once when an application creates its Entity component. */
55
+ export type Options = {
56
+ registry: HostRegistry
57
+ vocab: Vocab
58
+ store: Store
59
+ subscribe?: Subscribe
60
+ }
61
+
62
+ let childrenOf = <E>(children: readonly Child<Node<E>>[]): Node<E>[] =>
63
+ (children as unknown[]).flat(Infinity) as Node<E>[]
64
+
65
+ let hyperscript = <E>(
66
+ bundle: Bundle,
67
+ ctx: Events,
68
+ ): H<Node<E>> =>
69
+ (tag, props, ...children) => {
70
+ let { class: named, onDblClick, ...attrs } = props ?? {}
71
+ let control = committed(attrs, bundle, ctx)
72
+ if (
73
+ control !== attrs && control &&
74
+ (tag == 'input' || tag == 'textarea' || tag == 'select')
75
+ ) {
76
+ let { value, checked, ...uncontrolled } = control
77
+ control = {
78
+ ...uncontrolled,
79
+ ...value !== undefined && { defaultValue: value },
80
+ ...checked !== undefined && { defaultChecked: checked },
81
+ }
82
+ }
83
+ props = {
84
+ ...control,
85
+ className: attrs.className ?? named,
86
+ onDoubleClick: attrs.onDoubleClick ?? onDblClick,
87
+ }
88
+ return h<Record<string, unknown>>(tag, props, ...childrenOf(children))
89
+ }
90
+
91
+ /**
92
+ * Resolve a view and build a React node. Native components are passed to h,
93
+ * never invoked directly. Native props default to {e: bundle, ...ctx}; callers
94
+ * with another entity shape supply their typed props as the sixth argument.
95
+ */
96
+ export function render(
97
+ registry: Selection<Renderer>,
98
+ bundle: Bundle,
99
+ view: string | undefined,
100
+ vocab: Vocab,
101
+ ctx?: Context & Events,
102
+ ): ReactElement<Record<string, unknown>> | null
103
+ export function render(
104
+ registry: HostRegistry,
105
+ bundle: Bundle,
106
+ view: string | undefined,
107
+ vocab: Vocab,
108
+ ctx?: Context & Events,
109
+ ): Node | null
110
+ export function render<E>(
111
+ registry: HostRegistry<E>,
112
+ bundle: Bundle,
113
+ view: string | undefined,
114
+ vocab: Vocab,
115
+ ctx: Context & Events,
116
+ props: Context & { e: E },
117
+ ): Node<E> | null
118
+ export function render<E>(
119
+ registry: HostRegistry<E>,
120
+ bundle: Bundle,
121
+ view: string | undefined,
122
+ vocab: Vocab,
123
+ ctx: Context & Events = {},
124
+ props?: Context & { e: E },
125
+ ): Node<E> | null {
126
+ let renderer = resolve(registry, bundle, view, vocab, ctx)
127
+ if (!renderer) return null
128
+ return renderSelected(registry, renderer, bundle, vocab, ctx, props)
129
+ }
130
+
131
+ /** Build the node for a renderer the host has already selected. */
132
+ export let renderSelected = <E>(
133
+ registry: HostRegistry<E>,
134
+ renderer: Renderer | ComponentRenderer<E>,
135
+ bundle: Bundle,
136
+ vocab: Vocab,
137
+ ctx: Context & Events,
138
+ props?: Context & { e: E },
139
+ ): Node<E> | null => {
140
+ let context: RenderContext<Node<E>> & Events = {
141
+ ...ctx,
142
+ render: (view, overrides) => {
143
+ let next = { ...ctx, ...overrides }
144
+ return render(
145
+ registry,
146
+ bundle,
147
+ view,
148
+ vocab,
149
+ next,
150
+ { ...props, ...next, e: props ? props.e : bundle as E },
151
+ )
152
+ },
153
+ }
154
+ if ('Render' in renderer) {
155
+ let words = viewIn(renderer, vocab)
156
+ if (words) {
157
+ let e = props?.e as unknown
158
+ let local = words.into.read([bundle])[0]
159
+ if (e && typeof e == 'object' && e !== bundle) {
160
+ let { entity: _, ...components } = local
161
+ let value = { ...e } as Record<string, unknown>
162
+ for (let name of Object.keys(bundle)) {
163
+ if (name in value && words.into.comp(name) != name) delete value[name]
164
+ }
165
+ local = { ...value, ...components } as Bundle
166
+ }
167
+ let comp = context.comp
168
+ return h(renderer.Render, {
169
+ ...context,
170
+ ...props,
171
+ e: local as E,
172
+ vocab: words.local,
173
+ comp: comp && words.into.comp(comp),
174
+ prop: comp && context.prop
175
+ ? words.into.prop(comp, context.prop)
176
+ : context.prop,
177
+ onPatch: ctx.onPatch &&
178
+ ((patch: Parameters<NonNullable<Events['onPatch']>>[0]) => {
179
+ let [{ entity: _, ...out }] = words.out.write([{
180
+ ...patch,
181
+ entity: bundle.entity,
182
+ }])
183
+ ctx.onPatch!(out as typeof patch, bundle)
184
+ }),
185
+ render: (view: string, overrides?: Context) =>
186
+ context.render!(
187
+ view,
188
+ overrides && {
189
+ ...overrides,
190
+ comp: overrides.comp && words.out.comp(overrides.comp),
191
+ prop: overrides.comp && overrides.prop
192
+ ? words.out.prop(overrides.comp, overrides.prop)
193
+ : overrides.prop,
194
+ },
195
+ ),
196
+ })
197
+ }
198
+ // Without explicit props the public overload requires E to be Bundle.
199
+ return h(
200
+ renderer.Render,
201
+ {
202
+ ...context,
203
+ ...props,
204
+ render: context.render,
205
+ e: props ? props.e : bundle as E,
206
+ },
207
+ )
208
+ }
209
+ return rendering(renderer, context)?.(bundle, hyperscript(bundle, ctx), {
210
+ ...context,
211
+ vocab,
212
+ }) ?? null
213
+ }
214
+
215
+ /** The vocabulary the host has most recently merged into its page graph. */
216
+ export let Vocabulary: Provided<Vocab | undefined> = createContext<
217
+ Vocab | undefined
218
+ >(undefined)
219
+
220
+ /** The registry selected by the page host for mounted entities. */
221
+ export let Registry: Provided<HostRegistry | undefined> = createContext<
222
+ HostRegistry | undefined
223
+ >(undefined)
224
+
225
+ /**
226
+ * Bind the application once: `let Entity = entity({registry, vocab, store,
227
+ * subscribe})`, then mount `<Entity eid='a' view='Tile'/>` through React.
228
+ * A notification re-reads the store even if it mutated a bundle in place.
229
+ */
230
+ export let entity = (
231
+ { registry, vocab, store, subscribe }: Options,
232
+ ): FunctionComponent<EntityProps> =>
233
+ ({ eid, view, ...ctx }) => {
234
+ let now = useContext(Registry) ?? registry
235
+ let words = useContext(Vocabulary) ?? vocab
236
+ let [, revision] = useState(0)
237
+ let refresh = () => revision((n) => n + 1)
238
+ useLayoutEffect(() => {
239
+ if (!subscribe) return
240
+ let active = true
241
+ let off = subscribe(eid, () => {
242
+ if (active) refresh()
243
+ })
244
+ // A store may move between the render read and subscribing, including
245
+ // mutating the same object. Re-read once after the listener is installed.
246
+ refresh()
247
+ return () => {
248
+ active = false
249
+ off()
250
+ }
251
+ }, [eid])
252
+ let bundle = store(eid)
253
+ if (!bundle) return null
254
+ return render(now, bundle, view, words, ctx)
255
+ }
package/package.json CHANGED
@@ -1,6 +1,37 @@
1
1
  {
2
2
  "name": "@yaks/react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.36",
4
+ "homepage": "https://jsr.io/@yaks/react",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./_dist/mod.d.ts",
9
+ "default": "./mod.js"
10
+ },
11
+ "./host": {
12
+ "types": "./_dist/host.d.ts",
13
+ "default": "./host.js"
14
+ },
15
+ "./draw": {
16
+ "types": "./_dist/draw.d.ts",
17
+ "default": "./draw.js"
18
+ },
19
+ "./testing": {
20
+ "types": "./_dist/testing.d.ts",
21
+ "default": "./testing.js"
22
+ }
23
+ },
24
+ "license": "Apache-2.0",
25
+ "dependencies": {
26
+ "@yaks/dom": "0.2.36",
27
+ "@yaks/preact": "0.2.36",
28
+ "@yaks/render": "0.2.36",
29
+ "@yaks/vocab": "0.2.36",
30
+ "@types/react": "19.3.0",
31
+ "@types/react-dom": "19.3.0",
32
+ "react": "19.3.0",
33
+ "react-dom": "19.3.0"
34
+ },
35
+ "description": "Renders registry-selected views with React and binds them to an entity store.",
36
+ "repository": "https://github.com/yak-sh/yak.git"
37
+ }
package/test-dom.ts ADDED
@@ -0,0 +1,38 @@
1
+ /* @ts-types="npm:@types/react@19.3.0" */ import type { ReactNode } from 'npm:react@19.3.0'
2
+ import { parseHTML } from './linkedom'
3
+ import { render } from './draw.ts'
4
+
5
+ let page = parseHTML('<html><body></body></html>')
6
+ let { document } = page
7
+
8
+ export let { CustomEvent } = page
9
+
10
+ /** Install a document and give a root of its own. */
11
+ export let stage = () => {
12
+ let prior = Object.getOwnPropertyDescriptor(globalThis, 'document')
13
+ Object.defineProperty(globalThis, 'document', {
14
+ value: document,
15
+ configurable: true,
16
+ })
17
+ let root = document.createElement('main')
18
+ document.body.append(root)
19
+ let free = () => {
20
+ render(null, root)
21
+ root.remove()
22
+ if (prior) Object.defineProperty(globalThis, 'document', prior)
23
+ else Reflect.deleteProperty(globalThis, 'document')
24
+ }
25
+ return { root, document, free }
26
+ }
27
+
28
+ /** Mount a view and return what takes it down and restores the document. */
29
+ export let mount = (node: ReactNode) => {
30
+ let { root, free } = stage()
31
+ try {
32
+ render(node, root)
33
+ } catch (error) {
34
+ free()
35
+ throw error
36
+ }
37
+ return { root, free }
38
+ }
package/testing.js ADDED
@@ -0,0 +1,37 @@
1
+ /** Drain a renderer's updates while a test drives a view. @module */ /* @ts-types="npm:@types/react@19.3.0" */ import * as React from "react";
2
+ /* @ts-types="npm:@types/react-dom@19.3.0" */ import { flushSync } from "react-dom";
3
+ import { act as compatible } from "@yaks/preact/test-utils";
4
+ let settle = React.act;
5
+ /** Run one synchronous fixture action and draw before the next assertion. */ export let actNow = (run)=>{
6
+ if (settle) return flushSync(run);
7
+ let value;
8
+ compatible(()=>void (value = run()));
9
+ return value;
10
+ };
11
+ /** Run an action and let its scheduled renders finish. */ export let act = (run)=>{
12
+ if (!settle) return compatible(run);
13
+ let flag = 'IS_REACT_ACT_ENVIRONMENT';
14
+ let before = Object.getOwnPropertyDescriptor(globalThis, flag);
15
+ Object.defineProperty(globalThis, flag, {
16
+ value: true,
17
+ configurable: true
18
+ });
19
+ let restore = ()=>{
20
+ if (before) Object.defineProperty(globalThis, flag, before);
21
+ else Reflect.deleteProperty(globalThis, flag);
22
+ };
23
+ try {
24
+ return settle(()=>{
25
+ let pending = run();
26
+ if (typeof pending?.then == 'function') {
27
+ return Promise.resolve(pending).finally(restore);
28
+ }
29
+ restore();
30
+ });
31
+ } catch (error) {
32
+ restore();
33
+ throw error;
34
+ }
35
+ };
36
+ /** Read mutable fixture state through React's subscription interface. */ export let useValue = (source)=>React.useSyncExternalStore((changed)=>source.subscribe(changed), ()=>source.value);
37
+ //# sourceMappingURL=testing.js.map
package/testing.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"testing.js","sources":["./testing.ts"],"names":[],"mappings":"AAAA,mEAAmE,GAEnE,uCAAuC,GAAG,YAAY,mBAA6B;AACnF,2CAA2C,GAAG,SAAS,SAAS,oBAA8B;AAC9F,SAAS,OAAO,UAAU,uCAA4C;AAEtE,IAAI,SAAS,AAAC,MAAsC,GAAG;AAEvD,2EAA2E,GAC3E,OAAO,IAAI,SAAS,CAAI;EACtB,IAAI,QAAQ,OAAO,UAAU;EAC7B,IAAI;EACJ,WAAW,IAAM,KAAK,CAAC,QAAQ,KAAK;EACpC,OAAO;AACT,EAAC;AAED,wDAAwD,GACxD,OAAO,IAAI,MAAyB,CAAC;EACnC,IAAI,CAAC,QAAQ,OAAO,WAAW;EAC/B,IAAI,OAAO;EACX,IAAI,SAAS,OAAO,wBAAwB,CAAC,YAAY;EACzD,OAAO,cAAc,CAAC,YAAY,MAAM;IAAE,OAAO;IAAM,cAAc;EAAK;EAC1E,IAAI,UAAU;IACZ,IAAI,QAAQ,OAAO,cAAc,CAAC,YAAY,MAAM;SAC/C,QAAQ,cAAc,CAAC,YAAY;EAC1C;EACA,IAAI;IACF,OAAO,OAAO;MACZ,IAAI,UAAU;MACd,IAAI,OAAO,SAAS,QAAQ,YAAY;QACtC,OAAO,QAAQ,OAAO,CAAC,SAAS,OAAO,CAAC;MAC1C;MACA;IACF;EACF,EAAE,OAAO,OAAO;IACd;IACA,MAAM;EACR;AACF,EAAC;AAED,uEAAuE,GACvE,OAAO,IAAI,WAAW,CAAI,SAIxB,MAAM,oBAAoB,CACxB,CAAC,UAAY,OAAO,SAAS,CAAC,UAC9B,IAAM,OAAO,KAAK,EACnB"}
package/testing.ts ADDED
@@ -0,0 +1,49 @@
1
+ /** Drain a renderer's updates while a test drives a view. @module */
2
+
3
+ /* @ts-types="npm:@types/react@19.3.0" */ import * as React from 'react'
4
+ /* @ts-types="npm:@types/react-dom@19.3.0" */ import { flushSync } from 'react-dom'
5
+ import { act as compatible } from '@yaks/preact/test-utils'
6
+
7
+ let settle = (React as { act?: typeof compatible }).act
8
+
9
+ /** Run one synchronous fixture action and draw before the next assertion. */
10
+ export let actNow = <T>(run: () => T): T => {
11
+ if (settle) return flushSync(run)
12
+ let value!: T
13
+ compatible(() => void (value = run()))
14
+ return value
15
+ }
16
+
17
+ /** Run an action and let its scheduled renders finish. */
18
+ export let act: typeof compatible = (run) => {
19
+ if (!settle) return compatible(run)
20
+ let flag = 'IS_REACT_ACT_ENVIRONMENT'
21
+ let before = Object.getOwnPropertyDescriptor(globalThis, flag)
22
+ Object.defineProperty(globalThis, flag, { value: true, configurable: true })
23
+ let restore = () => {
24
+ if (before) Object.defineProperty(globalThis, flag, before)
25
+ else Reflect.deleteProperty(globalThis, flag)
26
+ }
27
+ try {
28
+ return settle(() => {
29
+ let pending = run()
30
+ if (typeof pending?.then == 'function') {
31
+ return Promise.resolve(pending).finally(restore)
32
+ }
33
+ restore()
34
+ })
35
+ } catch (error) {
36
+ restore()
37
+ throw error
38
+ }
39
+ }
40
+
41
+ /** Read mutable fixture state through React's subscription interface. */
42
+ export let useValue = <T>(source: {
43
+ value: T
44
+ subscribe: (changed: () => void) => () => void
45
+ }): T =>
46
+ React.useSyncExternalStore(
47
+ (changed) => source.subscribe(changed),
48
+ () => source.value,
49
+ )