import * as RadixDialog from '@radix-ui/react-dialog' import type { ComponentPropsWithoutRef, ReactNode } from 'react' import { cn } from '../../lib/cn' /** * 浮层行为层:Radix Dialog。 * * 自己实现一份「浮层」要付的账(旧 Overlay.tsx 全踩过): * 焦点进不去、Tab 能跑到背后的页面上、Esc 靠 window 全局监听互相抢、 * 滚动锁只写 body{overflow:hidden}(有滚动条的页面开抽屉会左右跳 15px)、 * 读屏拿不到标题(role=dialog 却没有可关联的名字)。 * Radix 把这五件事做完了,并且和 Popover / Menu 共用同一个 DismissableLayer 栈, * 「点下拉面板不算点外面」这类判断才有一个统一的地方。 * * 皮肤仍然在我们的令牌里(tokens.css + Tailwind),Radix 只给 data-state。 * * ⚠️ 可访问性名字必须显式给:Radix 找不到 又没看到 * aria-label / aria-labelledby 就在控制台报警告。我们的标题是

{title}

* 这种自带结构的节点(还常常套一层 ),所以用 useId 把 * id 牵到 aria-labelledby 上,而不是把 h2 塞进 Dialog.Title 里改排版; * 没有副标题时传 aria-describedby={undefined}(键存在就足够让警告闭嘴)。 */ export const DialogRoot = RadixDialog.Root export const DialogTrigger = RadixDialog.Trigger export const DialogClose = RadixDialog.Close export const DialogPortal = RadixDialog.Portal /** 遮罩。层级低于面板、高于页面内容;下拉面板(Popup 的 400)再压在面板上 */ export const scrimLayer = 'fixed inset-0 z-[100] bg-scrim backdrop-blur-[4px] data-[state=open]:animate-fade-in' export function Scrim(props: ComponentPropsWithoutRef) { return } /** * 「点外面」判定。 * * 两种不关:① closeOnBackdrop=false(分镜那种要看旁边面板的浮层); * ② 点到的其实是浮层里那个下拉面板——Radix Popover 把面板 portal 到 body, * DOM 上不在 Content 里。Radix 自己的层栈能认出大多数情况,这里再兜一道, * 免得选中一项就把整个抽屉关掉(也顺便挡住焦点移出触发的 FocusOutside)。 */ function floatingLayerOf(target: EventTarget | null) { const el = target instanceof Element ? target : null return !!el?.closest('[data-nex-layer],[data-radix-popper-content-wrapper]') } function keepOpen(closeOnBackdrop: boolean) { return (e: E) => { const original = e.detail?.originalEvent if (!closeOnBackdrop || floatingLayerOf(original?.target ?? null)) e.preventDefault() } } /** 打开时不把焦点丢进第一个输入框:抽屉里 autofokus 会顶掉页面滚动位置 */ function holdFocus() { return (e: Event) => { e.preventDefault() ;(e.currentTarget as HTMLElement | null)?.focus?.({ preventScroll: true }) } } /** 右侧抽屉的面板(配合 DrawerBody/DrawerActions 三段式皮肤) */ export function SheetContent({ children, width, closeOnBackdrop = true, labelledBy, describedBy, label, className }: { children: ReactNode /** Tailwind 宽度类;默认 1360px 表单抽屉 */ width?: string closeOnBackdrop?: boolean labelledBy?: string describedBy?: string label?: string className?: string }) { return ( e.preventDefault()} onInteractOutside={keepOpen(closeOnBackdrop)} onFocusOutside={keepOpen(closeOnBackdrop)} className={cn('fixed inset-y-0 right-0 z-[101] flex flex-col min-w-0 h-full border-l border-line-strong ' + 'bg-shell shadow-drawer data-[state=open]:animate-sheet-in', width || 'w-[min(1360px,calc(100vw-20px))] max-huge:w-[calc(100vw-16px)] max-short:w-screen', className)}> {children} ) } /** 居中模态的面板:只放一两句确认文案 + 两个动作 */ export function ModalContent({ children, width = 560, closeOnBackdrop = true, labelledBy, describedBy, label, className }: { children: ReactNode /** 像素宽度上限(窄屏由 calc(100vw-32px) 兜住) */ width?: number closeOnBackdrop?: boolean labelledBy?: string describedBy?: string label?: string className?: string }) { return ( e.preventDefault()} onInteractOutside={keepOpen(closeOnBackdrop)} onFocusOutside={keepOpen(closeOnBackdrop)} style={{ maxWidth: `min(${width}px, calc(100vw - 32px))` }} className={cn('fixed left-1/2 top-1/2 z-[111] -translate-x-1/2 -translate-y-1/2 ' + 'flex max-h-[calc(100vh-32px)] w-full flex-col overflow-hidden rounded-float ' + 'border border-line-strong bg-shell shadow-pop data-[state=open]:animate-pop-in', className)}> {children} ) }