UNPKG

2.44 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7exports.__esModule = true;
8exports.useDropdownToggle = useDropdownToggle;
9exports["default"] = void 0;
10
11var _propTypes = _interopRequireDefault(require("prop-types"));
12
13var _react = _interopRequireWildcard(require("react"));
14
15var _DropdownContext = _interopRequireDefault(require("./DropdownContext"));
16
17var noop = function noop() {};
18/**
19 * Wires up Dropdown toggle functionality, returning a set a props to attach
20 * to the element that functions as the dropdown toggle (generally a button).
21 *
22 * @memberOf Dropdown
23 */
24
25
26function useDropdownToggle() {
27 var _ref = (0, _react.useContext)(_DropdownContext["default"]) || {},
28 _ref$show = _ref.show,
29 show = _ref$show === void 0 ? false : _ref$show,
30 _ref$toggle = _ref.toggle,
31 toggle = _ref$toggle === void 0 ? noop : _ref$toggle,
32 setToggle = _ref.setToggle;
33
34 var handleClick = (0, _react.useCallback)(function (e) {
35 toggle(!show, e);
36 }, [show, toggle]);
37 return [{
38 ref: setToggle || noop,
39 onClick: handleClick,
40 'aria-haspopup': true,
41 'aria-expanded': !!show
42 }, {
43 show: show,
44 toggle: toggle
45 }];
46}
47
48var propTypes = {
49 /**
50 * A render prop that returns a Toggle element. The `props`
51 * argument should spread through to **a component that can accept a ref**. Use
52 * the `onToggle` argument to toggle the menu open or closed
53 *
54 * @type {Function ({
55 * show: boolean,
56 * toggle: (show: boolean) => void,
57 * props: {
58 * ref: (?HTMLElement) => void,
59 * aria-haspopup: true
60 * aria-expanded: boolean
61 * },
62 * }) => React.Element}
63 */
64 children: _propTypes["default"].func.isRequired
65};
66
67/**
68 * Also exported as `<Dropdown.Toggle>` from `Dropdown`.
69 *
70 * @displayName DropdownToggle
71 * @memberOf Dropdown
72 */
73function DropdownToggle(_ref2) {
74 var children = _ref2.children;
75
76 var _useDropdownToggle = useDropdownToggle(),
77 props = _useDropdownToggle[0],
78 meta = _useDropdownToggle[1];
79
80 return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, children(props, meta));
81}
82
83DropdownToggle.displayName = 'ReactOverlaysDropdownToggle';
84DropdownToggle.propTypes = propTypes;
85/** @component */
86
87var _default = DropdownToggle;
88exports["default"] = _default;
\No newline at end of file