UNPKG

8.6 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["default"] = void 0;
9
10var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
11
12var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
13
14var _propTypes = _interopRequireDefault(require("prop-types"));
15
16var _react = _interopRequireWildcard(require("react"));
17
18var _reactDom = _interopRequireDefault(require("react-dom"));
19
20var _useCallbackRef3 = _interopRequireDefault(require("@restart/hooks/useCallbackRef"));
21
22var _useMergedRefs = _interopRequireDefault(require("@restart/hooks/useMergedRefs"));
23
24var _popper = require("./popper");
25
26var _usePopper2 = _interopRequireDefault(require("./usePopper"));
27
28var _useRootClose = _interopRequireDefault(require("./useRootClose"));
29
30var _useWaitForDOMRef = _interopRequireDefault(require("./useWaitForDOMRef"));
31
32var _mergeOptionsWithPopperConfig = _interopRequireDefault(require("./mergeOptionsWithPopperConfig"));
33
34/**
35 * Built on top of `Popper.js`, the overlay component is
36 * great for custom tooltip overlays.
37 */
38var Overlay = /*#__PURE__*/_react["default"].forwardRef(function (props, outerRef) {
39 var flip = props.flip,
40 offset = props.offset,
41 placement = props.placement,
42 _props$containerPaddi = props.containerPadding,
43 containerPadding = _props$containerPaddi === void 0 ? 5 : _props$containerPaddi,
44 _props$popperConfig = props.popperConfig,
45 popperConfig = _props$popperConfig === void 0 ? {} : _props$popperConfig,
46 Transition = props.transition;
47
48 var _useCallbackRef = (0, _useCallbackRef3["default"])(),
49 rootElement = _useCallbackRef[0],
50 attachRef = _useCallbackRef[1];
51
52 var _useCallbackRef2 = (0, _useCallbackRef3["default"])(),
53 arrowElement = _useCallbackRef2[0],
54 attachArrowRef = _useCallbackRef2[1];
55
56 var mergedRef = (0, _useMergedRefs["default"])(attachRef, outerRef);
57 var container = (0, _useWaitForDOMRef["default"])(props.container);
58 var target = (0, _useWaitForDOMRef["default"])(props.target);
59
60 var _useState = (0, _react.useState)(!props.show),
61 exited = _useState[0],
62 setExited = _useState[1];
63
64 var _usePopper = (0, _usePopper2["default"])(target, rootElement, (0, _mergeOptionsWithPopperConfig["default"])({
65 placement: placement,
66 enableEvents: !!props.show,
67 containerPadding: containerPadding || 5,
68 flip: flip,
69 offset: offset,
70 arrowElement: arrowElement,
71 popperConfig: popperConfig
72 })),
73 styles = _usePopper.styles,
74 attributes = _usePopper.attributes,
75 popper = (0, _objectWithoutPropertiesLoose2["default"])(_usePopper, ["styles", "attributes"]);
76
77 if (props.show) {
78 if (exited) setExited(false);
79 } else if (!props.transition && !exited) {
80 setExited(true);
81 }
82
83 var handleHidden = function handleHidden() {
84 setExited(true);
85
86 if (props.onExited) {
87 props.onExited.apply(props, arguments);
88 }
89 }; // Don't un-render the overlay while it's transitioning out.
90
91
92 var mountOverlay = props.show || Transition && !exited;
93 (0, _useRootClose["default"])(rootElement, props.onHide, {
94 disabled: !props.rootClose || props.rootCloseDisabled,
95 clickTrigger: props.rootCloseEvent
96 });
97
98 if (!mountOverlay) {
99 // Don't bother showing anything if we don't have to.
100 return null;
101 }
102
103 var child = props.children((0, _extends2["default"])({}, popper, {
104 show: !!props.show,
105 props: (0, _extends2["default"])({}, attributes.popper, {
106 style: styles.popper,
107 ref: mergedRef
108 }),
109 arrowProps: (0, _extends2["default"])({}, attributes.arrow, {
110 style: styles.arrow,
111 ref: attachArrowRef
112 })
113 }));
114
115 if (Transition) {
116 var onExit = props.onExit,
117 onExiting = props.onExiting,
118 onEnter = props.onEnter,
119 onEntering = props.onEntering,
120 onEntered = props.onEntered;
121 child = /*#__PURE__*/_react["default"].createElement(Transition, {
122 "in": props.show,
123 appear: true,
124 onExit: onExit,
125 onExiting: onExiting,
126 onExited: handleHidden,
127 onEnter: onEnter,
128 onEntering: onEntering,
129 onEntered: onEntered
130 }, child);
131 }
132
133 return container ? /*#__PURE__*/_reactDom["default"].createPortal(child, container) : null;
134});
135
136Overlay.displayName = 'Overlay';
137Overlay.propTypes = {
138 /**
139 * Set the visibility of the Overlay
140 */
141 show: _propTypes["default"].bool,
142
143 /** Specify where the overlay element is positioned in relation to the target element */
144 placement: _propTypes["default"].oneOf(_popper.placements),
145
146 /**
147 * A DOM Element, Ref to an element, or function that returns either. The `target` element is where
148 * the overlay is positioned relative to.
149 */
150 target: _propTypes["default"].any,
151
152 /**
153 * A DOM Element, Ref to an element, or function that returns either. The `container` will have the Portal children
154 * appended to it.
155 */
156 container: _propTypes["default"].any,
157
158 /**
159 * Enables the Popper.js `flip` modifier, allowing the Overlay to
160 * automatically adjust it's placement in case of overlap with the viewport or toggle.
161 * Refer to the [flip docs](https://popper.js.org/popper-documentation.html#modifiers..flip.enabled) for more info
162 */
163 flip: _propTypes["default"].bool,
164
165 /**
166 * A render prop that returns an element to overlay and position. See
167 * the [react-popper documentation](https://github.com/FezVrasta/react-popper#children) for more info.
168 *
169 * @type {Function ({
170 * show: boolean,
171 * placement: Placement,
172 * update: () => void,
173 * forceUpdate: () => void,
174 * props: {
175 * ref: (?HTMLElement) => void,
176 * style: { [string]: string | number },
177 * aria-labelledby: ?string
178 * [string]: string | number,
179 * },
180 * arrowProps: {
181 * ref: (?HTMLElement) => void,
182 * style: { [string]: string | number },
183 * [string]: string | number,
184 * },
185 * }) => React.Element}
186 */
187 children: _propTypes["default"].func.isRequired,
188
189 /**
190 * Control how much space there is between the edge of the boundary element and overlay.
191 * A convenience shortcut to setting `popperConfig.modfiers.preventOverflow.padding`
192 */
193 containerPadding: _propTypes["default"].number,
194
195 /**
196 * A set of popper options and props passed directly to react-popper's Popper component.
197 */
198 popperConfig: _propTypes["default"].object,
199
200 /**
201 * Specify whether the overlay should trigger `onHide` when the user clicks outside the overlay
202 */
203 rootClose: _propTypes["default"].bool,
204
205 /**
206 * Specify event for toggling overlay
207 */
208 rootCloseEvent: _propTypes["default"].oneOf(['click', 'mousedown']),
209
210 /**
211 * Specify disabled for disable RootCloseWrapper
212 */
213 rootCloseDisabled: _propTypes["default"].bool,
214
215 /**
216 * A Callback fired by the Overlay when it wishes to be hidden.
217 *
218 * __required__ when `rootClose` is `true`.
219 *
220 * @type func
221 */
222 onHide: function onHide(props) {
223 for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
224 args[_key - 1] = arguments[_key];
225 }
226
227 if (props.rootClose) {
228 var _PropTypes$func;
229
230 return (_PropTypes$func = _propTypes["default"].func).isRequired.apply(_PropTypes$func, [props].concat(args));
231 }
232
233 return _propTypes["default"].func.apply(_propTypes["default"], [props].concat(args));
234 },
235
236 /**
237 * A `react-transition-group@2.0.0` `<Transition/>` component
238 * used to animate the overlay as it changes visibility.
239 */
240 // @ts-ignore
241 transition: _propTypes["default"].elementType,
242
243 /**
244 * Callback fired before the Overlay transitions in
245 */
246 onEnter: _propTypes["default"].func,
247
248 /**
249 * Callback fired as the Overlay begins to transition in
250 */
251 onEntering: _propTypes["default"].func,
252
253 /**
254 * Callback fired after the Overlay finishes transitioning in
255 */
256 onEntered: _propTypes["default"].func,
257
258 /**
259 * Callback fired right before the Overlay transitions out
260 */
261 onExit: _propTypes["default"].func,
262
263 /**
264 * Callback fired as the Overlay begins to transition out
265 */
266 onExiting: _propTypes["default"].func,
267
268 /**
269 * Callback fired after the Overlay finishes transitioning out
270 */
271 onExited: _propTypes["default"].func
272};
273var _default = Overlay;
274exports["default"] = _default;
275module.exports = exports.default;
\No newline at end of file