nex_animation/frontend/src/components/ui/Overlay.tsx

146 lines
6.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { useEffect, type ReactNode } from 'react'
import { cn } from '../../lib/cn'
/**
* 浮层:抽屉(右滑全高)与居中模态。
*
* 旧站每个页面各抄一份 .drawer-backdrop + .drawer + .drawer-x,
* 三处 padding、两处宽度、四种关闭条件互不相同;抽屉内控件高度也因此
* 各写各的 --ctl-h(历史散落在 MediaLightbox / AssetLibrary / ModelCenter)。
* 现在宽度、留白、高度档只在这里有一份,页面只提供内容。
*/
/** Esc 关闭 + 锁定背后页面滚动(多个浮层叠加时用引用计数,关掉一个不会误放开) */
function useOverlay(open: boolean, onClose: () => void, closeOnEsc = true) {
useEffect(() => {
if (!open) return
if (closeOnEsc) {
const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') { e.stopPropagation(); onClose() } }
window.addEventListener('keydown', onKey)
return () => window.removeEventListener('keydown', onKey)
}
return undefined
}, [open, onClose, closeOnEsc])
}
let locked = 0
function useScrollLock(open: boolean) {
useEffect(() => {
if (!open) return
locked += 1
const prev = document.body.style.overflow
document.body.style.overflow = 'hidden'
return () => {
locked = Math.max(0, locked - 1)
if (locked === 0) document.body.style.overflow = prev
}
}, [open])
}
/* ── 抽屉三段式的皮肤(UI规范 §3)────────────────────────────────────────
<Drawer> 一处用它们时,类名写死在 JSX 里就够了;分镜「检查器」是第二处:
它刻意不做成 Drawer(Drawer 是模态——遮罩会吞掉左边看板的点击、Esc 一按就关,
而改分镜要在看板和字段之间来回看),外观却又必须和抽屉一模一样。
皮肤抄第二遍,下次调 padding 只会改到一处,另一处慢慢就长歪。
所以三段类名提到这里,页面写成 <header className={DRAWER_HEAD}>,
结构各留各的(检查器有自己的页签条和逐镜导航),视觉只有一个来源。 */
export const DRAWER_HEAD = 'flex-none flex justify-between items-start gap-[14px] p-[20px_24px_15px] '
+ 'max-short:p-[20px_14px_15px] border-b border-[--c-bg-emphasis] '
+ 'bg-[linear-gradient(120deg,var(--c-bg-raised),var(--c-bg-panel)_55%)]'
/** 唯一滚动区:只有 body 滚,标题条与动作条不动 */
export const DRAWER_BODY = 'flex-1 min-w-0 overflow-y-auto scrollbar-slim p-[20px_24px_24px] '
+ 'max-short:p-[20px_14px_14px] [scrollbar-gutter:stable]'
/** 吸底动作条。flex-wrap 不能省:窄屏下按钮挤成一排是溢出,不是折行 */
export const DRAWER_ACTIONS = 'flex-none flex flex-wrap items-center gap-2 p-[14px_24px] max-short:p-[14px] '
+ 'border-t border-line bg-raised [--ctl-h:var(--ctl-h-md)]'
/**
* 浮层右上角的 ✕。抽屉、模态、分镜检查器各抄过一份,
* 其中模态那份就漏了 hover 描边——同一个动作长三种样子,这里收成一个。
* 提示统一带「(Esc)」:浮层一律 Esc 可关(见 useOverlay),别让人去猜。
*/
export function CloseButton({ onClose, label = '关闭' }: { onClose: () => void; label?: string }) {
return (
<button type="button" aria-label={label} title={`${label}(Esc)`} onClick={onClose}
className="flex-none grid place-items-center w-(--ctl-h-md) h-(--ctl-h-md) border border-line-strong rounded-tile bg-card text-ink-2 text-ui hover:border-[--c-accent-deep] hover:text-accent-bright hover:bg-accent-tint">✕</button>
)
}
export function Drawer({ open, onClose, title, sub, children, actions, width, closeOnBackdrop = true, label }: {
open: boolean
onClose: () => void
title: ReactNode
sub?: ReactNode
children: ReactNode
/** 底部动作条;不传就不渲染这一行 */
actions?: ReactNode
/** 宽度档:默认 1360px 表单抽屉;只放一块面板的抽屉要窄一些,
* 好让左侧列表仍可见(旧站 .gen-drawer 就是 980px),传 Tailwind 宽度类即可 */
width?: string
closeOnBackdrop?: boolean
label?: string
}) {
useOverlay(open, onClose)
useScrollLock(open)
if (!open) return null
return (
<div className="fixed inset-0 z-[100] flex justify-end bg-scrim backdrop-blur-[4px]"
onMouseDown={(e) => { if (closeOnBackdrop && e.target === e.currentTarget) onClose() }}>
<section role="dialog" aria-modal="true" aria-label={typeof label === 'string' ? label : undefined}
className={cn('flex flex-col min-w-0 h-screen border-l border-line-strong bg-shell shadow-drawer',
width || 'w-[min(1360px,calc(100vw-20px))] max-huge:w-[calc(100vw-16px)] max-short:w-screen')}>
<header className={DRAWER_HEAD}>
<div className="min-w-0">
<h2 className="text-lg font-bold mt-0 mb-0">{title}</h2>
{sub && <p className="mt-[5px] mb-0 text-tiny text-dim leading-[1.6]">{sub}</p>}
</div>
<CloseButton onClose={onClose} />
</header>
<div className={DRAWER_BODY}>
{children}
</div>
{actions && (
<footer className={DRAWER_ACTIONS}>
{actions}
</footer>
)}
</section>
</div>
)
}
/** 居中模态:只放一两句确认文案 + 两个动作;长表单请用 Drawer */
export function Modal({ open, onClose, title, children, footer, width = 560, closeOnBackdrop = true, label }: {
open: boolean
onClose: () => void
title?: ReactNode
children: ReactNode
footer?: ReactNode
width?: number
closeOnBackdrop?: boolean
label?: string
}) {
useOverlay(open, onClose)
useScrollLock(open)
if (!open) return null
return (
<div className="fixed inset-0 z-[110] grid place-items-center p-4 bg-scrim backdrop-blur-[4px]"
onMouseDown={(e) => { if (closeOnBackdrop && e.target === e.currentTarget) onClose() }}>
<section role="dialog" aria-modal="true" aria-label={label}
style={{ maxWidth: width }}
className="w-full max-w-full bg-shell border border-line-strong rounded-float shadow-pop overflow-hidden max-h-[calc(100vh-32px)] flex flex-col">
{title && (
<header className="flex-none flex items-center justify-between gap-3 p-[14px_18px] border-b border-[--c-bg-emphasis] bg-[linear-gradient(120deg,var(--c-bg-raised),var(--c-bg-panel)_55%)]">
<div className="min-w-0">
<h2 className="text-md font-bold m-0">{title}</h2>
</div>
<CloseButton onClose={onClose} />
</header>
)}
<div className="flex-1 min-w-0 overflow-y-auto scrollbar-slim p-[18px] [scrollbar-gutter:stable]">{children}</div>
{footer && <footer className="flex-none flex items-center justify-end gap-2 p-[12px_18px] border-t border-line bg-raised [--ctl-h:var(--ctl-h-md)]">{footer}</footer>}
</section>
</div>
)
}