@kovca/glyph-react 0.0.0-stage → 0.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.
- package/LICENSE +21 -0
- package/README.md +95 -2
- package/dist/index.d.ts +7 -0
- package/dist/index.js +3 -0
- package/dist/overlay.d.ts +12 -0
- package/dist/overlay.js +12 -0
- package/dist/renderer.d.ts +19 -0
- package/dist/renderer.js +133 -0
- package/dist/surface.d.ts +26 -0
- package/dist/surface.js +48 -0
- package/dist/types.d.ts +82 -0
- package/dist/types.js +1 -0
- package/package.json +32 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kovca
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,96 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kovca/glyph-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A headless React renderer for [Kovca Glyph](https://kovca.io/docs/rendering) UI. You supply a React component for each Glyph component name; this package validates the model's response, walks the component tree, resolves bindings and actions, and keeps the user's edits.
|
|
4
|
+
|
|
5
|
+
Want styled components out of the box? Install the shadcn/ui set into your app. It's built on this package, and its source is yours to change:
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npx shadcn@latest add https://kovca.io/r/glyph-shadcn.json
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
It needs a Radix shadcn style in `components.json` (such as `radix-nova`). Base UI styles (`base-*`) aren't supported yet.
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { GlyphSurface } from '@/components/glyph/glyph-surface'
|
|
15
|
+
|
|
16
|
+
<GlyphSurface request={request} response={response} onAction={handleAction} />
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install @kovca/glyph-react
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Requires React 18.2 or later. The components are client components (`'use client'`).
|
|
26
|
+
|
|
27
|
+
## Render a response
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
import { GlyphSurface, type GlyphComponents } from '@kovca/glyph-react'
|
|
31
|
+
|
|
32
|
+
const components: GlyphComponents = {
|
|
33
|
+
Card: (p) => <section aria-label={p.label}>{p.children()}</section>,
|
|
34
|
+
Text: (p) => <p>{p.text('text')}</p>,
|
|
35
|
+
Button: (p) => {
|
|
36
|
+
const action = p.action()
|
|
37
|
+
return <button disabled={action.disabled} onClick={action.invoke}>{p.children()}</button>
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function Answer({ request, response }: { request: unknown; response: unknown }) {
|
|
42
|
+
return (
|
|
43
|
+
<GlyphSurface
|
|
44
|
+
request={request}
|
|
45
|
+
response={response}
|
|
46
|
+
components={components}
|
|
47
|
+
onAction={(event) => console.log(event.name, event.context)}
|
|
48
|
+
renderRefusal={(reason) => <p role="status">{reason}</p>}
|
|
49
|
+
renderError={({ code }) => <p role="alert">This UI can't be shown ({code}).</p>}
|
|
50
|
+
/>
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
`GlyphSurface` validates the pair first (with `@kovca/glyph`). A response that uses a component missing from `components`, or that fails validation, renders `renderError` and nothing else. A component that throws while rendering does the same, with the code `render_rejected`.
|
|
56
|
+
|
|
57
|
+
For `glyph-1-oui`, compile the program first: `response = fromOpenUI(result.openui, result.request)` from `@kovca/glyph`.
|
|
58
|
+
|
|
59
|
+
### `GlyphSurface` props
|
|
60
|
+
|
|
61
|
+
| Prop | |
|
|
62
|
+
| --- | --- |
|
|
63
|
+
| `request`, `response` | The request you sent and the model's response. Untrusted input is fine. |
|
|
64
|
+
| `components` | A component for every Glyph component name you render. |
|
|
65
|
+
| `onAction(event)` | A user triggered an action: `{ name, context, sourceComponentId }`. Your app decides what it does. |
|
|
66
|
+
| `onValuesChange(values)` | The data after each accepted edit. Edits are kept inside the surface until `request` changes. |
|
|
67
|
+
| `renderRefusal(reason)` | The model declined to build a UI. Default: nothing. |
|
|
68
|
+
| `renderError({ code, message })` | The response can't be shown. Default: nothing. |
|
|
69
|
+
|
|
70
|
+
`GlyphRenderer` is the lower-level renderer underneath: it takes an already validated pair, throws on anything invalid, and reports edits as `onDataChange(path, value)` without keeping them.
|
|
71
|
+
|
|
72
|
+
## Write a component
|
|
73
|
+
|
|
74
|
+
Every component receives `GlyphComponentProps`:
|
|
75
|
+
|
|
76
|
+
| Prop | |
|
|
77
|
+
| --- | --- |
|
|
78
|
+
| `node`, `nodeKey` | The raw component node and a stable React key. |
|
|
79
|
+
| `label`, `aria` | An accessible name for the node, and ready-made `aria-*` attributes. |
|
|
80
|
+
| `has(prop)` | Whether the node sets `prop`. |
|
|
81
|
+
| `text(prop)` | A prop as display text, with bindings resolved and formatted. |
|
|
82
|
+
| `value(prop)` | A prop's raw value. |
|
|
83
|
+
| `uri(prop)` | A safe `https` media URL, or `undefined`. |
|
|
84
|
+
| `children(prop = 'children')` | The rendered child components. |
|
|
85
|
+
| `items(prop = 'children')` | The children as `{ key, id, name, node, text, value, render }`, for components that wrap each child (tabs, lists, tables). |
|
|
86
|
+
| `input(prop = 'value')` | The bound field: `{ path, value, field, change }`. Call `change(next)` when the user edits it. |
|
|
87
|
+
| `action(prop = 'action')` | `{ name, disabled, invoke, event }`. Call `invoke()` to send it to `onAction`. |
|
|
88
|
+
| `choices(prop = 'options')` | Option children as `{ id, label, value }`. |
|
|
89
|
+
|
|
90
|
+
Overlays (dialogs, sheets) can wrap their content in `GlyphOverlayProvider value={{ close }}` so a component inside can call `useGlyphOverlay()?.close()` after it fires its action.
|
|
91
|
+
|
|
92
|
+
See [Build your own renderer](https://kovca.io/docs/rendering/custom) for the full guide.
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { GlyphRenderer } from './renderer.tsx';
|
|
2
|
+
export type { GlyphRendererProps } from './renderer.tsx';
|
|
3
|
+
export { GlyphSurface } from './surface.tsx';
|
|
4
|
+
export type { GlyphSurfaceError, GlyphSurfaceProps } from './surface.tsx';
|
|
5
|
+
export { GlyphOverlayProvider, useGlyphOverlay } from './overlay.tsx';
|
|
6
|
+
export type { GlyphOverlay } from './overlay.tsx';
|
|
7
|
+
export type { GlyphAction, GlyphChoice, GlyphComponent, GlyphComponentProps, GlyphComponents, GlyphInput, GlyphItem, } from './types.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
/** Lets a component inside an overlay (Dialog, Sheet, ...) close it, e.g. a Button after it fires its action. */
|
|
3
|
+
export type GlyphOverlay = {
|
|
4
|
+
close: () => void;
|
|
5
|
+
};
|
|
6
|
+
/** Provide this around an overlay's content so `useGlyphOverlay()` can close it. */
|
|
7
|
+
export declare function GlyphOverlayProvider({ value, children }: {
|
|
8
|
+
value: GlyphOverlay;
|
|
9
|
+
children: ReactNode;
|
|
10
|
+
}): import("react").JSX.Element;
|
|
11
|
+
/** The enclosing overlay, or null outside one. */
|
|
12
|
+
export declare function useGlyphOverlay(): GlyphOverlay | null;
|
package/dist/overlay.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useContext } from 'react';
|
|
4
|
+
const OverlayContext = createContext(null);
|
|
5
|
+
/** Provide this around an overlay's content so `useGlyphOverlay()` can close it. */
|
|
6
|
+
export function GlyphOverlayProvider({ value, children }) {
|
|
7
|
+
return _jsx(OverlayContext.Provider, { value: value, children: children });
|
|
8
|
+
}
|
|
9
|
+
/** The enclosing overlay, or null outside one. */
|
|
10
|
+
export function useGlyphOverlay() {
|
|
11
|
+
return useContext(OverlayContext);
|
|
12
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ActionEvent, GlyphRequest, GlyphResponse } from '@kovca/glyph';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
import type { GlyphComponents } from './types.ts';
|
|
4
|
+
export type GlyphRendererProps = {
|
|
5
|
+
/** A validated-or-not Glyph 0.2.0 request; it is validated again here. */
|
|
6
|
+
request: GlyphRequest;
|
|
7
|
+
response: GlyphResponse;
|
|
8
|
+
/** Implementations by catalog component name. */
|
|
9
|
+
components: GlyphComponents;
|
|
10
|
+
/** Receives authorized action events. Glyph never runs actions itself. */
|
|
11
|
+
onAction?: (event: ActionEvent) => void;
|
|
12
|
+
/** Receives edits to writable fields (absolute data path and the control's value). */
|
|
13
|
+
onDataChange?: (path: string, value: unknown) => void;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Renders a Glyph pair with your components. Validates first and throws (`ContractError`, `RenderError`) instead of
|
|
17
|
+
* drawing anything unsafe, so wrap it in an error boundary, or use `GlyphSurface`, which does that for you.
|
|
18
|
+
*/
|
|
19
|
+
export declare function GlyphRenderer({ request, response, components, onAction, onDataChange }: GlyphRendererProps): ReactNode;
|
package/dist/renderer.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { fail, resolveAction, resolvePath, resolveValue, textOf, uriOf, validatePair } from '@kovca/glyph';
|
|
4
|
+
function lookup(engine, id) {
|
|
5
|
+
return engine.nodes.get(id) ?? fail(`missing component reference ${id}`);
|
|
6
|
+
}
|
|
7
|
+
function slotted(engine, value, scope) {
|
|
8
|
+
if (typeof value === 'string')
|
|
9
|
+
return [{ node: lookup(engine, value), scope, key: value }];
|
|
10
|
+
if (Array.isArray(value))
|
|
11
|
+
return value.map((id) => ({ node: lookup(engine, id), scope, key: id }));
|
|
12
|
+
if (!value || typeof value !== 'object' || !('path' in value) || !('componentId' in value))
|
|
13
|
+
return [];
|
|
14
|
+
const { path, componentId } = value;
|
|
15
|
+
const schema = engine.request.data.schema;
|
|
16
|
+
const list = resolvePath(path, engine.data, schema, scope);
|
|
17
|
+
if (list.field.type !== 'list')
|
|
18
|
+
fail('template path must be a list');
|
|
19
|
+
if (list.value === undefined)
|
|
20
|
+
return [];
|
|
21
|
+
if (!Array.isArray(list.value))
|
|
22
|
+
fail('template value must be a list');
|
|
23
|
+
const item = list.field.item;
|
|
24
|
+
const node = lookup(engine, componentId);
|
|
25
|
+
return list.value.map((value, index) => {
|
|
26
|
+
const itemPath = `${list.path}/${index}`;
|
|
27
|
+
return { node, scope: { value, field: item, path: itemPath }, key: itemPath };
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function element(engine, entry, parent, slot, inheritedLabel) {
|
|
31
|
+
const { node, scope, key } = entry;
|
|
32
|
+
const name = node.component;
|
|
33
|
+
const catalog = engine.request.catalog.components;
|
|
34
|
+
const definition = Object.hasOwn(catalog, name) ? catalog[name] : fail(`component ${name} is not in the catalog`);
|
|
35
|
+
if (definition.allowedParents && !definition.allowedParents.includes(parent))
|
|
36
|
+
fail(`unallowed parent for ${name}`);
|
|
37
|
+
if (parent !== 'Surface' && catalog[parent]?.allowedChildren && !catalog[parent].allowedChildren.includes(name)) {
|
|
38
|
+
fail(`unallowed child ${name}`);
|
|
39
|
+
}
|
|
40
|
+
const Component = Object.hasOwn(engine.components, name)
|
|
41
|
+
? engine.components[name]
|
|
42
|
+
: fail(`component ${name} is not registered`);
|
|
43
|
+
return _jsx(Component, { ...nodeProps(engine, node, scope, key, parent, slot, inheritedLabel) }, key);
|
|
44
|
+
}
|
|
45
|
+
function nodeProps(engine, node, scope, nodeKey, parent, slot, inheritedLabel) {
|
|
46
|
+
const { request, data } = engine;
|
|
47
|
+
const schema = request.data.schema;
|
|
48
|
+
const name = node.component;
|
|
49
|
+
const text = (prop) => textOf(node[prop], data, schema, scope);
|
|
50
|
+
const value = (prop) => {
|
|
51
|
+
const resolved = resolveValue(node[prop], data, schema, scope);
|
|
52
|
+
return resolved ? resolved.value : node[prop];
|
|
53
|
+
};
|
|
54
|
+
const items = (prop = 'children', label) => slotted(engine, node[prop], scope).map((entry) => ({
|
|
55
|
+
key: entry.key,
|
|
56
|
+
id: entry.node.id,
|
|
57
|
+
name: entry.node.component,
|
|
58
|
+
node: entry.node,
|
|
59
|
+
scope: entry.scope,
|
|
60
|
+
text: (field) => textOf(entry.node[field], data, schema, entry.scope),
|
|
61
|
+
value: (field) => {
|
|
62
|
+
const resolved = resolveValue(entry.node[field], data, schema, entry.scope);
|
|
63
|
+
return resolved ? resolved.value : entry.node[field];
|
|
64
|
+
},
|
|
65
|
+
render: () => element(engine, entry, name, prop, inheritedLabel ?? label),
|
|
66
|
+
}));
|
|
67
|
+
const accessibility = node['@accessibility'];
|
|
68
|
+
const aria = {};
|
|
69
|
+
if (accessibility?.label !== undefined)
|
|
70
|
+
aria['aria-label'] = textOf(accessibility.label, data, schema, scope);
|
|
71
|
+
if (accessibility?.description !== undefined) {
|
|
72
|
+
aria['aria-description'] = textOf(accessibility.description, data, schema, scope);
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
node,
|
|
76
|
+
nodeKey,
|
|
77
|
+
parent,
|
|
78
|
+
slot,
|
|
79
|
+
request,
|
|
80
|
+
scope,
|
|
81
|
+
label: text('label') || inheritedLabel || '',
|
|
82
|
+
aria,
|
|
83
|
+
has: (prop) => node[prop] !== undefined,
|
|
84
|
+
text,
|
|
85
|
+
value,
|
|
86
|
+
uri: (prop) => uriOf(node[prop], data, schema, scope),
|
|
87
|
+
children: (prop = 'children', options) => {
|
|
88
|
+
const rendered = items(prop, options?.label).map((item) => item.render());
|
|
89
|
+
return typeof node[prop] === 'string' ? rendered[0] : rendered;
|
|
90
|
+
},
|
|
91
|
+
items: (prop = 'children') => items(prop),
|
|
92
|
+
input: (prop = 'value') => {
|
|
93
|
+
const target = resolveValue(node[prop], data, schema, scope);
|
|
94
|
+
if (!target || target.field.type === 'object')
|
|
95
|
+
fail(`${name}.${prop} needs a data binding`);
|
|
96
|
+
if (!target.field.writable)
|
|
97
|
+
fail(`${name}.${prop} is not bound to a writable field`);
|
|
98
|
+
return { path: target.path, value: target.value, field: target.field, change: (next) => engine.onDataChange(target.path, next) };
|
|
99
|
+
},
|
|
100
|
+
action: (prop = 'action') => {
|
|
101
|
+
const resolved = resolveAction(node, prop, request, data, scope);
|
|
102
|
+
return { ...resolved, invoke: () => { if (!resolved.disabled)
|
|
103
|
+
engine.onAction(resolved.event); } };
|
|
104
|
+
},
|
|
105
|
+
choices: (prop = 'options') => items(prop).map((item) => {
|
|
106
|
+
if (item.name !== 'Option')
|
|
107
|
+
fail('choices must be Option components');
|
|
108
|
+
return { id: item.key, label: item.text('label'), value: item.text('value') };
|
|
109
|
+
}),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Renders a Glyph pair with your components. Validates first and throws (`ContractError`, `RenderError`) instead of
|
|
114
|
+
* drawing anything unsafe, so wrap it in an error boundary, or use `GlyphSurface`, which does that for you.
|
|
115
|
+
*/
|
|
116
|
+
export function GlyphRenderer({ request, response, components, onAction, onDataChange }) {
|
|
117
|
+
validatePair(request, response);
|
|
118
|
+
if (!('components' in response.output))
|
|
119
|
+
fail('a refusal has no renderable surface');
|
|
120
|
+
for (const node of response.output.components) {
|
|
121
|
+
if (!Object.hasOwn(components, node.component))
|
|
122
|
+
fail(`component ${node.component} is not registered`);
|
|
123
|
+
}
|
|
124
|
+
const engine = {
|
|
125
|
+
request,
|
|
126
|
+
nodes: new Map(response.output.components.map((node) => [node.id, node])),
|
|
127
|
+
data: request.data.values ?? {},
|
|
128
|
+
components,
|
|
129
|
+
onAction: onAction ?? (() => undefined),
|
|
130
|
+
onDataChange: onDataChange ?? (() => undefined),
|
|
131
|
+
};
|
|
132
|
+
return element(engine, { node: lookup(engine, 'root'), key: 'root' }, 'Surface', 'root');
|
|
133
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ActionEvent } from '@kovca/glyph';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
import type { GlyphComponents } from './types.ts';
|
|
4
|
+
/** Why nothing was rendered: a validation code from `validateGlyphPair`, or `render_rejected`. */
|
|
5
|
+
export type GlyphSurfaceError = {
|
|
6
|
+
code: string;
|
|
7
|
+
message: string;
|
|
8
|
+
};
|
|
9
|
+
export type GlyphSurfaceProps = {
|
|
10
|
+
/** The Glyph 0.2.0 request, untrusted. */
|
|
11
|
+
request: unknown;
|
|
12
|
+
/** The model's response for it, untrusted. */
|
|
13
|
+
response: unknown;
|
|
14
|
+
components: GlyphComponents;
|
|
15
|
+
/** Receives authorized action events. Authorize and run them on your side. */
|
|
16
|
+
onAction?: (event: ActionEvent) => void;
|
|
17
|
+
/** Called with the full data values after each accepted edit. */
|
|
18
|
+
onValuesChange?: (values: Record<string, unknown>) => void;
|
|
19
|
+
renderRefusal?: (reason: string) => ReactNode;
|
|
20
|
+
renderError?: (error: GlyphSurfaceError) => ReactNode;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Validates an untrusted pair and renders it with your components. Shows a refusal or an error instead of anything
|
|
24
|
+
* invalid, and never throws. Edits to writable fields are kept locally and validated before they apply.
|
|
25
|
+
*/
|
|
26
|
+
export declare function GlyphSurface({ request, response, components, onAction, onValuesChange, renderRefusal, renderError, }: GlyphSurfaceProps): string | number | bigint | boolean | import("react").JSX.Element | Iterable<ReactNode> | Promise<string | number | bigint | boolean | Iterable<ReactNode> | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | import("react").ReactPortal | null | undefined> | null | undefined;
|
package/dist/surface.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { applyEdit, validateGlyphPair } from '@kovca/glyph';
|
|
4
|
+
import { Component, useMemo, useState } from 'react';
|
|
5
|
+
import { GlyphRenderer } from './renderer.js';
|
|
6
|
+
const defaultRefusal = (reason) => _jsxs("p", { role: "status", children: ["The model refused: ", reason] });
|
|
7
|
+
const defaultError = ({ code }) => _jsxs("div", { role: "alert", children: ["This UI can't be shown. The response didn't pass validation (", code, ")."] });
|
|
8
|
+
/**
|
|
9
|
+
* Validates an untrusted pair and renders it with your components. Shows a refusal or an error instead of anything
|
|
10
|
+
* invalid, and never throws. Edits to writable fields are kept locally and validated before they apply.
|
|
11
|
+
*/
|
|
12
|
+
export function GlyphSurface({ request, response, components, onAction, onValuesChange, renderRefusal = defaultRefusal, renderError = defaultError, }) {
|
|
13
|
+
const result = useMemo(() => validateGlyphPair(request, response, { components: Object.keys(components) }), [request, response, components]);
|
|
14
|
+
if (!result.ok)
|
|
15
|
+
return renderError({ code: result.code, message: result.message });
|
|
16
|
+
if (result.kind === 'refusal')
|
|
17
|
+
return renderRefusal(result.reason);
|
|
18
|
+
return _jsx(RenderBoundary, { pair: result.response, renderError: renderError, children: _jsx(EditableSurface, { request: result.request, response: result.response, components: components, onAction: onAction, onValuesChange: onValuesChange }) });
|
|
19
|
+
}
|
|
20
|
+
class RenderBoundary extends Component {
|
|
21
|
+
state = { pair: this.props.pair };
|
|
22
|
+
static getDerivedStateFromProps(props, state) {
|
|
23
|
+
return props.pair === state.pair ? null : { pair: props.pair, error: undefined };
|
|
24
|
+
}
|
|
25
|
+
static getDerivedStateFromError(error) {
|
|
26
|
+
return { error: { code: 'render_rejected', message: error instanceof Error ? error.message : 'render failed' } };
|
|
27
|
+
}
|
|
28
|
+
render() {
|
|
29
|
+
return this.state.error ? this.props.renderError(this.state.error) : this.props.children;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function EditableSurface({ request, response, components, onAction, onValuesChange }) {
|
|
33
|
+
const [source, setSource] = useState(request);
|
|
34
|
+
const [values, setValues] = useState(request.data.values);
|
|
35
|
+
if (source !== request) {
|
|
36
|
+
setSource(request);
|
|
37
|
+
setValues(request.data.values);
|
|
38
|
+
}
|
|
39
|
+
const current = useMemo(() => ({ ...request, data: { ...request.data, values } }), [request, values]);
|
|
40
|
+
function change(path, value) {
|
|
41
|
+
const next = applyEdit(request, response, values, path, value);
|
|
42
|
+
if (!next)
|
|
43
|
+
return;
|
|
44
|
+
setValues(next);
|
|
45
|
+
onValuesChange?.(next);
|
|
46
|
+
}
|
|
47
|
+
return _jsx(GlyphRenderer, { request: current, response: response, components: components, onAction: onAction, onDataChange: change });
|
|
48
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { ActionEvent, ComponentNode, Field, GlyphRequest, Scope } from '@kovca/glyph';
|
|
3
|
+
/** A child in a slot, before it is rendered: use it when a component lays out each child itself. */
|
|
4
|
+
export type GlyphItem = {
|
|
5
|
+
/** Stable React key (the component ID, or its list path inside a template). */
|
|
6
|
+
key: string;
|
|
7
|
+
id: string;
|
|
8
|
+
/** The child's catalog component name. */
|
|
9
|
+
name: string;
|
|
10
|
+
node: ComponentNode;
|
|
11
|
+
scope?: Scope;
|
|
12
|
+
text: (prop: string) => string;
|
|
13
|
+
value: (prop: string) => unknown;
|
|
14
|
+
render: () => ReactNode;
|
|
15
|
+
};
|
|
16
|
+
/** A writable field bound to a control. Call `change` with the user's value; invalid edits are ignored. */
|
|
17
|
+
export type GlyphInput = {
|
|
18
|
+
path: string;
|
|
19
|
+
value: unknown;
|
|
20
|
+
field: Field;
|
|
21
|
+
change: (value: unknown) => void;
|
|
22
|
+
};
|
|
23
|
+
/** An authorized action. `invoke` hands the event to the host's `onAction`; it does nothing when disabled. */
|
|
24
|
+
export type GlyphAction = {
|
|
25
|
+
name: string;
|
|
26
|
+
disabled: boolean;
|
|
27
|
+
invoke: () => void;
|
|
28
|
+
event: ActionEvent;
|
|
29
|
+
};
|
|
30
|
+
/** An `Option` child of a choice control. */
|
|
31
|
+
export type GlyphChoice = {
|
|
32
|
+
id: string;
|
|
33
|
+
label: string;
|
|
34
|
+
value: string;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* What every component in a Glyph registry receives. Props are read through these helpers, which resolve data
|
|
38
|
+
* bindings and enforce the contract, so a component never touches raw model output.
|
|
39
|
+
*/
|
|
40
|
+
export type GlyphComponentProps = {
|
|
41
|
+
/** The validated component node (`node.component` is its catalog name). */
|
|
42
|
+
node: ComponentNode;
|
|
43
|
+
/** React key of this node; unique within the surface, also inside list templates. */
|
|
44
|
+
nodeKey: string;
|
|
45
|
+
/** The parent's component name (`"Surface"` for the root). */
|
|
46
|
+
parent: string;
|
|
47
|
+
/** The parent prop this node was placed in (`"children"`, `"header"`, `"cells"`, ...; `"root"` for the root). */
|
|
48
|
+
slot: string;
|
|
49
|
+
request: GlyphRequest;
|
|
50
|
+
/** The list item a template is rendering, if any. */
|
|
51
|
+
scope?: Scope;
|
|
52
|
+
/** The node's `label` prop, or the label of an enclosing `Field`. */
|
|
53
|
+
label: string;
|
|
54
|
+
/** `aria-label` / `aria-description` from the node's `@accessibility`. Spread it onto the root element. */
|
|
55
|
+
aria: {
|
|
56
|
+
'aria-label'?: string;
|
|
57
|
+
'aria-description'?: string;
|
|
58
|
+
};
|
|
59
|
+
/** True when the node sets this prop. */
|
|
60
|
+
has: (prop: string) => boolean;
|
|
61
|
+
/** The prop as display text: bindings resolved and formatted by their field's format. */
|
|
62
|
+
text: (prop: string) => string;
|
|
63
|
+
/** The prop's raw value: a binding's data value, or the literal. */
|
|
64
|
+
value: (prop: string) => unknown;
|
|
65
|
+
/** A `uri` prop: an https URL from a `format: "uri"` field, or undefined. */
|
|
66
|
+
uri: (prop: string) => string | undefined;
|
|
67
|
+
/** Renders a `child`/`children` slot. Pass `label` to give descendant controls a default label. */
|
|
68
|
+
children: (prop?: string, options?: {
|
|
69
|
+
label?: string;
|
|
70
|
+
}) => ReactNode;
|
|
71
|
+
/** The children in a slot, unrendered. */
|
|
72
|
+
items: (prop?: string) => GlyphItem[];
|
|
73
|
+
/** The writable field an `input` prop binds to. */
|
|
74
|
+
input: (prop?: string) => GlyphInput;
|
|
75
|
+
/** The authorized action in an `action` prop. */
|
|
76
|
+
action: (prop?: string) => GlyphAction;
|
|
77
|
+
/** The `Option` children of a slot (default `options`). */
|
|
78
|
+
choices: (prop?: string) => GlyphChoice[];
|
|
79
|
+
};
|
|
80
|
+
export type GlyphComponent = (props: GlyphComponentProps) => ReactNode;
|
|
81
|
+
/** Component implementations by catalog name. Responses using a name that isn't here are rejected. */
|
|
82
|
+
export type GlyphComponents = Record<string, GlyphComponent>;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kovca/glyph-react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Headless React renderer for Kovca Glyph UI: bring your own components, or install the shadcn/ui set.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/CMoyle336/Glyph.git",
|
|
11
|
+
"directory": "packages/glyph-react"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://kovca.io/docs/rendering",
|
|
14
|
+
"keywords": ["glyph", "kovca", "react", "a2ui", "openui", "generative-ui", "renderer", "shadcn"],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"files": ["dist", "LICENSE", "README.md"],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc -p tsconfig.build.json",
|
|
25
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
26
|
+
"test": "vitest run"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@kovca/glyph": "^0.1.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": "^18.2.0 || ^19.0.0"
|
|
33
|
+
}
|
|
34
|
+
}
|