GoScrobble/web/node_modules/react-overlays/esm/DropdownToggle.js

74 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2022-04-25 02:47:15 +00:00
import PropTypes from 'prop-types';
import React, { useContext, useCallback } from 'react';
import DropdownContext from './DropdownContext';
var noop = function noop() {};
/**
* Wires up Dropdown toggle functionality, returning a set a props to attach
* to the element that functions as the dropdown toggle (generally a button).
*
* @memberOf Dropdown
*/
export function useDropdownToggle() {
var _ref = useContext(DropdownContext) || {},
_ref$show = _ref.show,
show = _ref$show === void 0 ? false : _ref$show,
_ref$toggle = _ref.toggle,
toggle = _ref$toggle === void 0 ? noop : _ref$toggle,
setToggle = _ref.setToggle;
var handleClick = useCallback(function (e) {
toggle(!show, e);
}, [show, toggle]);
return [{
ref: setToggle || noop,
onClick: handleClick,
'aria-haspopup': true,
'aria-expanded': !!show
}, {
show: show,
toggle: toggle
}];
}
var propTypes = {
/**
* A render prop that returns a Toggle element. The `props`
* argument should spread through to **a component that can accept a ref**. Use
* the `onToggle` argument to toggle the menu open or closed
*
* @type {Function ({
* show: boolean,
* toggle: (show: boolean) => void,
* props: {
* ref: (?HTMLElement) => void,
* aria-haspopup: true
* aria-expanded: boolean
* },
* }) => React.Element}
*/
children: PropTypes.func.isRequired
};
/**
* Also exported as `<Dropdown.Toggle>` from `Dropdown`.
*
* @displayName DropdownToggle
* @memberOf Dropdown
*/
function DropdownToggle(_ref2) {
var children = _ref2.children;
var _useDropdownToggle = useDropdownToggle(),
props = _useDropdownToggle[0],
meta = _useDropdownToggle[1];
return /*#__PURE__*/React.createElement(React.Fragment, null, children(props, meta));
}
DropdownToggle.displayName = 'ReactOverlaysDropdownToggle';
DropdownToggle.propTypes = propTypes;
/** @component */
export default DropdownToggle;