For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/v/0.6.0/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/v/0.6.0/llms-full.txt, and this page is available as Markdown at https://ifhover.github.io/leaf-ui/v/0.6.0/components/dropdown.md.
  • 简体中文
  • Dropdown 下拉菜单

    在菜单中收纳更多操作。

    操作菜单

    basic.tsx
    import type { DropdownItem } from '@ifhover/leaf-ui';
    
    import { Button, Dropdown, useMessage } from '@ifhover/leaf-ui';
    
    import { ChevronDown, Copy, Pencil, Trash2 } from 'lucide-react';
    
    const items: DropdownItem[] = [
      { key: 'edit', label: '编辑', icon: <Pencil size={15} /> },
      { key: 'copy', label: '复制', icon: <Copy size={15} /> },
      { key: 'archive', label: '归档', disabled: true },
      { key: 'divider', type: 'divider' },
      { key: 'delete', label: '删除', icon: <Trash2 size={15} />, danger: true },
    ];
    
    export function DropdownBasic() {
      const { message } = useMessage();
      return (
        <Dropdown items={items} onSelect={(key) => message.info(key)}>
          <Button variant="outline" endIcon={<ChevronDown size={16} />}>
            更多操作
          </Button>
        </Dropdown>
      );
    }
    

    扩展用法

    extensions.tsx
    import { Button, Dropdown } from '@ifhover/leaf-ui';
    
    import { useState } from 'react';
    
    export function ExtensionDemo() {
      const [checked, setChecked] = useState(true);
      const [view, setView] = useState('list');
      return (
        <Dropdown
          items={[
            {
              key: 'group',
              label: '视图',
              type: 'group',
              children: [
                {
                  key: 'list',
                  label: '列表',
                  type: 'radio',
                  group: 'view',
                  checked: view === 'list',
                  onCheckedChange: () => setView('list'),
                },
                {
                  key: 'grid',
                  label: '网格',
                  type: 'radio',
                  group: 'view',
                  checked: view === 'grid',
                  onCheckedChange: () => setView('grid'),
                },
              ],
            },
            { key: 'line', type: 'divider' },
            {
              key: 'detail',
              label: '显示详情',
              type: 'checkbox',
              checked,
              onCheckedChange: setChecked,
            },
            {
              key: 'more',
              label: '更多操作',
              children: [
                { key: 'copy', label: '复制' },
                { key: 'archive', label: '归档' },
              ],
            },
          ]}
        >
          <Button variant="outline">视图与操作</Button>
        </Dropdown>
      );
    }
    

    悬停与右键

    capabilities.tsx
    import { Button, Dropdown, Space } from '@ifhover/leaf-ui';
    
    const items = [
      { key: 'edit', label: 'Edit' },
      { key: 'more', label: 'More', children: [{ key: 'archive', label: 'Archive' }] },
    ];
    
    export function Capabilities() {
      return (
        <Space>
          <Dropdown trigger="hover" items={items}>
            <Button variant="outline">{'悬停展开'}</Button>
          </Dropdown>
          <Dropdown trigger="contextMenu" items={items}>
            <Button variant="outline">{'在此右键点击'}</Button>
          </Dropdown>
        </Space>
      );
    }
    
    属性类型默认值说明
    popupPlacement'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'left' | 'left-start' | 'left-end' | 'right' | 'right-start' | 'right-end'—浮层相对触发控件的位置。
    popupClassNamestring—浮层的自定义类名。
    popupStyleCSSProperties—浮层的自定义样式。
    popupRender(content: React.ReactNode) => React.ReactNode—包装或补充面板内容,保留传入的内容以维持交互。
    getPopupContainer() => Element | DocumentFragment—返回浮层的挂载容器,也可返回 ShadowRoot。
    itemsreadonly DropdownItem[]必填必需。菜单项列表
    childrenReactElement< ButtonHTMLAttributes<HTMLButtonElement> & { ref?: Ref<HTMLButtonElement> } >必填必需。单个按钮,支持 Leaf Button 或 button
    disabledbooleanfalse禁用交互。
    openbooleanfalse受控 / 初始展开状态
    defaultOpenbooleanfalse受控 / 初始展开状态
    onOpenChange(open: boolean) => void—展开状态变更
    onSelect(key: string) => void—选择可用菜单项
    placement'top-start' | 'top-end' | 'bottom-start' | 'bottom-end''bottom-start'弹出方向
    popupWidthnumber | string—浮层宽度 / 最大宽度
    popupMaxWidthnumber | string420浮层宽度 / 最大宽度
    trigger'click' | 'hover' | 'contextMenu' | readonly ('click' | 'hover' | 'contextMenu')[]'click'触发方式;下拉菜单支持点击、悬停和右键。
    mouseEnterDelaynumber100设置 mouseEnterDelay。
    mouseLeaveDelaynumber150设置 mouseLeaveDelay。
    itemRender(item: DropdownItem) => ReactNode—自定义条目内容。
    属性类型默认值说明
    keystring必填必需。唯一标识
    labelReactNode—文字与图标
    iconReactNode—文字与图标
    type'item' | 'divider' | 'group' | 'checkbox' | 'radio''item'菜单项或分割线
    childrenreadonly DropdownItem[]—子内容;嵌套数据类型表示子节点。
    disabledbooleanfalse禁用交互。
    dangerbooleanfalse禁用 / 危险操作
    checkedboolean—设置 checked。
    groupstring—条目的分组标题。
    closeOnSelectboolean—设置 closeOnSelect。
    onCheckedChange(checked: boolean) => void—设置 onCheckedChange。
    hrefstring—链接目标。
    targetstring—目标的选择器或返回元素的函数;省略时居中。
    onClick(event: MouseEvent<HTMLElement>) => void—点击此项时触发

    类型

    定义组件属性或组装变量时可导入相关类型。

    import type { DropdownProps, DropdownItem } from '@ifhover/leaf-ui';

    使用说明

    按钮按 ↓ / ↑ 打开菜单,菜单中使用方向键、Home、End 或文字首字符定位;Enter 选择,Escape 关闭并恢复按钮焦点。disabled 菜单项与分割线会被跳过。