84 lines
4.7 KiB
TypeScript
84 lines
4.7 KiB
TypeScript
/**
|
||
* 确认框:破坏性操作的第二道闸,自绘表面,**不用 `window.confirm`**(2026-11 第十三轮)。
|
||
*
|
||
* 症状(用户原话「整体修改系统中所有提示窗、完成后的反馈 tips 等。不要使用默认组件」):
|
||
* 全站 13 处 `window.confirm()` 弹的是**操作系统**的窗口——
|
||
* ① 主题、字号、按钮、语言全由 Chrome/macOS 决定,深色工作台里突然插进一块白色系统框;
|
||
* ② 只有一行灰字 + 两颗灰按钮,「删除不可恢复」和「继续切换」长得一模一样;
|
||
* ③ 问话与后果挤成一句("删除 v3「正片」?\n只删除编排记录,已导出成片不受影响。"),
|
||
* 原生框把它当一行文本折行,`\n` 甚至只是某些浏览器才认的运气;
|
||
* ④ 同步阻塞:脚本停在原地,进度条、轮询、动画全部冻住。
|
||
* 做法:状态在 `stores/confirm.ts`(`confirmAction(): Promise<boolean>`),皮肤在这里——
|
||
* 浮层唯一实现 kit `Modal`(遮罩 / Esc / 焦点陷阱 / 滚动锁交给 antd,与抽屉同源)
|
||
* + 32px 语义色块 + 「一句问话 / 一层后果」两行文案 + 图标按钮,
|
||
* 底部沿用全站口径「取消 → 主行动」右对齐、最多一颗 primary、危险档 primary danger。
|
||
* 为什么回车=确认而 Esc=取消:原生 confirm 就是这个映射,肌肉记忆不用重建;
|
||
* 但焦点落在按钮上时不许接管(否则 Tab 到「取消」按回车会误删)。
|
||
* 为什么整站只可能有一张:`confirmAction` 在已有确认框时直接 resolve(false),
|
||
* 键盘连按不会排出一串窗口(原生 confirm 会)。
|
||
*/
|
||
import { useEffect } from 'react'
|
||
import { LAYER } from '../../lib/layers'
|
||
import { useConfirmStore } from '../../stores/confirm'
|
||
import { Button } from './Button'
|
||
import { Modal } from './Overlay'
|
||
import { TONE_TINT, type FeedbackTone } from './tone'
|
||
import { IconCheck, IconClose, IconInfo, IconWarn } from '../icons'
|
||
|
||
/** 确认框的三档语气 → 反馈语义(色块与色值仍从 tone.tsx 那张唯一的表里取) */
|
||
const CONFIRM_TONE: Record<'danger' | 'warn' | 'info', FeedbackTone> = {
|
||
danger: 'bad', warn: 'warn', info: 'info'
|
||
}
|
||
|
||
/** 挂在 app/providers.tsx 上(唯一一处),任何页面调用 confirmAction 都由这一颗应答 */
|
||
export function ConfirmHost() {
|
||
const req = useConfirmStore((s) => s.req)
|
||
const answer = useConfirmStore((s) => s.answer)
|
||
const open = !!req
|
||
const o = req?.options
|
||
|
||
/* 回车 = 确认。挂在 document 上而不是内容区上:antd 打开模态时把焦点落在
|
||
`.ant-modal`(tabIndex=-1)那层,keydown 只会向上冒泡,落在 body 上的监听收不到。 */
|
||
useEffect(() => {
|
||
if (!open) return
|
||
const onKey = (e: KeyboardEvent) => {
|
||
if (e.key !== 'Enter' || e.isComposing) return
|
||
/* 焦点在按钮 / 输入框上时交给它们自己处理(Tab 到「取消」按回车不该确认) */
|
||
const t = e.target as HTMLElement | null
|
||
if (t && (t.tagName === 'BUTTON' || t.tagName === 'INPUT' || t.tagName === 'TEXTAREA'
|
||
|| t.tagName === 'SELECT' || t.isContentEditable)) return
|
||
e.preventDefault()
|
||
answer(true)
|
||
}
|
||
document.addEventListener('keydown', onKey)
|
||
return () => document.removeEventListener('keydown', onKey)
|
||
}, [open, answer])
|
||
|
||
const tone = CONFIRM_TONE[o?.tone ?? 'danger']
|
||
return (
|
||
/* zIndex:确认框必须盖住自绘灯箱(1020)与录音面板(1030)——
|
||
灯箱里的「删除这一版」就是从这一层弹出去的,谁在上面由 LAYER 说了算。 */
|
||
<Modal open={open} onClose={() => answer(false)} width={460} zIndex={LAYER.confirm} className="nex-confirm"
|
||
label={typeof o?.title === 'string' ? o.title : '请确认操作'}
|
||
footer={<>
|
||
<Button icon={<IconClose />} onClick={() => answer(false)}>{o?.cancelText ?? '取消'}</Button>
|
||
<Button type="primary" danger={o?.tone !== 'info'} icon={o?.confirmIcon ?? <IconCheck />}
|
||
onClick={() => answer(true)}>{o?.confirmText ?? '确定'}</Button>
|
||
</>}>
|
||
<div className="nex-confirm-body" data-tone={o?.tone ?? 'danger'}>
|
||
<span className="nex-confirm-mark" aria-hidden
|
||
style={{ ['--nex-mark-tint' as string]: TONE_TINT[tone] }}>
|
||
{/* 问话用的图形:危险与注意都是「先停下看一眼」,信息档才是普通的 i */}
|
||
{tone === 'info' ? <IconInfo /> : <IconWarn />}
|
||
</span>
|
||
<div className="min-w-0 flex-1">
|
||
<div className="nex-confirm-title">{o?.title}</div>
|
||
{o?.desc && <div className="nex-confirm-desc">{o.desc}</div>}
|
||
</div>
|
||
</div>
|
||
</Modal>
|
||
)
|
||
}
|
||
|
||
export default ConfirmHost
|