@atlaskit/popup 1.15.0 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _typeof = require("@babel/runtime/helpers/typeof");
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.PopupTrigger = exports.PopupContent = exports.Popup = void 0;
9
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
10
+ var _react = _interopRequireWildcard(require("react"));
11
+ var _tinyInvariant = _interopRequireDefault(require("tiny-invariant"));
12
+ var _noop = _interopRequireDefault(require("@atlaskit/ds-lib/noop"));
13
+ var _layering = require("@atlaskit/layering");
14
+ var _popper = require("@atlaskit/popper");
15
+ var _portal = _interopRequireDefault(require("@atlaskit/portal"));
16
+ var _constants = require("@atlaskit/theme/constants");
17
+ var _popperWrapper = _interopRequireDefault(require("../popper-wrapper"));
18
+ var _useGetMemoizedMergedTriggerRef = require("../use-get-memoized-merged-trigger-ref");
19
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
20
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
21
+ var IsOpenContext = /*#__PURE__*/(0, _react.createContext)(false);
22
+ var IdContext = /*#__PURE__*/(0, _react.createContext)(undefined);
23
+ var TriggerRefContext = /*#__PURE__*/(0, _react.createContext)(null);
24
+ var SetTriggerRefContext = /*#__PURE__*/(0, _react.createContext)(_noop.default);
25
+ var EnsureIsInsidePopupContext = /*#__PURE__*/(0, _react.createContext)(false);
26
+
27
+ // Used to ensure popup sub-components are used within a Popup
28
+ // and provide a useful error message if not.
29
+ var useEnsureIsInsidePopup = function useEnsureIsInsidePopup() {
30
+ var context = (0, _react.useContext)(EnsureIsInsidePopupContext);
31
+ (0, _tinyInvariant.default)(context, 'PopupTrigger and PopupContent components must be used within a Popup');
32
+ };
33
+ /**
34
+ * __Popup__
35
+ *
36
+ * Popup is a composable component that provides the context for the trigger and content components.
37
+ *
38
+ * Usage example:
39
+ * ```jsx
40
+ * <Popup>
41
+ * <PopupTrigger>
42
+ * {(props) => (
43
+ * <button type="button" {...props}>Click me</button>
44
+ * )}
45
+ * </PopupTrigger>
46
+ * <PopupContent>
47
+ * {(props) => <div>Hello world</div>}
48
+ * </PopupContent>
49
+ * </Popup>
50
+ * ```
51
+ */
52
+ var Popup = exports.Popup = function Popup(_ref) {
53
+ var children = _ref.children,
54
+ id = _ref.id,
55
+ _ref$isOpen = _ref.isOpen,
56
+ isOpen = _ref$isOpen === void 0 ? false : _ref$isOpen;
57
+ var _useState = (0, _react.useState)(null),
58
+ _useState2 = (0, _slicedToArray2.default)(_useState, 2),
59
+ triggerRef = _useState2[0],
60
+ setTriggerRef = _useState2[1];
61
+ return /*#__PURE__*/_react.default.createElement(EnsureIsInsidePopupContext.Provider, {
62
+ value: true
63
+ }, /*#__PURE__*/_react.default.createElement(IdContext.Provider, {
64
+ value: id
65
+ }, /*#__PURE__*/_react.default.createElement(TriggerRefContext.Provider, {
66
+ value: triggerRef
67
+ }, /*#__PURE__*/_react.default.createElement(SetTriggerRefContext.Provider, {
68
+ value: setTriggerRef
69
+ }, /*#__PURE__*/_react.default.createElement(IsOpenContext.Provider, {
70
+ value: isOpen
71
+ }, /*#__PURE__*/_react.default.createElement(_popper.Manager, null, children))))));
72
+ };
73
+ /**
74
+ * __Popup trigger__
75
+ *
76
+ * Popup trigger is the component that renders the trigger for the popup.
77
+ *
78
+ * It must be a child of the Popup component.
79
+ */
80
+ var PopupTrigger = exports.PopupTrigger = function PopupTrigger(_ref2) {
81
+ var children = _ref2.children;
82
+ useEnsureIsInsidePopup();
83
+ var isOpen = (0, _react.useContext)(IsOpenContext);
84
+ var id = (0, _react.useContext)(IdContext);
85
+ var setTriggerRef = (0, _react.useContext)(SetTriggerRefContext);
86
+ var getMergedTriggerRef = (0, _useGetMemoizedMergedTriggerRef.useGetMemoizedMergedTriggerRef)();
87
+ return /*#__PURE__*/_react.default.createElement(_popper.Reference, null, function (_ref3) {
88
+ var ref = _ref3.ref;
89
+ return children({
90
+ ref: getMergedTriggerRef(ref, setTriggerRef, isOpen),
91
+ 'aria-controls': id,
92
+ 'aria-expanded': isOpen,
93
+ 'aria-haspopup': true
94
+ });
95
+ });
96
+ };
97
+ var defaultLayer = _constants.layers.layer();
98
+ /**
99
+ * __Popup content__
100
+ *
101
+ * Popup content is the component that renders the content of the popup.
102
+ *
103
+ * It must be a child of the Popup component.
104
+ */
105
+ var PopupContent = exports.PopupContent = function PopupContent(_ref4) {
106
+ var children = _ref4.children,
107
+ boundary = _ref4.boundary,
108
+ offset = _ref4.offset,
109
+ strategy = _ref4.strategy,
110
+ onClose = _ref4.onClose,
111
+ testId = _ref4.testId,
112
+ _ref4$rootBoundary = _ref4.rootBoundary,
113
+ rootBoundary = _ref4$rootBoundary === void 0 ? 'viewport' : _ref4$rootBoundary,
114
+ _ref4$shouldFlip = _ref4.shouldFlip,
115
+ shouldFlip = _ref4$shouldFlip === void 0 ? true : _ref4$shouldFlip,
116
+ _ref4$placement = _ref4.placement,
117
+ placement = _ref4$placement === void 0 ? 'auto' : _ref4$placement,
118
+ fallbackPlacements = _ref4.fallbackPlacements,
119
+ popupComponent = _ref4.popupComponent,
120
+ _ref4$autoFocus = _ref4.autoFocus,
121
+ autoFocus = _ref4$autoFocus === void 0 ? true : _ref4$autoFocus,
122
+ _ref4$zIndex = _ref4.zIndex,
123
+ zIndex = _ref4$zIndex === void 0 ? defaultLayer : _ref4$zIndex,
124
+ _ref4$shouldUseCaptur = _ref4.shouldUseCaptureOnOutsideClick,
125
+ shouldUseCaptureOnOutsideClick = _ref4$shouldUseCaptur === void 0 ? false : _ref4$shouldUseCaptur,
126
+ _ref4$shouldRenderToP = _ref4.shouldRenderToParent,
127
+ shouldRenderToParent = _ref4$shouldRenderToP === void 0 ? false : _ref4$shouldRenderToP,
128
+ _ref4$shouldDisableFo = _ref4.shouldDisableFocusLock,
129
+ shouldDisableFocusLock = _ref4$shouldDisableFo === void 0 ? false : _ref4$shouldDisableFo;
130
+ useEnsureIsInsidePopup();
131
+ var isOpen = (0, _react.useContext)(IsOpenContext);
132
+ var id = (0, _react.useContext)(IdContext);
133
+ var triggerRef = (0, _react.useContext)(TriggerRefContext);
134
+ if (!isOpen) {
135
+ return null;
136
+ }
137
+ var popperWrapper = /*#__PURE__*/_react.default.createElement(_layering.UNSAFE_LAYERING, {
138
+ isDisabled: false
139
+ }, /*#__PURE__*/_react.default.createElement(_popperWrapper.default, {
140
+ content: children,
141
+ isOpen: isOpen,
142
+ placement: placement,
143
+ fallbackPlacements: fallbackPlacements,
144
+ boundary: boundary,
145
+ rootBoundary: rootBoundary,
146
+ shouldFlip: shouldFlip,
147
+ offset: offset,
148
+ popupComponent: popupComponent,
149
+ id: id,
150
+ testId: testId,
151
+ onClose: onClose,
152
+ autoFocus: autoFocus,
153
+ shouldUseCaptureOnOutsideClick: shouldUseCaptureOnOutsideClick,
154
+ shouldRenderToParent: shouldRenderToParent,
155
+ shouldDisableFocusLock: shouldDisableFocusLock,
156
+ triggerRef: triggerRef,
157
+ strategy: strategy
158
+ }));
159
+ if (shouldRenderToParent) {
160
+ return popperWrapper;
161
+ }
162
+ return /*#__PURE__*/_react.default.createElement(_portal.default, {
163
+ zIndex: zIndex
164
+ }, popperWrapper);
165
+ };
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "Popup", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _popup.Popup;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "PopupContent", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _popup.PopupContent;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "PopupTrigger", {
19
+ enumerable: true,
20
+ get: function get() {
21
+ return _popup.PopupTrigger;
22
+ }
23
+ });
24
+ var _popup = require("../../compositional/popup");
package/dist/cjs/popup.js CHANGED
@@ -8,12 +8,12 @@ exports.default = exports.Popup = void 0;
8
8
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
9
  var _react = require("react");
