@tbox.cn/app-agent-ui-kit 1.0.0-alpha.8 → 1.0.0-alpha.9
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/README.md +21 -1
- package/package.json +11 -3
- package/src/ChatPage.tsx +30 -1
- package/src/card/primitives.test.tsx +10 -0
- package/src/card/primitives.tsx +14 -10
- package/src/card/view-card.tsx +6 -1
- package/src/chat-config.ts +11 -7
- package/src/components/ActionBar/DebugButton.test.tsx +3 -0
- package/src/components/ActionBar/DebugButton.tsx +2 -7
- package/src/components/BottomSheet/index.test.tsx +90 -0
- package/src/components/BottomSheet/index.tsx +129 -0
- package/src/components/ChatInput/index.test.tsx +2 -2
- package/src/components/ChatInput/index.tsx +21 -16
- package/src/components/HistoryEntry/index.tsx +1 -0
- package/src/components/QuickCommands/index.tsx +2 -2
- package/src/components/ScrollToBottomButton/index.tsx +7 -3
- package/src/components/ToolCallBlock/index.tsx +4 -2
- package/src/index.ts +4 -0
- package/src/shell.contract.json +9 -0
- package/src/shell.test.ts +10 -0
- package/src/shell.ts +15 -0
- package/src/theme/contract.json +78 -1
- package/src/theme/theme-contract.test.ts +16 -0
- package/src/theme/tokens.css +1 -1
package/README.md
CHANGED
|
@@ -6,12 +6,32 @@ Agent 对话壳 UI 组件包(源码发布)——单一模板统一化 W2-1
|
|
|
6
6
|
(name 改写 `@tbox.cn/app-agent-ui-kit` → `@tbox.cn/app-agent-ui-kit`),guard identity 锁定一致。
|
|
7
7
|
- **消费面**:应用 `<ChatPage store={useChatStore} cards={CHAT_REGISTRY} slots={clientCtx.slots}/>`
|
|
8
8
|
单点装配;域 UI(如商圈主屏)经 home 槽渲染函数自带。
|
|
9
|
-
- **主题**:kit 默认 tokens(`src/theme/tokens.
|
|
9
|
+
- **主题**:kit 默认 tokens(`src/theme/tokens.css`);品牌资产包经 `html[data-theme]` 级联覆写 +
|
|
10
10
|
接线三行(`import '<pkg>/index.css'` + `import '<pkg>/brand.js'`[可选] + `applyTheme(id)`;见 docs/reference/templates.md)。
|
|
11
11
|
|
|
12
|
+
## 半屏弹层
|
|
13
|
+
|
|
14
|
+
`BottomSheet` 为统一的受控底部弹层,基于已有 `antd-mobile Popup`,使用 React + Tailwind 工具类 与平台主题变量;不依赖 shadcn/ui。外壳负责 portal、遮罩、滚动锁、安全区、标题、关闭按钮、Escape 和焦点循环/恢复,业务负责请求、加载、错误与内容。
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { BottomSheet } from '@tbox.cn/app-agent-ui-kit/bottom-sheet';
|
|
18
|
+
|
|
19
|
+
<BottomSheet open={open} title="详情" onClose={() => setOpen(false)}>
|
|
20
|
+
<DetailContent />
|
|
21
|
+
</BottomSheet>
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
支持 `className` 定制内容区,默认最大高度为视口的 65%,长内容在弹层内部滚动。
|
|
25
|
+
|
|
26
|
+
模板副本沿用同名 `@tbox.cn/app-agent-ui-kit/bottom-sheet`(`sync-agent-ui-kit` 投影,仅依赖面转 catalog:);仓内模块使用公开名并声明 optional peer,由宿主提供源码包,不从 npm 拉取。
|
|
27
|
+
|
|
28
|
+
**SSR / Node 边界**:`BottomSheet` 对 `antd-mobile` 的 `Popup` 采用浏览器按需加载(`import()`),`bottom-sheet` 子路径在 Vite/Node SSR 里可直接 import,不再触发 antd-mobile CJS 入口的 `global.css` 解析错误。需要首开无等待时,宿主可调用 `preloadBottomSheet()`(根入口或 `bottom-sheet` 子路径)预热;加载失败会在下一次 open 重试。kit **根入口**仍依赖 SVG/PNG/CSS 资产,只承诺 bundler(Vite 等)消费,不承诺 raw-Node 直跑。
|
|
29
|
+
|
|
12
30
|
## 验证
|
|
13
31
|
|
|
14
32
|
```sh
|
|
15
33
|
pnpm --filter @tbox.cn/app-agent-ui-kit typecheck
|
|
16
34
|
pnpm --filter @tbox.cn/app-agent-ui-kit test
|
|
17
35
|
```
|
|
36
|
+
|
|
37
|
+
轻提示可从 `@tbox.cn/app-agent-ui-kit/toast` 独立导入 `toast`,避免引入完整聊天壳。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbox.cn/app-agent-ui-kit",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Agent 对话壳 UI 组件包(源码发布,单一模板统一化 W2-1):ChatPage/壳组件族/主题 tokens。",
|
|
6
6
|
"exports": {
|
|
@@ -18,6 +18,14 @@
|
|
|
18
18
|
"./icons": {
|
|
19
19
|
"types": "./src/icons.ts",
|
|
20
20
|
"import": "./src/icons.ts"
|
|
21
|
+
},
|
|
22
|
+
"./toast": {
|
|
23
|
+
"types": "./src/utils/toast.ts",
|
|
24
|
+
"import": "./src/utils/toast.ts"
|
|
25
|
+
},
|
|
26
|
+
"./bottom-sheet": {
|
|
27
|
+
"types": "./src/components/BottomSheet/index.tsx",
|
|
28
|
+
"import": "./src/components/BottomSheet/index.tsx"
|
|
21
29
|
}
|
|
22
30
|
},
|
|
23
31
|
"files": [
|
|
@@ -32,8 +40,8 @@
|
|
|
32
40
|
"react-markdown": "9.1.0",
|
|
33
41
|
"remark-gfm": "4.0.1",
|
|
34
42
|
"zustand": "4.5.7",
|
|
35
|
-
"@tbox.cn/app-contracts": "1.0.0-alpha.
|
|
36
|
-
"@tbox.cn/app-agent-sdk-client": "1.0.0-alpha.
|
|
43
|
+
"@tbox.cn/app-contracts": "1.0.0-alpha.9",
|
|
44
|
+
"@tbox.cn/app-agent-sdk-client": "1.0.0-alpha.9"
|
|
37
45
|
},
|
|
38
46
|
"peerDependencies": {
|
|
39
47
|
"react": "18.3.1",
|
package/src/ChatPage.tsx
CHANGED
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
* - 域自持段(首屏数据/门禁/详情弹层/活动预热)经槽渲染函数自带——本页零域语义。
|
|
8
8
|
*/
|
|
9
9
|
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
10
|
+
|
|
11
|
+
// SSR(预览/探针)没有 DOM 可量:useLayoutEffect 会打印不可消除的告警,服务端退化为 useEffect。
|
|
12
|
+
const useIsomorphicLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
|
|
10
13
|
import { consumeAutoSendQuery, runAutoSend } from '@tbox.cn/app-agent-sdk-client';
|
|
11
14
|
import { usePullDownHistory } from './hooks/use-pull-down-history';
|
|
12
15
|
import type { TboxCardPayload } from '@tbox.cn/app-contracts';
|
|
@@ -89,6 +92,23 @@ function ChatPageInner() {
|
|
|
89
92
|
const homeSlot = slots?.resolve('home');
|
|
90
93
|
const identity = useChatConfigStore((s) => s.identity);
|
|
91
94
|
const welcome = useChatConfigStore((s) => s.welcome);
|
|
95
|
+
|
|
96
|
+
// 页面元信息跟身份与主题走:标题、主题色不再停在模板出厂值(B9)。
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
if (typeof document === 'undefined') return;
|
|
99
|
+
const name = identity?.name;
|
|
100
|
+
if (name) document.title = name;
|
|
101
|
+
const bg = getComputedStyle(document.documentElement).getPropertyValue('--tbox-bg-page').trim();
|
|
102
|
+
if (bg) {
|
|
103
|
+
let meta = document.querySelector('meta[name="theme-color"]');
|
|
104
|
+
if (!meta) {
|
|
105
|
+
meta = document.createElement('meta');
|
|
106
|
+
meta.setAttribute('name', 'theme-color');
|
|
107
|
+
document.head.appendChild(meta);
|
|
108
|
+
}
|
|
109
|
+
meta.setAttribute('content', bg);
|
|
110
|
+
}
|
|
111
|
+
}, [identity?.name]);
|
|
92
112
|
// 常驻品牌条:chat.json identity.name → HELLO_ACK 的 mallName → 不渲染(壳契约见 shell.ts)
|
|
93
113
|
const brandName = resolveBrandName(identity, mallName);
|
|
94
114
|
|
|
@@ -167,7 +187,7 @@ function ChatPageInner() {
|
|
|
167
187
|
|
|
168
188
|
// 历史插入发生在当前内容上方:绘制前只补一次新增高度,保持用户原本看到的位置。
|
|
169
189
|
// 不设置定时器、不钉住 scrollTop;补偿后下一次滚动立即由用户接管。
|
|
170
|
-
|
|
190
|
+
useIsomorphicLayoutEffect(() => {
|
|
171
191
|
const pending = pendingHistoryViewportRef.current;
|
|
172
192
|
const anchor = historyAnchorRef.current;
|
|
173
193
|
if (!pending || !anchor) return;
|
|
@@ -255,8 +275,17 @@ function ChatPageInner() {
|
|
|
255
275
|
const delta = element.getBoundingClientRect().top - body.getBoundingClientRect().top;
|
|
256
276
|
body.scrollTop += delta - 8;
|
|
257
277
|
isNearBottomRef.current = false;
|
|
278
|
+
// 回合锚定后内容通常超出视口:必须让「回到底部」入口出现,否则用户只能靠手势找底。
|
|
279
|
+
if (body.scrollHeight > body.clientHeight + 8) setShowScrollToBottom(true);
|
|
258
280
|
}, [messages]);
|
|
259
281
|
|
|
282
|
+
// 已脱离底部时内容继续变高(流式回复、卡片后挂载):保持浮标可见,不追底。
|
|
283
|
+
useEffect(() => {
|
|
284
|
+
if (isNearBottomRef.current) return;
|
|
285
|
+
const el = bodyRef.current;
|
|
286
|
+
if (el && el.scrollHeight > el.clientHeight + 8) setShowScrollToBottom(true);
|
|
287
|
+
}, [messages, liveCardSignature, orphanCards]);
|
|
288
|
+
|
|
260
289
|
// 没有新回合、但卡片在文字之后挂载时,内容会继续变高:只在用户本来就在底部时补滚,
|
|
261
290
|
// 且不改变「本轮问题已在视口内」的锚点。
|
|
262
291
|
useEffect(() => {
|
|
@@ -89,6 +89,16 @@ describe('第二版基元', () => {
|
|
|
89
89
|
);
|
|
90
90
|
expect(markup).toContain('tbox-hero');
|
|
91
91
|
expect(markup).toContain('我的积分');
|
|
92
|
+
// 深底主视觉:底用 primary-dark、字用 primary-foreground,全部走令牌(无字面量色值)。
|
|
93
|
+
const inverse = html(<Hero eyebrow="ROUTE" title="导航" value={8} unit="分钟" tone="inverse" />);
|
|
94
|
+
expect(inverse).toContain('tbox-hero--inverse');
|
|
95
|
+
expect(inverse).toContain('bg-primary-dark');
|
|
96
|
+
expect(inverse).toContain('text-primary-foreground');
|
|
97
|
+
expect(inverse).not.toMatch(/#[0-9a-fA-F]{3,8}\b|rgba?\(/);
|
|
98
|
+
// 未知 tone(旧数据 / 新 kit 错配)回落 primary,不拼空类名。
|
|
99
|
+
const unknown = html(<Hero title="兜底" tone={'legacy' as never} />);
|
|
100
|
+
expect(unknown).toContain('tbox-hero--legacy');
|
|
101
|
+
expect(unknown).toContain('from-primary-foreground');
|
|
92
102
|
expect(markup).toContain('tbox-stats__value');
|
|
93
103
|
expect(markup).toContain('tbox-timeline__item--gain');
|
|
94
104
|
expect(markup).toContain('+100');
|
package/src/card/primitives.tsx
CHANGED
|
@@ -18,18 +18,18 @@ export type Tone = 'neutral' | 'info' | 'success' | 'warning' | 'error';
|
|
|
18
18
|
/** 状态色:中性走契约语义色,其余是旧 less 里的固定色值(透明度底 + 深色字) */
|
|
19
19
|
const TAG_TONE: Record<Tone, string> = {
|
|
20
20
|
neutral: 'bg-muted text-secondary-foreground',
|
|
21
|
-
info: 'bg-
|
|
22
|
-
success: 'bg-
|
|
23
|
-
warning: 'bg-
|
|
24
|
-
error: 'bg-
|
|
21
|
+
info: 'bg-primary-soft text-primary',
|
|
22
|
+
success: 'bg-success-soft text-success',
|
|
23
|
+
warning: 'bg-warning-soft text-warn-ink',
|
|
24
|
+
error: 'bg-danger-soft text-destructive',
|
|
25
25
|
};
|
|
26
26
|
|
|
27
27
|
const NOTICE_TONE: Record<Tone, string> = {
|
|
28
28
|
neutral: 'bg-muted',
|
|
29
|
-
info: 'bg-
|
|
30
|
-
success: 'bg-
|
|
31
|
-
warning: 'bg-
|
|
32
|
-
error: 'bg-
|
|
29
|
+
info: 'bg-primary-soft',
|
|
30
|
+
success: 'bg-success-soft',
|
|
31
|
+
warning: 'bg-warning-soft',
|
|
32
|
+
error: 'bg-danger-soft',
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
export interface CardProps {
|
|
@@ -288,6 +288,8 @@ const HERO_TONE = {
|
|
|
288
288
|
primary: 'bg-linear-to-br from-primary-foreground to-card',
|
|
289
289
|
warm: 'bg-linear-to-br from-warm to-card',
|
|
290
290
|
jade: 'bg-linear-to-br from-jade to-card',
|
|
291
|
+
// 深色底:只用契约里的令牌(primary-dark / primary-light),不引入字面量色值。
|
|
292
|
+
inverse: 'bg-primary-dark text-primary-foreground [&_.tbox-hero__eyebrow]:text-primary-foreground [&_.tbox-hero__footnote]:text-primary-foreground [&_.tbox-hero__value_small]:text-primary-foreground',
|
|
291
293
|
neutral: 'bg-muted',
|
|
292
294
|
} as const;
|
|
293
295
|
|
|
@@ -382,13 +384,15 @@ export interface HeroProps {
|
|
|
382
384
|
/** 数值下方一句说明 */
|
|
383
385
|
footnote?: ReactNode;
|
|
384
386
|
icon?: ReactNode;
|
|
385
|
-
tone?: 'primary' | 'warm' | 'jade' | 'neutral';
|
|
387
|
+
tone?: 'primary' | 'warm' | 'jade' | 'inverse' | 'neutral';
|
|
386
388
|
}
|
|
387
389
|
|
|
388
390
|
/** 品牌色块:主数值 + 说明,用于会员、积分、钱包类卡片的头部。 */
|
|
389
391
|
export function Hero({ eyebrow, title, value, unit, footnote, icon, tone = 'primary' }: HeroProps) {
|
|
392
|
+
// 旧 kit 与新数据错配时(未知 tone)回落 primary,不拼出空类名。
|
|
393
|
+
const toneClass = HERO_TONE[tone] ?? HERO_TONE.primary;
|
|
390
394
|
return (
|
|
391
|
-
<div className={`tbox-hero tbox-hero--${tone} flex items-center gap-2 rounded-md p-3.5 pb-3.25 ${
|
|
395
|
+
<div className={`tbox-hero tbox-hero--${tone} flex items-center gap-2 rounded-md p-3.5 pb-3.25 ${toneClass}`}>
|
|
392
396
|
<div className="tbox-hero__text flex min-w-0 flex-1 flex-col gap-1.5">
|
|
393
397
|
{eyebrow ? <span className="tbox-hero__eyebrow text-xs tracking-wide text-muted-foreground">{eyebrow}</span> : null}
|
|
394
398
|
<span className="tbox-hero__title text-sm font-semibold">{title}</span>
|
package/src/card/view-card.tsx
CHANGED
|
@@ -237,7 +237,12 @@ export function ViewCard({
|
|
|
237
237
|
<div key={block.kind + '-' + index}>{renderBlock(block, inline.trigger, inline.busy)}</div>
|
|
238
238
|
))}
|
|
239
239
|
{safeView.actions && safeView.actions.length > 0 ? (
|
|
240
|
-
<ActionList
|
|
240
|
+
<ActionList
|
|
241
|
+
api={api}
|
|
242
|
+
actions={safeView.actions.map((action, index) =>
|
|
243
|
+
toSpec(index === 0 ? { ...action, style: action.style ?? 'primary' } : { ...action, style: 'secondary' }),
|
|
244
|
+
)}
|
|
245
|
+
/>
|
|
241
246
|
) : null}
|
|
242
247
|
{inline.sheet}
|
|
243
248
|
</Card>
|
package/src/chat-config.ts
CHANGED
|
@@ -40,14 +40,18 @@ interface ChatConfigStore {
|
|
|
40
40
|
applyServerSettings: (settings: ChatUiSettings | undefined) => void;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
// SSR / 探针注入点:浏览器生产环境不设置,零影响(探针看不到配置会误判)。
|
|
44
|
+
const ssrSnapshot = ((globalThis as { __TBOX_CHAT_CONFIG__?: Record<string, unknown> }).__TBOX_CHAT_CONFIG__ ?? {});
|
|
43
45
|
export const useChatConfigStore = create<ChatConfigStore>()((set) => ({
|
|
44
|
-
// kit 内建缺省(应用快照经 configureChatConfigDefaults
|
|
45
|
-
enableImageUpload: true,
|
|
46
|
-
enableVideoUpload: true,
|
|
47
|
-
enableTTS: true,
|
|
48
|
-
enableVoiceInput: true,
|
|
49
|
-
defaultInputMode: 'TEXT',
|
|
50
|
-
quickCommands: [],
|
|
46
|
+
// kit 内建缺省(应用快照经 configureChatConfigDefaults 覆盖;SSR 探针经 globalThis 注入)
|
|
47
|
+
enableImageUpload: (ssrSnapshot['enableImageUpload'] as boolean | undefined) ?? true,
|
|
48
|
+
enableVideoUpload: (ssrSnapshot['enableVideoUpload'] as boolean | undefined) ?? true,
|
|
49
|
+
enableTTS: (ssrSnapshot['enableTTS'] as boolean | undefined) ?? true,
|
|
50
|
+
enableVoiceInput: (ssrSnapshot['enableVoiceInput'] as boolean | undefined) ?? true,
|
|
51
|
+
defaultInputMode: (ssrSnapshot['defaultInputMode'] as 'TEXT' | 'VOICE' | undefined) ?? 'TEXT',
|
|
52
|
+
quickCommands: (ssrSnapshot['quickCommands'] as QuickCommand[] | undefined) ?? [],
|
|
53
|
+
identity: ssrSnapshot['identity'] as ChatIdentity | undefined,
|
|
54
|
+
welcome: ssrSnapshot['welcome'] as ChatWelcome | undefined,
|
|
51
55
|
retryOnError: true,
|
|
52
56
|
applyServerSettings: (settings) =>
|
|
53
57
|
set((cur) => ({
|
|
@@ -22,6 +22,8 @@ describe('DebugButton 平台契约(tbox:debug-trace 载荷锁定)', () => {
|
|
|
22
22
|
let postSpy: ReturnType<typeof vi.fn>;
|
|
23
23
|
|
|
24
24
|
beforeEach(() => {
|
|
25
|
+
// 调试入口只在显式 ?debug=1 下渲染(B5:dev 构建与 iframe 预览不再触发)
|
|
26
|
+
window.history.replaceState({}, '', '/?debug=1');
|
|
25
27
|
host = document.createElement('div');
|
|
26
28
|
document.body.appendChild(host);
|
|
27
29
|
root = createRoot(host);
|
|
@@ -33,6 +35,7 @@ describe('DebugButton 平台契约(tbox:debug-trace 载荷锁定)', () => {
|
|
|
33
35
|
afterEach(() => {
|
|
34
36
|
act(() => root.unmount());
|
|
35
37
|
host.remove();
|
|
38
|
+
window.history.replaceState({}, '', '/');
|
|
36
39
|
});
|
|
37
40
|
|
|
38
41
|
const clickDebug = () => {
|
|
@@ -11,13 +11,8 @@ import { Bug } from '@phosphor-icons/react';
|
|
|
11
11
|
*/
|
|
12
12
|
function shouldShow(): boolean {
|
|
13
13
|
if (typeof window === 'undefined') return false;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
try {
|
|
17
|
-
return window.self !== window.top;
|
|
18
|
-
} catch {
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
14
|
+
// 只在显式调试请求下显示:dev 构建与 iframe 预览都不再触发(B5)。
|
|
15
|
+
return new URLSearchParams(window.location.search).get('debug') === '1';
|
|
21
16
|
}
|
|
22
17
|
|
|
23
18
|
export function DebugButton({
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { act } from 'react';
|
|
4
|
+
import { createRoot } from 'react-dom/client';
|
|
5
|
+
import type { Root } from 'react-dom/client';
|
|
6
|
+
import { BottomSheet } from './index';
|
|
7
|
+
|
|
8
|
+
let root: Root | undefined;
|
|
9
|
+
afterEach(async () => {
|
|
10
|
+
await act(async () => root?.unmount());
|
|
11
|
+
root = undefined;
|
|
12
|
+
document.body.innerHTML = '';
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
async function waitForDialog() {
|
|
16
|
+
for (let attempt = 0; attempt < 50; attempt += 1) {
|
|
17
|
+
const dialog = document.querySelector('[role="dialog"]');
|
|
18
|
+
if (dialog) return dialog;
|
|
19
|
+
await act(async () => {
|
|
20
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
throw new Error('BottomSheet 懒加载后仍未挂载弹层');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function renderSheet(open: boolean, onClose = vi.fn()) {
|
|
27
|
+
if (!root) {
|
|
28
|
+
const container = document.createElement('div');
|
|
29
|
+
document.body.append(container);
|
|
30
|
+
root = createRoot(container);
|
|
31
|
+
}
|
|
32
|
+
await act(async () => {
|
|
33
|
+
root!.render(<BottomSheet open={open} title="奖品详情" onClose={onClose}>
|
|
34
|
+
<button type="button">内容操作</button>
|
|
35
|
+
</BottomSheet>);
|
|
36
|
+
});
|
|
37
|
+
return onClose;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('BottomSheet', () => {
|
|
41
|
+
it('关闭态不挂载内容;打开通过 portal 展示命名对话框和业务内容', async () => {
|
|
42
|
+
await renderSheet(false);
|
|
43
|
+
expect(document.querySelector('[role="dialog"]')).toBeNull();
|
|
44
|
+
await renderSheet(true);
|
|
45
|
+
const dialog = await waitForDialog();
|
|
46
|
+
expect(dialog.getAttribute('aria-modal')).toBe('true');
|
|
47
|
+
expect(document.getElementById(dialog.getAttribute('aria-labelledby')!)?.textContent).toBe('奖品详情');
|
|
48
|
+
expect(dialog.textContent).toContain('内容操作');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('关闭按钮与 Escape 请求受控关闭,卸载后恢复触发按钮焦点并清理监听', async () => {
|
|
52
|
+
const trigger = document.createElement('button');
|
|
53
|
+
document.body.append(trigger);
|
|
54
|
+
trigger.focus();
|
|
55
|
+
const onClose = await renderSheet(true);
|
|
56
|
+
await waitForDialog();
|
|
57
|
+
await act(async () => {
|
|
58
|
+
(document.querySelector('[aria-label="关闭奖品详情"]') as HTMLButtonElement).click();
|
|
59
|
+
});
|
|
60
|
+
expect(onClose).toHaveBeenCalledTimes(1);
|
|
61
|
+
await act(async () => document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' })));
|
|
62
|
+
expect(onClose).toHaveBeenCalledTimes(2);
|
|
63
|
+
await act(async () => root?.unmount());
|
|
64
|
+
root = undefined;
|
|
65
|
+
expect(document.activeElement).toBe(trigger);
|
|
66
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' }));
|
|
67
|
+
expect(onClose).toHaveBeenCalledTimes(2);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('Tab 与 Shift+Tab 在弹层内循环,不落到背景页面', async () => {
|
|
71
|
+
await renderSheet(true);
|
|
72
|
+
await waitForDialog();
|
|
73
|
+
const close = document.querySelector('[aria-label="关闭奖品详情"]') as HTMLButtonElement;
|
|
74
|
+
const last = document.querySelector('.tbox-bottom-sheet__body button') as HTMLButtonElement;
|
|
75
|
+
last.focus();
|
|
76
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab', cancelable: true }));
|
|
77
|
+
expect(document.activeElement).toBe(close);
|
|
78
|
+
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab', shiftKey: true, cancelable: true }));
|
|
79
|
+
expect(document.activeElement).toBe(last);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('遮罩点击请求关闭,内容点击不关闭', async () => {
|
|
83
|
+
const onClose = await renderSheet(true);
|
|
84
|
+
await waitForDialog();
|
|
85
|
+
await act(async () => (document.querySelector('.tbox-bottom-sheet__body button') as HTMLButtonElement).click());
|
|
86
|
+
expect(onClose).not.toHaveBeenCalled();
|
|
87
|
+
await act(async () => (document.querySelector('.adm-mask') as HTMLElement).click());
|
|
88
|
+
expect(onClose).toHaveBeenCalledTimes(1);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { useEffect, useId, useRef, useState } from 'react';
|
|
2
|
+
import type { ComponentType, ReactNode } from 'react';
|
|
3
|
+
import type { PopupProps } from 'antd-mobile';
|
|
4
|
+
|
|
5
|
+
export interface BottomSheetProps {
|
|
6
|
+
open: boolean;
|
|
7
|
+
title: string;
|
|
8
|
+
onClose: () => void;
|
|
9
|
+
children: ReactNode;
|
|
10
|
+
/** 内容容器的业务样式;弹层外壳由组件统一管理。 */
|
|
11
|
+
className?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type PopupComponent = ComponentType<PopupProps>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* antd-mobile 带 CSS 的运行时依赖只在浏览器按需加载:kit 源码在 Node/Vite SSR 里可直接 import
|
|
18
|
+
* (静态 import 会让 Node 读到 CJS 入口 require 的 global.css,整条证据链/SSR 消费者失败)。
|
|
19
|
+
* 失败时清空缓存,允许下一次交互重试。
|
|
20
|
+
*/
|
|
21
|
+
let popupPromise: Promise<PopupComponent> | undefined;
|
|
22
|
+
|
|
23
|
+
export function preloadBottomSheet(): Promise<ComponentType<PopupProps>> {
|
|
24
|
+
popupPromise ??= import('antd-mobile')
|
|
25
|
+
.then((module) => module.Popup as PopupComponent)
|
|
26
|
+
.catch((error: unknown) => {
|
|
27
|
+
popupPromise = undefined;
|
|
28
|
+
throw error;
|
|
29
|
+
});
|
|
30
|
+
return popupPromise;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 受控底部弹层:业务只提供内容与开关,不自行处理遮罩、滚动和焦点。 */
|
|
34
|
+
export function BottomSheet({ open, title, onClose, children, className }: BottomSheetProps) {
|
|
35
|
+
const titleId = useId();
|
|
36
|
+
const panelRef = useRef<HTMLDivElement>(null);
|
|
37
|
+
const closeRef = useRef<HTMLButtonElement>(null);
|
|
38
|
+
const onCloseRef = useRef(onClose);
|
|
39
|
+
onCloseRef.current = onClose;
|
|
40
|
+
const [Popup, setPopup] = useState<PopupComponent | null>(null);
|
|
41
|
+
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (Popup) return;
|
|
44
|
+
let alive = true;
|
|
45
|
+
preloadBottomSheet()
|
|
46
|
+
.then((loaded) => {
|
|
47
|
+
if (alive) setPopup(() => loaded);
|
|
48
|
+
})
|
|
49
|
+
.catch((error: unknown) => {
|
|
50
|
+
// 一次 chunk 失败不应静默:保留可诊断信息;open 变化会再次进入本 effect 重试(promise 已清空)。
|
|
51
|
+
console.warn('BottomSheet: antd-mobile Popup 加载失败', error);
|
|
52
|
+
});
|
|
53
|
+
return () => {
|
|
54
|
+
alive = false;
|
|
55
|
+
};
|
|
56
|
+
}, [open, Popup]);
|
|
57
|
+
|
|
58
|
+
// 懒加载窗口:弹层未就绪时先挡住背景交互(无遮罩会让「模态」破窗)。
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (!open || Popup) return;
|
|
61
|
+
const previous = document.body.style.overflow;
|
|
62
|
+
document.body.style.overflow = 'hidden';
|
|
63
|
+
return () => {
|
|
64
|
+
document.body.style.overflow = previous;
|
|
65
|
+
};
|
|
66
|
+
}, [open, Popup]);
|
|
67
|
+
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
if (!open) return;
|
|
70
|
+
const previous = document.activeElement;
|
|
71
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
72
|
+
if (event.key === 'Escape') {
|
|
73
|
+
event.preventDefault();
|
|
74
|
+
event.stopPropagation();
|
|
75
|
+
onCloseRef.current();
|
|
76
|
+
}
|
|
77
|
+
if (event.key !== 'Tab' || !panelRef.current) return;
|
|
78
|
+
const focusable = [...panelRef.current.querySelectorAll<HTMLElement>(
|
|
79
|
+
'button:not(:disabled), a[href], input:not(:disabled), select:not(:disabled), textarea:not(:disabled), [tabindex="0"]',
|
|
80
|
+
)].filter((element) => !element.hidden && element.getAttribute('aria-hidden') !== 'true');
|
|
81
|
+
const first = focusable[0];
|
|
82
|
+
const last = focusable[focusable.length - 1];
|
|
83
|
+
if (!first || !last) return;
|
|
84
|
+
const active = document.activeElement;
|
|
85
|
+
if (event.shiftKey && (active === first || !panelRef.current.contains(active))) {
|
|
86
|
+
event.preventDefault();
|
|
87
|
+
last.focus();
|
|
88
|
+
} else if (!event.shiftKey && (active === last || !panelRef.current.contains(active))) {
|
|
89
|
+
event.preventDefault();
|
|
90
|
+
first.focus();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
document.addEventListener('keydown', onKeyDown);
|
|
94
|
+
return () => {
|
|
95
|
+
document.removeEventListener('keydown', onKeyDown);
|
|
96
|
+
if (previous instanceof HTMLElement && previous.isConnected) previous.focus();
|
|
97
|
+
};
|
|
98
|
+
}, [open]);
|
|
99
|
+
|
|
100
|
+
// Popup 晚于 open 就绪时 afterShow 不保证触发,这里补一次显式聚焦。
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
if (!open || !Popup) return;
|
|
103
|
+
const timer = window.setTimeout(() => closeRef.current?.focus(), 0);
|
|
104
|
+
return () => window.clearTimeout(timer);
|
|
105
|
+
}, [open, Popup]);
|
|
106
|
+
|
|
107
|
+
if (!Popup) {
|
|
108
|
+
return open ? <div className="fixed inset-0 z-[1100] bg-scrim" role="presentation" aria-hidden="true" /> : null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return (
|
|
112
|
+
<Popup className="motion-reduce:[&_.adm-popup-body]:transition-none motion-reduce:[&_.adm-mask]:transition-none" visible={open} position="bottom" onClose={onClose} closeOnMaskClick
|
|
113
|
+
destroyOnClose disableBodyScroll bodyClassName="tbox-bottom-sheet mx-auto w-full overflow-hidden rounded-t-[14px] bg-card text-foreground"
|
|
114
|
+
style={{ '--z-index': '1100' }} afterShow={() => closeRef.current?.focus()}>
|
|
115
|
+
<div ref={panelRef} className="tbox-bottom-sheet__panel flex max-h-[65vh] flex-col supports-[height:100dvh]:max-h-[65dvh]" role="dialog"
|
|
116
|
+
aria-modal="true" aria-labelledby={titleId}>
|
|
117
|
+
<header className="tbox-bottom-sheet__header flex shrink-0 items-center justify-between gap-3 px-3.5 py-3">
|
|
118
|
+
<h2 id={titleId} className="m-0 text-base font-semibold leading-[21px]">{title}</h2>
|
|
119
|
+
<button className="flex size-9 shrink-0 cursor-pointer items-center justify-center rounded-full border-0 bg-transparent p-1.5 text-inherit focus-visible:outline-2 focus-visible:outline-primary" ref={closeRef} type="button" aria-label={`关闭${title}`} onClick={onClose}>
|
|
120
|
+
<svg className="size-4.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" aria-hidden="true">
|
|
121
|
+
<path d="m6 6 12 12M6 18 18 6" />
|
|
122
|
+
</svg>
|
|
123
|
+
</button>
|
|
124
|
+
</header>
|
|
125
|
+
<div className={['tbox-bottom-sheet__body min-h-0 overflow-y-auto overscroll-contain px-3.5 pt-0 pb-[calc(16px+env(safe-area-inset-bottom))] text-sm leading-[19px]', className].filter(Boolean).join(' ')}>{children}</div>
|
|
126
|
+
</div>
|
|
127
|
+
</Popup>
|
|
128
|
+
);
|
|
129
|
+
}
|
|
@@ -83,9 +83,9 @@ describe('ChatInput 语音活动状态', () => {
|
|
|
83
83
|
});
|
|
84
84
|
|
|
85
85
|
describe('ChatInput 派生模式(服务端 defaultInputMode 水合)', () => {
|
|
86
|
-
/** voice
|
|
86
|
+
/** voice 态投影:录音模式的可访问名为「切换到键盘输入」;text 态为「切换到语音输入」 */
|
|
87
87
|
const inVoiceMode = (container: HTMLElement) =>
|
|
88
|
-
container.querySelector('
|
|
88
|
+
container.querySelector('.tbox-voice-toggle[aria-label="切换到键盘输入"]') !== null;
|
|
89
89
|
|
|
90
90
|
const renderInput = async (root: ReturnType<typeof createRoot>) => {
|
|
91
91
|
await act(async () => root.render(<ChatInput onSend={vi.fn()} />));
|
|
@@ -190,9 +190,9 @@ export function ChatInput({
|
|
|
190
190
|
|
|
191
191
|
<div className="tbox-chat-input__row flex flex-wrap items-end gap-2">
|
|
192
192
|
{onNewChat && (
|
|
193
|
-
<
|
|
194
|
-
<img className="tbox-new-chat-btn__icon size-[22px]" src={newChatIcon} alt="
|
|
195
|
-
</
|
|
193
|
+
<button type="button" data-tbox-control="new-chat" aria-label="新建对话" className="tbox-new-chat-btn flex size-11 shrink-0 cursor-pointer appearance-none items-center justify-center rounded-full border-0 bg-[rgba(255,255,255,0.5)] p-0 shadow-[0_1px_10px_rgba(110,139,180,0.24)] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary" onClick={onNewChat}>
|
|
194
|
+
<img className="tbox-new-chat-btn__icon size-[22px]" src={newChatIcon} alt="" />
|
|
195
|
+
</button>
|
|
196
196
|
)}
|
|
197
197
|
|
|
198
198
|
<div className="tbox-chat-input__wrapper flex min-w-0 flex-1 items-end gap-[14px] rounded-[var(--tbox-input-radius)] border border-border bg-[var(--tbox-input-bg)] p-[var(--tbox-input-padding)] shadow-[0_1px_10px_rgba(110,139,180,0.24)] transition-[border-color,box-shadow] duration-[var(--tbox-motion-duration-fast)] ease-[var(--tbox-motion-easing-standard)] focus-within:border-primary focus-within:shadow-[0_0_0_1.5px_rgba(9,107,250,0.12)]">
|
|
@@ -224,31 +224,36 @@ export function ChatInput({
|
|
|
224
224
|
)}
|
|
225
225
|
|
|
226
226
|
{enableVoice && (
|
|
227
|
-
<
|
|
227
|
+
<button type="button" data-tbox-control="voice-toggle" aria-label={inputMode === 'voice' ? '切换到键盘输入' : '切换到语音输入'} aria-pressed={inputMode === 'voice'} className="tbox-voice-toggle flex size-11 -m-[11px] shrink-0 cursor-pointer appearance-none items-center justify-center border-0 bg-transparent p-0 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary" onClick={toggleMode}>
|
|
228
228
|
<img
|
|
229
229
|
className="tbox-voice-toggle__icon size-[22px]"
|
|
230
230
|
src={inputMode === 'voice' ? keyboardIcon : voiceIcon}
|
|
231
|
-
alt=
|
|
231
|
+
alt=""
|
|
232
232
|
/>
|
|
233
|
-
</
|
|
233
|
+
</button>
|
|
234
234
|
)}
|
|
235
235
|
|
|
236
236
|
{blocked ? null : generating ? (
|
|
237
|
-
<
|
|
238
|
-
<img className="tbox-chat-input__send-img block size-[var(--tbox-send-btn-size)]" src={stopIcon} alt="
|
|
239
|
-
</
|
|
237
|
+
<button type="button" data-tbox-control="stop" aria-label="停止生成" className="tbox-chat-input__send tbox-chat-input__send--active flex size-11 -m-[11px] shrink-0 cursor-pointer appearance-none items-center justify-center border-0 bg-transparent p-0 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary" onClick={onCancel}>
|
|
238
|
+
<img className="tbox-chat-input__send-img block size-[var(--tbox-send-btn-size)]" src={stopIcon} alt="" />
|
|
239
|
+
</button>
|
|
240
240
|
) : canSend ? (
|
|
241
|
-
<
|
|
242
|
-
<img className="tbox-chat-input__send-img block size-[var(--tbox-send-btn-size)]" src={sendIcon} alt="
|
|
243
|
-
</
|
|
241
|
+
<button type="button" data-tbox-control="send" aria-label="发送" className="tbox-chat-input__send tbox-chat-input__send--active flex size-11 -m-[11px] shrink-0 cursor-pointer appearance-none items-center justify-center border-0 bg-transparent p-0 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary" onClick={submit}>
|
|
242
|
+
<img className="tbox-chat-input__send-img block size-[var(--tbox-send-btn-size)]" src={sendIcon} alt="" />
|
|
243
|
+
</button>
|
|
244
244
|
) : menuOptions.length > 0 ? (
|
|
245
245
|
<div className="tbox-media-picker relative shrink-0">
|
|
246
|
-
<
|
|
247
|
-
|
|
246
|
+
<button
|
|
247
|
+
type="button"
|
|
248
|
+
data-tbox-control="media-add"
|
|
249
|
+
aria-label="添加图片或视频"
|
|
250
|
+
aria-haspopup="menu"
|
|
251
|
+
aria-expanded={menuVisible}
|
|
252
|
+
className={`tbox-media-picker__trigger flex size-11 -m-[11px] shrink-0 cursor-pointer appearance-none items-center justify-center border-0 bg-transparent p-0 transition-transform duration-[var(--tbox-motion-duration-base)] ease-[ease] ${menuVisible ? ' tbox-media-picker__trigger--open rotate-45' : ''}`}
|
|
248
253
|
onClick={onPlus}
|
|
249
254
|
>
|
|
250
|
-
<img className="tbox-media-picker__trigger-icon size-[var(--tbox-send-btn-size)]" src={moreIcon} alt="
|
|
251
|
-
</
|
|
255
|
+
<img className="tbox-media-picker__trigger-icon size-[var(--tbox-send-btn-size)]" src={moreIcon} alt="" />
|
|
256
|
+
</button>
|
|
252
257
|
</div>
|
|
253
258
|
) : null}
|
|
254
259
|
</div>
|
|
@@ -29,6 +29,7 @@ export default function HistoryEntry({
|
|
|
29
29
|
<div className="history-entry flex justify-center p-0">
|
|
30
30
|
<button
|
|
31
31
|
type="button"
|
|
32
|
+
data-tbox-control="history"
|
|
32
33
|
className="history-entry__button inline-flex min-h-11 appearance-none items-center gap-[5px] rounded-[25px] border-0 bg-muted px-4 py-1 text-sm font-normal leading-[18px] text-muted-foreground transition-colors duration-[var(--tbox-motion-duration-fast)] ease-[var(--tbox-motion-easing-standard)] [font-family:inherit] active:bg-border disabled:cursor-default"
|
|
33
34
|
disabled={disabled}
|
|
34
35
|
onClick={onLoad}
|
|
@@ -57,8 +57,8 @@ export function QuickCommands({
|
|
|
57
57
|
aria-hidden="true"
|
|
58
58
|
data-icon={cmd.icon ?? starIcon}
|
|
59
59
|
style={{
|
|
60
|
-
maskImage: `url(${cmd.icon ?? starIcon})`,
|
|
61
|
-
WebkitMaskImage: `url(${cmd.icon ?? starIcon})`,
|
|
60
|
+
maskImage: `url("${cmd.icon ?? starIcon}")`,
|
|
61
|
+
WebkitMaskImage: `url("${cmd.icon ?? starIcon}")`,
|
|
62
62
|
maskSize: 'contain',
|
|
63
63
|
WebkitMaskSize: 'contain',
|
|
64
64
|
maskRepeat: 'no-repeat',
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
/** 生成时出现的“回到底部”悬浮按钮。 */
|
|
2
2
|
export default function ScrollToBottomButton({ visible, onClick }: { visible: boolean; onClick: () => void }) {
|
|
3
3
|
return (
|
|
4
|
-
<
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
<button
|
|
5
|
+
type="button"
|
|
6
|
+
data-tbox-control="scroll-bottom"
|
|
7
|
+
aria-label="回到底部"
|
|
8
|
+
className={`scroll-to-bottom-btn absolute right-4 bottom-4 z-10 flex size-11 cursor-pointer appearance-none items-center justify-center rounded-full border-0 bg-white p-0 shadow-[0_2px_8px_0_rgba(0,0,0,0.12)] transition-[opacity,transform] duration-[var(--tbox-motion-duration-fast)] ease-[ease] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary ${visible ? ' scroll-to-bottom-btn--visible pointer-events-auto opacity-100 [transform:scale(1)]' : ' pointer-events-none opacity-0 [transform:scale(0.8)]'}`} onClick={onClick}>
|
|
9
|
+
<span className="scroll-to-bottom-btn__arrow h-0 w-0 border-x-[6px] border-t-[7px] border-x-transparent border-t-[#666]" aria-hidden="true" />
|
|
10
|
+
</button>
|
|
7
11
|
);
|
|
8
12
|
}
|
|
@@ -54,6 +54,8 @@ export default function ToolCallBlock({
|
|
|
54
54
|
const listId = useId();
|
|
55
55
|
const items = useMemo(() => toolCalls.map(deriveToolCallItem), [toolCalls]);
|
|
56
56
|
const summary = useMemo(() => deriveSummary(items), [items]);
|
|
57
|
+
// 已恢复的失败不打扰用户:只有整批都失败时才展示失败计数与错误图标(B11)。
|
|
58
|
+
const allFailed = summary.failedCount > 0 && summary.failedCount === items.length;
|
|
57
59
|
|
|
58
60
|
const toggleDetail = (id: string) => {
|
|
59
61
|
setExpandedDetailIds((current) => {
|
|
@@ -79,14 +81,14 @@ export default function ToolCallBlock({
|
|
|
79
81
|
className="tool-calls__spinner size-3.5 shrink-0 animate-[tool-spin_0.8s_linear_infinite] rounded-full border-2 border-[rgba(9,107,250,0.2)] border-t-primary motion-reduce:animate-none"
|
|
80
82
|
aria-hidden="true"
|
|
81
83
|
/>
|
|
82
|
-
) :
|
|
84
|
+
) : allFailed ? (
|
|
83
85
|
<X className="tool-calls__icon tool-calls__icon--error shrink-0 self-center text-muted-foreground" size={16} strokeWidth={2.5} />
|
|
84
86
|
) : (
|
|
85
87
|
<Check className="tool-calls__icon tool-calls__icon--success shrink-0 self-center text-muted-foreground" size={16} strokeWidth={2.5} />
|
|
86
88
|
)}
|
|
87
89
|
<span className="tool-calls__summary-text min-w-0 truncate text-xs font-normal leading-[1.4]">
|
|
88
90
|
{buildSummaryText(summary).join(' · ')}
|
|
89
|
-
{
|
|
91
|
+
{allFailed && (
|
|
90
92
|
<span className="tool-calls__failed text-muted-foreground"> · {summary.failedCount} 个失败</span>
|
|
91
93
|
)}
|
|
92
94
|
</span>
|
package/src/index.ts
CHANGED
package/src/shell.contract.json
CHANGED
|
@@ -51,6 +51,15 @@
|
|
|
51
51
|
"brandable": false
|
|
52
52
|
}
|
|
53
53
|
],
|
|
54
|
+
"controls": [
|
|
55
|
+
{ "id": "new-chat", "selector": "[data-tbox-control='new-chat']", "name": "新建对话", "minHit": 44 },
|
|
56
|
+
{ "id": "voice-toggle", "selector": "[data-tbox-control='voice-toggle']", "name": "语音输入", "minHit": 44, "capability": "input.voice" },
|
|
57
|
+
{ "id": "send", "selector": "[data-tbox-control='send']", "name": "发送", "minHit": 44 },
|
|
58
|
+
{ "id": "stop", "selector": "[data-tbox-control='stop']", "name": "停止生成", "minHit": 44 },
|
|
59
|
+
{ "id": "media-add", "selector": "[data-tbox-control='media-add']", "name": "添加图片或视频", "minHit": 44, "capability": ["input.image", "input.video"] },
|
|
60
|
+
{ "id": "history", "selector": "[data-tbox-control='history']", "name": "历史记录", "minHit": 44 },
|
|
61
|
+
{ "id": "scroll-bottom", "selector": "[data-tbox-control='scroll-bottom']", "name": "回到底部", "minHit": 44 }
|
|
62
|
+
],
|
|
54
63
|
"internal": [
|
|
55
64
|
"MessageBubble 内部结构(.msg-row / .msg-main / .msg-thinking)",
|
|
56
65
|
"ChatInput 内部结构(.tbox-media-picker、发送按钮)",
|
package/src/shell.test.ts
CHANGED
|
@@ -43,6 +43,16 @@ describe('壳契约', () => {
|
|
|
43
43
|
expect(region.drivenBy.length, `${region.id} 缺 drivenBy`).toBeGreaterThan(0);
|
|
44
44
|
}
|
|
45
45
|
});
|
|
46
|
+
|
|
47
|
+
it('控件契约的 data-tbox-control 都在源码里', () => {
|
|
48
|
+
const sources = ['components/ChatInput/index.tsx', 'components/HistoryEntry/index.tsx', 'components/ScrollToBottomButton/index.tsx'];
|
|
49
|
+
for (const control of SHELL_CONTRACT.controls ?? []) {
|
|
50
|
+
const attr = /data-tbox-control=['"]([^'"]+)['"]/.exec(control.selector)?.[1];
|
|
51
|
+
expect(attr, `控件 ${control.id} 的选择器必须用 data-tbox-control`).toBeTruthy();
|
|
52
|
+
expect(sources.some((file) => read(file).includes('data-tbox-control="' + attr + '"')), `控件 ${control.id} 在源码里不存在`).toBe(true);
|
|
53
|
+
expect(control.minHit).toBeGreaterThanOrEqual(44);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
46
56
|
});
|
|
47
57
|
|
|
48
58
|
describe('品牌名三级来源', () => {
|
package/src/shell.ts
CHANGED
|
@@ -28,8 +28,23 @@ export interface ShellRegion {
|
|
|
28
28
|
brandable: boolean;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
export interface ShellControl {
|
|
32
|
+
/** 稳定 id(探针与文档引用) */
|
|
33
|
+
id: string;
|
|
34
|
+
/** 可定位的选择器(data-tbox-control) */
|
|
35
|
+
selector: string;
|
|
36
|
+
/** 可访问名 */
|
|
37
|
+
name: string;
|
|
38
|
+
/** 最小命中区(px) */
|
|
39
|
+
minHit: number;
|
|
40
|
+
/** 能力开关(关闭时控件可缺席) */
|
|
41
|
+
capability?: string | string[];
|
|
42
|
+
}
|
|
43
|
+
|
|
31
44
|
export interface ShellContract {
|
|
32
45
|
regions: ShellRegion[];
|
|
46
|
+
/** 交互控件契约:探针按它查,不按内部类名猜 */
|
|
47
|
+
controls?: ShellControl[];
|
|
33
48
|
/** 不承诺稳定、不要覆写的内部结构 */
|
|
34
49
|
internal: string[];
|
|
35
50
|
}
|
package/src/theme/contract.json
CHANGED
|
@@ -111,6 +111,83 @@
|
|
|
111
111
|
"bg": "--tbox-bg-card",
|
|
112
112
|
"min": 4.5,
|
|
113
113
|
"usage": "链接与主色文字"
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
"fg": "--tbox-primary-light",
|
|
117
|
+
"bg": "--tbox-primary-dark",
|
|
118
|
+
"min": 4.5,
|
|
119
|
+
"usage": "深底主视觉文字(hero tone=inverse)"
|
|
120
|
+
}
|
|
121
|
+
],
|
|
122
|
+
"derive": {
|
|
123
|
+
"$comment": "浅色模式派生规则(pnpm exec tbox-app theme derive 消费):from = seed | accent;l 为相对种子的 L 增量,L/C 为绝对目标;派生结果须 pairs 全部闭合。键必须 ⊆ platform。",
|
|
124
|
+
"--tbox-primary": {
|
|
125
|
+
"from": "seed"
|
|
126
|
+
},
|
|
127
|
+
"--tbox-primary-dark": {
|
|
128
|
+
"from": "seed",
|
|
129
|
+
"l": -0.12
|
|
130
|
+
},
|
|
131
|
+
"--tbox-primary-light": {
|
|
132
|
+
"from": "seed",
|
|
133
|
+
"L": 0.96,
|
|
134
|
+
"C": 0.03
|
|
135
|
+
},
|
|
136
|
+
"--tbox-accent": {
|
|
137
|
+
"from": "accent"
|
|
138
|
+
},
|
|
139
|
+
"--tbox-accent-light": {
|
|
140
|
+
"from": "accent",
|
|
141
|
+
"L": 0.96,
|
|
142
|
+
"C": 0.03
|
|
143
|
+
},
|
|
144
|
+
"--tbox-bg-page": {
|
|
145
|
+
"from": "seed",
|
|
146
|
+
"L": 0.975,
|
|
147
|
+
"C": 0.008,
|
|
148
|
+
"mood": {
|
|
149
|
+
"warm": {
|
|
150
|
+
"H": 80
|
|
151
|
+
},
|
|
152
|
+
"cool": {
|
|
153
|
+
"H": 240
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
"--tbox-bg-card": {
|
|
158
|
+
"from": "seed",
|
|
159
|
+
"L": 0.99,
|
|
160
|
+
"C": 0.004
|
|
161
|
+
},
|
|
162
|
+
"--tbox-bg-subtle": {
|
|
163
|
+
"from": "seed",
|
|
164
|
+
"L": 0.95,
|
|
165
|
+
"C": 0.01
|
|
166
|
+
},
|
|
167
|
+
"--tbox-text-primary": {
|
|
168
|
+
"from": "seed",
|
|
169
|
+
"L": 0.25,
|
|
170
|
+
"C": 0.02
|
|
171
|
+
},
|
|
172
|
+
"--tbox-text-secondary": {
|
|
173
|
+
"from": "seed",
|
|
174
|
+
"L": 0.42,
|
|
175
|
+
"C": 0.02
|
|
176
|
+
},
|
|
177
|
+
"--tbox-text-muted": {
|
|
178
|
+
"from": "seed",
|
|
179
|
+
"L": 0.52,
|
|
180
|
+
"C": 0.015
|
|
181
|
+
},
|
|
182
|
+
"--tbox-border": {
|
|
183
|
+
"from": "seed",
|
|
184
|
+
"L": 0.9,
|
|
185
|
+
"C": 0.01
|
|
186
|
+
},
|
|
187
|
+
"--tbox-border-strong": {
|
|
188
|
+
"from": "seed",
|
|
189
|
+
"L": 0.82,
|
|
190
|
+
"C": 0.015
|
|
114
191
|
}
|
|
115
|
-
|
|
192
|
+
}
|
|
116
193
|
}
|
|
@@ -18,6 +18,7 @@ const contract = JSON.parse(readFileSync(path.join(themeDir, 'contract.json'), '
|
|
|
18
18
|
platform: string[];
|
|
19
19
|
domains: string[];
|
|
20
20
|
assets: string[];
|
|
21
|
+
derive?: Record<string, { from: 'seed' | 'accent'; L?: number; C?: number; l?: number; mood?: Record<string, { H: number }> }>;
|
|
21
22
|
};
|
|
22
23
|
|
|
23
24
|
function extractRootVars(file: string): string[] {
|
|
@@ -55,6 +56,21 @@ describe('主题契约(contract.json)', () => {
|
|
|
55
56
|
for (const key of contract.assets) expect(key.startsWith('--tbox-asset-')).toBe(true);
|
|
56
57
|
});
|
|
57
58
|
|
|
59
|
+
it('derive 段的键 ⊆ platform,且带 from 来源', () => {
|
|
60
|
+
const derive = contract.derive ?? {};
|
|
61
|
+
const allowed = new Set(contract.platform);
|
|
62
|
+
const drift = Object.entries(derive)
|
|
63
|
+
.filter(([key]) => !key.startsWith('$'))
|
|
64
|
+
.filter(([key]) => !allowed.has(key))
|
|
65
|
+
.map(([key]) => key);
|
|
66
|
+
expect(drift, 'derive 声明了契约外的令牌:' + drift.join(', ')).toEqual([]);
|
|
67
|
+
for (const [key, rule] of Object.entries(derive)) {
|
|
68
|
+
if (key.startsWith('$')) continue;
|
|
69
|
+
expect(['seed', 'accent']).toContain(rule.from);
|
|
70
|
+
}
|
|
71
|
+
expect(Object.keys(derive).length).toBeGreaterThan(8);
|
|
72
|
+
});
|
|
73
|
+
|
|
58
74
|
it('motion 族 5 键在清单内(主题轨 T-2.1 契约)', () => {
|
|
59
75
|
for (const key of [
|
|
60
76
|
'--tbox-motion-duration-fast',
|
package/src/theme/tokens.css
CHANGED