UNPKG

6.81 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5exports.__esModule = true;
6exports["default"] = void 0;
7
8var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9
10var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
11
12var _react = require("react");
13
14var _useSafeState2 = _interopRequireDefault(require("@restart/hooks/useSafeState"));
15
16var _popper = require("./popper");
17
18var initialPopperStyles = function initialPopperStyles(position) {
19 return {
20 position: position,
21 top: '0',
22 left: '0',
23 opacity: '0',
24 pointerEvents: 'none'
25 };
26};
27
28var disabledApplyStylesModifier = {
29 name: 'applyStyles',
30 enabled: false
31}; // In order to satisfy the current usage of options, including undefined
32
33var ariaDescribedByModifier = {
34 name: 'ariaDescribedBy',
35 enabled: true,
36 phase: 'afterWrite',
37 effect: function effect(_ref) {
38 var state = _ref.state;
39 return function () {
40 var _state$elements = state.elements,
41 reference = _state$elements.reference,
42 popper = _state$elements.popper;
43
44 if ('removeAttribute' in reference) {
45 var ids = (reference.getAttribute('aria-describedby') || '').split(',').filter(function (id) {
46 return id.trim() !== popper.id;
47 });
48 if (!ids.length) reference.removeAttribute('aria-describedby');else reference.setAttribute('aria-describedby', ids.join(','));
49 }
50 };
51 },
52 fn: function fn(_ref2) {
53 var _popper$getAttribute;
54
55 var state = _ref2.state;
56 var _state$elements2 = state.elements,
57 popper = _state$elements2.popper,
58 reference = _state$elements2.reference;
59 var role = (_popper$getAttribute = popper.getAttribute('role')) == null ? void 0 : _popper$getAttribute.toLowerCase();
60
61 if (popper.id && role === 'tooltip' && 'setAttribute' in reference) {
62 var ids = reference.getAttribute('aria-describedby');
63
64 if (ids && ids.split(',').indexOf(popper.id) !== -1) {
65 return;
66 }
67
68 reference.setAttribute('aria-describedby', ids ? ids + "," + popper.id : popper.id);
69 }
70 }
71};
72var EMPTY_MODIFIERS = [];
73/**
74 * Position an element relative some reference element using Popper.js
75 *
76 * @param referenceElement
77 * @param popperElement
78 * @param {object} options
79 * @param {object=} options.modifiers Popper.js modifiers
80 * @param {boolean=} options.enabled toggle the popper functionality on/off
81 * @param {string=} options.placement The popper element placement relative to the reference element
82 * @param {string=} options.strategy the positioning strategy
83 * @param {boolean=} options.eventsEnabled have Popper listen on window resize events to reposition the element
84 * @param {function=} options.onCreate called when the popper is created
85 * @param {function=} options.onUpdate called when the popper is updated
86 *
87 * @returns {UsePopperState} The popper state
88 */
89
90function usePopper(referenceElement, popperElement, _temp) {
91 var _ref3 = _temp === void 0 ? {} : _temp,
92 _ref3$enabled = _ref3.enabled,
93 enabled = _ref3$enabled === void 0 ? true : _ref3$enabled,
94 _ref3$placement = _ref3.placement,
95 placement = _ref3$placement === void 0 ? 'bottom' : _ref3$placement,
96 _ref3$strategy = _ref3.strategy,
97 strategy = _ref3$strategy === void 0 ? 'absolute' : _ref3$strategy,
98 _ref3$modifiers = _ref3.modifiers,
99 modifiers = _ref3$modifiers === void 0 ? EMPTY_MODIFIERS : _ref3$modifiers,
100 config = (0, _objectWithoutPropertiesLoose2["default"])(_ref3, ["enabled", "placement", "strategy", "modifiers"]);
101
102 var popperInstanceRef = (0, _react.useRef)();
103 var update = (0, _react.useCallback)(function () {
104 var _popperInstanceRef$cu;
105
106 (_popperInstanceRef$cu = popperInstanceRef.current) == null ? void 0 : _popperInstanceRef$cu.update();
107 }, []);
108 var forceUpdate = (0, _react.useCallback)(function () {
109 var _popperInstanceRef$cu2;
110
111 (_popperInstanceRef$cu2 = popperInstanceRef.current) == null ? void 0 : _popperInstanceRef$cu2.forceUpdate();
112 }, []);
113
114 var _useSafeState = (0, _useSafeState2["default"])((0, _react.useState)({
115 placement: placement,
116 update: update,
117 forceUpdate: forceUpdate,
118 attributes: {},
119 styles: {
120 popper: initialPopperStyles(strategy),
121 arrow: {}
122 }
123 })),
124 popperState = _useSafeState[0],
125 setState = _useSafeState[1];
126
127 var updateModifier = (0, _react.useMemo)(function () {
128 return {
129 name: 'updateStateModifier',
130 enabled: true,
131 phase: 'write',
132 requires: ['computeStyles'],
133 fn: function fn(_ref4) {
134 var state = _ref4.state;
135 var styles = {};
136 var attributes = {};
137 Object.keys(state.elements).forEach(function (element) {
138 styles[element] = state.styles[element];
139 attributes[element] = state.attributes[element];
140 });
141 setState({
142 state: state,
143 styles: styles,
144 attributes: attributes,
145 update: update,
146 forceUpdate: forceUpdate,
147 placement: state.placement
148 });
149 }
150 };
151 }, [update, forceUpdate, setState]);
152 (0, _react.useEffect)(function () {
153 if (!popperInstanceRef.current || !enabled) return;
154 popperInstanceRef.current.setOptions({
155 placement: placement,
156 strategy: strategy,
157 modifiers: [].concat(modifiers, [updateModifier, disabledApplyStylesModifier])
158 }); // intentionally NOT re-running on new modifiers
159 // eslint-disable-next-line react-hooks/exhaustive-deps
160 }, [strategy, placement, updateModifier, enabled]);
161 (0, _react.useEffect)(function () {
162 if (!enabled || referenceElement == null || popperElement == null) {
163 return undefined;
164 }
165
166 popperInstanceRef.current = (0, _popper.createPopper)(referenceElement, popperElement, (0, _extends2["default"])({}, config, {
167 placement: placement,
168 strategy: strategy,
169 modifiers: [].concat(modifiers, [ariaDescribedByModifier, updateModifier])
170 }));
171 return function () {
172 if (popperInstanceRef.current != null) {
173 popperInstanceRef.current.destroy();
174 popperInstanceRef.current = undefined;
175 setState(function (s) {
176 return (0, _extends2["default"])({}, s, {
177 attributes: {},
178 styles: {
179 popper: initialPopperStyles(strategy)
180 }
181 });
182 });
183 }
184 }; // This is only run once to _create_ the popper
185 // eslint-disable-next-line react-hooks/exhaustive-deps
186 }, [enabled, referenceElement, popperElement]);
187 return popperState;
188}
189
190var _default = usePopper;
191exports["default"] = _default;
192module.exports = exports.default;
\No newline at end of file