You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
3.8 KiB
118 lines
3.8 KiB
const _excluded = ["children"];
|
|
|
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
|
|
import { useContext, useRef } from 'react';
|
|
import * as React from 'react';
|
|
import useCallbackRef from '@restart/hooks/useCallbackRef';
|
|
import DropdownContext from './DropdownContext';
|
|
import usePopper from './usePopper';
|
|
import useClickOutside from './useClickOutside';
|
|
import mergeOptionsWithPopperConfig from './mergeOptionsWithPopperConfig';
|
|
import { Fragment as _Fragment } from "react/jsx-runtime";
|
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
|
const noop = () => {};
|
|
/**
|
|
* @memberOf Dropdown
|
|
* @param {object} options
|
|
* @param {boolean} options.flip Automatically adjust the menu `drop` position based on viewport edge detection
|
|
* @param {[number, number]} options.offset Define an offset distance between the Menu and the Toggle
|
|
* @param {boolean} options.show Display the menu manually, ignored in the context of a `Dropdown`
|
|
* @param {boolean} options.usePopper opt in/out of using PopperJS to position menus. When disabled you must position it yourself.
|
|
* @param {string} options.rootCloseEvent The pointer event to listen for when determining "clicks outside" the menu for triggering a close.
|
|
* @param {object} options.popperConfig Options passed to the [`usePopper`](/api/usePopper) hook.
|
|
*/
|
|
|
|
|
|
export function useDropdownMenu(options = {}) {
|
|
const context = useContext(DropdownContext);
|
|
const [arrowElement, attachArrowRef] = useCallbackRef();
|
|
const hasShownRef = useRef(false);
|
|
const {
|
|
flip,
|
|
offset,
|
|
rootCloseEvent,
|
|
fixed = false,
|
|
placement: placementOverride,
|
|
popperConfig = {},
|
|
enableEventListeners = true,
|
|
usePopper: shouldUsePopper = !!context
|
|
} = options;
|
|
const show = (context == null ? void 0 : context.show) == null ? !!options.show : context.show;
|
|
|
|
if (show && !hasShownRef.current) {
|
|
hasShownRef.current = true;
|
|
}
|
|
|
|
const handleClose = e => {
|
|
context == null ? void 0 : context.toggle(false, e);
|
|
};
|
|
|
|
const {
|
|
placement,
|
|
setMenu,
|
|
menuElement,
|
|
toggleElement
|
|
} = context || {};
|
|
const popper = usePopper(toggleElement, menuElement, mergeOptionsWithPopperConfig({
|
|
placement: placementOverride || placement || 'bottom-start',
|
|
enabled: shouldUsePopper,
|
|
enableEvents: enableEventListeners == null ? show : enableEventListeners,
|
|
offset,
|
|
flip,
|
|
fixed,
|
|
arrowElement,
|
|
popperConfig
|
|
}));
|
|
const menuProps = Object.assign({
|
|
ref: setMenu || noop,
|
|
'aria-labelledby': toggleElement == null ? void 0 : toggleElement.id
|
|
}, popper.attributes.popper, {
|
|
style: popper.styles.popper
|
|
});
|
|
const metadata = {
|
|
show,
|
|
placement,
|
|
hasShown: hasShownRef.current,
|
|
toggle: context == null ? void 0 : context.toggle,
|
|
popper: shouldUsePopper ? popper : null,
|
|
arrowProps: shouldUsePopper ? Object.assign({
|
|
ref: attachArrowRef
|
|
}, popper.attributes.arrow, {
|
|
style: popper.styles.arrow
|
|
}) : {}
|
|
};
|
|
useClickOutside(menuElement, handleClose, {
|
|
clickTrigger: rootCloseEvent,
|
|
disabled: !show
|
|
});
|
|
return [menuProps, metadata];
|
|
}
|
|
const defaultProps = {
|
|
usePopper: true
|
|
};
|
|
|
|
/**
|
|
* Also exported as `<Dropdown.Menu>` from `Dropdown`.
|
|
*
|
|
* @displayName DropdownMenu
|
|
* @memberOf Dropdown
|
|
*/
|
|
function DropdownMenu(_ref) {
|
|
let {
|
|
children
|
|
} = _ref,
|
|
options = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
|
|
const [props, meta] = useDropdownMenu(options);
|
|
return /*#__PURE__*/_jsx(_Fragment, {
|
|
children: children(props, meta)
|
|
});
|
|
}
|
|
|
|
DropdownMenu.displayName = 'DropdownMenu';
|
|
DropdownMenu.defaultProps = defaultProps;
|
|
/** @component */
|
|
|
|
export default DropdownMenu; |