块编辑器开发文档

💡 云策文档标注

概述

Dropdown 是 WordPress 组件库中的一个 React 组件,用于渲染一个按钮,点击时打开浮动内容模态框。它管理下拉菜单的打开/关闭状态,支持点击外部关闭,并通过 render props 渲染按钮和内容。

关键要点

  • Dropdown 组件基于 Popover 实现,提供可控和不可控的打开状态选项。
  • 使用 renderToggle 和 renderContent 两个必需的回调函数来分别渲染触发按钮和下拉内容。
  • 支持通过 popoverProps 传递额外属性到 Popover 组件,如自定义位置或隐藏箭头。
  • 提供移动端全屏显示、焦点管理、自定义样式类等可配置属性。

代码示例

import { Button, Dropdown } from '@wordpress/components';

const MyDropdown = () => (
    <Dropdown
        className="my-container-class-name"
        contentClassName="my-popover-content-classname"
        popoverProps={ { placement: 'bottom-start' } }
        renderToggle={ ( { isOpen, onToggle } ) => (
            <Button
                variant="primary"
                onClick={ onToggle }
                aria-expanded={ isOpen }
            >
                Toggle Popover!
            </Button>
        ) }
        renderContent={ () => <div>This is the content of the popover.</div> }
    />
);

注意事项

  • renderToggle 和 renderContent 是必需的回调函数,用于渲染按钮和内容,确保传递正确的 props 如 isOpen 和 onToggle。
  • 当使用 open 属性控制打开状态时,必须同时提供 onToggle 回调以处理状态变化。
  • contentClassName 用于样式化下拉菜单内容,因为内容节点不是容器节点的直接子元素。
  • focusOnMount 默认聚焦于第一个可聚焦元素,可根据无障碍需求调整。

📄 原文内容

Dropdown is a React component to render a button that opens a floating content modal when clicked.

This component takes care of updating the state of the dropdown menu (opened/closed), handles closing the menu when clicking outside and uses render props to render the button and the content.

Usage

import { Button, Dropdown } from '@wordpress/components';

const MyDropdown = () => (
    <Dropdown
        className="my-container-class-name"
        contentClassName="my-popover-content-classname"
        popoverProps={ { placement: 'bottom-start' } }
        renderToggle={ ( { isOpen, onToggle } ) => (
            <Button
                variant="primary"
                onClick={ onToggle }
                aria-expanded={ isOpen }
            >
                Toggle Popover!
            </Button>
        ) }
        renderContent={ () => <div>This is the content of the popover.</div> }
    />
);

Props

The component accepts the following props. Props not included in this set will be applied to the element wrapping Popover content.

className: string

className of the global container

  • Required: No

contentClassName: string

If you want to target the dropdown menu for styling purposes, you need to provide a contentClassName because it’s not being rendered as a child of the container node.

  • Required: No

defaultOpen: boolean

The open state of the dropdown when initially rendered. Use when you do not need to control its open state. It will be overridden by the open prop if it is specified on the component’s first render.

  • Required: No

expandOnMobile: boolean

Opt-in prop to show popovers fullscreen on mobile.

  • Required: No
  • Default: false

focusOnMount: 'firstElement' | boolean

By default, the first tabbable element in the popover will receive focus when it mounts. This is the same as setting this prop to "firstElement".

Specifying a true value will focus the container instead.

Specifying a false value disables the focus handling entirely (this should only be done when an appropriately accessible substitute behavior exists).

  • Required: No
  • Default: "firstElement"

headerTitle: string

Set this to customize the text that is shown in the dropdown’s header when it is fullscreen on mobile.

  • Required: No

onClose: () => void

A callback invoked when the popover should be closed.

  • Required: No

open: boolean

The controlled open state of the dropdown. Must be used in conjunction with onToggle.

  • Required: No

onToggle: ( willOpen: boolean ) => void

A callback invoked when the state of the dropdown changes from open to closed and vice versa.

  • Required: No

popoverProps: WordPressComponentProps< Omit< PopoverProps, 'children' > 'div', false >

Properties of popoverProps object will be passed as props to the Popover component.

Use this object to access properties/features of the Popover component that are not already exposed in the Dropdown component, e.g.: the ability to have the popover without an arrow.

  • Required: No

renderContent: ( props: CallbackProps ) => ReactNode

A callback invoked to render the content of the dropdown menu.

  • isOpen: whether the dropdown menu is opened or not
  • onToggle: A function switching the dropdown menu’s state from open to closed and vice versa
  • onClose: A function that closes the menu if invoked

  • Required: Yes

renderToggle: ( props: CallbackProps ) => ReactNode

A callback invoked to render the Dropdown Toggle Button.

Its props are the same as the renderContent props.

  • Required: Yes

style: React.CSSProperties

The style of the global container

  • Required: No