@fluentui/react-positioning 9.0.0-nightly.f81b28ceb3.1 → 9.0.0-rc.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. package/CHANGELOG.json +235 -10
  2. package/CHANGELOG.md +72 -7
  3. package/dist/react-positioning.d.ts +113 -35
  4. package/lib/createArrowStyles.d.ts +64 -0
  5. package/lib/createArrowStyles.js +87 -0
  6. package/lib/createArrowStyles.js.map +1 -0
  7. package/lib/createVirtualElementFromClick.js +17 -15
  8. package/lib/createVirtualElementFromClick.js.map +1 -1
  9. package/lib/index.d.ts +2 -1
  10. package/lib/index.js +2 -1
  11. package/lib/index.js.map +1 -1
  12. package/lib/isIntersectingModifier.d.ts +4 -0
  13. package/lib/isIntersectingModifier.js +26 -0
  14. package/lib/isIntersectingModifier.js.map +1 -0
  15. package/lib/types.d.ts +28 -7
  16. package/lib/types.js.map +1 -1
  17. package/lib/usePopper.d.ts +2 -14
  18. package/lib/usePopper.js +369 -319
  19. package/lib/usePopper.js.map +1 -1
  20. package/lib/usePopperMouseTarget.js +28 -22
  21. package/lib/usePopperMouseTarget.js.map +1 -1
  22. package/lib/utils/getBasePlacement.js +1 -1
  23. package/lib/utils/getBasePlacement.js.map +1 -1
  24. package/lib/utils/getBoundary.js +15 -10
  25. package/lib/utils/getBoundary.js.map +1 -1
  26. package/lib/utils/getReactFiberFromNode.d.ts +0 -1
  27. package/lib/utils/getReactFiberFromNode.js +39 -35
  28. package/lib/utils/getReactFiberFromNode.js.map +1 -1
  29. package/lib/utils/getScrollParent.js +46 -32
  30. package/lib/utils/getScrollParent.js.map +1 -1
  31. package/lib/utils/index.js.map +1 -1
  32. package/lib/utils/mergeArrowOffset.js +30 -25
  33. package/lib/utils/mergeArrowOffset.js.map +1 -1
  34. package/lib/utils/positioningHelper.d.ts +2 -2
  35. package/lib/utils/positioningHelper.js +42 -34
  36. package/lib/utils/positioningHelper.js.map +1 -1
  37. package/lib/utils/resolvePositioningShorthand.js +58 -20
  38. package/lib/utils/resolvePositioningShorthand.js.map +1 -1
  39. package/lib/utils/useCallbackRef.js +35 -29
  40. package/lib/utils/useCallbackRef.js.map +1 -1
  41. package/lib-commonjs/createArrowStyles.d.ts +64 -0
  42. package/lib-commonjs/createArrowStyles.js +100 -0
  43. package/lib-commonjs/createArrowStyles.js.map +1 -0
  44. package/lib-commonjs/createVirtualElementFromClick.js +23 -16
  45. package/lib-commonjs/createVirtualElementFromClick.js.map +1 -1
  46. package/lib-commonjs/index.d.ts +2 -1
  47. package/lib-commonjs/index.js +28 -6
  48. package/lib-commonjs/index.js.map +1 -1
  49. package/lib-commonjs/isIntersectingModifier.d.ts +4 -0
  50. package/lib-commonjs/isIntersectingModifier.js +34 -0
  51. package/lib-commonjs/isIntersectingModifier.js.map +1 -0
  52. package/lib-commonjs/types.d.ts +28 -7
  53. package/lib-commonjs/types.js +4 -1
  54. package/lib-commonjs/types.js.map +1 -1
  55. package/lib-commonjs/usePopper.d.ts +2 -14
  56. package/lib-commonjs/usePopper.js +385 -324
  57. package/lib-commonjs/usePopper.js.map +1 -1
  58. package/lib-commonjs/usePopperMouseTarget.js +38 -25
  59. package/lib-commonjs/usePopperMouseTarget.js.map +1 -1
  60. package/lib-commonjs/utils/getBasePlacement.js +7 -2
  61. package/lib-commonjs/utils/getBasePlacement.js.map +1 -1
  62. package/lib-commonjs/utils/getBoundary.js +23 -12
  63. package/lib-commonjs/utils/getBoundary.js.map +1 -1
  64. package/lib-commonjs/utils/getReactFiberFromNode.d.ts +0 -1
  65. package/lib-commonjs/utils/getReactFiberFromNode.js +44 -36
  66. package/lib-commonjs/utils/getReactFiberFromNode.js.map +1 -1
  67. package/lib-commonjs/utils/getScrollParent.js +53 -33
  68. package/lib-commonjs/utils/getScrollParent.js.map +1 -1
  69. package/lib-commonjs/utils/index.js +14 -2
  70. package/lib-commonjs/utils/index.js.map +1 -1
  71. package/lib-commonjs/utils/mergeArrowOffset.js +36 -26
  72. package/lib-commonjs/utils/mergeArrowOffset.js.map +1 -1
  73. package/lib-commonjs/utils/positioningHelper.d.ts +2 -2
  74. package/lib-commonjs/utils/positioningHelper.js +50 -35
  75. package/lib-commonjs/utils/positioningHelper.js.map +1 -1
  76. package/lib-commonjs/utils/resolvePositioningShorthand.js +66 -23
  77. package/lib-commonjs/utils/resolvePositioningShorthand.js.map +1 -1
  78. package/lib-commonjs/utils/useCallbackRef.js +45 -32
  79. package/lib-commonjs/utils/useCallbackRef.js.map +1 -1
  80. package/package.json +8 -8
