UNPKG

4.26 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.getPlacement = getPlacement;
11exports["default"] = void 0;
12
13var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
14
15var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
16
17var React = _interopRequireWildcard(require("react"));
18
19var _propTypes = _interopRequireDefault(require("prop-types"));
20
21var _reactPopper = require("react-popper");
22
23var _utils = require("../utils");
24
25var _constants = require("../constants");
26
27/* eslint-disable react/no-unused-prop-types */
28// `Element` is not defined during server-side rendering, so shim it here.
29
30/* istanbul ignore next */
31var SafeElement = typeof Element === 'undefined' ? function () {} : Element;
32var propTypes = {
33 /**
34 * Specify menu alignment. The default value is `justify`, which makes the
35 * menu as wide as the input and truncates long values. Specifying `left`
36 * or `right` will align the menu to that side and the width will be
37 * determined by the length of menu item values.
38 */
39 align: _propTypes["default"].oneOf((0, _utils.values)(_constants.ALIGN)),
40 children: _propTypes["default"].func.isRequired,
41
42 /**
43 * Specify whether the menu should appear above the input.
44 */
45 dropup: _propTypes["default"].bool,
46
47 /**
48 * Whether or not to automatically adjust the position of the menu when it
49 * reaches the viewport boundaries.
50 */
51 flip: _propTypes["default"].bool,
52 isMenuShown: _propTypes["default"].bool,
53 positionFixed: _propTypes["default"].bool,
54 referenceElement: _propTypes["default"].instanceOf(SafeElement)
55};
56var defaultProps = {
57 align: _constants.ALIGN.JUSTIFY,
58 dropup: false,
59 flip: false,
60 isMenuShown: false,
61 positionFixed: false
62};
63
64function getModifiers(_ref) {
65 var align = _ref.align,
66 flip = _ref.flip;
67 return {
68 computeStyles: {
69 enabled: true,
70 fn: function fn(_ref2) {
71 var styles = _ref2.styles,
72 data = (0, _objectWithoutPropertiesLoose2["default"])(_ref2, ["styles"]);
73 return (0, _extends2["default"])({}, data, {
74 styles: (0, _extends2["default"])({}, styles, {
75 // Use the following condition instead of `align === 'justify'`
76 // since it allows the component to fall back to justifying the
77 // menu width if `align` is undefined.
78 width: align !== _constants.ALIGN.RIGHT && align !== _constants.ALIGN.LEFT ? // Set the popper width to match the target width.
79 data.offsets.reference.width : styles.width
80 })
81 });
82 }
83 },
84 flip: {
85 enabled: flip
86 },
87 preventOverflow: {
88 escapeWithReference: true
89 }
90 };
91} // Flow expects a string literal value for `placement`.
92
93
94var PLACEMENT = {
95 bottom: {
96 end: 'bottom-end',
97 start: 'bottom-start'
98 },
99 top: {
100 end: 'top-end',
101 start: 'top-start'
102 }
103};
104
105function getPlacement(_ref3) {
106 var align = _ref3.align,
107 dropup = _ref3.dropup;
108 var x = align === _constants.ALIGN.RIGHT ? 'end' : 'start';
109 var y = dropup ? 'top' : 'bottom';
110 return PLACEMENT[y][x];
111}
112
113var Overlay = function Overlay(props) {
114 var children = props.children,
115 isMenuShown = props.isMenuShown,
116 positionFixed = props.positionFixed,
117 referenceElement = props.referenceElement;
118
119 if (!isMenuShown) {
120 return null;
121 }
122
123 return /*#__PURE__*/React.createElement(_reactPopper.Popper, {
124 modifiers: getModifiers(props),
125 placement: getPlacement(props),
126 positionFixed: positionFixed,
127 referenceElement: referenceElement
128 }, function (_ref4) {
129 var ref = _ref4.ref,
130 popperProps = (0, _objectWithoutPropertiesLoose2["default"])(_ref4, ["ref"]);
131 return children((0, _extends2["default"])({}, popperProps, {
132 innerRef: ref,
133 inputHeight: referenceElement ? referenceElement.offsetHeight : 0
134 }));
135 });
136};
137
138Overlay.propTypes = propTypes;
139Overlay.defaultProps = defaultProps;
140var _default = Overlay;
141exports["default"] = _default;
\No newline at end of file