10
10
  var _react2 = require("@emotion/react");
11
- var _memoizeOne = _interopRequireDefault(require("memoize-one"));
12
11
  var _layering = require("@atlaskit/layering");
13
12
  var _popper = require("@atlaskit/popper");
14
13
  var _portal = _interopRequireDefault(require("@atlaskit/portal"));
15
14
  var _constants = require("@atlaskit/theme/constants");
16
15
  var _popperWrapper = _interopRequireDefault(require("./popper-wrapper"));
16
+ var _useGetMemoizedMergedTriggerRef = require("./use-get-memoized-merged-trigger-ref");
17
17
  /* eslint-disable @repo/internal/react/require-jsdoc */
18
18
  /** @jsx jsx */
19
19
 
@@ -50,33 +50,7 @@ var Popup = exports.Popup = /*#__PURE__*/(0, _react.memo)(function (_ref) {
50
50
  _useState2 = (0, _slicedToArray2.default)(_useState, 2),
51
51
  triggerRef = _useState2[0],
52
52
  setTriggerRef = _useState2[1];
53
-
54
- /*
55
- * Get a memoized functional ref for use within this Popup's Trigger.
56
- * This is still very volatile to change as `prop.isOpen` will regularly change, but it's better than nothing.
57
- * This is memoized within our component as to not be shared across all Popup instances, just this one.
58
- *
59
- * This is complex because the inputs are split across three different scopes:
60
- * - `props.isOpen`
61
- * - `useState.setTriggerRef`
62
- * - `renderProps.ref`
63
- */
64
- var _useState3 = (0, _react.useState)(function () {
65
- return (0, _memoizeOne.default)(function (ref, setTriggerRef, isOpen) {
66
- return function (node) {
67
- if (node && isOpen) {
68
- if (typeof ref === 'function') {
69
- ref(node);
70
- } else if (ref) {
71
- ref.current = node;
72
- }
73
- setTriggerRef(node);
74
- }
75
- };
76
- });
77
- }),
78
- _useState4 = (0, _slicedToArray2.default)(_useState3, 1),
79
- getMergedTriggerRef = _useState4[0];
53
+ var getMergedTriggerRef = (0, _useGetMemoizedMergedTriggerRef.useGetMemoizedMergedTriggerRef)();
80
54
  var renderPopperWrapper = (0, _react2.jsx)(_layering.UNSAFE_LAYERING, {
81
55
  isDisabled: false
82
56
  }, (0, _react2.jsx)(_popperWrapper.default, {
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.useGetMemoizedMergedTriggerRef = void 0;
8
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
+ var _react = require("react");
10
+ var _memoizeOne = _interopRequireDefault(require("memoize-one"));
11
+ /*
12
+ * Get a memoized functional ref for use within a Popup's Trigger.
13
+ * This is still very volatile to change as `prop.isOpen` will regularly change, but it's better than nothing.
14
+ * This is memoized within a component as to not be shared across all Popup instances.
15
+ *
16
+ * This is complex because the inputs are split across three different scopes:
17
+ * - `props.isOpen`
18
+ * - `useState.setTriggerRef`
19
+ * - `renderProps.ref`
20
+ */
21
+ var useGetMemoizedMergedTriggerRef = exports.useGetMemoizedMergedTriggerRef = function useGetMemoizedMergedTriggerRef() {
22
+ var _useState = (0, _react.useState)(function () {
23
+ return (0, _memoizeOne.default)(function (ref, setTriggerRef, isOpen) {
24
+ return function (node) {
25
+ if (node && isOpen) {
26
+ if (typeof ref === 'function') {
27
+ ref(node);
28
+ } else if (ref) {
29
+ ref.current = node;
30
+ }
31
+ setTriggerRef(node);
32
+ }
33
+ };
34
+ });
35
+ }),
36
+ _useState2 = (0, _slicedToArray2.default)(_useState, 1),
37
+ getMemoizedMergedTriggerRef = _useState2[0];
38
+ return getMemoizedMergedTriggerRef;
39
+ };
@@ -0,0 +1,144 @@
1
+ import React, { createContext, useContext, useState } from 'react';
2
+ import invariant from 'tiny-invariant';
3
+ import noop from '@atlaskit/ds-lib/noop';
4
+ import { UNSAFE_LAYERING } from '@atlaskit/layering';
5
+ import { Manager, Reference } from '@atlaskit/popper';
6
+ import Portal from '@atlaskit/portal';
7
+ import { layers } from '@atlaskit/theme/constants';
8
+ import PopperWrapper from '../popper-wrapper';
9
+ import { useGetMemoizedMergedTriggerRef } from '../use-get-memoized-merged-trigger-ref';
10
+ const IsOpenContext = /*#__PURE__*/createContext(false);
11
+ const IdContext = /*#__PURE__*/createContext(undefined);
12
+ const TriggerRefContext = /*#__PURE__*/createContext(null);
13
+ const SetTriggerRefContext = /*#__PURE__*/createContext(noop);
14
+ const EnsureIsInsidePopupContext = /*#__PURE__*/createContext(false);
15
+
16
+ // Used to ensure popup sub-components are used within a Popup
17
+ // and provide a useful error message if not.
18
+ const useEnsureIsInsidePopup = () => {
19
+ const context = useContext(EnsureIsInsidePopupContext);
20
+ invariant(context, 'PopupTrigger and PopupContent components must be used within a Popup');
21
+ };
22
+ /**
23
+ * __Popup__
24
+ *
25
+ * Popup is a composable component that provides the context for the trigger and content components.
26
+ *
27
+ * Usage example:
28
+ * ```jsx
29
+ * <Popup>
30
+ * <PopupTrigger>
31
+ * {(props) => (
32
+ * <button type="button" {...props}>Click me</button>
33
+ * )}
34
+ * </PopupTrigger>
35
+ * <PopupContent>
36
+ * {(props) => <div>Hello world</div>}
37
+ * </PopupContent>
38
+ * </Popup>
39
+ * ```
40
+ */
41
+ export const Popup = ({
42
+ children,
43
+ id,
44
+ isOpen = false
45
+ }) => {
46
+ const [triggerRef, setTriggerRef] = useState(null);
47
+ return /*#__PURE__*/React.createElement(EnsureIsInsidePopupContext.Provider, {
48
+ value: true
49
+ }, /*#__PURE__*/React.createElement(IdContext.Provider, {
50
+ value: id
51
+ }, /*#__PURE__*/React.createElement(TriggerRefContext.Provider, {
52
+ value: triggerRef
53
+ }, /*#__PURE__*/React.createElement(SetTriggerRefContext.Provider, {
54
+ value: setTriggerRef
55
+ }, /*#__PURE__*/React.createElement(IsOpenContext.Provider, {
56
+ value: isOpen
57
+ }, /*#__PURE__*/React.createElement(Manager, null, children))))));
58
+ };
59
+ /**
60
+ * __Popup trigger__
61
+ *
62
+ * Popup trigger is the component that renders the trigger for the popup.
63
+ *
64
+ * It must be a child of the Popup component.
65
+ */
66
+ export const PopupTrigger = ({
67
+ children
68
+ }) => {
69
+ useEnsureIsInsidePopup();
70
+ const isOpen = useContext(IsOpenContext);
71
+ const id = useContext(IdContext);
72
+ const setTriggerRef = useContext(SetTriggerRefContext);
73
+ const getMergedTriggerRef = useGetMemoizedMergedTriggerRef();
74
+ return /*#__PURE__*/React.createElement(Reference, null, ({
75
+ ref
76
+ }) => children({
77
+ ref: getMergedTriggerRef(ref, setTriggerRef, isOpen),
78
+ 'aria-controls': id,
79
+ 'aria-expanded': isOpen,
80
+ 'aria-haspopup': true
81
+ }));
82
+ };
83
+ const defaultLayer = layers.layer();
84
+ /**
85
+ * __Popup content__
86
+ *
87
+ * Popup content is the component that renders the content of the popup.
88
+ *
89
+ * It must be a child of the Popup component.
90
+ */
91
+ export const PopupContent = ({
92
+ children,
93
+ boundary,
94
+ offset,
95
+ strategy,
96
+ onClose,
97
+ testId,
98
+ rootBoundary = 'viewport',
99
+ shouldFlip = true,
100
+ placement = 'auto',
101
+ fallbackPlacements,
102
+ popupComponent,
103
+ autoFocus = true,
104
+ zIndex = defaultLayer,
105
+ shouldUseCaptureOnOutsideClick = false,
106
+ shouldRenderToParent = false,
107
+ shouldDisableFocusLock = false
108
+ }) => {
109
+ useEnsureIsInsidePopup();
110
+ const isOpen = useContext(IsOpenContext);
111
+ const id = useContext(IdContext);
112
+ const triggerRef = useContext(TriggerRefContext);
113
+ if (!isOpen) {
114
+ return null;
115
+ }
116
+ const popperWrapper = /*#__PURE__*/React.createElement(UNSAFE_LAYERING, {
117
+ isDisabled: false
118
+ }, /*#__PURE__*/React.createElement(PopperWrapper, {
119
+ content: children,
120
+ isOpen: isOpen,
121
+ placement: placement,
122
+ fallbackPlacements: fallbackPlacements,
123
+ boundary: boundary,
124
+ rootBoundary: rootBoundary,
125
+ shouldFlip: shouldFlip,
126
+ offset: offset,
127
+ popupComponent: popupComponent,
128
+ id: id,
129
+ testId: testId,
130
+ onClose: onClose,
131
+ autoFocus: autoFocus,
132
+ shouldUseCaptureOnOutsideClick: shouldUseCaptureOnOutsideClick,
133
+ shouldRenderToParent: shouldRenderToParent,
134
+ shouldDisableFocusLock: shouldDisableFocusLock,
135
+ triggerRef: triggerRef,
136
+ strategy: strategy
137
+ }));
138
+ if (shouldRenderToParent) {
139
+ return popperWrapper;
140
+ }
141
+ return /*#__PURE__*/React.createElement(Portal, {
142
+ zIndex: zIndex
143
+ }, popperWrapper);
144
+ };
@@ -0,0 +1 @@
1
+ export { Popup, PopupContent, PopupTrigger } from '../../compositional/popup';
@@ -2,12 +2,12 @@
2
2
  /** @jsx jsx */
3
3
  import { memo, useState } from 'react';
4
4
  import { jsx } from '@emotion/react';
5
- import memoizeOne from 'memoize-one';
6
5
  import { UNSAFE_LAYERING } from '@atlaskit/layering';
7
6
  import { Manager, Reference } from '@atlaskit/popper';
8
7
  import Portal from '@atlaskit/portal';
9
8
  import { layers } from '@atlaskit/theme/constants';
10
9
  import PopperWrapper from './popper-wrapper';
10
+ import { useGetMemoizedMergedTriggerRef } from './use-get-memoized-merged-trigger-ref';
11
11
  const defaultLayer = layers.layer();
12
12
  export const Popup = /*#__PURE__*/memo(({
13
13
  isOpen,
@@ -31,29 +31,7 @@ export const Popup = /*#__PURE__*/memo(({
31
31
  strategy
32
32
  }) => {
33
33
  const [triggerRef, setTriggerRef] = useState(null);
34
-
35
- /*
36
- * Get a memoized functional ref for use within this Popup's Trigger.
37
- * This is still very volatile to change as `prop.isOpen` will regularly change, but it's better than nothing.
38
- * This is memoized within our component as to not be shared across all Popup instances, just this one.
39
- *
40
- * This is complex because the inputs are split across three different scopes:
41
- * - `props.isOpen`
42
- * - `useState.setTriggerRef`
43
- * - `renderProps.ref`
44
- */
45
- const [getMergedTriggerRef] = useState(() => memoizeOne((ref, setTriggerRef, isOpen) => {
46
- return node => {
47
- if (node && isOpen) {
48
- if (typeof ref === 'function') {
49
- ref(node);
50
- } else if (ref) {
51
- ref.current = node;
52
- }
53
- setTriggerRef(node);
54
- }
55
- };
56
- }));
34
+ const getMergedTriggerRef = useGetMemoizedMergedTriggerRef();
57
35
  const renderPopperWrapper = jsx(UNSAFE_LAYERING, {
58
36
  isDisabled: false
59
37
  }, jsx(PopperWrapper, {
@@ -0,0 +1,28 @@
1
+ import { useState } from 'react';
2
+ import memoizeOne from 'memoize-one';
3
+
4
+ /*
5
+ * Get a memoized functional ref for use within a Popup's Trigger.
6
+ * This is still very volatile to change as `prop.isOpen` will regularly change, but it's better than nothing.
7
+ * This is memoized within a component as to not be shared across all Popup instances.
8
+ *
9
+ * This is complex because the inputs are split across three different scopes:
10
+ * - `props.isOpen`
11
+ * - `useState.setTriggerRef`
12
+ * - `renderProps.ref`
13
+ */
14
+ export const useGetMemoizedMergedTriggerRef = () => {
15
+ const [getMemoizedMergedTriggerRef] = useState(() => memoizeOne((ref, setTriggerRef, isOpen) => {
16
+ return node => {
17
+ if (node && isOpen) {
18
+ if (typeof ref === 'function') {
19
+ ref(node);
20
+ } else if (ref) {
21
+ ref.current = node;
22
+ }
23
+ setTriggerRef(node);
24
+ }
25
+ };
26
+ }));
27
+ return getMemoizedMergedTriggerRef;
28
+ };
@@ -0,0 +1,155 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import React, { createContext, useContext, useState } from 'react';
3
+ import invariant from 'tiny-invariant';
4
+ import noop from '@atlaskit/ds-lib/noop';
5
+ import { UNSAFE_LAYERING } from '@atlaskit/layering';
6
+ import { Manager, Reference } from '@atlaskit/popper';
7
+ import Portal from '@atlaskit/portal';
8
+ import { layers } from '@atlaskit/theme/constants';
9
+ import PopperWrapper from '../popper-wrapper';
10
+ import { useGetMemoizedMergedTriggerRef } from '../use-get-memoized-merged-trigger-ref';
11
+ var IsOpenContext = /*#__PURE__*/createContext(false);
12
+ var IdContext = /*#__PURE__*/createContext(undefined);
13
+ var TriggerRefContext = /*#__PURE__*/createContext(null);
14
+ var SetTriggerRefContext = /*#__PURE__*/createContext(noop);
15
+ var EnsureIsInsidePopupContext = /*#__PURE__*/createContext(false);
16
+
17
+ // Used to ensure popup sub-components are used within a Popup
18
+ // and provide a useful error message if not.
19
+ var useEnsureIsInsidePopup = function useEnsureIsInsidePopup() {
20
+ var context = useContext(EnsureIsInsidePopupContext);
21
+ invariant(context, 'PopupTrigger and PopupContent components must be used within a Popup');
22
+ };
23
+ /**
24
+ * __Popup__
25
+ *
26
+ * Popup is a composable component that provides the context for the trigger and content components.
27
+ *
28
+ * Usage example:
29
+ * ```jsx
30
+ * <Popup>
31
+ * <PopupTrigger>
32
+ * {(props) => (
33
+ * <button type="button" {...props}>Click me</button>
34
+ * )}
35
+ * </PopupTrigger>
36
+ * <PopupContent>
37
+ * {(props) => <div>Hello world</div>}
38
+ * </PopupContent>
39
+ * </Popup>
40
+ * ```
41
+ */
42
+ export var Popup = function Popup(_ref) {
43
+ var children = _ref.children,
44
+ id = _ref.id,
45
+ _ref$isOpen = _ref.isOpen,
46
+ isOpen = _ref$isOpen === void 0 ? false : _ref$isOpen;
47
+ var _useState = useState(null),
48
+ _useState2 = _slicedToArray(_useState, 2),
49
+ triggerRef = _useState2[0],
50
+ setTriggerRef = _useState2[1];
51
+ return /*#__PURE__*/React.createElement(EnsureIsInsidePopupContext.Provider, {
52
+ value: true
53
+ }, /*#__PURE__*/React.createElement(IdContext.Provider, {
54
+ value: id
55
+ }, /*#__PURE__*/React.createElement(TriggerRefContext.Provider, {
56
+ value: triggerRef
57
+ }, /*#__PURE__*/React.createElement(SetTriggerRefContext.Provider, {
58
+ value: setTriggerRef
59
+ }, /*#__PURE__*/React.createElement(IsOpenContext.Provider, {
60
+ value: isOpen
61
+ }, /*#__PURE__*/React.createElement(Manager, null, children))))));
62
+ };
63
+ /**
64
+ * __Popup trigger__
65
+ *
66
+ * Popup trigger is the component that renders the trigger for the popup.
67
+ *
68
+ * It must be a child of the Popup component.
69
+ */
70
+ export var PopupTrigger = function PopupTrigger(_ref2) {
71
+ var children = _ref2.children;
72
+ useEnsureIsInsidePopup();
73
+ var isOpen = useContext(IsOpenContext);
74
+ var id = useContext(IdContext);
75
+ var setTriggerRef = useContext(SetTriggerRefContext);
76
+ var getMergedTriggerRef = useGetMemoizedMergedTriggerRef();
77
+ return /*#__PURE__*/React.createElement(Reference, null, function (_ref3) {
78
+ var ref = _ref3.ref;
79
+ return children({
80
+ ref: getMergedTriggerRef(ref, setTriggerRef, isOpen),
81
+ 'aria-controls': id,
82
+ 'aria-expanded': isOpen,
83
+ 'aria-haspopup': true
84
+ });
85
+ });
86
+ };
87
+ var defaultLayer = layers.layer();
88
+ /**
89
+ * __Popup content__
90
+ *
91
+ * Popup content is the component that renders the content of the popup.
92
+ *
93
+ * It must be a child of the Popup component.
94
+ */
95
+ export var PopupContent = function PopupContent(_ref4) {
96
+ var children = _ref4.children,
97
+ boundary = _ref4.boundary,
98
+ offset = _ref4.offset,
99
+ strategy = _ref4.strategy,
100
+ onClose = _ref4.onClose,
101
+ testId = _ref4.testId,
102
+ _ref4$rootBoundary = _ref4.rootBoundary,
103
+ rootBoundary = _ref4$rootBoundary === void 0 ? 'viewport' : _ref4$rootBoundary,
104
+ _ref4$shouldFlip = _ref4.shouldFlip,
105
+ shouldFlip = _ref4$shouldFlip === void 0 ? true : _ref4$shouldFlip,
106
+ _ref4$placement = _ref4.placement,
107
+ placement = _ref4$placement === void 0 ? 'auto' : _ref4$placement,
108
+ fallbackPlacements = _ref4.fallbackPlacements,
109
+ popupComponent = _ref4.popupComponent,
110
+ _ref4$autoFocus = _ref4.autoFocus,
111
+ autoFocus = _ref4$autoFocus === void 0 ? true : _ref4$autoFocus,
112
+ _ref4$zIndex = _ref4.zIndex,
113
+ zIndex = _ref4$zIndex === void 0 ? defaultLayer : _ref4$zIndex,
114
+ _ref4$shouldUseCaptur = _ref4.shouldUseCaptureOnOutsideClick,
115
+ shouldUseCaptureOnOutsideClick = _ref4$shouldUseCaptur === void 0 ? false : _ref4$shouldUseCaptur,
116
+ _ref4$shouldRenderToP = _ref4.shouldRenderToParent,
117
+ shouldRenderToParent = _ref4$shouldRenderToP === void 0 ? false : _ref4$shouldRenderToP,
118
+ _ref4$shouldDisableFo = _ref4.shouldDisableFocusLock,
119
+ shouldDisableFocusLock = _ref4$shouldDisableFo === void 0 ? false : _ref4$shouldDisableFo;
120
+ useEnsureIsInsidePopup();
121
+ var isOpen = useContext(IsOpenContext);
122
+ var id = useContext(IdContext);
123
+ var triggerRef = useContext(TriggerRefContext);
124
+ if (!isOpen) {
125
+ return null;
126
+ }
127
+ var popperWrapper = /*#__PURE__*/React.createElement(UNSAFE_LAYERING, {
128
+ isDisabled: false
129
+ }, /*#__PURE__*/React.createElement(PopperWrapper, {
130
+ content: children,
131
+ isOpen: isOpen,
132
+ placement: placement,
133
+ fallbackPlacements: fallbackPlacements,
134
+ boundary: boundary,
135
+ rootBoundary: rootBoundary,
136
+ shouldFlip: shouldFlip,
137
+ offset: offset,
138
+ popupComponent: popupComponent,
139
+ id: id,
140
+ testId: testId,
141
+ onClose: onClose,
142
+ autoFocus: autoFocus,
143
+ shouldUseCaptureOnOutsideClick: shouldUseCaptureOnOutsideClick,
144
+ shouldRenderToParent: shouldRenderToParent,
145
+ shouldDisableFocusLock: shouldDisableFocusLock,
146
+ triggerRef: triggerRef,
147
+ strategy: strategy
148
+ }));
149
+ if (shouldRenderToParent) {
150
+ return popperWrapper;
151
+ }
152
+ return /*#__PURE__*/React.createElement(Portal, {
153
+ zIndex: zIndex
154
+ }, popperWrapper);
155
+ };
@@ -0,0 +1 @@
1
+ export { Popup, PopupContent, PopupTrigger } from '../../compositional/popup';