@@ -1,32 +1,45 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
3
6
  exports.usePopper = void 0;
4
- var react_utilities_1 = require("@fluentui/react-utilities");
5
- var react_shared_contexts_1 = require("@fluentui/react-shared-contexts");
6
- var index_1 = require("./utils/index");
7
- var PopperJs = require("@popperjs/core");
8
- var React = require("react");
9
- //
7
+
8
+ const react_utilities_1 = /*#__PURE__*/require("@fluentui/react-utilities");
9
+
10
+ const react_shared_contexts_1 = /*#__PURE__*/require("@fluentui/react-shared-contexts");
11
+
12
+ const PopperJs = /*#__PURE__*/require("@popperjs/core");
13
+
14
+ const React = /*#__PURE__*/require("react");
15
+
16
+ const isIntersectingModifier_1 = /*#__PURE__*/require("./isIntersectingModifier");
17
+
18
+ const index_1 = /*#__PURE__*/require("./utils/index"); //
10
19
  // Dev utils to detect if nodes have "autoFocus" props.
11
20
  //
21
+
12
22
  /**
13
23
  * Detects if a passed HTML node has "autoFocus" prop on a React's fiber. Is needed as React handles autofocus behavior
14
24
  * in React DOM and will not pass "autoFocus" to an actual HTML.
15
25
  */
26
+
27
+
16
28
  function hasAutofocusProp(node) {
17
- var _a;
18
- // https://github.com/facebook/react/blob/848bb2426e44606e0a55dfe44c7b3ece33772485/packages/react-dom/src/client/ReactDOMHostConfig.js#L157-L166
19
- var isAutoFocusableElement = node.nodeName === 'BUTTON' ||
20
- node.nodeName === 'INPUT' ||
21
- node.nodeName === 'SELECT' ||
22
- node.nodeName === 'TEXTAREA';
23
- if (isAutoFocusableElement) {
24
- return !!((_a = index_1.getReactFiberFromNode(node)) === null || _a === void 0 ? void 0 : _a.pendingProps.autoFocus);
25
- }
26
- return false;
29
+ var _a; // https://github.com/facebook/react/blob/848bb2426e44606e0a55dfe44c7b3ece33772485/packages/react-dom/src/client/ReactDOMHostConfig.js#L157-L166
30
+
31
+
32
+ const isAutoFocusableElement = node.nodeName === 'BUTTON' || node.nodeName === 'INPUT' || node.nodeName === 'SELECT' || node.nodeName === 'TEXTAREA';
33
+
34
+ if (isAutoFocusableElement) {
35
+ return !!((_a = index_1.getReactFiberFromNode(node)) === null || _a === void 0 ? void 0 : _a.pendingProps.autoFocus);
36
+ }
37
+
38
+ return false;
27
39
  }
40
+
28
41
  function hasAutofocusFilter(node) {
29
- return hasAutofocusProp(node) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
42
+ return hasAutofocusProp(node) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
30
43
  }
31
44
  /**
32
45
  * Provides a callback to resolve Popper options, it's stable and should be used as a dependency to trigger updates
@@ -35,194 +48,217 @@ function hasAutofocusFilter(node) {
35
48
  * A callback is used there intentionally as some of Popper.js modifiers require DOM nodes (targer, container, arrow)
36
49
  * that can't be resolved properly during an initial rendering.
37
50
  */
