@surfaice/react 0.0.1
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 +52 -0
- package/dist/components.d.ts +40 -0
- package/dist/components.js +43 -0
- package/dist/context.d.ts +9 -0
- package/dist/context.js +8 -0
- package/dist/hooks.d.ts +7 -0
- package/dist/hooks.js +11 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/provider.d.ts +7 -0
- package/dist/provider.js +30 -0
- package/dist/registry.d.ts +21 -0
- package/dist/registry.js +66 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 surfaiceai
|
|
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
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# @surfaice/react
|
|
2
|
+
|
|
3
|
+
React annotation components for Surfaice — describe your UI once, agents navigate it, CI verifies it.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @surfaice/react @surfaice/format
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { SurfaiceProvider, ui } from '@surfaice/react'
|
|
15
|
+
|
|
16
|
+
// Wrap your app
|
|
17
|
+
<SurfaiceProvider enabled={process.env.SURFAICE_ENABLED === 'true'}>
|
|
18
|
+
<App />
|
|
19
|
+
</SurfaiceProvider>
|
|
20
|
+
|
|
21
|
+
// Annotate your pages
|
|
22
|
+
<ui.page route="/settings" name="Settings Page" states={['auth-required']}>
|
|
23
|
+
<ui.section name="Profile">
|
|
24
|
+
<ui.element id="name" type="textbox" label="Display Name"
|
|
25
|
+
value={user.name}
|
|
26
|
+
current="{user.name}"
|
|
27
|
+
action="PUT /api/profile">
|
|
28
|
+
<input value={user.name} onChange={handleChange} />
|
|
29
|
+
</ui.element>
|
|
30
|
+
<ui.element id="save" type="button" label="Save Changes"
|
|
31
|
+
action="PUT /api/profile"
|
|
32
|
+
result="toast 'Saved!'">
|
|
33
|
+
<button onClick={handleSave}>Save</button>
|
|
34
|
+
</ui.element>
|
|
35
|
+
</ui.section>
|
|
36
|
+
</ui.page>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## API
|
|
40
|
+
|
|
41
|
+
### `<SurfaiceProvider enabled? />`
|
|
42
|
+
Context provider. Set `enabled={true}` to collect annotations. Zero overhead when disabled.
|
|
43
|
+
|
|
44
|
+
### `ui.page` / `ui.section` / `ui.element`
|
|
45
|
+
Annotation components — always render children as-is, optionally register metadata.
|
|
46
|
+
|
|
47
|
+
### `useSurfaicePage(): SurfaicePage | null`
|
|
48
|
+
Hook to access the collected page AST. Use in middleware or dev tools.
|
|
49
|
+
|
|
50
|
+
## Zero Overhead When Disabled
|
|
51
|
+
|
|
52
|
+
All annotation components are pure passthroughs when `enabled={false}` (the default). Ship to production without worry.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { ElementType, Capability } from '@surfaice/format';
|
|
3
|
+
export interface UIPageProps {
|
|
4
|
+
route: string;
|
|
5
|
+
name?: string;
|
|
6
|
+
states?: string[];
|
|
7
|
+
capabilities?: Capability[];
|
|
8
|
+
children: React.ReactNode;
|
|
9
|
+
}
|
|
10
|
+
declare function UIPage({ route, name, states, capabilities, children }: UIPageProps): import("react/jsx-runtime").JSX.Element;
|
|
11
|
+
export interface UISectionProps {
|
|
12
|
+
name: string;
|
|
13
|
+
children: React.ReactNode;
|
|
14
|
+
}
|
|
15
|
+
declare function UISection({ name, children }: UISectionProps): import("react/jsx-runtime").JSX.Element;
|
|
16
|
+
export interface UIElementProps {
|
|
17
|
+
id: string;
|
|
18
|
+
type: ElementType;
|
|
19
|
+
label: string;
|
|
20
|
+
attributes?: string[];
|
|
21
|
+
action?: string;
|
|
22
|
+
result?: string;
|
|
23
|
+
navigates?: string;
|
|
24
|
+
/** Live runtime value — injected into agent-facing markdown */
|
|
25
|
+
value?: string | number | boolean;
|
|
26
|
+
/** Template binding for static export, e.g. "{user.name}" */
|
|
27
|
+
current?: string;
|
|
28
|
+
accepts?: string;
|
|
29
|
+
options?: string[];
|
|
30
|
+
/** Value displayed by display elements at runtime */
|
|
31
|
+
shows?: string | number;
|
|
32
|
+
children: React.ReactNode;
|
|
33
|
+
}
|
|
34
|
+
declare function UIElement({ children, value, shows, ...props }: UIElementProps): import("react/jsx-runtime").JSX.Element;
|
|
35
|
+
export declare const ui: {
|
|
36
|
+
page: typeof UIPage;
|
|
37
|
+
section: typeof UISection;
|
|
38
|
+
element: typeof UIElement;
|
|
39
|
+
};
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useContext, useEffect, createContext } from 'react';
|
|
3
|
+
import { SurfaiceContext } from './context.js';
|
|
4
|
+
// Inner context to pass section name down to ui.element
|
|
5
|
+
const SectionNameContext = createContext(null);
|
|
6
|
+
function UIPage({ route, name, states, capabilities, children }) {
|
|
7
|
+
const ctx = useContext(SurfaiceContext);
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
if (ctx.enabled) {
|
|
10
|
+
ctx.registerPage({ version: 'v1', route, name, states, capabilities });
|
|
11
|
+
}
|
|
12
|
+
}, [ctx.enabled, route, name]);
|
|
13
|
+
return _jsx(_Fragment, { children: children });
|
|
14
|
+
}
|
|
15
|
+
function UISection({ name, children }) {
|
|
16
|
+
const ctx = useContext(SurfaiceContext);
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (ctx.enabled) {
|
|
19
|
+
ctx.registerSection({ name });
|
|
20
|
+
}
|
|
21
|
+
}, [ctx.enabled, name]);
|
|
22
|
+
return (_jsx(SectionNameContext.Provider, { value: name, children: children }));
|
|
23
|
+
}
|
|
24
|
+
function UIElement({ children, value, shows, ...props }) {
|
|
25
|
+
const ctx = useContext(SurfaiceContext);
|
|
26
|
+
const sectionName = useContext(SectionNameContext);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (ctx.enabled) {
|
|
29
|
+
ctx.registerElement({
|
|
30
|
+
...props,
|
|
31
|
+
value: value !== undefined ? String(value) : props.current,
|
|
32
|
+
shows: shows !== undefined ? String(shows) : undefined,
|
|
33
|
+
}, sectionName ?? undefined);
|
|
34
|
+
}
|
|
35
|
+
}, [ctx.enabled, value, shows, props.id, sectionName]);
|
|
36
|
+
return _jsx(_Fragment, { children: children });
|
|
37
|
+
}
|
|
38
|
+
// ── ui namespace export ───────────────────────────────────────────────────────
|
|
39
|
+
export const ui = {
|
|
40
|
+
page: UIPage,
|
|
41
|
+
section: UISection,
|
|
42
|
+
element: UIElement,
|
|
43
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { SurfaicePage, Section, Element } from '@surfaice/format';
|
|
2
|
+
export interface SurfaiceContextValue {
|
|
3
|
+
enabled: boolean;
|
|
4
|
+
page: SurfaicePage | null;
|
|
5
|
+
registerPage: (page: Partial<SurfaicePage>) => void;
|
|
6
|
+
registerSection: (section: Partial<Section>) => void;
|
|
7
|
+
registerElement: (element: Partial<Element>, sectionName?: string) => void;
|
|
8
|
+
}
|
|
9
|
+
export declare const SurfaiceContext: import("react").Context<SurfaiceContextValue>;
|
package/dist/context.js
ADDED
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SurfaicePage } from '@surfaice/format';
|
|
2
|
+
/**
|
|
3
|
+
* Hook to access the current collected SurfaicePage AST.
|
|
4
|
+
* Reactive — re-renders when annotations change.
|
|
5
|
+
* Returns null when provider is disabled or no page has been registered.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useSurfaicePage(): SurfaicePage | null;
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { SurfaiceContext } from './context.js';
|
|
3
|
+
/**
|
|
4
|
+
* Hook to access the current collected SurfaicePage AST.
|
|
5
|
+
* Reactive — re-renders when annotations change.
|
|
6
|
+
* Returns null when provider is disabled or no page has been registered.
|
|
7
|
+
*/
|
|
8
|
+
export function useSurfaicePage() {
|
|
9
|
+
const ctx = useContext(SurfaiceContext);
|
|
10
|
+
return ctx.page;
|
|
11
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { SurfaiceProvider } from './provider.js';
|
|
2
|
+
export { ui } from './components.js';
|
|
3
|
+
export { useSurfaicePage } from './hooks.js';
|
|
4
|
+
export type { SurfaiceProviderProps } from './provider.js';
|
|
5
|
+
export type { UIPageProps, UISectionProps, UIElementProps } from './components.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export interface SurfaiceProviderProps {
|
|
3
|
+
/** Enable Surfaice annotation collection. Default: false (zero overhead in production) */
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
}
|
|
7
|
+
export declare function SurfaiceProvider({ enabled, children }: SurfaiceProviderProps): import("react/jsx-runtime").JSX.Element;
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useRef, useState, useCallback } from 'react';
|
|
3
|
+
import { SurfaiceContext } from './context.js';
|
|
4
|
+
import { SurfaiceRegistry } from './registry.js';
|
|
5
|
+
export function SurfaiceProvider({ enabled = false, children }) {
|
|
6
|
+
const registryRef = useRef(new SurfaiceRegistry());
|
|
7
|
+
const [page, setPage] = useState(null);
|
|
8
|
+
const syncPage = useCallback(() => {
|
|
9
|
+
setPage(registryRef.current.toSurfaicePage());
|
|
10
|
+
}, []);
|
|
11
|
+
const registerPage = useCallback((p) => {
|
|
12
|
+
registryRef.current.setPage(p);
|
|
13
|
+
syncPage();
|
|
14
|
+
}, [syncPage]);
|
|
15
|
+
const registerSection = useCallback((s) => {
|
|
16
|
+
registryRef.current.addSection(s);
|
|
17
|
+
syncPage();
|
|
18
|
+
}, [syncPage]);
|
|
19
|
+
const registerElement = useCallback((e, sectionName) => {
|
|
20
|
+
registryRef.current.addElement(e, sectionName);
|
|
21
|
+
syncPage();
|
|
22
|
+
}, [syncPage]);
|
|
23
|
+
return (_jsx(SurfaiceContext.Provider, { value: {
|
|
24
|
+
enabled,
|
|
25
|
+
page: enabled ? page : null,
|
|
26
|
+
registerPage,
|
|
27
|
+
registerSection,
|
|
28
|
+
registerElement,
|
|
29
|
+
}, children: children }));
|
|
30
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { SurfaicePage, Section, Element } from '@surfaice/format';
|
|
2
|
+
/**
|
|
3
|
+
* SurfaiceRegistry collects UI annotations at render time.
|
|
4
|
+
*
|
|
5
|
+
* React fires useEffect bottom-up (children before parents), so registration
|
|
6
|
+
* order is: elements → sections → page. We buffer elements with their target
|
|
7
|
+
* section name and reconcile when producing the final AST.
|
|
8
|
+
*/
|
|
9
|
+
export declare class SurfaiceRegistry {
|
|
10
|
+
private _meta;
|
|
11
|
+
private _sectionNames;
|
|
12
|
+
private _pendingElements;
|
|
13
|
+
private _currentSectionName;
|
|
14
|
+
setPage(page: Partial<SurfaicePage>): void;
|
|
15
|
+
addSection(section: Partial<Section>): void;
|
|
16
|
+
/** Register an element, tagged to the current section name */
|
|
17
|
+
addElement(element: Partial<Element>, sectionName?: string): void;
|
|
18
|
+
toSurfaicePage(): SurfaicePage | null;
|
|
19
|
+
get page(): SurfaicePage | null;
|
|
20
|
+
reset(): void;
|
|
21
|
+
}
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SurfaiceRegistry collects UI annotations at render time.
|
|
3
|
+
*
|
|
4
|
+
* React fires useEffect bottom-up (children before parents), so registration
|
|
5
|
+
* order is: elements → sections → page. We buffer elements with their target
|
|
6
|
+
* section name and reconcile when producing the final AST.
|
|
7
|
+
*/
|
|
8
|
+
export class SurfaiceRegistry {
|
|
9
|
+
_meta = null;
|
|
10
|
+
_sectionNames = [];
|
|
11
|
+
_pendingElements = [];
|
|
12
|
+
// Track which section name the most recently registered section belongs to,
|
|
13
|
+
// so elements registered before their section still land correctly.
|
|
14
|
+
_currentSectionName = null;
|
|
15
|
+
setPage(page) {
|
|
16
|
+
this._meta = { ...page };
|
|
17
|
+
}
|
|
18
|
+
addSection(section) {
|
|
19
|
+
const name = section.name ?? 'Unnamed Section';
|
|
20
|
+
if (!this._sectionNames.includes(name)) {
|
|
21
|
+
this._sectionNames.push(name);
|
|
22
|
+
}
|
|
23
|
+
this._currentSectionName = name;
|
|
24
|
+
}
|
|
25
|
+
/** Register an element, tagged to the current section name */
|
|
26
|
+
addElement(element, sectionName) {
|
|
27
|
+
const target = sectionName ?? this._currentSectionName;
|
|
28
|
+
if (!target)
|
|
29
|
+
return;
|
|
30
|
+
const el = {
|
|
31
|
+
id: element.id ?? '',
|
|
32
|
+
type: element.type ?? 'text',
|
|
33
|
+
label: element.label ?? '',
|
|
34
|
+
...element,
|
|
35
|
+
};
|
|
36
|
+
this._pendingElements.push({ sectionName: target, element: el });
|
|
37
|
+
}
|
|
38
|
+
toSurfaicePage() {
|
|
39
|
+
if (!this._meta)
|
|
40
|
+
return null;
|
|
41
|
+
// Build sections, attaching elements
|
|
42
|
+
const sections = this._sectionNames.map(name => ({
|
|
43
|
+
name,
|
|
44
|
+
elements: this._pendingElements
|
|
45
|
+
.filter(p => p.sectionName === name)
|
|
46
|
+
.map(p => p.element),
|
|
47
|
+
}));
|
|
48
|
+
return {
|
|
49
|
+
version: this._meta.version ?? 'v1',
|
|
50
|
+
route: this._meta.route ?? '/',
|
|
51
|
+
...(this._meta.name !== undefined && { name: this._meta.name }),
|
|
52
|
+
...(this._meta.states !== undefined && { states: this._meta.states }),
|
|
53
|
+
...(this._meta.capabilities !== undefined && { capabilities: this._meta.capabilities }),
|
|
54
|
+
sections,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
get page() {
|
|
58
|
+
return this.toSurfaicePage();
|
|
59
|
+
}
|
|
60
|
+
reset() {
|
|
61
|
+
this._meta = null;
|
|
62
|
+
this._sectionNames = [];
|
|
63
|
+
this._pendingElements = [];
|
|
64
|
+
this._currentSectionName = null;
|
|
65
|
+
}
|
|
66
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@surfaice/react",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "React annotations for Surfaice — describe your UI once, agents navigate it",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/surfaiceai/surfaice",
|
|
11
|
+
"directory": "packages/react"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@surfaice/format": "0.0.1"
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"react": ">=18.0.0",
|
|
18
|
+
"react-dom": ">=18.0.0"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@testing-library/react": "^16.0.0",
|
|
22
|
+
"@types/react": "^18.0.0",
|
|
23
|
+
"@types/react-dom": "^18.0.0",
|
|
24
|
+
"react": "^18.0.0",
|
|
25
|
+
"react-dom": "^18.0.0",
|
|
26
|
+
"vitest": "^2.0.0",
|
|
27
|
+
"typescript": "^5.5.0",
|
|
28
|
+
"@vitejs/plugin-react": "^4.0.0",
|
|
29
|
+
"jsdom": "^25.0.0",
|
|
30
|
+
"@testing-library/jest-dom": "^6.0.0"
|
|
31
|
+
},
|
|
32
|
+
"type": "module",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"import": "./dist/index.js",
|
|
36
|
+
"types": "./dist/index.d.ts"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"README.md"
|
|
42
|
+
],
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public",
|
|
45
|
+
"registry": "https://registry.npmjs.org"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsc",
|
|
49
|
+
"test": "vitest run",
|
|
50
|
+
"dev": "tsc --watch"
|
|
51
|
+
}
|
|
52
|
+
}
|