@tbox.cn/app-agent-ui-kit 0.0.0-stage → 1.0.0-alpha.10
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 +69 -2
- package/assets/keyboard-circle.svg +12 -0
- package/assets/more-circle.svg +3 -0
- package/assets/new-chat.svg +3 -0
- package/assets/quick-command-star.svg +3 -0
- package/assets/send-gradient.svg +14 -0
- package/assets/stop@2x.png +0 -0
- package/assets/voice-circle.svg +3 -0
- package/package.json +70 -3
- package/src/ChatPage.tsx +400 -0
- package/src/card/action-list-pending.test.tsx +91 -0
- package/src/card/define-card.test.ts +25 -0
- package/src/card/define-card.ts +47 -0
- package/src/card/primitives.test.tsx +157 -0
- package/src/card/primitives.tsx +717 -0
- package/src/card/use-card-actions.ts +86 -0
- package/src/card/view-card-inline.test.tsx +42 -0
- package/src/card/view-card.test.tsx +79 -0
- package/src/card/view-card.tsx +250 -0
- package/src/chat-config.test.ts +46 -0
- package/src/chat-config.ts +84 -0
- package/src/components/ActionBar/DebugButton.test.tsx +87 -0
- package/src/components/ActionBar/DebugButton.tsx +48 -0
- package/src/components/ActionBar/index.test.tsx +83 -0
- package/src/components/ActionBar/index.tsx +158 -0
- package/src/components/BottomSheet/index.test.tsx +97 -0
- package/src/components/BottomSheet/index.tsx +49 -0
- package/src/components/CardRenderer.tsx +36 -0
- package/src/components/ChatHeader/index.test.tsx +74 -0
- package/src/components/ChatHeader/index.tsx +77 -0
- package/src/components/ChatInput/index.test.tsx +156 -0
- package/src/components/ChatInput/index.tsx +295 -0
- package/src/components/ChatInput/voice/VoicePressField.tsx +43 -0
- package/src/components/ChatInput/voice/VoiceRecordingOverlay.tsx +51 -0
- package/src/components/ChatInput/voice/machine.ts +27 -0
- package/src/components/ChatInput/voice/useVoicePress.ts +207 -0
- package/src/components/HistoryEntry/index.tsx +42 -0
- package/src/components/MediaList/index.tsx +270 -0
- package/src/components/MessageBubble/index.test.tsx +134 -0
- package/src/components/MessageBubble/index.tsx +208 -0
- package/src/components/PullDownIndicator/index.tsx +31 -0
- package/src/components/QuickCommands/index.test.tsx +101 -0
- package/src/components/QuickCommands/index.tsx +75 -0
- package/src/components/ReasoningBlock/index.tsx +61 -0
- package/src/components/ScrollToBottomButton/index.tsx +12 -0
- package/src/components/ToolCallBlock/detail.test.ts +346 -0
- package/src/components/ToolCallBlock/detail.ts +204 -0
- package/src/components/ToolCallBlock/index.test.tsx +261 -0
- package/src/components/ToolCallBlock/index.tsx +232 -0
- package/src/components/ToolCallBlock/titles.ts +102 -0
- package/src/hooks/use-drag-gesture.test.ts +66 -0
- package/src/hooks/use-drag-gesture.ts +129 -0
- package/src/hooks/use-pull-down-history.ts +241 -0
- package/src/hooks/use-pull-down-wheel-native.test.ts +64 -0
- package/src/hooks/useMediaUpload.ts +242 -0
- package/src/hooks/useTTSAutoPlay.ts +82 -0
- package/src/icons.ts +39 -0
- package/src/index.ts +143 -0
- package/src/overlay/Overlay.tsx +154 -0
- package/src/overlay/focus.ts +53 -0
- package/src/overlay/index.ts +16 -0
- package/src/overlay/inert.ts +27 -0
- package/src/overlay/layers.ts +21 -0
- package/src/overlay/overlay.ssr.test.tsx +27 -0
- package/src/overlay/overlay.test.tsx +209 -0
- package/src/overlay/presence.ts +86 -0
- package/src/overlay/scroll-lock.ts +62 -0
- package/src/overlay/stack.ts +111 -0
- package/src/overlay/use-overlay.ts +80 -0
- package/src/preview-provider.tsx +47 -0
- package/src/shell.contract.json +75 -0
- package/src/shell.test.ts +72 -0
- package/src/shell.ts +60 -0
- package/src/store-context.tsx +92 -0
- package/src/surface.test.tsx +35 -0
- package/src/surface.tsx +44 -0
- package/src/test/setup.ts +7 -0
- package/src/theme/base.css +167 -0
- package/src/theme/contract.json +193 -0
- package/src/theme/motion.css +19 -0
- package/src/theme/theme-contract.test.ts +85 -0
- package/src/theme/theme.ts +17 -0
- package/src/theme/tokens.css +78 -0
- package/src/types.ts +29 -0
- package/src/utils/copyText.ts +11 -0
- package/src/utils/haptics.test.ts +51 -0
- package/src/utils/haptics.ts +44 -0
- package/src/utils/image-preview-host.tsx +95 -0
- package/src/utils/imagePreview.test.ts +32 -0
- package/src/utils/imagePreview.ts +19 -0
- package/src/utils/pull-down-machine.test.ts +188 -0
- package/src/utils/pull-down-machine.ts +157 -0
- package/src/utils/session-error.ts +5 -0
- package/src/utils/toast.test.ts +64 -0
- package/src/utils/toast.ts +108 -0
- package/src/utils/tool-call-display.test.ts +240 -0
- package/src/utils/tool-call-display.ts +146 -0
- package/src/vite-env.d.ts +1 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import type { CoreChatState } from '@tbox.cn/app-agent-sdk-client';
|
|
3
|
+
import { useChatStoreApi } from '../store-context';
|
|
4
|
+
import { useTTSAdapter } from '@tbox.cn/app-agent-sdk-client';
|
|
5
|
+
|
|
6
|
+
const FLUSH_MS = 300; // 累积 delta 后批量送,避免过多 WS 消息
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* TTS 自动播放:开启后订阅流式消息,**边生成边送** TTS 流式播放(对齐原模板的流式自动朗读)。
|
|
10
|
+
* 关闭 / 卸载时停止。
|
|
11
|
+
*/
|
|
12
|
+
export function useTTSAutoPlay(enabled: boolean) {
|
|
13
|
+
const tts = useTTSAdapter();
|
|
14
|
+
const store = useChatStoreApi();
|
|
15
|
+
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
if (!enabled || !tts || !tts.startStreaming) return;
|
|
18
|
+
|
|
19
|
+
let activeId: string | null = null;
|
|
20
|
+
let prevContent = '';
|
|
21
|
+
let pending = '';
|
|
22
|
+
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
23
|
+
|
|
24
|
+
const clearTimer = () => {
|
|
25
|
+
if (timer) {
|
|
26
|
+
clearTimeout(timer);
|
|
27
|
+
timer = null;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const flush = () => {
|
|
31
|
+
clearTimer();
|
|
32
|
+
if (pending) {
|
|
33
|
+
tts.sendText?.(pending);
|
|
34
|
+
pending = '';
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const enqueue = (delta: string) => {
|
|
38
|
+
pending += delta;
|
|
39
|
+
if (!timer) timer = setTimeout(flush, FLUSH_MS);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const handle = (state: CoreChatState) => {
|
|
43
|
+
const { messages, streamingMsgId } = state;
|
|
44
|
+
const msg = messages.find((m) => m.id === streamingMsgId);
|
|
45
|
+
|
|
46
|
+
// 流结束 / 无流式消息 → 结束这轮
|
|
47
|
+
if (!msg || msg.status === 'done') {
|
|
48
|
+
if (activeId) {
|
|
49
|
+
flush();
|
|
50
|
+
tts.finishStreaming?.();
|
|
51
|
+
activeId = null;
|
|
52
|
+
prevContent = '';
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
// 新流式消息 → 开一轮
|
|
57
|
+
if (msg.status === 'streaming' && msg.id !== activeId) {
|
|
58
|
+
tts.stop();
|
|
59
|
+
clearTimer();
|
|
60
|
+
pending = '';
|
|
61
|
+
activeId = msg.id;
|
|
62
|
+
prevContent = '';
|
|
63
|
+
tts.startStreaming?.();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
// 内容增量 → 入队
|
|
67
|
+
if (activeId === msg.id && msg.content.length > prevContent.length) {
|
|
68
|
+
enqueue(msg.content.slice(prevContent.length));
|
|
69
|
+
prevContent = msg.content;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const unsub = store.subscribe(handle);
|
|
74
|
+
handle(store.getState());
|
|
75
|
+
|
|
76
|
+
return () => {
|
|
77
|
+
unsub();
|
|
78
|
+
clearTimer();
|
|
79
|
+
if (activeId) tts.stop();
|
|
80
|
+
};
|
|
81
|
+
}, [enabled, tts, store]);
|
|
82
|
+
}
|
package/src/icons.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 图标入口:应用侧不再直接依赖 `@phosphor-icons/react`。
|
|
3
|
+
*
|
|
4
|
+
* 只再导出卡片与空态常用的一组,含义稳定、体积可控;需要新图标时在此增补,
|
|
5
|
+
* 应用侧 `import { IconName } from '@tbox.cn/app-agent-ui-kit/icons'`。
|
|
6
|
+
*/
|
|
7
|
+
export {
|
|
8
|
+
ArrowRight,
|
|
9
|
+
ArrowsClockwise,
|
|
10
|
+
Calendar,
|
|
11
|
+
CaretDown,
|
|
12
|
+
CaretRight,
|
|
13
|
+
Check,
|
|
14
|
+
CheckCircle,
|
|
15
|
+
Clock,
|
|
16
|
+
Coins,
|
|
17
|
+
Copy,
|
|
18
|
+
CreditCard,
|
|
19
|
+
CurrencyCny,
|
|
20
|
+
Gift,
|
|
21
|
+
Info,
|
|
22
|
+
MapPin,
|
|
23
|
+
MapTrifold,
|
|
24
|
+
Plus,
|
|
25
|
+
QrCode,
|
|
26
|
+
Receipt,
|
|
27
|
+
SealCheck,
|
|
28
|
+
ShareNetwork,
|
|
29
|
+
Sparkle,
|
|
30
|
+
Star,
|
|
31
|
+
Storefront,
|
|
32
|
+
Ticket,
|
|
33
|
+
Trash,
|
|
34
|
+
User,
|
|
35
|
+
UserCircle,
|
|
36
|
+
Warning,
|
|
37
|
+
WarningCircle,
|
|
38
|
+
X,
|
|
39
|
+
} from '@phosphor-icons/react';
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @tbox.cn/app-agent-ui-kit:Agent 对话壳 UI 组件包(源码发布,单一模板统一化 W2-1)。
|
|
3
|
+
*
|
|
4
|
+
* 消费面:应用经 `<ChatPage store cards slots .../>` 单点装配(或自组 KitChatProvider 树);
|
|
5
|
+
* 域模块(如 mall-core)经 home 槽渲染函数自持域 UI——kit 组件零域语义。
|
|
6
|
+
* 主题:`@tbox.cn/app-agent-ui-kit/theme`(applyTheme)+ `theme/tokens.css` + `theme/base.css`(配合纯 CSS 主题包级联)。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// ── 首屏页面与注入位 ──
|
|
10
|
+
export { ChatPage } from './ChatPage';
|
|
11
|
+
export type { ChatPageProps } from './ChatPage';
|
|
12
|
+
// ── 行为面(W2-3 收口:SDK client 导出退役后宿主——下拉加载历史手势族)──
|
|
13
|
+
export { usePullDownHistory } from './hooks/use-pull-down-history';
|
|
14
|
+
export type { UsePullDownHistoryOptions } from './hooks/use-pull-down-history';
|
|
15
|
+
export {
|
|
16
|
+
KitChatProvider,
|
|
17
|
+
useChatStore,
|
|
18
|
+
useChatStoreApi,
|
|
19
|
+
useChatCards,
|
|
20
|
+
useChatRawCards,
|
|
21
|
+
useClientSlots,
|
|
22
|
+
} from './store-context';
|
|
23
|
+
export type { KitChatContextValue } from './store-context';
|
|
24
|
+
export { KitPreviewProvider, createPreviewStore } from './preview-provider';
|
|
25
|
+
export { createSurfaceHarness } from './surface';
|
|
26
|
+
export type { SurfaceHarness, SurfaceHarnessOptions } from './surface';
|
|
27
|
+
|
|
28
|
+
// ── chat 设置(构建期快照 + HELLO_ACK 水合) ──
|
|
29
|
+
export { configureChatConfigDefaults, useChatConfigStore } from './chat-config';
|
|
30
|
+
export type { QuickCommand } from './chat-config';
|
|
31
|
+
|
|
32
|
+
// ── 主题 ──
|
|
33
|
+
export { applyTheme, currentTheme } from './theme/theme';
|
|
34
|
+
|
|
35
|
+
// ── 壳契约(区域、可覆写选择器、品牌名来源) ──
|
|
36
|
+
export { SHELL_CONTRACT, resolveBrandName } from './shell';
|
|
37
|
+
export type { ShellArea, ShellContract, ShellRegion } from './shell';
|
|
38
|
+
|
|
39
|
+
// ── 卡片渲染(context 注册表注入) ──
|
|
40
|
+
export { CardRenderer, RawCardRenderer } from './components/CardRenderer';
|
|
41
|
+
|
|
42
|
+
// ── 应用级卡片工厂与卡片内交互 ──
|
|
43
|
+
export { defineCard } from './card/define-card';
|
|
44
|
+
export { ViewCard } from './card/view-card';
|
|
45
|
+
export type { CardRenderProps, DefinedCard } from './card/define-card';
|
|
46
|
+
export { useCardActions } from './card/use-card-actions';
|
|
47
|
+
export type { CardActions, UseCardActionsOptions } from './card/use-card-actions';
|
|
48
|
+
|
|
49
|
+
// ── 卡片基元(信息类卡片直接拼,零样式文件) ──
|
|
50
|
+
export {
|
|
51
|
+
ActionList,
|
|
52
|
+
ActionRow,
|
|
53
|
+
Bill,
|
|
54
|
+
Card,
|
|
55
|
+
CardButton,
|
|
56
|
+
CardHeader,
|
|
57
|
+
CheckRow,
|
|
58
|
+
ConfirmSheet,
|
|
59
|
+
EmptyState,
|
|
60
|
+
EntityRow,
|
|
61
|
+
Hero,
|
|
62
|
+
KeyValueList,
|
|
63
|
+
MediaRow,
|
|
64
|
+
Notice,
|
|
65
|
+
StatGrid,
|
|
66
|
+
StatusTag,
|
|
67
|
+
Steps,
|
|
68
|
+
TagList,
|
|
69
|
+
Ticket,
|
|
70
|
+
Timeline,
|
|
71
|
+
} from './card/primitives';
|
|
72
|
+
export type {
|
|
73
|
+
ActionRowProps,
|
|
74
|
+
BillLine,
|
|
75
|
+
CardActionSpec,
|
|
76
|
+
CardHeaderProps,
|
|
77
|
+
CardProps,
|
|
78
|
+
CheckRowProps,
|
|
79
|
+
ConfirmSheetProps,
|
|
80
|
+
EntityRowProps,
|
|
81
|
+
HeroProps,
|
|
82
|
+
KeyValueItem,
|
|
83
|
+
MediaRowProps,
|
|
84
|
+
NoticeProps,
|
|
85
|
+
StatItem,
|
|
86
|
+
StepItem,
|
|
87
|
+
TicketProps,
|
|
88
|
+
TimelineItem,
|
|
89
|
+
Tone,
|
|
90
|
+
} from './card/primitives';
|
|
91
|
+
|
|
92
|
+
// ── 壳组件族 ──
|
|
93
|
+
export { MessageBubble } from './components/MessageBubble';
|
|
94
|
+
export { ChatInput } from './components/ChatInput';
|
|
95
|
+
export { QuickCommands } from './components/QuickCommands';
|
|
96
|
+
export { MediaList } from './components/MediaList';
|
|
97
|
+
export { ActionBar, CopyButton, getMessageCopyText } from './components/ActionBar';
|
|
98
|
+
export { DebugButton } from './components/ActionBar/DebugButton';
|
|
99
|
+
export { default as ChatHeader } from './components/ChatHeader';
|
|
100
|
+
export { default as HistoryEntry } from './components/HistoryEntry';
|
|
101
|
+
export { default as PullDownIndicator } from './components/PullDownIndicator';
|
|
102
|
+
export { default as ReasoningBlock } from './components/ReasoningBlock';
|
|
103
|
+
export { default as ScrollToBottomButton } from './components/ScrollToBottomButton';
|
|
104
|
+
export { default as ToolCallBlock } from './components/ToolCallBlock';
|
|
105
|
+
|
|
106
|
+
// ── hooks / utils ──
|
|
107
|
+
export { useTTSAutoPlay } from './hooks/useTTSAutoPlay';
|
|
108
|
+
export { useMediaUpload } from './hooks/useMediaUpload';
|
|
109
|
+
export { toast } from './utils/toast';
|
|
110
|
+
export type { ToastTone } from './utils/toast';
|
|
111
|
+
export { copyText } from './utils/copyText';
|
|
112
|
+
export { previewImages } from './utils/imagePreview';
|
|
113
|
+
|
|
114
|
+
// ── 类型(协议类型 re-export 单一来源 + UI 层约定) ──
|
|
115
|
+
export type {
|
|
116
|
+
ChatInputMessage,
|
|
117
|
+
ChatMessage,
|
|
118
|
+
FileTypeConfig,
|
|
119
|
+
InputMethod,
|
|
120
|
+
MediaConfig,
|
|
121
|
+
MediaItem,
|
|
122
|
+
MediaType,
|
|
123
|
+
MessageRole,
|
|
124
|
+
MessageStatus,
|
|
125
|
+
ToolCallInfo,
|
|
126
|
+
} from './types';
|
|
127
|
+
|
|
128
|
+
// ── 通用弹层 ──
|
|
129
|
+
export { BottomSheet, preloadBottomSheet } from './components/BottomSheet';
|
|
130
|
+
export type { BottomSheetProps } from './components/BottomSheet';
|
|
131
|
+
export { Overlay, useOverlay, usePresence, registerScrollHost } from './overlay';
|
|
132
|
+
export { OVERLAY_LAYER_CLASS, OVERLAY_LAYER_Z } from './overlay';
|
|
133
|
+
export type {
|
|
134
|
+
DismissReason,
|
|
135
|
+
FocusRestore,
|
|
136
|
+
InitialFocus,
|
|
137
|
+
OverlayLayer,
|
|
138
|
+
OverlayProps,
|
|
139
|
+
PresenceOptions,
|
|
140
|
+
PresenceResult,
|
|
141
|
+
PresenceState,
|
|
142
|
+
UseOverlayOptions,
|
|
143
|
+
} from './overlay';
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 统一弹层外壳:传送 + 根 + 遮罩 + 面板(role/aria)+ 布局预设。
|
|
3
|
+
*
|
|
4
|
+
* DOM 契约(测试与业务 BEM 钩子依赖):
|
|
5
|
+
* div[data-tbox-overlay][data-state] ← 业务 rootClassName 落这里
|
|
6
|
+
* div[data-tbox-overlay-mask] ← 遮罩
|
|
7
|
+
* section[data-tbox-overlay-panel][role=dialog] ← 业务 panelClassName 落这里
|
|
8
|
+
* 退出动画期间(data-state="closed")面板不再带 role/aria-modal,且整层 pointer-events:none。
|
|
9
|
+
* 服务端没有 document:就地渲染(不做 portal),保证 renderToStaticMarkup 证据链可用。
|
|
10
|
+
*/
|
|
11
|
+
import { useRef } from 'react';
|
|
12
|
+
import type { HTMLAttributes, PointerEvent as ReactPointerEvent, ReactNode } from 'react';
|
|
13
|
+
import { createPortal } from 'react-dom';
|
|
14
|
+
import { OVERLAY_LAYER_CLASS, OVERLAY_ROOT_CLASS } from './layers';
|
|
15
|
+
import type { DismissReason, OverlayLayer } from './layers';
|
|
16
|
+
import type { FocusRestore, InitialFocus } from './focus';
|
|
17
|
+
import { usePresence } from './presence';
|
|
18
|
+
import { useOverlay } from './use-overlay';
|
|
19
|
+
|
|
20
|
+
const canUseDom = typeof document !== 'undefined' && typeof document.createElement === 'function';
|
|
21
|
+
|
|
22
|
+
const PLACEMENT_CLASS = {
|
|
23
|
+
bottom: 'flex items-end justify-center',
|
|
24
|
+
center: 'flex items-center justify-center p-2',
|
|
25
|
+
none: '',
|
|
26
|
+
} as const;
|
|
27
|
+
|
|
28
|
+
export interface OverlayProps {
|
|
29
|
+
open: boolean;
|
|
30
|
+
onDismiss: (reason: DismissReason) => void;
|
|
31
|
+
/** 'bottom' = 底部贴边居中;'center' = 视口居中;'none' = 调用方自定位面板。 */
|
|
32
|
+
placement?: 'bottom' | 'center' | 'none';
|
|
33
|
+
layer?: OverlayLayer;
|
|
34
|
+
mask?: boolean;
|
|
35
|
+
maskClassName?: string;
|
|
36
|
+
/** 'click'(默认)| 'self-pointerdown':按下点必须落在遮罩本身(防拖拽结束误关)。 */
|
|
37
|
+
maskDismiss?: 'click' | 'self-pointerdown' | false;
|
|
38
|
+
rootClassName?: string;
|
|
39
|
+
panelClassName?: string;
|
|
40
|
+
labelledBy?: string;
|
|
41
|
+
label?: string;
|
|
42
|
+
describedBy?: string;
|
|
43
|
+
panelProps?: HTMLAttributes<HTMLElement>;
|
|
44
|
+
initialFocus?: InitialFocus;
|
|
45
|
+
restoreFocus?: FocusRestore;
|
|
46
|
+
lockScroll?: boolean;
|
|
47
|
+
inertApp?: boolean;
|
|
48
|
+
trapFocus?: boolean;
|
|
49
|
+
closeOnEscape?: boolean;
|
|
50
|
+
/** 退出兜底时长(毫秒),与 CSS 里的退出动画时长一致。 */
|
|
51
|
+
exitMs?: number;
|
|
52
|
+
/** 退出动画结束(卸载前)回调;自管挂载的宿主用它把 onClose 推迟到动画之后。 */
|
|
53
|
+
onExited?: () => void;
|
|
54
|
+
children: ReactNode;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function Overlay({
|
|
58
|
+
open,
|
|
59
|
+
onDismiss,
|
|
60
|
+
placement = 'bottom',
|
|
61
|
+
layer = 'modal',
|
|
62
|
+
mask = true,
|
|
63
|
+
maskClassName,
|
|
64
|
+
maskDismiss = 'click',
|
|
65
|
+
rootClassName,
|
|
66
|
+
panelClassName,
|
|
67
|
+
labelledBy,
|
|
68
|
+
label,
|
|
69
|
+
describedBy,
|
|
70
|
+
panelProps,
|
|
71
|
+
initialFocus,
|
|
72
|
+
restoreFocus,
|
|
73
|
+
lockScroll,
|
|
74
|
+
inertApp,
|
|
75
|
+
trapFocus,
|
|
76
|
+
closeOnEscape,
|
|
77
|
+
exitMs,
|
|
78
|
+
onExited,
|
|
79
|
+
children,
|
|
80
|
+
}: OverlayProps) {
|
|
81
|
+
const rootRef = useRef<HTMLDivElement>(null);
|
|
82
|
+
const panelRef = useRef<HTMLElement>(null);
|
|
83
|
+
const { mounted, state } = usePresence(open, { exitMs, rootRef, onExited });
|
|
84
|
+
useOverlay({
|
|
85
|
+
active: open,
|
|
86
|
+
onDismiss,
|
|
87
|
+
panelRef,
|
|
88
|
+
rootRef,
|
|
89
|
+
initialFocus,
|
|
90
|
+
restoreFocus,
|
|
91
|
+
lockScroll: lockScroll ?? true,
|
|
92
|
+
inertApp: inertApp ?? true,
|
|
93
|
+
trapFocus: trapFocus ?? true,
|
|
94
|
+
closeOnEscape: closeOnEscape ?? true,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
if (!mounted) return null;
|
|
98
|
+
const openState = state === 'open';
|
|
99
|
+
const rootClass = [
|
|
100
|
+
OVERLAY_ROOT_CLASS,
|
|
101
|
+
OVERLAY_LAYER_CLASS[layer],
|
|
102
|
+
PLACEMENT_CLASS[placement],
|
|
103
|
+
rootClassName,
|
|
104
|
+
]
|
|
105
|
+
.filter(Boolean)
|
|
106
|
+
.join(' ');
|
|
107
|
+
// 退场期间只吞事件、不再触发关闭:根保持可交互(遮罩继续挡住背景,避免「淡出中还点到底下页面」),
|
|
108
|
+
// 面板自己 pointer-events:none 所以点不到按钮。
|
|
109
|
+
const onMaskClick = maskDismiss === 'click' && openState ? () => onDismiss('mask') : undefined;
|
|
110
|
+
const onMaskPointerDown =
|
|
111
|
+
maskDismiss === 'self-pointerdown' && openState
|
|
112
|
+
? (event: ReactPointerEvent<HTMLDivElement>) => {
|
|
113
|
+
if (event.target === event.currentTarget) onDismiss('mask');
|
|
114
|
+
}
|
|
115
|
+
: undefined;
|
|
116
|
+
|
|
117
|
+
const content = (
|
|
118
|
+
<div
|
|
119
|
+
ref={rootRef}
|
|
120
|
+
className={rootClass}
|
|
121
|
+
data-tbox-overlay=""
|
|
122
|
+
data-state={state}
|
|
123
|
+
role="presentation"
|
|
124
|
+
aria-hidden={openState ? undefined : true}
|
|
125
|
+
>
|
|
126
|
+
{mask ? (
|
|
127
|
+
<div
|
|
128
|
+
className={['absolute inset-0 touch-none', maskClassName ?? 'bg-scrim'].filter(Boolean).join(' ')}
|
|
129
|
+
data-tbox-overlay-mask=""
|
|
130
|
+
aria-hidden="true"
|
|
131
|
+
onClick={onMaskClick}
|
|
132
|
+
onPointerDown={onMaskPointerDown}
|
|
133
|
+
/>
|
|
134
|
+
) : null}
|
|
135
|
+
<section
|
|
136
|
+
{...panelProps}
|
|
137
|
+
ref={panelRef}
|
|
138
|
+
className={[panelClassName, openState ? '' : 'pointer-events-none'].filter(Boolean).join(' ')}
|
|
139
|
+
data-tbox-overlay-panel=""
|
|
140
|
+
role={openState ? 'dialog' : undefined}
|
|
141
|
+
aria-modal={openState ? true : undefined}
|
|
142
|
+
aria-label={label}
|
|
143
|
+
aria-labelledby={labelledBy}
|
|
144
|
+
aria-describedby={describedBy}
|
|
145
|
+
tabIndex={-1}
|
|
146
|
+
>
|
|
147
|
+
{children}
|
|
148
|
+
</section>
|
|
149
|
+
</div>
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
if (!canUseDom) return content;
|
|
153
|
+
return createPortal(content, document.body);
|
|
154
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 弹层焦点工具:可聚焦元素查询、初始焦点、关闭后焦点恢复。
|
|
3
|
+
*
|
|
4
|
+
* 选择器取现有多份手写实现的并集(button / a[href] / 可编辑控件 / tabindex),
|
|
5
|
+
* 再过滤 `hidden`、`aria-hidden` 祖先与 `inert` 祖先——弹层栈给下层根节点设 inert 后,
|
|
6
|
+
* 这里的过滤让焦点陷阱不会把焦点送进惰性区域。
|
|
7
|
+
*/
|
|
8
|
+
export const FOCUSABLE_SELECTOR = [
|
|
9
|
+
'a[href]',
|
|
10
|
+
'button:not(:disabled)',
|
|
11
|
+
'input:not(:disabled):not([type="hidden"])',
|
|
12
|
+
'select:not(:disabled)',
|
|
13
|
+
'textarea:not(:disabled)',
|
|
14
|
+
'[tabindex]:not([tabindex="-1"])',
|
|
15
|
+
].join(', ');
|
|
16
|
+
|
|
17
|
+
export type InitialFocus = { current: HTMLElement | null } | 'panel' | 'first' | undefined;
|
|
18
|
+
export type FocusRestore = { current: HTMLElement | null } | 'previous' | false | undefined;
|
|
19
|
+
|
|
20
|
+
export function getFocusable(root: HTMLElement | null): HTMLElement[] {
|
|
21
|
+
if (!root) return [];
|
|
22
|
+
return [...root.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR)].filter((el) => {
|
|
23
|
+
if (el.hidden) return false;
|
|
24
|
+
if (el.closest('[aria-hidden="true"]')) return false;
|
|
25
|
+
if (el.closest('[inert]')) return false;
|
|
26
|
+
return true;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 打开时的初始焦点:显式目标 → `[data-autofocus]` → 首个可聚焦元素 → 面板自身。 */
|
|
31
|
+
export function focusInitial(panel: HTMLElement | null, target: InitialFocus): void {
|
|
32
|
+
if (!panel) return;
|
|
33
|
+
if (target && target !== 'panel' && target !== 'first') {
|
|
34
|
+
if (target.current?.isConnected) {
|
|
35
|
+
target.current.focus({ preventScroll: true });
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (target === 'panel') {
|
|
40
|
+
panel.focus({ preventScroll: true });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const auto = panel.querySelector<HTMLElement>('[data-autofocus]');
|
|
44
|
+
const next = auto?.isConnected ? auto : getFocusable(panel)[0];
|
|
45
|
+
if (next) next.focus({ preventScroll: true });
|
|
46
|
+
else panel.focus({ preventScroll: true });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 关闭后的焦点恢复;目标已从 DOM 移除时静默跳过。 */
|
|
50
|
+
export function restoreFocus(target: HTMLElement | null | undefined): void {
|
|
51
|
+
if (!target || !target.isConnected) return;
|
|
52
|
+
target.focus({ preventScroll: true });
|
|
53
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 弹层能力公开入口(kit 子路径 `./overlay`,同时从根入口再导出)。
|
|
3
|
+
*
|
|
4
|
+
* 分层:内核 hooks(stack/scroll-lock/inert/focus/presence)→ `<Overlay>` 外壳 →
|
|
5
|
+
* `BottomSheet` / `ConfirmSheet` 与业务模块弹层。业务自己管 DOM 时只用 `useOverlay`。
|
|
6
|
+
*/
|
|
7
|
+
export { Overlay } from './Overlay';
|
|
8
|
+
export type { OverlayProps } from './Overlay';
|
|
9
|
+
export { useOverlay } from './use-overlay';
|
|
10
|
+
export type { UseOverlayOptions } from './use-overlay';
|
|
11
|
+
export { usePresence } from './presence';
|
|
12
|
+
export type { PresenceOptions, PresenceResult, PresenceState } from './presence';
|
|
13
|
+
export { registerScrollHost } from './scroll-lock';
|
|
14
|
+
export { OVERLAY_LAYER_CLASS, OVERLAY_LAYER_Z } from './layers';
|
|
15
|
+
export type { DismissReason, OverlayLayer } from './layers';
|
|
16
|
+
export type { FocusRestore, InitialFocus } from './focus';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `inert` / `aria-hidden` 管理:弹层打开时把应用根与下层弹层设为惰性,栈顶恢复。
|
|
3
|
+
*
|
|
4
|
+
* React 18 不认识 `inert` 属性(写成 JSX 会被丢弃),因此只在 effect 里操作 DOM,
|
|
5
|
+
* 并保存原值(含 `aria-hidden`)以便逐层还原。
|
|
6
|
+
*/
|
|
7
|
+
export function getAppRoot(): HTMLElement | null {
|
|
8
|
+
if (typeof document === 'undefined') return null;
|
|
9
|
+
return document.getElementById('root');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const saved = new WeakMap<HTMLElement, { inert: boolean; ariaHidden: string | null }>();
|
|
13
|
+
|
|
14
|
+
export function setElementInert(el: HTMLElement, inert: boolean): void {
|
|
15
|
+
if (inert) {
|
|
16
|
+
if (!saved.has(el)) saved.set(el, { inert: el.inert ?? false, ariaHidden: el.getAttribute('aria-hidden') });
|
|
17
|
+
el.inert = true;
|
|
18
|
+
el.setAttribute('aria-hidden', 'true');
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const previous = saved.get(el);
|
|
22
|
+
if (!previous) return;
|
|
23
|
+
el.inert = previous.inert;
|
|
24
|
+
if (previous.ariaHidden === null) el.removeAttribute('aria-hidden');
|
|
25
|
+
else el.setAttribute('aria-hidden', previous.ariaHidden);
|
|
26
|
+
saved.delete(el);
|
|
27
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** 弹层层级:只有三档(同一档内按打开先后叠放——后打开的在上)。 */
|
|
2
|
+
export const OVERLAY_LAYER_Z = {
|
|
3
|
+
modal: 1100,
|
|
4
|
+
preview: 1200,
|
|
5
|
+
toast: 1300,
|
|
6
|
+
} as const;
|
|
7
|
+
|
|
8
|
+
/** 与 `OVERLAY_LAYER_Z` 同值;Tailwind 只认源码里的字面量类名,因此这里保留静态写法。 */
|
|
9
|
+
export const OVERLAY_LAYER_CLASS = {
|
|
10
|
+
modal: 'z-[1100]',
|
|
11
|
+
preview: 'z-[1200]',
|
|
12
|
+
toast: 'z-[1300]',
|
|
13
|
+
} as const;
|
|
14
|
+
|
|
15
|
+
export type OverlayLayer = keyof typeof OVERLAY_LAYER_Z;
|
|
16
|
+
|
|
17
|
+
/** 关闭原因:Escape 或点遮罩(面板内的业务按钮不经过这里)。 */
|
|
18
|
+
export type DismissReason = 'escape' | 'mask';
|
|
19
|
+
|
|
20
|
+
/** 弹层根节点的块级定位:`fixed inset-0` + 层级(调用方的 rootClassName 叠在其后)。 */
|
|
21
|
+
export const OVERLAY_ROOT_CLASS = 'fixed inset-0';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { renderToStaticMarkup } from 'react-dom/server';
|
|
4
|
+
import { Overlay } from './Overlay';
|
|
5
|
+
|
|
6
|
+
describe('Overlay(服务端)', () => {
|
|
7
|
+
it('无 document 时就地渲染(不 portal),保留对话框语义', () => {
|
|
8
|
+
const html = renderToStaticMarkup(
|
|
9
|
+
<Overlay open onDismiss={() => {}} labelledBy="t" rootClassName="probe-sheet">
|
|
10
|
+
<h2 id="t">标题</h2>
|
|
11
|
+
</Overlay>,
|
|
12
|
+
);
|
|
13
|
+
expect(html).toContain('data-tbox-overlay');
|
|
14
|
+
expect(html).toContain('role="dialog"');
|
|
15
|
+
expect(html).toContain('aria-labelledby="t"');
|
|
16
|
+
expect(html).toContain('标题');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('关闭态不渲染任何标记', () => {
|
|
20
|
+
const html = renderToStaticMarkup(
|
|
21
|
+
<Overlay open={false} onDismiss={() => {}} labelledBy="t">
|
|
22
|
+
<h2 id="t">标题</h2>
|
|
23
|
+
</Overlay>,
|
|
24
|
+
);
|
|
25
|
+
expect(html).toBe('');
|
|
26
|
+
});
|
|
27
|
+
});
|