Dropdown 是 WordPress 组件库中的一个 React 组件,用于渲染一个按钮,点击时打开浮动内容模态框。它管理下拉菜单的打开/关闭状态,支持点击外部关闭,并通过 render props 渲染按钮和内容。
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> }
/>
);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.
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> }
/>
);
The component accepts the following props. Props not included in this set will be applied to the element wrapping Popover content.
className: stringclassName of the global container
contentClassName: stringIf 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.
defaultOpen: booleanThe 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.
expandOnMobile: booleanOpt-in prop to show popovers fullscreen on mobile.
falsefocusOnMount: 'firstElement' | booleanBy 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).
"firstElement"headerTitle: stringSet this to customize the text that is shown in the dropdown’s header when it is fullscreen on mobile.
onClose: () => voidA callback invoked when the popover should be closed.
open: booleanThe controlled open state of the dropdown. Must be used in conjunction with onToggle.
onToggle: ( willOpen: boolean ) => voidA callback invoked when the state of the dropdown changes from open to closed and vice versa.
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.
renderContent: ( props: CallbackProps ) => ReactNodeA callback invoked to render the content of the dropdown menu.
isOpen: whether the dropdown menu is opened or notonToggle: A function switching the dropdown menu’s state from open to closed and vice versaonClose: A function that closes the menu if invoked
Required: Yes
renderToggle: ( props: CallbackProps ) => ReactNodeA callback invoked to render the Dropdown Toggle Button.
Its props are the same as the renderContent props.
style: React.CSSPropertiesThe style of the global container