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

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