@tbox.cn/app-agent-ui-kit 1.0.0-alpha.15 → 1.0.0-alpha.17
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/package.json +3 -3
- package/src/ChatPage.tsx +204 -27
- package/src/card/confirm-fields.test.tsx +106 -0
- package/src/card/primitives.tsx +270 -59
- package/src/card/use-card-actions.ts +5 -1
- package/src/card/view-card.tsx +113 -22
- package/src/chat-config.ts +2 -0
- package/src/components/ActionBar/DebugButton.tsx +12 -4
- package/src/components/ActionBar/index.tsx +61 -27
- package/src/components/BottomSheet/index.tsx +26 -2
- package/src/components/QuickCommands/index.tsx +60 -14
- package/src/components/ToolCallBlock/detail.test.ts +2 -0
- package/src/components/ToolCallBlock/detail.ts +9 -2
- package/src/contract.test.tsx +176 -0
- package/src/hooks/use-stick-to-bottom.test.tsx +101 -0
- package/src/hooks/use-stick-to-bottom.ts +35 -7
- package/src/index.ts +2 -0
- package/src/overlay/Overlay.tsx +21 -2
- package/src/overlay/overlay.test.tsx +15 -0
- package/src/preview-provider.tsx +8 -0
- package/src/shell.contract.json +1 -1
- package/src/shell.test.ts +105 -2
- package/src/theme/base.css +42 -2
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { renderToStaticMarkup } from 'react-dom/server';
|
|
3
|
+
import { ActionList, CardActionButton, CardButton, ConfirmSheet } from './card/primitives';
|
|
4
|
+
import { Overlay } from './overlay';
|
|
5
|
+
import { welcomeSurfaceClasses } from './ChatPage';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 平台交互契约的 L1(零浏览器)守卫。
|
|
9
|
+
*
|
|
10
|
+
* 这些断言覆盖的是「用错一次就全站不可用」的静态类与结构不变量:
|
|
11
|
+
* 真浏览器里的几何命中测试在 L2 探针(stage / 发布闸)跑,ACI 镜像没有浏览器。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const html = (node: React.ReactElement) => renderToStaticMarkup(node);
|
|
15
|
+
|
|
16
|
+
/** 从静态标记里取出带某个 data-* 标记的标签,便于按属性断言 class(属性顺序不敏感)。 */
|
|
17
|
+
function tagOf(markup: string, attr: string): { className: string } {
|
|
18
|
+
const re = new RegExp(`<[a-z]+[^>]*${attr}[^>]*>`, 'i');
|
|
19
|
+
const tag = re.exec(markup)?.[0] ?? '';
|
|
20
|
+
const className = /class="([^"]*)"/.exec(tag)?.[1] ?? '';
|
|
21
|
+
return { className };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const settled = { status: 'settled' as const, outcome: 'updated' as const };
|
|
25
|
+
const api = {
|
|
26
|
+
send: () => Promise.resolve(settled),
|
|
27
|
+
invoke: () => Promise.resolve(settled),
|
|
28
|
+
disabled: false,
|
|
29
|
+
isHistory: false,
|
|
30
|
+
isGenerating: false,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
describe('Overlay 层叠不变量(L1)', () => {
|
|
34
|
+
it('bottom 弹层:遮罩下沉到 -z-[1]、面板只带定位上下文(不注入 z-index)', () => {
|
|
35
|
+
const markup = html(
|
|
36
|
+
<Overlay open onDismiss={() => {}} labelledBy="t" placement="bottom">
|
|
37
|
+
<span>正文</span>
|
|
38
|
+
</Overlay>,
|
|
39
|
+
);
|
|
40
|
+
const mask = tagOf(markup, 'data-tbox-overlay-mask');
|
|
41
|
+
const panel = tagOf(markup, 'data-tbox-overlay-panel');
|
|
42
|
+
// 主机制:遮罩恒在弹层根的最底层(与上游 alpha.16 的修法对齐)
|
|
43
|
+
expect(mask.className.split(/\s+/)).toContain('-z-[1]');
|
|
44
|
+
// 面板只负责定位上下文(锚定卡内 absolute 子元素);层叠由遮罩 -z-[1] 单点决定。
|
|
45
|
+
const panelClasses = panel.className.split(/\s+/);
|
|
46
|
+
expect(panelClasses).toContain('relative');
|
|
47
|
+
expect(panelClasses.some((cls) => /^z-(10|\[)/.test(cls))).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('placement=none:不注入定位与 z-index(调用方自带 fixed/absolute 即层叠依据)', () => {
|
|
51
|
+
const markup = html(
|
|
52
|
+
<Overlay open onDismiss={() => {}} labelledBy="t" placement="none" panelClassName="fixed inset-x-0 top-0">
|
|
53
|
+
<span>正文</span>
|
|
54
|
+
</Overlay>,
|
|
55
|
+
);
|
|
56
|
+
const panel = tagOf(markup, 'data-tbox-overlay-panel');
|
|
57
|
+
const panelClasses = panel.className.split(/\s+/);
|
|
58
|
+
expect(panelClasses).not.toContain('relative');
|
|
59
|
+
expect(panelClasses.some((cls) => /^z-(10|\[)/.test(cls))).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('ConfirmSheet 宽屏收口:面板宽度收口到 448px(内容列 720 太宽)与拖拽条', () => {
|
|
63
|
+
const markup = html(<ConfirmSheet open title="确认入会" onConfirm={() => {}} onCancel={() => {}} highlights={['+100 积分']} />);
|
|
64
|
+
expect(markup).toContain('max-w-md');
|
|
65
|
+
expect(markup).toContain('tbox-sheet__handle');
|
|
66
|
+
expect(markup).toContain('+100 积分');
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe('欢迎区表面(L1)', () => {
|
|
71
|
+
// 层序陷阱:`@layer base` 里的颜色覆写压不过 utilities 层的 `text-foreground`,
|
|
72
|
+
// 所以 brand 变体必须由组合出来的工具类给颜色(否则深底 + 深字,实测 1.01:1)。
|
|
73
|
+
it('brand:容器带 brand 类,标题/副标题改走 primary-foreground', () => {
|
|
74
|
+
const cls = welcomeSurfaceClasses('brand');
|
|
75
|
+
expect(cls.container).toContain('chat-page__welcome--brand');
|
|
76
|
+
expect(cls.title).toBe('text-primary-foreground');
|
|
77
|
+
expect(cls.subtitle).toBe('text-primary-foreground');
|
|
78
|
+
expect(cls.title).not.toContain('text-foreground');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('default / subtle:不误伤出厂深色文字', () => {
|
|
82
|
+
expect(welcomeSurfaceClasses('default')).toEqual({ container: '', title: 'text-foreground', subtitle: 'text-muted-foreground' });
|
|
83
|
+
const subtle = welcomeSurfaceClasses('subtle');
|
|
84
|
+
expect(subtle.container).toContain('bg-card');
|
|
85
|
+
expect(subtle.title).toBe('text-foreground');
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('按钮基座(L1)', () => {
|
|
90
|
+
it('secondary 变体保留边框:基础类不得出现 border-0', () => {
|
|
91
|
+
const markup = html(<CardButton variant="secondary">带我去</CardButton>);
|
|
92
|
+
const classes = /class="([^"]*)"/.exec(markup)?.[1] ?? '';
|
|
93
|
+
expect(classes).not.toContain('border-0');
|
|
94
|
+
expect(classes).toContain('border-border-strong');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('primary 变体边框透明但宽度存在(视觉胶囊不塌)', () => {
|
|
98
|
+
const markup = html(<CardButton>确认</CardButton>);
|
|
99
|
+
expect(markup).toContain('border-transparent');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('size="sm" 真正生效:尺寸类不与变体冲突(回归:变体曾自带 px-4/text-sm 把 sm 覆盖掉)', () => {
|
|
103
|
+
const markup = html(<CardButton size="sm">查看</CardButton>);
|
|
104
|
+
const classes = /class="([^"]*)"/.exec(markup)?.[1] ?? '';
|
|
105
|
+
expect(classes).toContain('text-xs');
|
|
106
|
+
expect(classes).toContain('px-3');
|
|
107
|
+
expect(classes).not.toContain('text-sm');
|
|
108
|
+
expect(classes).not.toContain('px-4');
|
|
109
|
+
// 命中区不因 sm 缩水
|
|
110
|
+
expect(classes).toContain('min-h-11');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('link 变体用自己的内距(不用胶囊内距撑空白)', () => {
|
|
114
|
+
const markup = html(<CardButton variant="link">查看全部</CardButton>);
|
|
115
|
+
const classes = /class="([^"]*)"/.exec(markup)?.[1] ?? '';
|
|
116
|
+
expect(classes).toContain('px-1');
|
|
117
|
+
expect(classes).not.toContain('px-4');
|
|
118
|
+
expect(classes).toContain('w-auto');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('loading 渲染 spinner 且带 aria-busy', () => {
|
|
122
|
+
const markup = html(<CardButton loading>处理中</CardButton>);
|
|
123
|
+
expect(markup).toContain('tbox-btn__spinner');
|
|
124
|
+
expect(markup).toContain('aria-busy="true"');
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('ActionList 双按钮并排(L1)', () => {
|
|
129
|
+
it('两个短按钮单行并排:DOM 顺序仍为主→次,靠 flex-row-reverse 视觉反转', () => {
|
|
130
|
+
const markup = html(
|
|
131
|
+
<ActionList
|
|
132
|
+
api={api}
|
|
133
|
+
actions={[
|
|
134
|
+
{ label: '碰一下入会', invoke: { id: 'join' } },
|
|
135
|
+
{ label: '先去逛逛', send: '今天有什么活动?' },
|
|
136
|
+
]}
|
|
137
|
+
/>,
|
|
138
|
+
);
|
|
139
|
+
expect(markup).toContain('tbox-action-list__row--inline');
|
|
140
|
+
expect(markup).toContain('flex-row-reverse');
|
|
141
|
+
// DOM 顺序:主按钮文案先出现(键盘首焦与按索引取按钮的既有契约)
|
|
142
|
+
expect(markup.indexOf('碰一下入会')).toBeLessThan(markup.indexOf('先去逛逛'));
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('长文案退回「整行主按钮 + 次行并排」布局', () => {
|
|
146
|
+
const markup = html(
|
|
147
|
+
<ActionList
|
|
148
|
+
api={api}
|
|
149
|
+
actions={[
|
|
150
|
+
{ label: '使用 2 小时停车抵扣券支付', invoke: { id: 'pay' } },
|
|
151
|
+
{ label: '直接支付', invoke: { id: 'pay2' } },
|
|
152
|
+
]}
|
|
153
|
+
/>,
|
|
154
|
+
);
|
|
155
|
+
expect(markup).not.toContain('flex-row-reverse');
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe('CardActionButton(三件套复用单飞/确认)', () => {
|
|
160
|
+
it('渲染单个胶囊按钮,禁用态跟随 api.disabled', () => {
|
|
161
|
+
const markup = html(
|
|
162
|
+
<CardActionButton api={{ ...api, disabled: true }} action={{ label: '领取', invoke: { id: 'claim' } }} />,
|
|
163
|
+
);
|
|
164
|
+
expect(markup).toContain('领取');
|
|
165
|
+
expect(markup).toContain('disabled');
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('action.style=primary 时用实心变体;缺省为描边次级', () => {
|
|
169
|
+
const primary = html(
|
|
170
|
+
<CardActionButton api={api} action={{ label: '去支付', invoke: { id: 'pay' }, style: 'primary' }} />,
|
|
171
|
+
);
|
|
172
|
+
const secondary = html(<CardActionButton api={api} action={{ label: '取消', send: '算了' }} />);
|
|
173
|
+
expect(primary).toContain('bg-primary');
|
|
174
|
+
expect(secondary).toContain('border-border-strong');
|
|
175
|
+
});
|
|
176
|
+
});
|
|
@@ -63,6 +63,20 @@ function Probe({ target }: { target: ReturnType<typeof makeScrollable> }) {
|
|
|
63
63
|
);
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
/** enabled 可变的探针:验证空会话首页关闭贴底、切回对话态后接力。 */
|
|
67
|
+
function EnabledProbe({ target, enabled }: { target: ReturnType<typeof makeScrollable>; enabled: boolean }) {
|
|
68
|
+
const scrollRef = useRef<HTMLElement | null>(target.scroll);
|
|
69
|
+
const contentRef = useRef<HTMLElement | null>(target.content);
|
|
70
|
+
const stick = useStickToBottom({ scrollRef, contentRef, enabled });
|
|
71
|
+
return (
|
|
72
|
+
<div>
|
|
73
|
+
<span data-testid="jump">{String(stick.showJump)}</span>
|
|
74
|
+
<button type="button" data-testid="follow" onClick={() => stick.follow()}>follow</button>
|
|
75
|
+
<button type="button" data-testid="hold" onClick={() => stick.hold()}>hold</button>
|
|
76
|
+
</div>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
66
80
|
/** 冲刷一轮 ResizeObserver 回调 + 等 happy-dom 的 rAF 真正执行 */
|
|
67
81
|
async function flushFrame() {
|
|
68
82
|
await act(async () => {
|
|
@@ -139,4 +153,91 @@ describe('贴底跟随', () => {
|
|
|
139
153
|
expect(scroller.top).toBe(500);
|
|
140
154
|
expect(el.querySelector('[data-testid="jump"]')!.textContent).toBe('false');
|
|
141
155
|
});
|
|
156
|
+
|
|
157
|
+
it('enabled=false:挂载与内容长高都不滚底(空会话首页保持首屏)', async () => {
|
|
158
|
+
const scroller = makeScrollable({ scrollHeight: 800, clientHeight: 300 });
|
|
159
|
+
const el = document.createElement('div');
|
|
160
|
+
document.body.appendChild(el);
|
|
161
|
+
const root = createRoot(el);
|
|
162
|
+
roots.push(root);
|
|
163
|
+
await act(async () => root.render(<EnabledProbe target={scroller} enabled={false} />));
|
|
164
|
+
await flushFrame();
|
|
165
|
+
expect(scroller.top).toBe(0);
|
|
166
|
+
scroller.grow(500);
|
|
167
|
+
await flushFrame();
|
|
168
|
+
expect(scroller.top).toBe(0);
|
|
169
|
+
expect(el.querySelector('[data-testid="jump"]')!.textContent).toBe('false');
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('enabled=false → true:send 的 follow 在关闭期记账,开启后立即贴底', async () => {
|
|
173
|
+
const scroller = makeScrollable({ scrollHeight: 800, clientHeight: 300 });
|
|
174
|
+
const el = document.createElement('div');
|
|
175
|
+
document.body.appendChild(el);
|
|
176
|
+
const root = createRoot(el);
|
|
177
|
+
roots.push(root);
|
|
178
|
+
await act(async () => root.render(<EnabledProbe target={scroller} enabled={false} />));
|
|
179
|
+
await flushFrame();
|
|
180
|
+
await act(async () => {
|
|
181
|
+
el.querySelector<HTMLButtonElement>('[data-testid="follow"]')!.click();
|
|
182
|
+
});
|
|
183
|
+
expect(scroller.top).toBe(0); // 关闭期不写滚动
|
|
184
|
+
await act(async () => root.render(<EnabledProbe target={scroller} enabled />));
|
|
185
|
+
await flushFrame();
|
|
186
|
+
expect(scroller.top).toBe(500);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('enabled=false → true 且 hold 过:开启后不抢历史定位(浮标可见)', async () => {
|
|
190
|
+
const scroller = makeScrollable({ scrollHeight: 800, clientHeight: 300 });
|
|
191
|
+
const el = document.createElement('div');
|
|
192
|
+
document.body.appendChild(el);
|
|
193
|
+
const root = createRoot(el);
|
|
194
|
+
roots.push(root);
|
|
195
|
+
await act(async () => root.render(<EnabledProbe target={scroller} enabled={false} />));
|
|
196
|
+
await flushFrame();
|
|
197
|
+
await act(async () => {
|
|
198
|
+
el.querySelector<HTMLButtonElement>('[data-testid="hold"]')!.click();
|
|
199
|
+
});
|
|
200
|
+
await act(async () => root.render(<EnabledProbe target={scroller} enabled />));
|
|
201
|
+
await flushFrame();
|
|
202
|
+
expect(scroller.top).toBe(0);
|
|
203
|
+
expect(el.querySelector('[data-testid="jump"]')!.textContent).toBe('true');
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('触摸中 enabled 翻转:cleanup 复位 touching,重新启用后仍会贴底', async () => {
|
|
207
|
+
const scroller = makeScrollable({ scrollHeight: 800, clientHeight: 300 });
|
|
208
|
+
const el = document.createElement('div');
|
|
209
|
+
document.body.appendChild(el);
|
|
210
|
+
const root = createRoot(el);
|
|
211
|
+
roots.push(root);
|
|
212
|
+
await act(async () => root.render(<EnabledProbe target={scroller} enabled />));
|
|
213
|
+
await flushFrame();
|
|
214
|
+
// 只 touchstart 不 touchend:模拟手势进行中切走(新建对话/会话重置)
|
|
215
|
+
await act(async () => {
|
|
216
|
+
scroller.scroll.dispatchEvent(new Event('touchstart'));
|
|
217
|
+
});
|
|
218
|
+
await act(async () => root.render(<EnabledProbe target={scroller} enabled={false} />));
|
|
219
|
+
await act(async () => root.render(<EnabledProbe target={scroller} enabled />));
|
|
220
|
+
scroller.grow(500);
|
|
221
|
+
await flushFrame();
|
|
222
|
+
expect(scroller.scroll.scrollHeight - scroller.top - scroller.scroll.clientHeight).toBe(0);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('enabled=true → false:清除「回到底部」浮标', async () => {
|
|
226
|
+
const scroller = makeScrollable({ scrollHeight: 400, clientHeight: 300 });
|
|
227
|
+
const el = document.createElement('div');
|
|
228
|
+
document.body.appendChild(el);
|
|
229
|
+
const root = createRoot(el);
|
|
230
|
+
roots.push(root);
|
|
231
|
+
await act(async () => root.render(<EnabledProbe target={scroller} enabled />));
|
|
232
|
+
await flushFrame();
|
|
233
|
+
scroller.grow(400);
|
|
234
|
+
await act(async () => {
|
|
235
|
+
scroller.scroll.dispatchEvent(new WheelEvent('wheel', { deltaY: -120 }));
|
|
236
|
+
});
|
|
237
|
+
await flushFrame();
|
|
238
|
+
expect(el.querySelector('[data-testid="jump"]')!.textContent).toBe('true');
|
|
239
|
+
await act(async () => root.render(<EnabledProbe target={scroller} enabled={false} />));
|
|
240
|
+
await flushFrame();
|
|
241
|
+
expect(el.querySelector('[data-testid="jump"]')!.textContent).toBe('false');
|
|
242
|
+
});
|
|
142
243
|
});
|
|
@@ -36,6 +36,12 @@ export interface UseStickToBottomOptions {
|
|
|
36
36
|
contentRef: RefObject<HTMLElement | null>;
|
|
37
37
|
/** 离底容差(px),缺省 80 */
|
|
38
38
|
threshold?: number;
|
|
39
|
+
/**
|
|
40
|
+
* 是否启用贴底跟随(缺省 true)。空会话(home 槽首页如 MallHome,或纯对话欢迎态)时关闭:
|
|
41
|
+
* 首屏高度可能超过视口,挂载/内容长高不应把首屏拽到底部;发送消息后随形态翻回 true,
|
|
42
|
+
* 由 send() 的 follow() + ResizeObserver 接力贴底。关闭期间浮标恒隐藏。
|
|
43
|
+
*/
|
|
44
|
+
enabled?: boolean;
|
|
39
45
|
}
|
|
40
46
|
|
|
41
47
|
const DEFAULT_THRESHOLD = 80;
|
|
@@ -44,6 +50,7 @@ const SCROLL_IDLE_MS = 150;
|
|
|
44
50
|
|
|
45
51
|
export function useStickToBottom(opts: UseStickToBottomOptions): StickToBottom {
|
|
46
52
|
const threshold = opts.threshold ?? DEFAULT_THRESHOLD;
|
|
53
|
+
const enabled = opts.enabled ?? true;
|
|
47
54
|
const followingRef = useRef(true);
|
|
48
55
|
const [showJump, setShowJump] = useState(false);
|
|
49
56
|
/** 程序写入的目标值:紧随其后的 scroll 事件按此识别并忽略 */
|
|
@@ -55,21 +62,26 @@ export function useStickToBottom(opts: UseStickToBottomOptions): StickToBottom {
|
|
|
55
62
|
const scrollIdleTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
56
63
|
|
|
57
64
|
const syncJump = useCallback(() => {
|
|
65
|
+
if (!enabled) {
|
|
66
|
+
setShowJump(false);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
58
69
|
const el = opts.scrollRef.current;
|
|
59
70
|
if (!el) return;
|
|
60
71
|
const distance = el.scrollHeight - el.scrollTop - el.clientHeight;
|
|
61
72
|
const shouldShow = !followingRef.current && distance > threshold;
|
|
62
73
|
setShowJump((prev) => (prev === shouldShow ? prev : shouldShow));
|
|
63
|
-
}, [opts.scrollRef, threshold]);
|
|
74
|
+
}, [enabled, opts.scrollRef, threshold]);
|
|
64
75
|
|
|
65
76
|
const writeBottom = useCallback(() => {
|
|
77
|
+
if (!enabled) return;
|
|
66
78
|
const el = opts.scrollRef.current;
|
|
67
79
|
if (!el || !followingRef.current || touchingRef.current) return;
|
|
68
80
|
const target = el.scrollHeight - el.clientHeight;
|
|
69
81
|
if (target <= el.scrollTop + 1) return;
|
|
70
82
|
programmaticTargetRef.current = target;
|
|
71
83
|
el.scrollTop = target;
|
|
72
|
-
}, [opts.scrollRef]);
|
|
84
|
+
}, [enabled, opts.scrollRef]);
|
|
73
85
|
|
|
74
86
|
const follow = useCallback(() => {
|
|
75
87
|
followingRef.current = true;
|
|
@@ -95,8 +107,14 @@ export function useStickToBottom(opts: UseStickToBottomOptions): StickToBottom {
|
|
|
95
107
|
syncJump();
|
|
96
108
|
}, [syncJump]);
|
|
97
109
|
|
|
110
|
+
// 关闭跟随(空会话首页)时清掉浮标:首页没有「回到底部」语义。
|
|
111
|
+
useEffect(() => {
|
|
112
|
+
if (!enabled) setShowJump(false);
|
|
113
|
+
}, [enabled]);
|
|
114
|
+
|
|
98
115
|
// 内容或视口尺寸变化 → 跟随时贴底(流式每帧最多一次写入,靠 rAF 合并)
|
|
99
116
|
useEffect(() => {
|
|
117
|
+
if (!enabled) return undefined;
|
|
100
118
|
const scrollEl = opts.scrollRef.current;
|
|
101
119
|
const contentEl = opts.contentRef.current;
|
|
102
120
|
if (!scrollEl || !contentEl) return undefined;
|
|
@@ -117,10 +135,11 @@ export function useStickToBottom(opts: UseStickToBottomOptions): StickToBottom {
|
|
|
117
135
|
if (frame) cancelAnimationFrame(frame);
|
|
118
136
|
ro.disconnect();
|
|
119
137
|
};
|
|
120
|
-
}, [opts.scrollRef, opts.contentRef, writeBottom, syncJump]);
|
|
138
|
+
}, [enabled, opts.scrollRef, opts.contentRef, writeBottom, syncJump]);
|
|
121
139
|
|
|
122
140
|
// 手势判定:滚轮向上 / 手指下拖 / PageUp·Home·ArrowUp
|
|
123
141
|
useEffect(() => {
|
|
142
|
+
if (!enabled) return undefined;
|
|
124
143
|
const scrollEl = opts.scrollRef.current;
|
|
125
144
|
if (!scrollEl) return undefined;
|
|
126
145
|
// 具名常量:函数声明提升会丢掉 opts.scrollRef.current 的窄化
|
|
@@ -206,13 +225,22 @@ export function useStickToBottom(opts: UseStickToBottomOptions): StickToBottom {
|
|
|
206
225
|
el.removeEventListener('touchcancel', onTouchEnd);
|
|
207
226
|
el.removeEventListener('scroll', onScroll);
|
|
208
227
|
el.removeEventListener('keydown', onKeyDown);
|
|
228
|
+
// 关闭/切换时事件监听被摘除:触摸与程序写入的瞬态必须一起复位,
|
|
229
|
+
// 否则重新启用后 touchingRef 永久为 true,writeBottom 被静默短路。
|
|
230
|
+
touchingRef.current = false;
|
|
231
|
+
touchStartYRef.current = null;
|
|
232
|
+
programmaticTargetRef.current = null;
|
|
209
233
|
if (scrollIdleTimerRef.current) {
|
|
210
234
|
clearTimeout(scrollIdleTimerRef.current);
|
|
211
235
|
scrollIdleTimerRef.current = null;
|
|
212
236
|
}
|
|
213
237
|
};
|
|
214
|
-
}, [opts.scrollRef, detach, follow, writeBottom, syncJump, threshold]);
|
|
215
|
-
|
|
216
|
-
// 返回对象按依赖稳定:否则每次渲染都新建,调用方把它放进 effect
|
|
217
|
-
|
|
238
|
+
}, [enabled, opts.scrollRef, detach, follow, writeBottom, syncJump, threshold]);
|
|
239
|
+
|
|
240
|
+
// 返回对象按依赖稳定:否则每次渲染都新建,调用方把它放进 effect 依赖会连带重跑。
|
|
241
|
+
// showJump 在关闭期直接投影为 false:避免 enabled 翻 false 后的一帧残留浮标。
|
|
242
|
+
return useMemo(
|
|
243
|
+
() => ({ followingRef, showJump: enabled && showJump, follow, hold, resume }),
|
|
244
|
+
[enabled, showJump, follow, hold, resume],
|
|
245
|
+
);
|
|
218
246
|
}
|
package/src/index.ts
CHANGED
|
@@ -52,6 +52,7 @@ export {
|
|
|
52
52
|
ActionRow,
|
|
53
53
|
Bill,
|
|
54
54
|
Card,
|
|
55
|
+
CardActionButton,
|
|
55
56
|
CardButton,
|
|
56
57
|
CardHeader,
|
|
57
58
|
CheckRow,
|
|
@@ -72,6 +73,7 @@ export {
|
|
|
72
73
|
export type {
|
|
73
74
|
ActionRowProps,
|
|
74
75
|
BillLine,
|
|
76
|
+
CardButtonVariant,
|
|
75
77
|
CardActionSpec,
|
|
76
78
|
CardHeaderProps,
|
|
77
79
|
CardProps,
|
package/src/overlay/Overlay.tsx
CHANGED
|
@@ -54,6 +54,23 @@ export interface OverlayProps {
|
|
|
54
54
|
children: ReactNode;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
/**
|
|
58
|
+
* 弹层层叠:**唯一主机制是遮罩下沉**(`-z-[1]`,见下方 mask 处的说明),
|
|
59
|
+
* 面板侧只补「定位上下文」,不再注入任何 z-index——
|
|
60
|
+
*
|
|
61
|
+
* - `bottom`/`center` 补 `relative`:给面板内的 `absolute` 子元素(抽屉悬浮关闭键、拖拽条)一个锚点;
|
|
62
|
+
* `relative` 不带偏移,不影响布局,也不会给调用方强行造新的层叠上下文。
|
|
63
|
+
* - `placement: 'none'` 完全不注入:调用方自带 `fixed`/`absolute`,它自己的定位就是层叠依据;
|
|
64
|
+
* 再补 `relative` 会在工具类声明序上覆盖调用方的定位,补 `z-10` 则会锁死其内部浮层的层级。
|
|
65
|
+
*
|
|
66
|
+
* 历史教训:只靠「入场动画的 transform」维持层级,`prefers-reduced-motion` 关掉动画后 100% 点不到。
|
|
67
|
+
*/
|
|
68
|
+
const PANEL_STACK_CLASS = {
|
|
69
|
+
bottom: 'relative',
|
|
70
|
+
center: 'relative',
|
|
71
|
+
none: '',
|
|
72
|
+
} as const;
|
|
73
|
+
|
|
57
74
|
export function Overlay({
|
|
58
75
|
open,
|
|
59
76
|
onDismiss,
|
|
@@ -125,7 +142,9 @@ export function Overlay({
|
|
|
125
142
|
>
|
|
126
143
|
{mask ? (
|
|
127
144
|
<div
|
|
128
|
-
|
|
145
|
+
// 遮罩固定在弹层根(已自成层叠上下文)的最底层:不依赖面板是否有定位、
|
|
146
|
+
// 也不依赖入场动画的 transform 维持层级 —— 动画结束或 reduced-motion 关掉动画后依然正确。
|
|
147
|
+
className={['absolute inset-0 -z-[1] touch-none', maskClassName ?? 'bg-scrim'].filter(Boolean).join(' ')}
|
|
129
148
|
data-tbox-overlay-mask=""
|
|
130
149
|
aria-hidden="true"
|
|
131
150
|
onClick={onMaskClick}
|
|
@@ -135,7 +154,7 @@ export function Overlay({
|
|
|
135
154
|
<section
|
|
136
155
|
{...panelProps}
|
|
137
156
|
ref={panelRef}
|
|
138
|
-
className={[panelClassName, openState ? '' : 'pointer-events-none'].filter(Boolean).join(' ')}
|
|
157
|
+
className={[PANEL_STACK_CLASS[placement], panelClassName, openState ? '' : 'pointer-events-none'].filter(Boolean).join(' ')}
|
|
139
158
|
data-tbox-overlay-panel=""
|
|
140
159
|
role={openState ? 'dialog' : undefined}
|
|
141
160
|
aria-modal={openState ? true : undefined}
|
|
@@ -62,6 +62,21 @@ function Nested({ opened, onInner }: { opened: boolean; onInner: () => void }) {
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
describe('Overlay', () => {
|
|
65
|
+
it('遮罩固定在弹层根的底层,不依赖面板入场动画或定位方式', async () => {
|
|
66
|
+
await mount(<Overlay open onDismiss={vi.fn()} placement="none" panelClassName="fixed bottom-0">
|
|
67
|
+
<p>详情正文</p>
|
|
68
|
+
<footer className="sticky bottom-0"><button type="button">立即参与</button></footer>
|
|
69
|
+
</Overlay>);
|
|
70
|
+
const overlay = document.querySelector('[data-tbox-overlay]') as HTMLElement;
|
|
71
|
+
const mask = overlay.querySelector('[data-tbox-overlay-mask]') as HTMLElement;
|
|
72
|
+
const panel = overlay.querySelector('[data-tbox-overlay-panel]') as HTMLElement;
|
|
73
|
+
expect(overlay.classList.contains('fixed')).toBe(true);
|
|
74
|
+
expect(overlay.classList.contains('z-[1100]')).toBe(true);
|
|
75
|
+
expect(mask.classList.contains('-z-[1]')).toBe(true);
|
|
76
|
+
expect(panel.classList.contains('fixed')).toBe(true);
|
|
77
|
+
expect(panel.classList.contains('relative')).toBe(false);
|
|
78
|
+
});
|
|
79
|
+
|
|
65
80
|
it('传送进 body,面板带 role/aria 与题注', async () => {
|
|
66
81
|
await mount(<Sheet onDismiss={vi.fn()} />);
|
|
67
82
|
const dialog = document.querySelector('[role="dialog"]') as HTMLElement;
|
package/src/preview-provider.tsx
CHANGED
|
@@ -21,6 +21,14 @@ export function createPreviewStore(state: Partial<CoreChatState> = {}): CoreChat
|
|
|
21
21
|
// 预览没有服务端:动作一律按 rejected 返回,按钮立即恢复(不进入挂起)
|
|
22
22
|
sendUiAction: () => ({ status: 'rejected' as const, reason: 'offline' as const }),
|
|
23
23
|
isGenerating: false,
|
|
24
|
+
// 结果卡呈递相关的客户端状态:预览页没有 WS,给稳定空值,避免 ChatPage 读到 undefined。
|
|
25
|
+
cardsByMessageId: {},
|
|
26
|
+
orphanCards: [],
|
|
27
|
+
cardAnchors: {},
|
|
28
|
+
cardSeq: 0,
|
|
29
|
+
activeSheetCardId: null,
|
|
30
|
+
openSheetCard: () => {},
|
|
31
|
+
closeSheetCard: () => {},
|
|
24
32
|
...state,
|
|
25
33
|
} as unknown as CoreChatState));
|
|
26
34
|
const hook = ((selector: (state: CoreChatState) => unknown) =>
|
package/src/shell.contract.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"invariants": [
|
|
4
4
|
{ "id": "no-fixed-chrome", "text": "滚动区之上不渲染任何常驻元素(没有页头、没有固定标题栏)" },
|
|
5
5
|
{ "id": "input-rows", "text": "空输入框高度 = 行高 × chat.json input.minRows;输入框外框命中区 ≥44px" },
|
|
6
|
-
{ "id": "follow-latest", "text": "
|
|
6
|
+
{ "id": "follow-latest", "text": "对话态跟随时内容增长后离底 ≤2px;用户手势上滑后停止跟随并显示「回到底部」。空会话(含 home 槽首页与欢迎态)不启用贴底(首屏停在顶部),发送消息或进入对话后恢复。" },
|
|
7
7
|
{ "id": "action-settle", "text": "卡片动作的结束信号到达后同一帧解锁按钮,不靠超时兜底" }
|
|
8
8
|
],
|
|
9
9
|
"regions": [
|
package/src/shell.test.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { ChatPage } from './ChatPage';
|
|
|
10
10
|
import { configureChatConfigDefaults, useChatConfigStore } from './chat-config';
|
|
11
11
|
import { createPreviewStore } from './preview-provider';
|
|
12
12
|
import { SHELL_CONTRACT } from './shell';
|
|
13
|
+
import { triggerResizeObservers } from './test/setup';
|
|
13
14
|
|
|
14
15
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
15
16
|
const read = (rel: string) => readFileSync(path.join(here, rel), 'utf8');
|
|
@@ -125,8 +126,12 @@ describe('商场对话壳的主题布局', () => {
|
|
|
125
126
|
// 壳没有页头:紧凑与非紧凑都不渲染常驻品牌区
|
|
126
127
|
expect(container.querySelector('.chat-page__brand')).toBeNull();
|
|
127
128
|
expect(container.querySelector('.chat-page__body')?.classList.contains('justify-center')).toBe(false);
|
|
128
|
-
|
|
129
|
-
expect(
|
|
129
|
+
// 有 home 槽:快捷胶囊固定在输入框上方,不沉到首页正文底部
|
|
130
|
+
expect(container.querySelector('.chat-page__body .quick-commands')).toBeNull();
|
|
131
|
+
expect(container.querySelector('.quick-commands + .tbox-chat-input')).not.toBeNull();
|
|
132
|
+
expect(
|
|
133
|
+
container.querySelector('.quick-commands')?.classList.contains(compact ? 'quick-commands--compact' : 'quick-commands--row'),
|
|
134
|
+
).toBe(true);
|
|
130
135
|
expect(container.querySelector('textarea')?.classList.contains('h-6')).toBe(compact);
|
|
131
136
|
expect(container.querySelector('.history-entry__button')?.classList.contains('min-h-11')).toBe(!compact);
|
|
132
137
|
} finally {
|
|
@@ -136,6 +141,27 @@ describe('商场对话壳的主题布局', () => {
|
|
|
136
141
|
useChatConfigStore.setState(previousConfig, true);
|
|
137
142
|
}
|
|
138
143
|
});
|
|
144
|
+
|
|
145
|
+
it('无 home 槽的纯对话欢迎态保留正文内两列网格', async () => {
|
|
146
|
+
const previousConfig = useChatConfigStore.getState();
|
|
147
|
+
const container = document.createElement('div');
|
|
148
|
+
document.body.append(container);
|
|
149
|
+
const root = createRoot(container);
|
|
150
|
+
const store = createPreviewStore({
|
|
151
|
+
messages: [], historyMessages: [], historyHasMore: false, orphanCards: [], cardsByMessageId: {},
|
|
152
|
+
runtimeStatus: 'ready', initConversation: async () => {}, destroy: () => {},
|
|
153
|
+
});
|
|
154
|
+
try {
|
|
155
|
+
configureChatConfigDefaults({ quickCommands: [{ label: '优惠', query: '有优惠吗?' }] });
|
|
156
|
+
await act(async () => root.render(createElement(ChatPage, { store, cards: {} })));
|
|
157
|
+
expect(container.querySelector('.chat-page__body .quick-commands--grid')).not.toBeNull();
|
|
158
|
+
expect(container.querySelector('.quick-commands + .tbox-chat-input')).toBeNull();
|
|
159
|
+
} finally {
|
|
160
|
+
await act(async () => root.unmount());
|
|
161
|
+
container.remove();
|
|
162
|
+
useChatConfigStore.setState(previousConfig, true);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
139
165
|
});
|
|
140
166
|
|
|
141
167
|
describe('历史首次打开', () => {
|
|
@@ -166,3 +192,80 @@ describe('历史首次打开', () => {
|
|
|
166
192
|
}
|
|
167
193
|
});
|
|
168
194
|
});
|
|
195
|
+
|
|
196
|
+
describe('空态滚动复位', () => {
|
|
197
|
+
const message = (id: string) => ({ id, role: 'user' as const, status: 'done' as const, content: id, createdAt: 1 });
|
|
198
|
+
/** 冲刷一轮 ResizeObserver 回调 + 等 rAF 真正执行(贴底写在 rAF 里)。 */
|
|
199
|
+
const flushFrame = async () => {
|
|
200
|
+
await act(async () => {
|
|
201
|
+
triggerResizeObservers();
|
|
202
|
+
await new Promise<void>((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => resolve())));
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* 空会话四段断言(home 槽首页与纯对话欢迎态共用):
|
|
208
|
+
* 空态不被拽底 → 进对话贴底 → 上滑脱离后经非按钮路径清空 → 回顶且恢复跟随 → 非 send() 首消息仍贴底。
|
|
209
|
+
*/
|
|
210
|
+
async function expectEmptyStateRecovery(slots?: ClientSlotRegistry) {
|
|
211
|
+
const container = document.createElement('div');
|
|
212
|
+
document.body.append(container);
|
|
213
|
+
const root = createRoot(container);
|
|
214
|
+
const store = createPreviewStore({
|
|
215
|
+
messages: [], historyMessages: [], historyHasMore: false, orphanCards: [], cardsByMessageId: {},
|
|
216
|
+
runtimeStatus: 'ready', initConversation: async () => {}, destroy: () => {},
|
|
217
|
+
});
|
|
218
|
+
try {
|
|
219
|
+
await act(async () => root.render(createElement(ChatPage, { store, cards: {}, ...(slots ? { slots } : {}) })));
|
|
220
|
+
const body = container.querySelector<HTMLElement>('.chat-page__body')!;
|
|
221
|
+
// 内容 1200 / 视口 400 → 贴底位 = 800;写法与真实滚动容器同形(含 clamp 与 scroll 事件)。
|
|
222
|
+
let scrollTop = 0;
|
|
223
|
+
Object.defineProperty(body, 'scrollHeight', { configurable: true, get: () => 1200 });
|
|
224
|
+
Object.defineProperty(body, 'clientHeight', { configurable: true, get: () => 400 });
|
|
225
|
+
Object.defineProperty(body, 'scrollTop', {
|
|
226
|
+
configurable: true,
|
|
227
|
+
get: () => scrollTop,
|
|
228
|
+
set: (value: number) => {
|
|
229
|
+
scrollTop = Math.max(0, Math.min(value, 800));
|
|
230
|
+
body.dispatchEvent(new Event('scroll'));
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
const jumpVisible = () => container
|
|
234
|
+
.querySelector('[data-tbox-control="scroll-bottom"]')!
|
|
235
|
+
.classList.contains('scroll-to-bottom-btn--visible');
|
|
236
|
+
|
|
237
|
+
await flushFrame();
|
|
238
|
+
expect(scrollTop).toBe(0);
|
|
239
|
+
expect(jumpVisible()).toBe(false);
|
|
240
|
+
|
|
241
|
+
await act(async () => store.setState({ messages: [message('m1')] }));
|
|
242
|
+
await flushFrame();
|
|
243
|
+
expect(scrollTop).toBe(800);
|
|
244
|
+
|
|
245
|
+
// 用户上滑脱离跟随,再经非按钮路径清空回空会话:应回到顶部并恢复跟随态
|
|
246
|
+
await act(async () => body.dispatchEvent(new WheelEvent('wheel', { deltaY: -120 })));
|
|
247
|
+
await act(async () => store.setState({ messages: [] }));
|
|
248
|
+
await flushFrame();
|
|
249
|
+
expect(scrollTop).toBe(0);
|
|
250
|
+
expect(jumpVisible()).toBe(false);
|
|
251
|
+
|
|
252
|
+
// 模拟 runAutoSend / 服务端首推等非 send() 路径:恢复后的跟随态应再次贴底
|
|
253
|
+
await act(async () => store.setState({ messages: [message('m2')] }));
|
|
254
|
+
await flushFrame();
|
|
255
|
+
expect(scrollTop).toBe(800);
|
|
256
|
+
} finally {
|
|
257
|
+
await act(async () => root.unmount());
|
|
258
|
+
container.remove();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
it('home 槽首页:首屏不被拽底;进对话贴底;清空回首页回顶并恢复跟随', async () => {
|
|
263
|
+
const slots = new ClientSlotRegistry();
|
|
264
|
+
slots.register('home', () => createElement('main', null, '商场首页'));
|
|
265
|
+
await expectEmptyStateRecovery(slots);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it('无 home 槽欢迎态:长首屏同样不拽底,清空回欢迎区后仍能贴底', async () => {
|
|
269
|
+
await expectEmptyStateRecovery();
|
|
270
|
+
});
|
|
271
|
+
});
|