@wordpress/components
Version:
UI components for WordPress.
205 lines (204 loc) • 6.99 kB
JavaScript
import clsx from "clsx";
import { menu } from "@wordpress/icons";
import { useEffect, useState } from "@wordpress/element";
import { contextConnectWithoutRef, useContextSystem } from "../context/index.mjs";
import Button from "../button/index.mjs";
import Dropdown from "../dropdown/index.mjs";
import { NavigableMenu } from "../navigable-container/index.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/components/src/dropdown-menu/index.tsx
function mergeProps(defaultProps = {}, props = {}) {
const mergedProps = {
...defaultProps,
...props
};
if (props.className && defaultProps.className) mergedProps.className = clsx(props.className, defaultProps.className);
return mergedProps;
}
function isFunction(maybeFunc) {
return typeof maybeFunc === "function";
}
function UnconnectedDropdownMenu(dropdownMenuProps) {
const { children, className, controls, icon = menu, label, popoverProps, toggleProps, menuProps, disableOpenOnArrowDown = false, text, noIcons, open, defaultOpen, onToggle: onToggleProp, variant } = useContextSystem(dropdownMenuProps, "DropdownMenu");
const [focusFirstItem, setFocusFirstItem] = useState(true);
useEffect(() => {
if (open === false) setFocusFirstItem(true);
}, [open]);
if (!controls?.length && !isFunction(children)) return null;
let controlSets;
if (controls?.length) {
controlSets = controls;
if (!Array.isArray(controlSets[0])) controlSets = [controls];
}
const mergedPopoverProps = mergeProps({
className: "components-dropdown-menu__popover",
variant
}, popoverProps);
const onPopoverFocusCapture = (event) => {
popoverProps?.onFocusCapture?.(event);
if (!focusFirstItem && popoverProps?.focusOnMount === void 0 && event.target === event.currentTarget && event.currentTarget.ownerDocument.activeElement === event.currentTarget) event.currentTarget.querySelector("[role=\"menu\"]")?.focus({ preventScroll: true });
};
return /* @__PURE__ */ jsx(Dropdown, {
className,
popoverProps: {
...mergedPopoverProps,
focusOnMount: popoverProps?.focusOnMount ?? (focusFirstItem ? "firstElement" : true),
onFocusCapture: onPopoverFocusCapture
},
renderToggle: ({ isOpen, onToggle }) => {
const openOnArrowDown = (event) => {
if (disableOpenOnArrowDown || toggleProps?.disabled) return;
if (!isOpen && event.code === "ArrowDown") {
event.preventDefault();
setFocusFirstItem(true);
onToggle();
}
};
const { as: Toggle = Button, ...restToggleProps } = toggleProps ?? {};
const mergedToggleProps = mergeProps({ className: clsx("components-dropdown-menu__toggle", { "is-opened": isOpen }) }, restToggleProps);
return /* @__PURE__ */ jsx(Toggle, {
...mergedToggleProps,
icon,
onClick: ((event) => {
if (!isOpen) {
const nativeEvent = event.nativeEvent;
setFocusFirstItem(event.detail === 0 || "pointerType" in nativeEvent && nativeEvent.pointerType === "");
}
onToggle();
if (mergedToggleProps.onClick) mergedToggleProps.onClick(event);
}),
onKeyDown: ((event) => {
openOnArrowDown(event);
if (mergedToggleProps.onKeyDown) mergedToggleProps.onKeyDown(event);
}),
"aria-haspopup": "true",
"aria-expanded": isOpen,
label,
text,
showTooltip: toggleProps?.showTooltip ?? true,
children: mergedToggleProps.children
});
},
renderContent: (props) => {
const mergedMenuProps = mergeProps({
"aria-label": label,
className: clsx("components-dropdown-menu__menu", { "no-icons": noIcons })
}, menuProps);
return /* @__PURE__ */ jsxs(NavigableMenu, {
...mergedMenuProps,
role: "menu",
tabIndex: mergedMenuProps.tabIndex ?? -1,
children: [isFunction(children) ? children(props) : null, controlSets?.flatMap((controlSet, indexOfSet) => controlSet.map((control, indexOfControl) => /* @__PURE__ */ jsx(Button, {
size: "compact",
onClick: (event) => {
event.stopPropagation();
props.onClose();
if (control.onClick) control.onClick();
},
className: clsx("components-dropdown-menu__menu-item", {
"has-separator": indexOfSet > 0 && indexOfControl === 0,
"is-active": control.isActive,
"is-icon-only": !control.title
}),
icon: control.icon,
label: control.label,
onMouseMove: (event) => event.currentTarget.focus({ preventScroll: true }),
"aria-checked": control.role === "menuitemcheckbox" || control.role === "menuitemradio" ? control.isActive : void 0,
role: control.role === "menuitemcheckbox" || control.role === "menuitemradio" ? control.role : "menuitem",
accessibleWhenDisabled: true,
disabled: control.isDisabled,
children: control.title
}, [indexOfSet, indexOfControl].join())))]
});
},
open,
defaultOpen,
onToggle: onToggleProp
});
}
/**
*
* The DropdownMenu displays a list of actions (each contained in a MenuItem,
* MenuItemsChoice, or MenuGroup) in a compact way. It appears in a Popover
* after the user has interacted with an element (a button or icon) or when
* they perform a specific action.
*
* When building for the Gutenberg repo, use `Menu` from `@wordpress/ui` instead.
* Otherwise, continue using `DropdownMenu` for now.
*
* Render a Dropdown Menu with a set of controls:
*
* ```jsx
* import { DropdownMenu } from '@wordpress/components';
* import {
* more,
* arrowLeft,
* arrowRight,
* arrowUp,
* arrowDown,
* } from '@wordpress/icons';
*
* const MyDropdownMenu = () => (
* <DropdownMenu
* icon={ more }
* label="Select a direction"
* controls={ [
* {
* title: 'Up',
* icon: arrowUp,
* onClick: () => console.log( 'up' ),
* },
* {
* title: 'Right',
* icon: arrowRight,
* onClick: () => console.log( 'right' ),
* },
* {
* title: 'Down',
* icon: arrowDown,
* onClick: () => console.log( 'down' ),
* },
* {
* title: 'Left',
* icon: arrowLeft,
* onClick: () => console.log( 'left' ),
* },
* ] }
* />
* );
* ```
*
* Alternatively, specify a `children` function which returns elements valid for
* use in a DropdownMenu: `MenuItem`, `MenuItemsChoice`, or `MenuGroup`.
*
* ```jsx
* import { DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components';
* import { more, arrowUp, arrowDown, trash } from '@wordpress/icons';
*
* const MyDropdownMenu = () => (
* <DropdownMenu icon={ more } label="Select a direction">
* { ( { onClose } ) => (
* <>
* <MenuGroup>
* <MenuItem icon={ arrowUp } onClick={ onClose }>
* Move Up
* </MenuItem>
* <MenuItem icon={ arrowDown } onClick={ onClose }>
* Move Down
* </MenuItem>
* </MenuGroup>
* <MenuGroup>
* <MenuItem icon={ trash } onClick={ onClose }>
* Remove
* </MenuItem>
* </MenuGroup>
* </>
* ) }
* </DropdownMenu>
* );
* ```
*
*/
const DropdownMenu = contextConnectWithoutRef(UnconnectedDropdownMenu, "DropdownMenu");
export { DropdownMenu, DropdownMenu as default };
//# sourceMappingURL=index.mjs.map