@jboltai/tokui-react 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 jboltai.com
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,56 @@
1
+ # @jboltai/tokui-react
2
+
3
+ > TokUI 的 React 适配器:`<TokUIView>` 组件 + `useTokUIStream()` 流式 hook。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @jboltai/tokui-react react
9
+ ```
10
+
11
+ ## 声明式渲染
12
+
13
+ ```jsx
14
+ import { TokUIView } from '@jboltai/tokui-react';
15
+
16
+ export function App() {
17
+ return (
18
+ <TokUIView
19
+ dsl="[card tt:你好 TokUI][p 流式 UI,零依赖][/card]"
20
+ theme="default"
21
+ />
22
+ );
23
+ }
24
+ ```
25
+
26
+ `dsl` 变化时自动重新渲染;组件卸载时自动断开清理。
27
+
28
+ ## 流式 / SSE
29
+
30
+ ```jsx
31
+ import { useTokUIStream } from '@jboltai/tokui-react';
32
+
33
+ export function Chat() {
34
+ const { ref, start, feed, end, connect } = useTokUIStream();
35
+ return (
36
+ <>
37
+ <div ref={ref} />
38
+ <button onClick={() => {
39
+ start();
40
+ feed('[card tt:');
41
+ feed('流式卡片]');
42
+ end();
43
+ }}>渲染</button>
44
+ <button onClick={() => connect('/api/chat', { prompt: '画登录卡片' })}>
45
+ SSE 连接
46
+ </button>
47
+ </>
48
+ );
49
+ }
50
+ ```
51
+
52
+ ## CSS
53
+
54
+ 样式随 `@jboltai/tokui` 自动注入,无需手动引 CSS。
55
+
56
+ License: MIT
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@jboltai/tokui-react",
3
+ "version": "0.1.0",
4
+ "description": "TokUI React adapter — <TokUIView> component and useTokUIStream() hook for streaming DSL UI.",
5
+ "license": "MIT",
6
+ "author": "sdxiaomu (https://jboltai.com)",
7
+ "homepage": "https://github.com/jboltai/tokui",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/jboltai/tokui.git",
11
+ "directory": "packages/react"
12
+ },
13
+ "main": "src/index.js",
14
+ "module": "src/index.js",
15
+ "types": "src/index.d.ts",
16
+ "type": "module",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./src/index.d.ts",
20
+ "import": "./src/index.js",
21
+ "require": "./src/index.js",
22
+ "default": "./src/index.js"
23
+ },
24
+ "./package.json": "./package.json"
25
+ },
26
+ "dependencies": {
27
+ "@jboltai/tokui": "^0.1.0"
28
+ },
29
+ "peerDependencies": {
30
+ "react": ">=16.8.0"
31
+ },
32
+ "peerDependenciesMeta": {
33
+ "react": {
34
+ "optional": false
35
+ }
36
+ },
37
+ "sideEffects": [
38
+ "**/*.css"
39
+ ],
40
+ "files": [
41
+ "src",
42
+ "README.md"
43
+ ],
44
+ "keywords": [
45
+ "tokui",
46
+ "react",
47
+ "streaming-ui",
48
+ "dsl",
49
+ "hook"
50
+ ],
51
+ "publishConfig": {
52
+ "access": "public"
53
+ }
54
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ import type * as React from 'react';
2
+ import type { TokUI as TokUIClass, TokUIEventCallback } from '@jboltai/tokui';
3
+
4
+ export interface TokUIViewProps extends React.HTMLAttributes<HTMLDivElement> {
5
+ /** DSL 文本,变化时重新渲染 */
6
+ dsl?: string;
7
+ /** 主题名(如 'dark') */
8
+ theme?: string;
9
+ /** 事件回调,签名 (eventName, data) */
10
+ onEvent?: TokUIEventCallback;
11
+ }
12
+
13
+ export declare const TokUIView: React.FC<TokUIViewProps>;
14
+
15
+ export interface UseTokUIStreamReturn {
16
+ ref: React.RefObject<HTMLDivElement>;
17
+ ui: React.MutableRefObject<TokUIClass | null>;
18
+ start: (container?: HTMLDivElement) => void;
19
+ feed: (chunk: string) => void;
20
+ end: () => void;
21
+ connect: (url: string, body?: Record<string, any>) => Promise<void>;
22
+ disconnect: () => void;
23
+ }
24
+
25
+ export declare function useTokUIStream(options?: {
26
+ theme?: string;
27
+ onEvent?: TokUIEventCallback;
28
+ }): UseTokUIStreamReturn;
29
+
30
+ export default TokUIView;
package/src/index.js ADDED
@@ -0,0 +1,88 @@
1
+ 'use strict';
2
+
3
+ // TokUI React 适配器:<TokUIView> 组件 + useTokUIStream() 流式 hook。
4
+ // 用 createElement 避免 JSX 运行时依赖,兼容任意 React 16.8+ 项目。
5
+ import { useEffect, useRef, useCallback, createElement } from 'react';
6
+ import { TokUI, setTheme } from '@jboltai/tokui';
7
+
8
+ /**
9
+ * 声明式渲染一段 TokUI DSL。
10
+ * @param {object} props
11
+ * @param {string} [props.dsl] DSL 文本(变化时重新渲染)
12
+ * @param {string} [props.theme] 主题名
13
+ * @param {function} [props.onEvent] 事件回调 (name, data)
14
+ */
15
+ export function TokUIView(props) {
16
+ const { dsl, theme, onEvent, ...rest } = props;
17
+ const ref = useRef(null);
18
+ const uiRef = useRef(null);
19
+
20
+ // 挂载时创建 TokUI 实例;卸载时断开清理
21
+ useEffect(() => {
22
+ const el = ref.current;
23
+ if (!el) return;
24
+ const ui = new TokUI({ container: el, theme, onEvent });
25
+ uiRef.current = ui;
26
+ if (theme) { try { setTheme(theme); } catch (e) { /* ignore */ } }
27
+ if (dsl) ui.render(dsl);
28
+ return () => {
29
+ try { if (ui.disconnect) ui.disconnect(); } catch (e) { /* ignore */ }
30
+ uiRef.current = null;
31
+ };
32
+ // 仅挂载时初始化(theme/onEvent 变化不重建实例)
33
+ // eslint-disable-next-line react-hooks/exhaustive-deps
34
+ }, []);
35
+
36
+ // dsl 变化 → 重新渲染
37
+ useEffect(() => {
38
+ const ui = uiRef.current;
39
+ if (ui && dsl != null) ui.render(dsl);
40
+ }, [dsl]);
41
+
42
+ return createElement('div', { ref, ...rest });
43
+ }
44
+
45
+ /**
46
+ * 流式渲染 hook(手动 feed / SSE connect)。
47
+ * @param {object} [options] TokUI 构造选项(theme/onEvent 等)
48
+ * @returns {{ref, ui, start, feed, end, connect, disconnect}}
49
+ */
50
+ export function useTokUIStream(options) {
51
+ const ref = useRef(null);
52
+ const uiRef = useRef(null);
53
+
54
+ const get = useCallback(() => {
55
+ if (!uiRef.current && ref.current) {
56
+ uiRef.current = new TokUI({ container: ref.current, ...(options || {}) });
57
+ }
58
+ return uiRef.current;
59
+ // eslint-disable-next-line react-hooks/exhaustive-deps
60
+ }, []);
61
+
62
+ const start = useCallback((container) => {
63
+ const ui = get();
64
+ if (ui) ui.startStream(container || ref.current);
65
+ }, [get]);
66
+
67
+ const feed = useCallback((chunk) => {
68
+ const ui = get();
69
+ if (ui) ui.feed(chunk);
70
+ }, [get]);
71
+
72
+ const end = useCallback(() => {
73
+ if (uiRef.current) uiRef.current.endStream();
74
+ }, []);
75
+
76
+ const connect = useCallback((url, body) => {
77
+ const ui = get();
78
+ return ui ? ui.connect(url, body) : Promise.resolve();
79
+ }, [get]);
80
+
81
+ const disconnect = useCallback(() => {
82
+ if (uiRef.current && uiRef.current.disconnect) uiRef.current.disconnect();
83
+ }, []);
84
+
85
+ return { ref, ui: uiRef, start, feed, end, connect, disconnect };
86
+ }
87
+
88
+ export default TokUIView;