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

52 lines
2.4 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.

/**
* 下拉菜单:Ant Design 5 Dropdown 的唯一出口。
*
* 症状:菜单原来是 primitives/Menu(Radix)+ 自己拼的面板,页面里另一批菜单又是
* div + useOutsideClick——同一种「点开一个浮层」有两套行为,Tab 会跑到菜单背后。
* 做法:焦点圈定、Esc、外点关闭、portal 全交给 antd Dropdown,皮肤来自 theme/antd.ts
* 的 Dropdown token;菜单项就是 Dropdown 的 items(自带 icon 位),
* 这一层不再自绘「一行菜单」——以前这里的 MenuRow/MenuDivider/MenuNote
* 是三条手写的行皮肤,全站零调用,浮层里的行一律走 antd 自己的 item。
* 为什么要在这一层转一道:`import 'antd'` 只允许出现在 theme/、app/providers.tsx
* 与 components/{ui,forms} 里(R8,classaudit 第 5 道闸),页面只认这里给出的名字。
*/
import { Dropdown } from 'antd'
import { Button } from './Button'
import { IconChevronDown } from '../icons'
export const Menu = Dropdown
export type MenuProps = React.ComponentProps<typeof Dropdown>
/**
* 导出菜单:一颗按钮 + 标准下拉列出格式。
*
* 病灶:分镜 / 时间线把「Markdown ↗ CSV ↗ JSON ↗」三个下载链接直接摊在筛选条右边,
* 它们的字号、下划线、热区高度和同一排的下拉按钮不是一个族,
* 加第四种格式时又得再抄一个链接。
* 做法:菜单项、键盘、外点关闭全部交给 antd Dropdown(见 R8),
* 点击走同一个下载器;页面只登记「格式名 + 取文件的接口」。
*/
export function ExportMenu({ label = '导出', items, size, className }: {
label?: React.ReactNode; className?: string; size?: 'sm' | 'md'
items: { label: React.ReactNode; href: string; hint?: string; icon?: React.ReactNode }[]
}) {
return (
<Dropdown trigger={['click']}
menu={{
items: items.map((it) => ({ key: it.href, label: it.label, title: it.hint, icon: it.icon })),
onClick: ({ key }) => {
const a = document.createElement('a')
a.href = key
a.download = ''
document.body.appendChild(a)
a.click()
a.remove()
}
}}>
<Button size={size} className={className} icon={<IconChevronDown />} iconPosition="end">
{label}
</Button>
</Dropdown>
)
}