1 line
5.3 KiB
JSON
1 line
5.3 KiB
JSON
{"ast":null,"code":"import _objectWithoutPropertiesLoose from \"@babel/runtime/helpers/esm/objectWithoutPropertiesLoose\";\nimport _extends from \"@babel/runtime/helpers/esm/extends\";\nimport PropTypes from 'prop-types';\nimport { useContext, useRef } from 'react';\nimport useCallbackRef from '@restart/hooks/useCallbackRef';\nimport DropdownContext from './DropdownContext';\nimport usePopper from './usePopper';\nimport useRootClose from './useRootClose';\nexport function useDropdownMenu(options) {\n if (options === void 0) {\n options = {};\n }\n\n var context = useContext(DropdownContext);\n\n var _useCallbackRef = useCallbackRef(),\n arrowElement = _useCallbackRef[0],\n attachArrowRef = _useCallbackRef[1];\n\n var hasShownRef = useRef(false);\n var _options = options,\n flip = _options.flip,\n rootCloseEvent = _options.rootCloseEvent,\n _options$popperConfig = _options.popperConfig,\n popperConfig = _options$popperConfig === void 0 ? {} : _options$popperConfig,\n _options$usePopper = _options.usePopper,\n shouldUsePopper = _options$usePopper === void 0 ? true : _options$usePopper;\n var show = context.show == null ? options.show : context.show;\n var alignEnd = context.alignEnd == null ? options.alignEnd : context.alignEnd;\n\n if (show && !hasShownRef.current) {\n hasShownRef.current = true;\n }\n\n var handleClose = function handleClose(e) {\n if (!context.toggle) return;\n context.toggle(false, e);\n };\n\n var drop = context.drop,\n setMenu = context.setMenu,\n menuElement = context.menuElement,\n toggleElement = context.toggleElement;\n var placement = alignEnd ? 'bottom-end' : 'bottom-start';\n if (drop === 'up') placement = alignEnd ? 'top-end' : 'top-start';else if (drop === 'right') placement = alignEnd ? 'right-end' : 'right-start';else if (drop === 'left') placement = alignEnd ? 'left-end' : 'left-start';\n var popper = usePopper(toggleElement, menuElement, {\n placement: placement,\n enabled: !!(shouldUsePopper && show),\n eventsEnabled: !!show,\n modifiers: _extends({\n flip: {\n enabled: !!flip\n },\n arrow: _extends({}, popperConfig.modifiers && popperConfig.modifiers.arrow, {\n enabled: !!arrowElement,\n element: arrowElement\n })\n }, popperConfig.modifiers)\n });\n var menu = null;\n var menuProps = {\n ref: setMenu,\n 'aria-labelledby': toggleElement && toggleElement.id\n };\n var childArgs = {\n show: show,\n alignEnd: alignEnd,\n hasShown: hasShownRef.current,\n close: handleClose\n };\n\n if (!shouldUsePopper) {\n menu = _extends({}, childArgs, {\n props: menuProps\n });\n } else {\n menu = _extends({}, popper, {}, childArgs, {\n props: _extends({}, menuProps, {\n style: popper.styles\n }),\n arrowProps: {\n ref: attachArrowRef,\n style: popper.arrowStyles\n }\n });\n }\n\n useRootClose(menuElement, handleClose, {\n clickTrigger: rootCloseEvent,\n disabled: !(menu && show)\n });\n return menu;\n}\nvar propTypes = {\n /**\n * A render prop that returns a Menu element. The `props`\n * argument should spread through to **a component that can accept a ref**.\n *\n * @type {Function ({\n * show: boolean,\n * alignEnd: boolean,\n * close: (?SyntheticEvent) => void,\n * placement: Placement,\n * outOfBoundaries: ?boolean,\n * scheduleUpdate: () => void,\n * props: {\n * ref: (?HTMLElement) => void,\n * style: { [string]: string | number },\n * aria-labelledby: ?string\n * },\n * arrowProps: {\n * ref: (?HTMLElement) => void,\n * style: { [string]: string | number },\n * },\n * }) => React.Element}\n */\n children: PropTypes.func.isRequired,\n\n /**\n * Controls the visible state of the menu, generally this is\n * provided by the parent `Dropdown` component,\n * but may also be specified as a prop directly.\n */\n show: PropTypes.bool,\n\n /**\n * Aligns the dropdown menu to the 'end' of it's placement position.\n * Generally this is provided by the parent `Dropdown` component,\n * but may also be specified as a prop directly.\n */\n alignEnd: PropTypes.bool,\n\n /**\n * Enables the Popper.js `flip` modifier, allowing the Dropdown to\n * automatically adjust it's placement in case of overlap with the viewport or toggle.\n * Refer to the [flip docs](https://popper.js.org/popper-documentation.html#modifiers..flip.enabled) for more info\n */\n flip: PropTypes.bool,\n usePopper: PropTypes.oneOf([true, false]),\n\n /**\n * A set of popper options and props passed directly to react-popper's Popper component.\n */\n popperConfig: PropTypes.object,\n\n /**\n * Override the default event used by RootCloseWrapper.\n */\n rootCloseEvent: PropTypes.string\n};\nvar defaultProps = {\n usePopper: true\n};\n\nfunction DropdownMenu(_ref) {\n var children = _ref.children,\n options = _objectWithoutPropertiesLoose(_ref, [\"children\"]);\n\n var args = useDropdownMenu(options);\n return args.hasShown ? children(args) : null;\n}\n\nDropdownMenu.displayName = 'ReactOverlaysDropdownMenu';\nDropdownMenu.propTypes = propTypes;\nDropdownMenu.defaultProps = defaultProps;\n/** @component */\n\nexport default DropdownMenu;","map":null,"metadata":{},"sourceType":"module"} |