51
+
52
+
38
53
  function usePopperOptions(options, popperOriginalPositionRef) {
39
- var arrowPadding = options.arrowPadding, autoSize = options.autoSize, coverTarget = options.coverTarget, flipBoundary = options.flipBoundary, offset = options.offset, onStateUpdate = options.onStateUpdate, overflowBoundary = options.overflowBoundary,
54
+ const {
55
+ align,
56
+ arrowPadding,
57
+ autoSize,
58
+ coverTarget,
59
+ flipBoundary,
60
+ offset,
61
+ overflowBoundary,
62
+ pinned,
63
+ position,
64
+ positionFixed,
40
65
  // eslint-disable-next-line @typescript-eslint/naming-convention
41
- unstable_disableTether = options.unstable_disableTether, pinned = options.pinned;
42
- var isRtl = react_shared_contexts_1.useFluent().dir === 'rtl';
43
- var placement = index_1.getPlacement(options.align, options.position, isRtl);
44
- var strategy = options.positionFixed ? 'fixed' : 'absolute';
45
- var handleStateUpdate = react_utilities_1.useEventCallback(function (_a) {
46
- var state = _a.state;
47
- if (onStateUpdate) {
48
- onStateUpdate(state);
66
+ unstable_disableTether
67
+ } = options;
68
+ const isRtl = react_shared_contexts_1.useFluent().dir === 'rtl';
69
+ const placement = index_1.getPlacement(align, position, isRtl);
70
+ const strategy = positionFixed ? 'fixed' : 'absolute';
71
+ const offsetModifier = React.useMemo(() => offset ? {
72
+ name: 'offset',
73
+ options: {
74
+ offset: isRtl ? index_1.applyRtlToOffset(offset) : offset
75
+ }
76
+ } : null, [offset, isRtl]);
77
+ return React.useCallback((target, container, arrow) => {
78
+ var _a;
79
+
80
+ const scrollParentElement = index_1.getScrollParent(container);
81
+ const hasScrollableElement = scrollParentElement ? scrollParentElement !== ((_a = scrollParentElement.ownerDocument) === null || _a === void 0 ? void 0 : _a.body) : false;
82
+ const modifiers = [isIntersectingModifier_1.isIntersectingModifier,
83
+ /**
84
+ * We are setting the position to `fixed` in the first effect to prevent scroll jumps in case of the content
85
+ * with managed focus. Modifier sets the position to `fixed` before all other modifier effects. Another part of
86
+ * a patch modifies ".forceUpdate()" directly after a Popper will be created.
87
+ */
88
+ {
89
+ name: 'positionStyleFix',
90
+ enabled: true,
91
+ phase: 'afterWrite',
92
+ effect: ({
93
+ state,
94
+ instance
95
+ }) => {
96
+ // ".isFirstRun" is a part of our patch, on a first evaluation it will "undefined"
97
+ // should be disabled for subsequent runs as it breaks positioning for them
98
+ if (instance.isFirstRun !== false) {
99
+ popperOriginalPositionRef.current = state.elements.popper.style.position;
100
+ state.elements.popper.style.position = 'fixed';
49
101
  }
50
- });
51
- var offsetModifier = React.useMemo(function () {
52
- return offset
53
- ? {
54
- name: 'offset',
55
- options: { offset: isRtl ? index_1.applyRtlToOffset(offset) : offset },
56
- }
57
- : null;
58
- }, [offset, isRtl]);
59
- return React.useCallback(function (target, container, arrow) {
60
- var _a;
61
- var scrollParentElement = index_1.getScrollParent(container);
62
- var hasScrollableElement = scrollParentElement
63
- ? scrollParentElement !== ((_a = scrollParentElement.ownerDocument) === null || _a === void 0 ? void 0 : _a.body)
64
- : false;
65
- var modifiers = [
66
- /**
67
- * We are setting the position to `fixed` in the first effect to prevent scroll jumps in case of the content
68
- * with managed focus. Modifier sets the position to `fixed` before all other modifier effects. Another part of
69
- * a patch modifies ".forceUpdate()" directly after a Popper will be created.
70
- */
71
- {
72
- name: 'positionStyleFix',
73
- enabled: true,
74
- phase: 'afterWrite',
75
- effect: function (_a) {
76
- var state = _a.state, instance = _a.instance;
77
- // ".isFirstRun" is a part of our patch, on a first evaluation it will "undefined"
78
- // should be disabled for subsequent runs as it breaks positioning for them
79
- if (instance.isFirstRun !== false) {
80
- popperOriginalPositionRef.current = state.elements.popper.style.position;
81
- state.elements.popper.style.position = 'fixed';
82
- }
83
- return function () { return undefined; };
84
- },
85
- requires: [],
86
- },
87
- { name: 'flip', options: { flipVariations: true } },
88
- /**
89
- * pinned disables the flip modifier by setting flip.enabled to false; this
90
- * disables automatic repositioning of the popper box; it will always be placed according to
91
- * the values of `align` and `position` props, regardless of the size of the component, the
92
- * reference element or the viewport.
93
- */
94
- pinned && { name: 'flip', enabled: false },
95
- /**
96
- * When the popper box is placed in the context of a scrollable element, we need to set
97
- * preventOverflow.escapeWithReference to true and flip.boundariesElement to 'scrollParent'
98
- * (default is 'viewport') so that the popper box will stick with the targetRef when we
99
- * scroll targetRef out of the viewport.
100
- */
101
- hasScrollableElement && { name: 'flip', options: { boundary: 'clippingParents' } },
102
- hasScrollableElement && { name: 'preventOverflow', options: { boundary: 'clippingParents' } },
103
- offsetModifier,
104
- /**
105
- * This modifier is necessary to retain behaviour from popper v1 where untethered poppers are allowed by
106
- * default. i.e. popper is still rendered fully in the viewport even if anchor element is no longer in the
107
- * viewport.
108
- */
109
- unstable_disableTether && {
110
- name: 'preventOverflow',
111
- options: { altAxis: unstable_disableTether === 'all', tether: false },
112
- },
113
- flipBoundary && {
114
- name: 'flip',
115
- options: {
116
- altBoundary: true,
117
- boundary: index_1.getBoundary(container, flipBoundary),
118
- },
119
- },
120
- overflowBoundary && {
121
- name: 'preventOverflow',
122
- options: {
123
- altBoundary: true,
124
- boundary: index_1.getBoundary(container, overflowBoundary),
125
- },
126
- },
127
- {
128
- name: 'onUpdate',
129
- enabled: true,
130
- phase: 'afterWrite',
131
- fn: handleStateUpdate,
132
- },
133
- {
134
- // Similar code as popper-maxsize-modifier: https://github.com/atomiks/popper.js/blob/master/src/modifiers/maxSize.js
135
- // popper-maxsize-modifier only calculates the max sizes.
136
- // This modifier can apply max sizes always, or apply the max sizes only when overflow is detected
137
- name: 'applyMaxSize',
138
- enabled: !!autoSize,
139
- phase: 'beforeWrite',
140
- requiresIfExists: ['offset', 'preventOverflow', 'flip'],
141
- options: {
142
- altBoundary: true,
143
- boundary: index_1.getBoundary(container, overflowBoundary),
144
- },
145
- fn: function (_a) {
146
- var state = _a.state, modifierOptions = _a.options;
147
- var overflow = PopperJs.detectOverflow(state, modifierOptions);
148
- var _b = state.modifiersData.preventOverflow || { x: 0, y: 0 }, x = _b.x, y = _b.y;
149
- var _c = state.rects.popper, width = _c.width, height = _c.height;
150
- var basePlacement = index_1.getBasePlacement(state.placement);
151
- var widthProp = basePlacement === 'left' ? 'left' : 'right';
152
- var heightProp = basePlacement === 'top' ? 'top' : 'bottom';
153
- var applyMaxWidth = autoSize === 'always' ||
154
- autoSize === 'width-always' ||
155
- (overflow[widthProp] > 0 && (autoSize === true || autoSize === 'width'));
156
- var applyMaxHeight = autoSize === 'always' ||
157
- autoSize === 'height-always' ||
158
- (overflow[heightProp] > 0 && (autoSize === true || autoSize === 'height'));
159
- if (applyMaxWidth) {
160
- state.styles.popper.maxWidth = width - overflow[widthProp] - x + "px";
161
- }
162
- if (applyMaxHeight) {
163
- state.styles.popper.maxHeight = height - overflow[heightProp] - y + "px";
164
- }
165
- },
166
- },
167
- /**
168
- * This modifier is necessary in order to render the pointer. Refs are resolved in effects, so it can't be
169
- * placed under computed modifiers. Deep merge is not required as this modifier has only these properties.
170
- */
171
- {
172
- name: 'arrow',
173
- enabled: !!arrow,
174
- options: { element: arrow, padding: arrowPadding },
175
- },
176
- /**
177
- * Modifies popper offsets to cover the reference rect, but still keep edge alignment
178
- */
179
- {
180
- name: 'coverTarget',
181
- enabled: !!coverTarget,
182
- phase: 'main',
183
- requiresIfExists: ['offset', 'preventOverflow', 'flip'],
184
- fn: function (_a) {
185
- var state = _a.state;
186
- var basePlacement = index_1.getBasePlacement(state.placement);
187
- switch (basePlacement) {
188
- case 'bottom':
189
- state.modifiersData.popperOffsets.y -= state.rects.reference.height;
190
- break;
191
- case 'top':
192
- state.modifiersData.popperOffsets.y += state.rects.reference.height;
193
- break;
194
- case 'left':
195
- state.modifiersData.popperOffsets.x += state.rects.reference.width;
196
- break;
197
- case 'right':
198
- state.modifiersData.popperOffsets.x -= state.rects.reference.width;
199
- break;
200
- }
201
- },
202
- },
203
- ].filter(Boolean); // filter boolean conditional spreading values
204
- var popperOptions = {
205
- modifiers: modifiers,
206
- placement: placement,
207
- strategy: strategy,
208
- onFirstUpdate: function (state) { return handleStateUpdate({ state: state }); },
102
+
103
+ return () => undefined;
104
+ },
105
+ requires: []
106
+ }, {
107
+ name: 'flip',
108
+ options: {
109
+ flipVariations: true
110
+ }
111
+ },
112
+ /**
113
+ * pinned disables the flip modifier by setting flip.enabled to false; this
114
+ * disables automatic repositioning of the popper box; it will always be placed according to
115
+ * the values of `align` and `position` props, regardless of the size of the component, the
116
+ * reference element or the viewport.
117
+ */
118
+ pinned && {
119
+ name: 'flip',
120
+ enabled: false
121
+ },
122
+ /**
123
+ * When the popper box is placed in the context of a scrollable element, we need to set
124
+ * preventOverflow.escapeWithReference to true and flip.boundariesElement to 'scrollParent'
125
+ * (default is 'viewport') so that the popper box will stick with the targetRef when we
126
+ * scroll targetRef out of the viewport.
127
+ */
128
+ hasScrollableElement && {
129
+ name: 'flip',
130
+ options: {
131
+ boundary: 'clippingParents'
132
+ }
133
+ }, hasScrollableElement && {
134
+ name: 'preventOverflow',
135
+ options: {
136
+ boundary: 'clippingParents'
137
+ }
138
+ }, offsetModifier,
139
+ /**
140
+ * This modifier is necessary to retain behaviour from popper v1 where untethered poppers are allowed by
141
+ * default. i.e. popper is still rendered fully in the viewport even if anchor element is no longer in the
142
+ * viewport.
143
+ */
144
+ unstable_disableTether && {
145
+ name: 'preventOverflow',
146
+ options: {
147
+ altAxis: unstable_disableTether === 'all',
148
+ tether: false
149
+ }
150
+ }, flipBoundary && {
151
+ name: 'flip',
152
+ options: {
153
+ altBoundary: true,
154
+ boundary: index_1.getBoundary(container, flipBoundary)
155
+ }
156
+ }, overflowBoundary && {
157
+ name: 'preventOverflow',
158
+ options: {
159
+ altBoundary: true,
160
+ boundary: index_1.getBoundary(container, overflowBoundary)
161
+ }
162
+ }, {
163
+ // Similar code as popper-maxsize-modifier: https://github.com/atomiks/popper.js/blob/master/src/modifiers/maxSize.js
164
+ // popper-maxsize-modifier only calculates the max sizes.
165
+ // This modifier can apply max sizes always, or apply the max sizes only when overflow is detected
166
+ name: 'applyMaxSize',
167
+ enabled: !!autoSize,
168
+ phase: 'beforeWrite',
169
+ requiresIfExists: ['offset', 'preventOverflow', 'flip'],
170
+ options: {
171
+ altBoundary: true,
172
+ boundary: index_1.getBoundary(container, overflowBoundary)
173
+ },
174
+
175
+ fn({
176
+ state,
177
+ options: modifierOptions
178
+ }) {
179
+ const overflow = PopperJs.detectOverflow(state, modifierOptions);
180
+ const {
181
+ x,
182
+ y
183
+ } = state.modifiersData.preventOverflow || {
184
+ x: 0,
185
+ y: 0
209
186
  };
210
- return popperOptions;
211
- }, [
212
- arrowPadding,
213
- autoSize,
214
- coverTarget,
215
- flipBoundary,
216
- offsetModifier,
217
- overflowBoundary,
218
- placement,
219
- strategy,
220
- unstable_disableTether,
221
- pinned,
222
- // These can be skipped from deps as they will not ever change
223
- handleStateUpdate,
224
- popperOriginalPositionRef,
225
- ]);
187
+ const {
188
+ width,
189
+ height
190
+ } = state.rects.popper;
191
+ const basePlacement = index_1.getBasePlacement(state.placement);
192
+ const widthProp = basePlacement === 'left' ? 'left' : 'right';
193
+ const heightProp = basePlacement === 'top' ? 'top' : 'bottom';
194
+ const applyMaxWidth = autoSize === 'always' || autoSize === 'width-always' || overflow[widthProp] > 0 && (autoSize === true || autoSize === 'width');
195
+ const applyMaxHeight = autoSize === 'always' || autoSize === 'height-always' || overflow[heightProp] > 0 && (autoSize === true || autoSize === 'height');
196
+
197
+ if (applyMaxWidth) {
198
+ state.styles.popper.maxWidth = `${width - overflow[widthProp] - x}px`;
199
+ }
200
+
201
+ if (applyMaxHeight) {
202
+ state.styles.popper.maxHeight = `${height - overflow[heightProp] - y}px`;
203
+ }
204
+ }
205
+
206
+ },
207
+ /**
208
+ * This modifier is necessary in order to render the pointer. Refs are resolved in effects, so it can't be
209
+ * placed under computed modifiers. Deep merge is not required as this modifier has only these properties.
210
+ */
211
+ {
212
+ name: 'arrow',
213
+ enabled: !!arrow,
214
+ options: {
215
+ element: arrow,
216
+ padding: arrowPadding
217
+ }
218
+ },
219
+ /**
220
+ * Modifies popper offsets to cover the reference rect, but still keep edge alignment
221
+ */
222
+ {
223
+ name: 'coverTarget',
224
+ enabled: !!coverTarget,
225
+ phase: 'main',
226
+ requiresIfExists: ['offset', 'preventOverflow', 'flip'],
227
+
228
+ fn({
229
+ state
230
+ }) {
231
+ const basePlacement = index_1.getBasePlacement(state.placement);
232
+
233
+ switch (basePlacement) {
234
+ case 'bottom':
235
+ state.modifiersData.popperOffsets.y -= state.rects.reference.height;
236
+ break;
237
+
238
+ case 'top':
239
+ state.modifiersData.popperOffsets.y += state.rects.reference.height;
240
+ break;
241
+
242
+ case 'left':
243
+ state.modifiersData.popperOffsets.x += state.rects.reference.width;
244
+ break;
245
+
246
+ case 'right':
247
+ state.modifiersData.popperOffsets.x -= state.rects.reference.width;
248
+ break;
249
+ }
250
+ }
251
+
252
+ }].filter(Boolean); // filter boolean conditional spreading values
253
+
254
+ const popperOptions = {
255
+ modifiers,
256
+ placement,
257
+ strategy
258
+ };
259
+ return popperOptions;
260
+ }, [arrowPadding, autoSize, coverTarget, flipBoundary, offsetModifier, overflowBoundary, placement, strategy, unstable_disableTether, pinned, // These can be skipped from deps as they will not ever change
261
+ popperOriginalPositionRef]);
226
262
  }
227
263
  /**
228
264
  * Exposes Popper positioning API via React hook. Contains few important differences between an official "react-popper"
@@ -232,130 +268,155 @@ function usePopperOptions(options, popperOriginalPositionRef) {
232
268
  * - contains a specific to React fix related to initial positioning when containers have components with managed focus
233
269
  * to avoid focus jumps
234
270
  */
235
- function usePopper(options) {
236
- if (options === void 0) { options = {}; }
237
- var _a = options.enabled, enabled = _a === void 0 ? true : _a;
238
- var isFirstMount = react_utilities_1.useFirstMount();
239
- var popperOriginalPositionRef = React.useRef('absolute');
240
- var resolvePopperOptions = usePopperOptions(options, popperOriginalPositionRef);
241
- var popperInstanceRef = React.useRef(null);
242
- var handlePopperUpdate = react_utilities_1.useEventCallback(function () {
243
- var _a;
244
- (_a = popperInstanceRef.current) === null || _a === void 0 ? void 0 : _a.destroy();
245
- popperInstanceRef.current = null;
246
- var _b = options.target, target = _b === void 0 ? targetRef.current : _b;
247
- var popperInstance = null;
248
- if (react_utilities_1.canUseDOM() && enabled) {
249
- if (target && containerRef.current) {
250
- popperInstance = PopperJs.createPopper(target, containerRef.current, resolvePopperOptions(target, containerRef.current, arrowRef.current));
251
- }
252
- }
253
- if (popperInstance) {
254
- /**
255
- * The patch updates `.forceUpdate()` Popper function which restores the original position before the first
256
- * forceUpdate() call. See also "positionStyleFix" modifier in usePopperOptions().
257
- */
258
- var originalForceUpdate_1 = popperInstance.forceUpdate;
259
- popperInstance.isFirstRun = true;
260
- popperInstance.forceUpdate = function () {
261
- if (popperInstance === null || popperInstance === void 0 ? void 0 : popperInstance.isFirstRun) {
262
- popperInstance.state.elements.popper.style.position = popperOriginalPositionRef.current;
263
- popperInstance.isFirstRun = false;
264
- }
265
- originalForceUpdate_1();
266
- };
267
- }
268
- popperInstanceRef.current = popperInstance;
269
- });
270
- // Refs are managed by useCallbackRef() to handle ref updates scenarios.
271
- //
272
- // The first scenario are updates for a targetRef, we can handle it properly only via callback refs, but it causes
273
- // re-renders (we would like to avoid them).
274
- //
275
- // The second problem is related to refs resolution on React's layer: refs are resolved in the same order as effects
276
- // that causes an issue when you have a container inside a target, for example: a menu in ChatMessage.
277
- //
278
- // function ChatMessage(props) {
279
- // <div className="message" ref={targetRef}> // 3) ref will be resolved only now, but it's too late already
280
- // <Popper target={targetRef}> // 2) create a popper instance
281
- // <div className="menu" /> // 1) capture ref from this element
282
- // </Popper>
283
- // </div>
284
- // }
285
- //
286
- // Check a demo on CodeSandbox: https://codesandbox.io/s/popper-refs-emy60.
287
- //
288
- // This again can be solved with callback refs. It's not a huge issue as with hooks we are moving popper's creation
289
- // to ChatMessage itself, however, without `useCallback()` refs it's still a Pandora box.
290
- var targetRef = index_1.useCallbackRef(null, handlePopperUpdate, true);
291
- var containerRef = index_1.useCallbackRef(null, handlePopperUpdate, true);
292
- var arrowRef = index_1.useCallbackRef(null, handlePopperUpdate, true);
293
- React.useImperativeHandle(options.popperRef, function () { return ({
294
- updatePosition: function () {
295
- var _a;
296
- (_a = popperInstanceRef.current) === null || _a === void 0 ? void 0 : _a.update();
297
- },
298
- }); }, []);
299
- react_utilities_1.useIsomorphicLayoutEffect(function () {
300
- handlePopperUpdate();
301
- return function () {
302
- var _a;
303
- (_a = popperInstanceRef.current) === null || _a === void 0 ? void 0 : _a.destroy();
304
- popperInstanceRef.current = null;
305
- };
306
- }, [handlePopperUpdate, options.enabled, options.target]);
307
- react_utilities_1.useIsomorphicLayoutEffect(function () {
308
- var _a;
309
- if (!isFirstMount) {
310
- (_a = popperInstanceRef.current) === null || _a === void 0 ? void 0 : _a.setOptions(resolvePopperOptions(options.target || targetRef.current, containerRef.current, arrowRef.current));
271
+
272
+
273
+ function usePopper(options = {}) {
274
+ const {
275
+ enabled = true
276
+ } = options;
277
+ const isFirstMount = react_utilities_1.useFirstMount();
278
+ const popperOriginalPositionRef = React.useRef('absolute');
279
+ const resolvePopperOptions = usePopperOptions(options, popperOriginalPositionRef);
280
+ const popperInstanceRef = React.useRef(null);
281
+ const handlePopperUpdate = react_utilities_1.useEventCallback(() => {
282
+ var _a, _b;
283
+
284
+ (_a = popperInstanceRef.current) === null || _a === void 0 ? void 0 : _a.destroy();
285
+ popperInstanceRef.current = null;
286
+ const target = (_b = overrideTargetRef.current) !== null && _b !== void 0 ? _b : targetRef.current;
287
+ let popperInstance = null;
288
+
289
+ if (react_utilities_1.canUseDOM() && enabled) {
290
+ if (target && containerRef.current) {
291
+ popperInstance = PopperJs.createPopper(target, containerRef.current, resolvePopperOptions(target, containerRef.current, arrowRef.current));
292
+ }
293
+ }
294
+
295
+ if (popperInstance) {
296
+ /**
297
+ * The patch updates `.forceUpdate()` Popper function which restores the original position before the first
298
+ * forceUpdate() call. See also "positionStyleFix" modifier in usePopperOptions().
299
+ */
300
+ const originalForceUpdate = popperInstance.forceUpdate;
301
+ popperInstance.isFirstRun = true;
302
+
303
+ popperInstance.forceUpdate = () => {
304
+ if (popperInstance === null || popperInstance === void 0 ? void 0 : popperInstance.isFirstRun) {
305
+ popperInstance.state.elements.popper.style.position = popperOriginalPositionRef.current;
306
+ popperInstance.isFirstRun = false;
311
307
  }
312
- },
313
- // Missing deps:
314
- // options.target - The useIsomorphicLayoutEffect before this will create a new popper instance if target changes
315
- // isFirstMount - Should never change after mount
316
- // arrowRef, containerRef, targetRef - Stable between renders
317
- // eslint-disable-next-line react-hooks/exhaustive-deps
318
- [resolvePopperOptions]);
319
- if (process.env.NODE_ENV !== 'production') {
320
- // This checked should run only in development mode
321
- // eslint-disable-next-line react-hooks/rules-of-hooks
322
- React.useEffect(function () {
323
- var _a;
324
- if (containerRef.current) {
325
- var contentNode = containerRef.current;
326
- var treeWalker = (_a = contentNode.ownerDocument) === null || _a === void 0 ? void 0 : _a.createTreeWalker(contentNode, NodeFilter.SHOW_ELEMENT, {
327
- acceptNode: hasAutofocusFilter,
328
- });
329
- while (treeWalker === null || treeWalker === void 0 ? void 0 : treeWalker.nextNode()) {
330
- var node = treeWalker.currentNode;
331
- // eslint-disable-next-line no-console
332
- console.warn('<Popper>:', node);
333
- // eslint-disable-next-line no-console
334
- console.warn([
335
- '<Popper>: ^ this node contains "autoFocus" prop on a React element. This can break the initial',
336
- 'positioning of an element and cause a window jump effect. This issue occurs because React polyfills',
337
- '"autoFocus" behavior to solve inconsistencies between different browsers:',
338
- 'https://github.com/facebook/react/issues/11851#issuecomment-351787078',
339
- '\n',
340
- 'However, ".focus()" in this case occurs before any other React effects will be executed',
341
- '(React.useEffect(), componentDidMount(), etc.) and we can not prevent this behavior. If you really',
342
- 'want to use "autoFocus" please add "position: fixed" to styles of the element that is wrapped by',
343
- '"Popper".',
344
- "In general, it's not recommended to use \"autoFocus\" as it may break accessibility aspects:",
345
- 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-autofocus.md',
346
- '\n',
347
- 'We suggest to use the "trapFocus" prop on Fluent components or a catch "ref" and then use',
348
- '"ref.current.focus" in React.useEffect():',
349
- 'https://reactjs.org/docs/refs-and-the-dom.html#adding-a-ref-to-a-dom-element',
350
- ].join(' '));
351
- }
352
- }
353
- // We run this check once, no need to add deps here
354
- // TODO: Should be rework to handle options.enabled and contentRef updates
355
- // eslint-disable-next-line react-hooks/exhaustive-deps
356
- }, []);
308
+
309
+ originalForceUpdate();
310
+ };
357
311
  }
358
- return { targetRef: targetRef, containerRef: containerRef, arrowRef: arrowRef };
312
+
313
+ popperInstanceRef.current = popperInstance;
314
+ }); // Refs are managed by useCallbackRef() to handle ref updates scenarios.
315
+ //
316
+ // The first scenario are updates for a targetRef, we can handle it properly only via callback refs, but it causes
317
+ // re-renders (we would like to avoid them).
318
+ //
319
+ // The second problem is related to refs resolution on React's layer: refs are resolved in the same order as effects
320
+ // that causes an issue when you have a container inside a target, for example: a menu in ChatMessage.
321
+ //
322
+ // function ChatMessage(props) {
323
+ // <div className="message" ref={targetRef}> // 3) ref will be resolved only now, but it's too late already
324
+ // <Popper target={targetRef}> // 2) create a popper instance
325
+ // <div className="menu" /> // 1) capture ref from this element
326
+ // </Popper>
327
+ // </div>
328
+ // }
329
+ //
330
+ // Check a demo on CodeSandbox: https://codesandbox.io/s/popper-refs-emy60.
331
+ //
332
+ // This again can be solved with callback refs. It's not a huge issue as with hooks we are moving popper's creation
333
+ // to ChatMessage itself, however, without `useCallback()` refs it's still a Pandora box.
334
+
335
+ const targetRef = index_1.useCallbackRef(null, handlePopperUpdate, true);
336
+ const containerRef = index_1.useCallbackRef(null, handlePopperUpdate, true);
337
+ const arrowRef = index_1.useCallbackRef(null, handlePopperUpdate, true); // Stores external target from options.target or setTarget
338
+
339
+ const overrideTargetRef = index_1.useCallbackRef(null, handlePopperUpdate, true);
340
+ React.useImperativeHandle(options.popperRef, () => ({
341
+ updatePosition: () => {
342
+ var _a;
343
+
344
+ (_a = popperInstanceRef.current) === null || _a === void 0 ? void 0 : _a.update();
345
+ },
346
+ setTarget: target => {
347
+ if (options.target && process.env.NODE_ENV !== 'production') {
348
+ const err = new Error(); // eslint-disable-next-line no-console
349
+
350
+ console.warn('Imperative setTarget should not be used at the same time as target option'); // eslint-disable-next-line no-console
351
+
352
+ console.warn(err.stack);
353
+ }
354
+
355
+ overrideTargetRef.current = target;
356
+ }
357
+ }), // Missing deps:
358
+ // options.target - only used for a runtime warning
359
+ // targetRef - Stable between renders
360
+ // eslint-disable-next-line react-hooks/exhaustive-deps
361
+ []);
362
+ react_utilities_1.useIsomorphicLayoutEffect(() => {
363
+ if (options.target) {
364
+ overrideTargetRef.current = options.target;
365
+ }
366
+ }, [options.target, overrideTargetRef]);
367
+ react_utilities_1.useIsomorphicLayoutEffect(() => {
368
+ handlePopperUpdate();
369
+ return () => {
370
+ var _a;
371
+
372
+ (_a = popperInstanceRef.current) === null || _a === void 0 ? void 0 : _a.destroy();
373
+ popperInstanceRef.current = null;
374
+ };
375
+ }, [handlePopperUpdate, options.enabled]);
376
+ react_utilities_1.useIsomorphicLayoutEffect(() => {
377
+ var _a, _b;
378
+
379
+ if (!isFirstMount) {
380
+ (_a = popperInstanceRef.current) === null || _a === void 0 ? void 0 : _a.setOptions(resolvePopperOptions((_b = overrideTargetRef.current) !== null && _b !== void 0 ? _b : targetRef.current, containerRef.current, arrowRef.current));
381
+ }
382
+ }, // Missing deps:
383
+ // isFirstMount - Should never change after mount
384
+ // arrowRef, containerRef, targetRef - Stable between renders
385
+ // eslint-disable-next-line react-hooks/exhaustive-deps
386
+ [resolvePopperOptions]);
387
+
388
+ if (process.env.NODE_ENV !== 'production') {
389
+ // This checked should run only in development mode
390
+ // eslint-disable-next-line react-hooks/rules-of-hooks
391
+ React.useEffect(() => {
392
+ var _a;
393
+
394
+ if (containerRef.current) {
395
+ const contentNode = containerRef.current;
396
+ const treeWalker = (_a = contentNode.ownerDocument) === null || _a === void 0 ? void 0 : _a.createTreeWalker(contentNode, NodeFilter.SHOW_ELEMENT, {
397
+ acceptNode: hasAutofocusFilter
398
+ });
399
+
400
+ while (treeWalker === null || treeWalker === void 0 ? void 0 : treeWalker.nextNode()) {
401
+ const node = treeWalker.currentNode; // eslint-disable-next-line no-console
402
+
403
+ console.warn('<Popper>:', node); // eslint-disable-next-line no-console
404
+
405
+ console.warn(['<Popper>: ^ this node contains "autoFocus" prop on a React element. This can break the initial', 'positioning of an element and cause a window jump effect. This issue occurs because React polyfills', '"autoFocus" behavior to solve inconsistencies between different browsers:', 'https://github.com/facebook/react/issues/11851#issuecomment-351787078', '\n', 'However, ".focus()" in this case occurs before any other React effects will be executed', '(React.useEffect(), componentDidMount(), etc.) and we can not prevent this behavior. If you really', 'want to use "autoFocus" please add "position: fixed" to styles of the element that is wrapped by', '"Popper".', `In general, it's not recommended to use "autoFocus" as it may break accessibility aspects:`, 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-autofocus.md', '\n', 'We suggest to use the "trapFocus" prop on Fluent components or a catch "ref" and then use', '"ref.current.focus" in React.useEffect():', 'https://reactjs.org/docs/refs-and-the-dom.html#adding-a-ref-to-a-dom-element'].join(' '));
406
+ }
407
+ } // We run this check once, no need to add deps here
408
+ // TODO: Should be rework to handle options.enabled and contentRef updates
409
+ // eslint-disable-next-line react-hooks/exhaustive-deps
410
+
411
+ }, []);
412
+ }
413
+
414
+ return {
415
+ targetRef,
416
+ containerRef,
417
+ arrowRef
418
+ };
359
419
  }
420
+
360
421
  exports.usePopper = usePopper;
361
422
  //# sourceMappingURL=usePopper.js.map