@fluentui/react-positioning 9.2.2 → 9.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,90 +1,53 @@
1
- import { computePosition, hide as hideMiddleware, arrow as arrowMiddleware } from '@floating-ui/dom';
1
+ import { hide as hideMiddleware, arrow as arrowMiddleware } from '@floating-ui/dom';
2
2
  import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
3
3
  import { canUseDOM, useIsomorphicLayoutEffect } from '@fluentui/react-utilities';
4
- import { useEventCallback } from '@fluentui/react-utilities';
5
4
  import * as React from 'react';
6
- import { useCallbackRef, toFloatingUIPlacement, toggleScrollListener, hasAutofocusFilter, debounce, hasScrollParent } from './utils';
5
+ import { useCallbackRef, toFloatingUIPlacement, hasAutofocusFilter, hasScrollParent } from './utils';
7
6
  import { shift as shiftMiddleware, flip as flipMiddleware, coverTarget as coverTargetMiddleware, maxSize as maxSizeMiddleware, offset as offsetMiddleware, intersecting as intersectingMiddleware } from './middleware';
8
- import { DATA_POSITIONING_ESCAPED, DATA_POSITIONING_INTERSECTING, DATA_POSITIONING_HIDDEN, DATA_POSITIONING_PLACEMENT } from './constants';
7
+ import { createPositionManager } from './createPositionManager';
9
8
  /**
10
9
  * @internal
11
10
  */
12
11
 
13
12
  export function usePositioning(options) {
14
- const {
15
- targetDocument
16
- } = useFluent();
13
+ const managerRef = React.useRef(null);
14
+ const targetRef = React.useRef(null);
15
+ const overrideTargetRef = React.useRef(null);
16
+ const containerRef = React.useRef(null);
17
+ const arrowRef = React.useRef(null);
17
18
  const {
18
19
  enabled = true
19
20
  } = options;
20
21
  const resolvePositioningOptions = usePositioningOptions(options);
21
- const forceUpdate = useEventCallback(() => {
22
+ const updatePositionManager = React.useCallback(() => {
22
23
  var _a;
23
24
 
24
- const target = (_a = overrideTargetRef.current) !== null && _a !== void 0 ? _a : targetRef.current;
25
-
26
- if (!canUseDOM || !enabled || !target || !containerRef.current) {
27
- return;
25
+ if (managerRef.current) {
26
+ managerRef.current.dispose();
28
27
  }
29
28
 
30
- const {
31
- placement,
32
- middleware,
33
- strategy
34
- } = resolvePositioningOptions(target, containerRef.current, arrowRef.current); // Container is always initialized with `position: fixed` to avoid scroll jumps
35
- // Before computing the positioned coordinates, revert the container to the deisred positioning strategy
36
-
37
- Object.assign(containerRef.current.style, {
38
- position: strategy
39
- });
40
- computePosition(target, containerRef.current, {
41
- placement,
42
- middleware,
43
- strategy
44
- }).then(({
45
- x,
46
- y,
47
- middlewareData,
48
- placement: computedPlacement
49
- }) => {
50
- var _a;
29
+ managerRef.current = null;
30
+ const target = (_a = overrideTargetRef.current) !== null && _a !== void 0 ? _a : targetRef.current;
51
31
 
52
- writeArrowUpdates({
53
- arrow: arrowRef.current,
54
- middlewareData
55
- });
56
- writeContainerUpdates({
32
+ if (enabled && canUseDOM() && target && containerRef.current) {
33
+ managerRef.current = createPositionManager({
57
34
  container: containerRef.current,
58
- middlewareData,
59
- placement: computedPlacement,
60
- coordinates: {
61
- x,
62
- y
63
- },
64
- lowPPI: (((_a = targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.defaultView) === null || _a === void 0 ? void 0 : _a.devicePixelRatio) || 1) <= 1,
65
- strategy
35
+ target,
36
+ arrow: arrowRef.current,
37
+ ...resolvePositioningOptions(containerRef.current, arrowRef.current)
66
38
  });
67
- }).catch(err => {
68
- // https://github.com/floating-ui/floating-ui/issues/1845
69
- // FIXME for node > 14
70
- // node 15 introduces promise rejection which means that any components
71
- // tests need to be `it('', async () => {})` otherwise there can be race conditions with
72
- // JSDOM being torn down before this promise is resolved so globals like `window` and `document` don't exist
73
- // Unless all tests that ever use `usePositioning` are turned into async tests, any logging during testing
74
- // will actually be counter productive
75
- if (process.env.NODE_ENV === 'development') {
76
- // eslint-disable-next-line no-console
77
- console.error('[usePositioning]: Failed to calculate position', err);
78
- }
79
- });
80
- });
81
- const updatePosition = React.useState(() => debounce(forceUpdate))[0];
82
- const targetRef = useTargetRef(updatePosition);
83
- const overrideTargetRef = useTargetRef(updatePosition);
84
- const containerRef = useContainerRef(updatePosition, enabled);
85
- const arrowRef = useArrowRef(updatePosition);
39
+ }
40
+ }, [enabled, resolvePositioningOptions]);
41
+ const setOverrideTarget = React.useCallback(target => {
42
+ overrideTargetRef.current = target;
43
+ updatePositionManager();
44
+ }, [updatePositionManager]);
86
45
  React.useImperativeHandle(options.positioningRef, () => ({
87
- updatePosition,
46
+ updatePosition: () => {
47
+ var _a;
48
+
49
+ return (_a = managerRef.current) === null || _a === void 0 ? void 0 : _a.updatePosition();
50
+ },
88
51
  setTarget: target => {
89
52
  if (options.target && process.env.NODE_ENV !== 'production') {
90
53
  const err = new Error(); // eslint-disable-next-line no-console
@@ -94,35 +57,17 @@ export function usePositioning(options) {
94
57
  console.warn(err.stack);
95
58
  }
96
59
 
97
- overrideTargetRef.current = target;
60
+ setOverrideTarget(target);
98
61
  }
99
- }), // Missing deps:
100
- // options.target - only used for a runtime warning
101
- // overrideTargetRef - Stable between renders
102
- // updatePosition - Stable between renders
103
- // eslint-disable-next-line react-hooks/exhaustive-deps
104
- []);
62
+ }), [options.target, setOverrideTarget]);
105
63
  useIsomorphicLayoutEffect(() => {
106
64
  var _a;
107
65
 
108
- overrideTargetRef.current = (_a = options.target) !== null && _a !== void 0 ? _a : null;
109
- }, [options.target, overrideTargetRef, containerRef]);
66
+ setOverrideTarget((_a = options.target) !== null && _a !== void 0 ? _a : null);
67
+ }, [options.target, setOverrideTarget]);
110
68
  useIsomorphicLayoutEffect(() => {
111
- updatePosition();
112
- }, [enabled, resolvePositioningOptions, updatePosition]); // Add window resize and scroll listeners to update position
113
-
114
- useIsomorphicLayoutEffect(() => {
115
- const win = targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.defaultView;
116
-
117
- if (win) {
118
- win.addEventListener('resize', updatePosition);
119
- win.addEventListener('scroll', updatePosition);
120
- return () => {
121
- win.removeEventListener('resize', updatePosition);
122
- win.removeEventListener('scroll', updatePosition);
123
- };
124
- }
125
- }, [updatePosition, targetDocument]);
69
+ updatePositionManager();
70
+ }, [updatePositionManager]);
126
71
 
127
72
  if (process.env.NODE_ENV !== 'production') {
128
73
  // This checked should run only in development mode
@@ -150,10 +95,29 @@ export function usePositioning(options) {
150
95
  }, []);
151
96
  }
152
97
 
98
+ const setTarget = useCallbackRef(null, target => {
99
+ if (targetRef.current !== target) {
100
+ targetRef.current = target;
101
+ updatePositionManager();
102
+ }
103
+ });
104
+ const setContainer = useCallbackRef(null, container => {
105
+ if (containerRef.current !== container) {
106
+ containerRef.current = container;
107
+ updatePositionManager();
108
+ }
109
+ });
110
+ const setArrow = useCallbackRef(null, arrow => {
111
+ if (arrowRef.current !== arrow) {
112
+ arrowRef.current = arrow;
113
+ updatePositionManager();
114
+ }
115
+ }); // Let users use callback refs so they feel like 'normal' DOM refs
116
+
153
117
  return {
154
- targetRef,
155
- containerRef,
156
- arrowRef
118
+ targetRef: setTarget,
119
+ containerRef: setContainer,
120
+ arrowRef: setArrow
157
121
  };
158
122
  }
159
123
 
@@ -176,7 +140,7 @@ function usePositioningOptions(options) {
176
140
  } = useFluent();
177
141
  const isRtl = dir === 'rtl';
178
142
  const strategy = positionFixed ? 'fixed' : 'absolute';
179
- return React.useCallback((target, container, arrow) => {
143
+ return React.useCallback((container, arrow) => {
180
144
  const hasScrollableElement = hasScrollParent(container);
181
145
  const placement = toFloatingUIPlacement(align, position, isRtl);
182
146
  const middleware = [offset && offsetMiddleware(offset), coverTarget && coverTargetMiddleware(), !pinned && flipMiddleware({
@@ -203,105 +167,4 @@ function usePositioningOptions(options) {
203
167
  };
204
168
  }, [align, arrowPadding, autoSize, coverTarget, disableTether, flipBoundary, isRtl, offset, overflowBoundary, pinned, position, strategy]);
205
169
  }
206
-
207
- function useContainerRef(updatePosition, enabled) {
208
- return useCallbackRef(null, (container, prevContainer) => {
209
- if (container && enabled) {
210
- // When the container is first resolved, set position `fixed` to avoid scroll jumps.
211
- // Without this scroll jumps can occur when the element is rendered initially and receives focus
212
- Object.assign(container.style, {
213
- position: 'fixed',
214
- left: 0,
215
- top: 0,
216
- margin: 0
217
- });
218
- }
219
-
220
- toggleScrollListener(container, prevContainer, updatePosition);
221
- updatePosition();
222
- });
223
- }
224
-
225
- function useTargetRef(updatePosition) {
226
- return useCallbackRef(null, (target, prevTarget) => {
227
- toggleScrollListener(target, prevTarget, updatePosition);
228
- updatePosition();
229
- });
230
- }
231
-
232
- function useArrowRef(updatePosition) {
233
- return useCallbackRef(null, updatePosition);
234
- }
235
- /**
236
- * Writes all DOM element updates after position is computed
237
- */
238
-
239
-
240
- function writeContainerUpdates(options) {
241
- var _a, _b;
242
-
243
- const {
244
- container,
245
- placement,
246
- middlewareData,
247
- strategy,
248
- lowPPI,
249
- coordinates: {
250
- x,
251
- y
252
- }
253
- } = options;
254
-
255
- if (!container) {
256
- return;
257
- }
258
-
259
- container.setAttribute(DATA_POSITIONING_PLACEMENT, placement);
260
- container.removeAttribute(DATA_POSITIONING_INTERSECTING);
261
-
262
- if (middlewareData.intersectionObserver.intersecting) {
263
- container.setAttribute(DATA_POSITIONING_INTERSECTING, '');
264
- }
265
-
266
- container.removeAttribute(DATA_POSITIONING_ESCAPED);
267
-
268
- if ((_a = middlewareData.hide) === null || _a === void 0 ? void 0 : _a.escaped) {
269
- container.setAttribute(DATA_POSITIONING_ESCAPED, '');
270
- }
271
-
272
- container.removeAttribute(DATA_POSITIONING_HIDDEN);
273
-
274
- if ((_b = middlewareData.hide) === null || _b === void 0 ? void 0 : _b.referenceHidden) {
275
- container.setAttribute(DATA_POSITIONING_HIDDEN, '');
276
- }
277
-
278
- Object.assign(container.style, {
279
- transform: lowPPI ? `translate(${x}px, ${y}px)` : `translate3d(${x}px, ${y}px, 0)`,
280
- position: strategy
281
- });
282
- }
283
- /**
284
- * Writes all DOM element updates after position is computed
285
- */
286
-
287
-
288
- function writeArrowUpdates(options) {
289
- const {
290
- arrow,
291
- middlewareData
292
- } = options;
293
-
294
- if (!middlewareData.arrow || !arrow) {
295
- return;
296
- }
297
-
298
- const {
299
- x: arrowX,
300
- y: arrowY
301
- } = middlewareData.arrow;
302
- Object.assign(arrow.style, {
303
- left: `${arrowX}px`,
304
- top: `${arrowY}px`
305
- });
306
- }
307
170
  //# sourceMappingURL=usePositioning.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["usePositioning.ts"],"names":[],"mappings":"AAAA,SAAS,eAAT,EAA0B,IAAI,IAAI,cAAlC,EAAkD,KAAK,IAAI,eAA3D,QAAkF,kBAAlF;AAEA,SAAS,kBAAkB,IAAI,SAA/B,QAAgD,iCAAhD;AACA,SAAS,SAAT,EAAoB,yBAApB,QAAqD,2BAArD;AACA,SAAS,gBAAT,QAAiC,2BAAjC;AACA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AAEA,SACE,cADF,EAEE,qBAFF,EAGE,oBAHF,EAIE,kBAJF,EAKE,QALF,EAME,eANF,QAOO,SAPP;AAQA,SACE,KAAK,IAAI,eADX,EAEE,IAAI,IAAI,cAFV,EAGE,WAAW,IAAI,qBAHjB,EAIE,OAAO,IAAI,iBAJb,EAKE,MAAM,IAAI,gBALZ,EAME,YAAY,IAAI,sBANlB,QAOO,cAPP;AAQA,SACE,wBADF,EAEE,6BAFF,EAGE,uBAHF,EAIE,0BAJF,QAKO,aALP;AAOA;;AAEG;;AACH,OAAM,SAAU,cAAV,CACJ,OADI,EAC0B;EAa9B,MAAM;IAAE;EAAF,IAAqB,SAAS,EAApC;EACA,MAAM;IAAE,OAAO,GAAG;EAAZ,IAAqB,OAA3B;EACA,MAAM,yBAAyB,GAAG,qBAAqB,CAAC,OAAD,CAAvD;EAEA,MAAM,WAAW,GAAG,gBAAgB,CAAC,MAAK;;;IACxC,MAAM,MAAM,GAAG,CAAA,EAAA,GAAA,iBAAiB,CAAC,OAAlB,MAAyB,IAAzB,IAAyB,EAAA,KAAA,KAAA,CAAzB,GAAyB,EAAzB,GAA6B,SAAS,CAAC,OAAtD;;IACA,IAAI,CAAC,SAAD,IAAc,CAAC,OAAf,IAA0B,CAAC,MAA3B,IA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A2B,cAAc,CAAC,KAAhD;EAEA,MAAM,CAAC,MAAP,CAAc,KAAK,CAAC,KAApB,EAA2B;IACzB,IAAI,EAAE,GAAG,MAAM,IADU;IAEzB,GAAG,EAAE,GAAG,MAAM;EAFW,CAA3B;AAID","sourcesContent":["import { computePosition, hide as hideMiddleware, arrow as arrowMiddleware } from '@floating-ui/dom';\nimport type { Middleware, Strategy, Placement, Coords, MiddlewareData } from '@floating-ui/dom';\nimport { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';\nimport { canUseDOM, useIsomorphicLayoutEffect } from '@fluentui/react-utilities';\nimport { useEventCallback } from '@fluentui/react-utilities';\nimport * as React from 'react';\nimport type { PositioningOptions, PositioningProps, PositioningVirtualElement } from './types';\nimport {\n useCallbackRef,\n toFloatingUIPlacement,\n toggleScrollListener,\n hasAutofocusFilter,\n debounce,\n hasScrollParent,\n} from './utils';\nimport {\n shift as shiftMiddleware,\n flip as flipMiddleware,\n coverTarget as coverTargetMiddleware,\n maxSize as maxSizeMiddleware,\n offset as offsetMiddleware,\n intersecting as intersectingMiddleware,\n} from './middleware';\nimport {\n DATA_POSITIONING_ESCAPED,\n DATA_POSITIONING_INTERSECTING,\n DATA_POSITIONING_HIDDEN,\n DATA_POSITIONING_PLACEMENT,\n} from './constants';\n\n/**\n * @internal\n */\nexport function usePositioning(\n options: UsePositioningOptions,\n): {\n // React refs are supposed to be contravariant\n // (allows a more general type to be passed rather than a more specific one)\n // However, Typescript currently can't infer that fact for refs\n // See https://github.com/microsoft/TypeScript/issues/30748 for more information\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n targetRef: React.MutableRefObject<any>;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n containerRef: React.MutableRefObject<any>;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n arrowRef: React.MutableRefObject<any>;\n} {\n const { targetDocument } = useFluent();\n const { enabled = true } = options;\n const resolvePositioningOptions = usePositioningOptions(options);\n\n const forceUpdate = useEventCallback(() => {\n const target = overrideTargetRef.current ?? targetRef.current;\n if (!canUseDOM || !enabled || !target || !containerRef.current) {\n return;\n }\n\n const { placement, middleware, strategy } = resolvePositioningOptions(\n target,\n containerRef.current,\n arrowRef.current,\n );\n\n // Container is always initialized with `position: fixed` to avoid scroll jumps\n // Before computing the positioned coordinates, revert the container to the deisred positioning strategy\n Object.assign(containerRef.current.style, { position: strategy });\n computePosition(target, containerRef.current, { placement, middleware, strategy })\n .then(({ x, y, middlewareData, placement: computedPlacement }) => {\n writeArrowUpdates({ arrow: arrowRef.current, middlewareData });\n writeContainerUpdates({\n container: containerRef.current,\n middlewareData,\n placement: computedPlacement,\n coordinates: { x, y },\n lowPPI: (targetDocument?.defaultView?.devicePixelRatio || 1) <= 1,\n strategy,\n });\n })\n .catch(err => {\n // https://github.com/floating-ui/floating-ui/issues/1845\n // FIXME for node > 14\n // node 15 introduces promise rejection which means that any components\n // tests need to be `it('', async () => {})` otherwise there can be race conditions with\n // JSDOM being torn down before this promise is resolved so globals like `window` and `document` don't exist\n // Unless all tests that ever use `usePositioning` are turned into async tests, any logging during testing\n // will actually be counter productive\n if (process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.error('[usePositioning]: Failed to calculate position', err);\n }\n });\n });\n\n const updatePosition = React.useState(() => debounce(forceUpdate))[0];\n\n const targetRef = useTargetRef(updatePosition);\n const overrideTargetRef = useTargetRef(updatePosition);\n const containerRef = useContainerRef(updatePosition, enabled);\n const arrowRef = useArrowRef(updatePosition);\n\n React.useImperativeHandle(\n options.positioningRef,\n () => ({\n updatePosition,\n setTarget: (target: HTMLElement | PositioningVirtualElement) => {\n if (options.target && process.env.NODE_ENV !== 'production') {\n const err = new Error();\n // eslint-disable-next-line no-console\n console.warn('Imperative setTarget should not be used at the same time as target option');\n // eslint-disable-next-line no-console\n console.warn(err.stack);\n }\n\n overrideTargetRef.current = target;\n },\n }),\n // Missing deps:\n // options.target - only used for a runtime warning\n // overrideTargetRef - Stable between renders\n // updatePosition - Stable between renders\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [],\n );\n\n useIsomorphicLayoutEffect(() => {\n overrideTargetRef.current = options.target ?? null;\n }, [options.target, overrideTargetRef, containerRef]);\n\n useIsomorphicLayoutEffect(() => {\n updatePosition();\n }, [enabled, resolvePositioningOptions, updatePosition]);\n\n // Add window resize and scroll listeners to update position\n useIsomorphicLayoutEffect(() => {\n const win = targetDocument?.defaultView;\n if (win) {\n win.addEventListener('resize', updatePosition);\n win.addEventListener('scroll', updatePosition);\n\n return () => {\n win.removeEventListener('resize', updatePosition);\n win.removeEventListener('scroll', updatePosition);\n };\n }\n }, [updatePosition, targetDocument]);\n\n if (process.env.NODE_ENV !== 'production') {\n // This checked should run only in development mode\n // eslint-disable-next-line react-hooks/rules-of-hooks\n React.useEffect(() => {\n if (containerRef.current) {\n const contentNode = containerRef.current;\n const treeWalker = contentNode.ownerDocument?.createTreeWalker(contentNode, NodeFilter.SHOW_ELEMENT, {\n acceptNode: hasAutofocusFilter,\n });\n\n while (treeWalker.nextNode()) {\n const node = treeWalker.currentNode;\n // eslint-disable-next-line no-console\n console.warn('<Popper>:', node);\n // eslint-disable-next-line no-console\n console.warn(\n [\n '<Popper>: ^ this node contains \"autoFocus\" prop on a React element. This can break the initial',\n 'positioning of an element and cause a window jump effect. This issue occurs because React polyfills',\n '\"autoFocus\" behavior to solve inconsistencies between different browsers:',\n 'https://github.com/facebook/react/issues/11851#issuecomment-351787078',\n '\\n',\n 'However, \".focus()\" in this case occurs before any other React effects will be executed',\n '(React.useEffect(), componentDidMount(), etc.) and we can not prevent this behavior. If you really',\n 'want to use \"autoFocus\" please add \"position: fixed\" to styles of the element that is wrapped by',\n '\"Popper\".',\n `In general, it's not recommended to use \"autoFocus\" as it may break accessibility aspects:`,\n 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-autofocus.md',\n '\\n',\n 'We suggest to use the \"trapFocus\" prop on Fluent components or a catch \"ref\" and then use',\n '\"ref.current.focus\" in React.useEffect():',\n 'https://reactjs.org/docs/refs-and-the-dom.html#adding-a-ref-to-a-dom-element',\n ].join(' '),\n );\n }\n }\n // We run this check once, no need to add deps here\n // TODO: Should be rework to handle options.enabled and contentRef updates\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n }\n\n return { targetRef, containerRef, arrowRef };\n}\n\ninterface UsePositioningOptions extends PositioningProps {\n /**\n * If false, does not position anything\n */\n enabled?: boolean;\n}\n\nfunction usePositioningOptions(options: PositioningOptions) {\n const {\n align,\n arrowPadding,\n autoSize,\n coverTarget,\n flipBoundary,\n offset,\n overflowBoundary,\n pinned,\n position,\n unstable_disableTether: disableTether,\n positionFixed,\n } = options;\n\n const { dir } = useFluent();\n const isRtl = dir === 'rtl';\n const strategy: Strategy = positionFixed ? 'fixed' : 'absolute';\n\n return React.useCallback(\n (\n target: HTMLElement | PositioningVirtualElement | null,\n container: HTMLElement | null,\n arrow: HTMLElement | null,\n ) => {\n const hasScrollableElement = hasScrollParent(container);\n\n const placement = toFloatingUIPlacement(align, position, isRtl);\n const middleware = [\n offset && offsetMiddleware(offset),\n coverTarget && coverTargetMiddleware(),\n !pinned && flipMiddleware({ container, flipBoundary, hasScrollableElement }),\n shiftMiddleware({ container, hasScrollableElement, overflowBoundary, disableTether }),\n autoSize && maxSizeMiddleware(autoSize),\n intersectingMiddleware(),\n arrow && arrowMiddleware({ element: arrow, padding: arrowPadding }),\n hideMiddleware({ strategy: 'referenceHidden' }),\n hideMiddleware({ strategy: 'escaped' }),\n ].filter(Boolean) as Middleware[];\n\n return {\n placement,\n middleware,\n strategy,\n };\n },\n [\n align,\n arrowPadding,\n autoSize,\n coverTarget,\n disableTether,\n flipBoundary,\n isRtl,\n offset,\n overflowBoundary,\n pinned,\n position,\n strategy,\n ],\n );\n}\n\nfunction useContainerRef(updatePosition: () => void, enabled: boolean) {\n return useCallbackRef<HTMLElement | null>(null, (container, prevContainer) => {\n if (container && enabled) {\n // When the container is first resolved, set position `fixed` to avoid scroll jumps.\n // Without this scroll jumps can occur when the element is rendered initially and receives focus\n Object.assign(container.style, { position: 'fixed', left: 0, top: 0, margin: 0 });\n }\n\n toggleScrollListener(container, prevContainer, updatePosition);\n\n updatePosition();\n });\n}\n\nfunction useTargetRef(updatePosition: () => void) {\n return useCallbackRef<HTMLElement | PositioningVirtualElement | null>(null, (target, prevTarget) => {\n toggleScrollListener(target, prevTarget, updatePosition);\n\n updatePosition();\n });\n}\n\nfunction useArrowRef(updatePosition: () => void) {\n return useCallbackRef<HTMLElement | null>(null, updatePosition);\n}\n\n/**\n * Writes all DOM element updates after position is computed\n */\nfunction writeContainerUpdates(options: {\n container: HTMLElement | null;\n placement: Placement;\n middlewareData: MiddlewareData;\n /**\n * Layer acceleration can disable subpixel rendering which causes slightly\n * blurry text on low PPI displays, so we want to use 2D transforms\n * instead\n */\n lowPPI: boolean;\n strategy: Strategy;\n coordinates: Coords;\n}) {\n const {\n container,\n placement,\n middlewareData,\n strategy,\n lowPPI,\n coordinates: { x, y },\n } = options;\n if (!container) {\n return;\n }\n container.setAttribute(DATA_POSITIONING_PLACEMENT, placement);\n container.removeAttribute(DATA_POSITIONING_INTERSECTING);\n if (middlewareData.intersectionObserver.intersecting) {\n container.setAttribute(DATA_POSITIONING_INTERSECTING, '');\n }\n\n container.removeAttribute(DATA_POSITIONING_ESCAPED);\n if (middlewareData.hide?.escaped) {\n container.setAttribute(DATA_POSITIONING_ESCAPED, '');\n }\n\n container.removeAttribute(DATA_POSITIONING_HIDDEN);\n if (middlewareData.hide?.referenceHidden) {\n container.setAttribute(DATA_POSITIONING_HIDDEN, '');\n }\n\n Object.assign(container.style, {\n transform: lowPPI ? `translate(${x}px, ${y}px)` : `translate3d(${x}px, ${y}px, 0)`,\n position: strategy,\n });\n}\n\n/**\n * Writes all DOM element updates after position is computed\n */\nfunction writeArrowUpdates(options: { arrow: HTMLElement | null; middlewareData: MiddlewareData }) {\n const { arrow, middlewareData } = options;\n if (!middlewareData.arrow || !arrow) {\n return;\n }\n\n const { x: arrowX, y: arrowY } = middlewareData.arrow;\n\n Object.assign(arrow.style, {\n left: `${arrowX}px`,\n top: `${arrowY}px`,\n });\n}\n"],"sourceRoot":"../src/"}
1
+ {"version":3,"sources":["usePositioning.ts"],"names":[],"mappings":"AAAA,SAAS,IAAI,IAAI,cAAjB,EAAiC,KAAK,IAAI,eAA1C,QAAiE,kBAAjE;AAEA,SAAS,kBAAkB,IAAI,SAA/B,QAAgD,iCAAhD;AACA,SAAS,SAAT,EAAoB,yBAApB,QAAqD,2BAArD;AACA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AAQA,SAAS,cAAT,EAAyB,qBAAzB,EAAgD,kBAAhD,EAAoE,eAApE,QAA2F,SAA3F;AACA,SACE,KAAK,IAAI,eADX,EAEE,IAAI,IAAI,cAFV,EAGE,WAAW,IAAI,qBAHjB,EAIE,OAAO,IAAI,iBAJb,EAKE,MAAM,IAAI,gBALZ,EAME,YAAY,IAAI,sBANlB,QAOO,cAPP;AAQA,SAAS,qBAAT,QAAsC,yBAAtC;AAEA;;AAEG;;AACH,OAAM,SAAU,cAAV,CAAyB,OAAzB,EAAuD;EAC3D,MAAM,UAAU,GAAG,KAAK,CAAC,MAAN,CAAqC,IAArC,CAAnB;EACA,MAAM,SAAS,GAAG,KAAK,CAAC,MAAN,CAAmC,IAAnC,CAAlB;EACA,MAAM,iBAAiB,GAAG,KAAK,CAAC,MAAN,CAAmC,IAAnC,CAA1B;EACA,MAAM,YAAY,GAAG,KAAK,CAAC,MAAN,CAAiC,IAAjC,CAArB;EACA,MAAM,QAAQ,GAAG,KAAK,CAAC,MAAN,CAAiC,IAAjC,CAAjB;EAEA,MAAM;IAAE,OAAO,GAAG;EAAZ,IAAqB,OAA3B;EACA,MAAM,yBAAyB,GAAG,qBAAqB,CAAC,OAAD,CAAvD;EACA,MAAM,qBAAqB,GAAG,KAAK,CAAC,WAAN,CAAkB,MAAK;;;IACnD,IAAI,UAAU,CAAC,OAAf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AK,KAAtB;EACA,MAAM,QAAQ,GAAa,aAAa,GAAG,OAAH,GAAa,UAArD;EAEA,OAAO,KAAK,CAAC,WAAN,CACL,CAAC,SAAD,EAAgC,KAAhC,KAA6D;IAC3D,MAAM,oBAAoB,GAAG,eAAe,CAAC,SAAD,CAA5C;IAEA,MAAM,SAAS,GAAG,qBAAqB,CAAC,KAAD,EAAQ,QAAR,EAAkB,KAAlB,CAAvC;IACA,MAAM,UAAU,GAAG,CACjB,MAAM,IAAI,gBAAgB,CAAC,MAAD,CADT,EAEjB,WAAW,IAAI,qBAAqB,EAFnB,EAGjB,CAAC,MAAD,IAAW,cAAc,CAAC;MAAE,SAAF;MAAa,YAAb;MAA2B;IAA3B,CAAD,CAHR,EAIjB,eAAe,CAAC;MAAE,SAAF;MAAa,oBAAb;MAAmC,gBAAnC;MAAqD;IAArD,CAAD,CAJE,EAKjB,QAAQ,IAAI,iBAAiB,CAAC,QAAD,CALZ,EAMjB,sBAAsB,EANL,EAOjB,KAAK,IAAI,eAAe,CAAC;MAAE,OAAO,EAAE,KAAX;MAAkB,OAAO,EAAE;IAA3B,CAAD,CAPP,EAQjB,cAAc,CAAC;MAAE,QAAQ,EAAE;IAAZ,CAAD,CARG,EASjB,cAAc,CAAC;MAAE,QAAQ,EAAE;IAAZ,CAAD,CATG,EAUjB,MAViB,CAUV,OAVU,CAAnB;IAYA,OAAO;MACL,SADK;MAEL,UAFK;MAGL;IAHK,CAAP;EAKD,CAtBI,EAuBL,CACE,KADF,EAEE,YAFF,EAGE,QAHF,EAIE,WAJF,EAKE,aALF,EAME,YANF,EAOE,KAPF,EAQE,MARF,EASE,gBATF,EAUE,MAVF,EAWE,QAXF,EAYE,QAZF,CAvBK,CAAP;AAsCD","sourcesContent":["import { hide as hideMiddleware, arrow as arrowMiddleware } from '@floating-ui/dom';\nimport type { Middleware, Strategy } from '@floating-ui/dom';\nimport { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';\nimport { canUseDOM, useIsomorphicLayoutEffect } from '@fluentui/react-utilities';\nimport * as React from 'react';\nimport type {\n PositioningOptions,\n PositioningProps,\n PositionManager,\n TargetElement,\n UsePositioningReturn,\n} from './types';\nimport { useCallbackRef, toFloatingUIPlacement, hasAutofocusFilter, hasScrollParent } from './utils';\nimport {\n shift as shiftMiddleware,\n flip as flipMiddleware,\n coverTarget as coverTargetMiddleware,\n maxSize as maxSizeMiddleware,\n offset as offsetMiddleware,\n intersecting as intersectingMiddleware,\n} from './middleware';\nimport { createPositionManager } from './createPositionManager';\n\n/**\n * @internal\n */\nexport function usePositioning(options: UsePositioningOptions): UsePositioningReturn {\n const managerRef = React.useRef<PositionManager | null>(null);\n const targetRef = React.useRef<TargetElement | null>(null);\n const overrideTargetRef = React.useRef<TargetElement | null>(null);\n const containerRef = React.useRef<HTMLElement | null>(null);\n const arrowRef = React.useRef<HTMLElement | null>(null);\n\n const { enabled = true } = options;\n const resolvePositioningOptions = usePositioningOptions(options);\n const updatePositionManager = React.useCallback(() => {\n if (managerRef.current) {\n managerRef.current.dispose();\n }\n managerRef.current = null;\n\n const target = overrideTargetRef.current ?? targetRef.current;\n\n if (enabled && canUseDOM() && target && containerRef.current) {\n managerRef.current = createPositionManager({\n container: containerRef.current,\n target,\n arrow: arrowRef.current,\n ...resolvePositioningOptions(containerRef.current, arrowRef.current),\n });\n }\n }, [enabled, resolvePositioningOptions]);\n\n const setOverrideTarget = React.useCallback(\n (target: TargetElement | null) => {\n overrideTargetRef.current = target;\n updatePositionManager();\n },\n [updatePositionManager],\n );\n\n React.useImperativeHandle(\n options.positioningRef,\n () => ({\n updatePosition: () => managerRef.current?.updatePosition(),\n setTarget: (target: TargetElement) => {\n if (options.target && process.env.NODE_ENV !== 'production') {\n const err = new Error();\n // eslint-disable-next-line no-console\n console.warn('Imperative setTarget should not be used at the same time as target option');\n // eslint-disable-next-line no-console\n console.warn(err.stack);\n }\n\n setOverrideTarget(target);\n },\n }),\n [options.target, setOverrideTarget],\n );\n\n useIsomorphicLayoutEffect(() => {\n setOverrideTarget(options.target ?? null);\n }, [options.target, setOverrideTarget]);\n\n useIsomorphicLayoutEffect(() => {\n updatePositionManager();\n }, [updatePositionManager]);\n\n if (process.env.NODE_ENV !== 'production') {\n // This checked should run only in development mode\n // eslint-disable-next-line react-hooks/rules-of-hooks\n React.useEffect(() => {\n if (containerRef.current) {\n const contentNode = containerRef.current;\n const treeWalker = contentNode.ownerDocument?.createTreeWalker(contentNode, NodeFilter.SHOW_ELEMENT, {\n acceptNode: hasAutofocusFilter,\n });\n\n while (treeWalker.nextNode()) {\n const node = treeWalker.currentNode;\n // eslint-disable-next-line no-console\n console.warn('<Popper>:', node);\n // eslint-disable-next-line no-console\n console.warn(\n [\n '<Popper>: ^ this node contains \"autoFocus\" prop on a React element. This can break the initial',\n 'positioning of an element and cause a window jump effect. This issue occurs because React polyfills',\n '\"autoFocus\" behavior to solve inconsistencies between different browsers:',\n 'https://github.com/facebook/react/issues/11851#issuecomment-351787078',\n '\\n',\n 'However, \".focus()\" in this case occurs before any other React effects will be executed',\n '(React.useEffect(), componentDidMount(), etc.) and we can not prevent this behavior. If you really',\n 'want to use \"autoFocus\" please add \"position: fixed\" to styles of the element that is wrapped by',\n '\"Popper\".',\n `In general, it's not recommended to use \"autoFocus\" as it may break accessibility aspects:`,\n 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-autofocus.md',\n '\\n',\n 'We suggest to use the \"trapFocus\" prop on Fluent components or a catch \"ref\" and then use',\n '\"ref.current.focus\" in React.useEffect():',\n 'https://reactjs.org/docs/refs-and-the-dom.html#adding-a-ref-to-a-dom-element',\n ].join(' '),\n );\n }\n }\n // We run this check once, no need to add deps here\n // TODO: Should be rework to handle options.enabled and contentRef updates\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n }\n\n const setTarget = useCallbackRef<TargetElement>(null, target => {\n if (targetRef.current !== target) {\n targetRef.current = target;\n updatePositionManager();\n }\n });\n\n const setContainer = useCallbackRef<HTMLElement | null>(null, container => {\n if (containerRef.current !== container) {\n containerRef.current = container;\n updatePositionManager();\n }\n });\n\n const setArrow = useCallbackRef<HTMLElement | null>(null, arrow => {\n if (arrowRef.current !== arrow) {\n arrowRef.current = arrow;\n updatePositionManager();\n }\n });\n\n // Let users use callback refs so they feel like 'normal' DOM refs\n return { targetRef: setTarget, containerRef: setContainer, arrowRef: setArrow };\n}\n\ninterface UsePositioningOptions extends PositioningProps {\n /**\n * If false, does not position anything\n */\n enabled?: boolean;\n}\n\nfunction usePositioningOptions(options: PositioningOptions) {\n const {\n align,\n arrowPadding,\n autoSize,\n coverTarget,\n flipBoundary,\n offset,\n overflowBoundary,\n pinned,\n position,\n unstable_disableTether: disableTether,\n positionFixed,\n } = options;\n\n const { dir } = useFluent();\n const isRtl = dir === 'rtl';\n const strategy: Strategy = positionFixed ? 'fixed' : 'absolute';\n\n return React.useCallback(\n (container: HTMLElement | null, arrow: HTMLElement | null) => {\n const hasScrollableElement = hasScrollParent(container);\n\n const placement = toFloatingUIPlacement(align, position, isRtl);\n const middleware = [\n offset && offsetMiddleware(offset),\n coverTarget && coverTargetMiddleware(),\n !pinned && flipMiddleware({ container, flipBoundary, hasScrollableElement }),\n shiftMiddleware({ container, hasScrollableElement, overflowBoundary, disableTether }),\n autoSize && maxSizeMiddleware(autoSize),\n intersectingMiddleware(),\n arrow && arrowMiddleware({ element: arrow, padding: arrowPadding }),\n hideMiddleware({ strategy: 'referenceHidden' }),\n hideMiddleware({ strategy: 'escaped' }),\n ].filter(Boolean) as Middleware[];\n\n return {\n placement,\n middleware,\n strategy,\n };\n },\n [\n align,\n arrowPadding,\n autoSize,\n coverTarget,\n disableTether,\n flipBoundary,\n isRtl,\n offset,\n overflowBoundary,\n pinned,\n position,\n strategy,\n ],\n );\n}\n"],"sourceRoot":"../src/"}
@@ -1 +1 @@
1
- {"version":3,"sources":["usePositioningMouseTarget.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AACA,SAAS,6BAAT,QAA8C,iCAA9C;AAGA;;;;;;;;AAQG;;AACH,OAAO,MAAM,yBAAyB,GACpC,YADuC,IAErC;EACF,MAAM,CAAC,cAAD,EAAiB,iBAAjB,IAAsC,KAAK,CAAC,QAAN,CAAsD,YAAtD,CAA5C;;EAEA,MAAM,qBAAqB,GAAI,KAAD,IAA4D;IACxF,IAAI,KAAK,KAAK,SAAV,IAAuB,KAAK,KAAK,IAArC,EAA2C;MACzC,iBAAiB,CAAC,SAAD,CAAjB;MACA;IACD;;IAED,IAAI,UAAJ;;IACA,IAAI,EAAE,KAAK,YAAY,UAAnB,CAAJ,EAAoC;MAClC,UAAU,GAAG,KAAK,CAAC,WAAnB;IACD,CAFD,MAEO;MACL,UAAU,GAAG,KAAb;IACD;;IAED,IAAI,EAAE,UAAU,YAAY,UAAxB,KAAuC,OAAO,CAAC,GAAR,CAAY,QAAZ,KAAyB,YAApE,EAAkF;MAChF;MACA,OAAO,CAAC,KAAR,CAAc,+DAAd;IACD;;IAED,MAAM,aAAa,GAAG,6BAA6B,CAAC,UAAD,CAAnD;IACA,iBAAiB,CAAC,aAAD,CAAjB;EACD,CApBD;;EAsBA,OAAO,CAAC,cAAD,EAAiB,qBAAjB,CAAP;AACD,CA5BM","sourcesContent":["import * as React from 'react';\nimport { createVirtualElementFromClick } from './createVirtualElementFromClick';\nimport { PositioningVirtualElement } from './types';\n\n/**\n * @internal\n * A state hook that manages a popper virtual element from mouseevents.\n * Useful for scenarios where a component needs to be positioned by mouse click (e.g. contextmenu)\n * React synthetic events are not persisted by this hook\n *\n * @param initialState - initializes a user provided state similare to useState\n * @returns state and dispatcher for a Popper virtual element that uses native/synthetic mouse events\n */\nexport const usePositioningMouseTarget = (\n initialState?: PositioningVirtualElement | (() => PositioningVirtualElement),\n) => {\n const [virtualElement, setVirtualElement] = React.useState<PositioningVirtualElement | undefined>(initialState);\n\n const setVirtualMouseTarget = (event: React.MouseEvent | MouseEvent | undefined | null) => {\n if (event === undefined || event === null) {\n setVirtualElement(undefined);\n return;\n }\n\n let mouseevent: MouseEvent;\n if (!(event instanceof MouseEvent)) {\n mouseevent = event.nativeEvent;\n } else {\n mouseevent = event;\n }\n\n if (!(mouseevent instanceof MouseEvent) && process.env.NODE_ENV !== 'production') {\n // eslint-disable-next-line no-console\n console.error('usePositioningMouseTarget should only be used with MouseEvent');\n }\n\n const contextTarget = createVirtualElementFromClick(mouseevent);\n setVirtualElement(contextTarget);\n };\n\n return [virtualElement, setVirtualMouseTarget] as const;\n};\n"],"sourceRoot":"../src/"}
1
+ {"version":3,"sources":["usePositioningMouseTarget.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAZ,MAAuB,OAAvB;AACA,SAAS,6BAAT,QAA8C,iCAA9C;AAGA;;;;;;;;AAQG;;AACH,OAAO,MAAM,yBAAyB,GACpC,YADuC,IAErC;EACF,MAAM,CAAC,cAAD,EAAiB,iBAAjB,IAAsC,KAAK,CAAC,QAAN,CAAsD,YAAtD,CAA5C;;EAEA,MAAM,qBAAqB,GAA0B,KAAK,IAAG;IAC3D,IAAI,KAAK,KAAK,SAAV,IAAuB,KAAK,KAAK,IAArC,EAA2C;MACzC,iBAAiB,CAAC,SAAD,CAAjB;MACA;IACD;;IAED,IAAI,UAAJ;;IACA,IAAI,EAAE,KAAK,YAAY,UAAnB,CAAJ,EAAoC;MAClC,UAAU,GAAG,KAAK,CAAC,WAAnB;IACD,CAFD,MAEO;MACL,UAAU,GAAG,KAAb;IACD;;IAED,IAAI,EAAE,UAAU,YAAY,UAAxB,KAAuC,OAAO,CAAC,GAAR,CAAY,QAAZ,KAAyB,YAApE,EAAkF;MAChF;MACA,OAAO,CAAC,KAAR,CAAc,+DAAd;IACD;;IAED,MAAM,aAAa,GAAG,6BAA6B,CAAC,UAAD,CAAnD;IACA,iBAAiB,CAAC,aAAD,CAAjB;EACD,CApBD;;EAsBA,OAAO,CAAC,cAAD,EAAiB,qBAAjB,CAAP;AACD,CA5BM","sourcesContent":["import * as React from 'react';\nimport { createVirtualElementFromClick } from './createVirtualElementFromClick';\nimport { PositioningVirtualElement, SetVirtualMouseTarget } from './types';\n\n/**\n * @internal\n * A state hook that manages a popper virtual element from mouseevents.\n * Useful for scenarios where a component needs to be positioned by mouse click (e.g. contextmenu)\n * React synthetic events are not persisted by this hook\n *\n * @param initialState - initializes a user provided state similare to useState\n * @returns state and dispatcher for a Popper virtual element that uses native/synthetic mouse events\n */\nexport const usePositioningMouseTarget = (\n initialState?: PositioningVirtualElement | (() => PositioningVirtualElement),\n) => {\n const [virtualElement, setVirtualElement] = React.useState<PositioningVirtualElement | undefined>(initialState);\n\n const setVirtualMouseTarget: SetVirtualMouseTarget = event => {\n if (event === undefined || event === null) {\n setVirtualElement(undefined);\n return;\n }\n\n let mouseevent: MouseEvent;\n if (!(event instanceof MouseEvent)) {\n mouseevent = event.nativeEvent;\n } else {\n mouseevent = event;\n }\n\n if (!(mouseevent instanceof MouseEvent) && process.env.NODE_ENV !== 'production') {\n // eslint-disable-next-line no-console\n console.error('usePositioningMouseTarget should only be used with MouseEvent');\n }\n\n const contextTarget = createVirtualElementFromClick(mouseevent);\n setVirtualElement(contextTarget);\n };\n\n return [virtualElement, setVirtualMouseTarget] as const;\n};\n"],"sourceRoot":"../src/"}
@@ -10,4 +10,6 @@ export * from './useCallbackRef';
10
10
  export * from './debounce';
11
11
  export * from './toggleScrollListener';
12
12
  export * from './hasAutoFocusFilter';
13
+ export * from './writeArrowUpdates';
14
+ export * from './writeContainerupdates';
13
15
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"../src/","sources":["utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,eAAe,CAAC;AAC9B,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC","sourcesContent":["export * from './parseFloatingUIPlacement';\nexport * from './getBoundary';\nexport * from './getReactFiberFromNode';\nexport * from './getScrollParent';\nexport * from './mergeArrowOffset';\nexport * from './toFloatingUIPlacement';\nexport * from './fromFloatingUIPlacement';\nexport * from './resolvePositioningShorthand';\nexport * from './useCallbackRef';\nexport * from './debounce';\nexport * from './toggleScrollListener';\nexport * from './hasAutoFocusFilter';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"../src/","sources":["utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,eAAe,CAAC;AAC9B,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC","sourcesContent":["export * from './parseFloatingUIPlacement';\nexport * from './getBoundary';\nexport * from './getReactFiberFromNode';\nexport * from './getScrollParent';\nexport * from './mergeArrowOffset';\nexport * from './toFloatingUIPlacement';\nexport * from './fromFloatingUIPlacement';\nexport * from './resolvePositioningShorthand';\nexport * from './useCallbackRef';\nexport * from './debounce';\nexport * from './toggleScrollListener';\nexport * from './hasAutoFocusFilter';\nexport * from './writeArrowUpdates';\nexport * from './writeContainerupdates';\n"]}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Writes all DOM element updates after position is computed
3
+ */
4
+ export function writeArrowUpdates(options) {
5
+ const {
6
+ arrow,
7
+ middlewareData
8
+ } = options;
9
+
10
+ if (!middlewareData.arrow || !arrow) {
11
+ return;
12
+ }
13
+
14
+ const {
15
+ x: arrowX,
16
+ y: arrowY
17
+ } = middlewareData.arrow;
18
+ Object.assign(arrow.style, {
19
+ left: `${arrowX}px`,
20
+ top: `${arrowY}px`
21
+ });
22
+ }
23
+ //# sourceMappingURL=writeArrowUpdates.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["utils/writeArrowUpdates.ts"],"names":[],"mappings":"AAEA;;AAEG;AACH,OAAM,SAAU,iBAAV,CAA4B,OAA5B,EAAkG;EACtG,MAAM;IAAE,KAAF;IAAS;EAAT,IAA4B,OAAlC;;EACA,IAAI,CAAC,cAAc,CAAC,KAAhB,IAAyB,CAAC,KAA9B,EAAqC;IACnC;EACD;;EAED,MAAM;IAAE,CAAC,EAAE,MAAL;IAAa,CAAC,EAAE;EAAhB,IAA2B,cAAc,CAAC,KAAhD;EAEA,MAAM,CAAC,MAAP,CAAc,KAAK,CAAC,KAApB,EAA2B;IACzB,IAAI,EAAE,GAAG,MAAM,IADU;IAEzB,GAAG,EAAE,GAAG,MAAM;EAFW,CAA3B;AAID","sourcesContent":["import { MiddlewareData } from '@floating-ui/dom';\n\n/**\n * Writes all DOM element updates after position is computed\n */\nexport function writeArrowUpdates(options: { arrow: HTMLElement | null; middlewareData: MiddlewareData }) {\n const { arrow, middlewareData } = options;\n if (!middlewareData.arrow || !arrow) {\n return;\n }\n\n const { x: arrowX, y: arrowY } = middlewareData.arrow;\n\n Object.assign(arrow.style, {\n left: `${arrowX}px`,\n top: `${arrowY}px`,\n });\n}\n"],"sourceRoot":"../src/"}
@@ -0,0 +1,49 @@
1
+ import { DATA_POSITIONING_ESCAPED, DATA_POSITIONING_HIDDEN, DATA_POSITIONING_INTERSECTING, DATA_POSITIONING_PLACEMENT } from '../constants';
2
+ /**
3
+ * Writes all container element position updates after the position is computed
4
+ */
5
+
6
+ export function writeContainerUpdates(options) {
7
+ var _a, _b;
8
+
9
+ const {
10
+ container,
11
+ placement,
12
+ middlewareData,
13
+ strategy,
14
+ lowPPI,
15
+ coordinates: {
16
+ x,
17
+ y
18
+ }
19
+ } = options;
20
+
21
+ if (!container) {
22
+ return;
23
+ }
24
+
25
+ container.setAttribute(DATA_POSITIONING_PLACEMENT, placement);
26
+ container.removeAttribute(DATA_POSITIONING_INTERSECTING);
27
+
28
+ if (middlewareData.intersectionObserver.intersecting) {
29
+ container.setAttribute(DATA_POSITIONING_INTERSECTING, '');
30
+ }
31
+
32
+ container.removeAttribute(DATA_POSITIONING_ESCAPED);
33
+
34
+ if ((_a = middlewareData.hide) === null || _a === void 0 ? void 0 : _a.escaped) {
35
+ container.setAttribute(DATA_POSITIONING_ESCAPED, '');
36
+ }
37
+
38
+ container.removeAttribute(DATA_POSITIONING_HIDDEN);
39
+
40
+ if ((_b = middlewareData.hide) === null || _b === void 0 ? void 0 : _b.referenceHidden) {
41
+ container.setAttribute(DATA_POSITIONING_HIDDEN, '');
42
+ }
43
+
44
+ Object.assign(container.style, {
45
+ transform: lowPPI ? `translate(${x}px, ${y}px)` : `translate3d(${x}px, ${y}px, 0)`,
46
+ position: strategy
47
+ });
48
+ }
49
+ //# sourceMappingURL=writeContainerupdates.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["utils/writeContainerupdates.ts"],"names":[],"mappings":"AACA,SACE,wBADF,EAEE,uBAFF,EAGE,6BAHF,EAIE,0BAJF,QAKO,cALP;AAOA;;AAEG;;AACH,OAAM,SAAU,qBAAV,CAAgC,OAAhC,EAYL;;;EACC,MAAM;IACJ,SADI;IAEJ,SAFI;IAGJ,cAHI;IAIJ,QAJI;IAKJ,MALI;IAMJ,WAAW,EAAE;MAAE,CAAF;MAAK;IAAL;EANT,IAOF,OAPJ;;EAQA,IAAI,CAAC,SAAL,EAAgB;IACd;EACD;;EACD,SAAS,CAAC,YAAV,CAAuB,0BAAvB,EAAmD,SAAnD;EACA,SAAS,CAAC,eAAV,CAA0B,6BAA1B;;EACA,IAAI,cAAc,CAAC,oBAAf,CAAoC,YAAxC,EAAsD;IACpD,SAAS,CAAC,YAAV,CAAuB,6BAAvB,EAAsD,EAAtD;EACD;;EAED,SAAS,CAAC,eAAV,CAA0B,wBAA1B;;EACA,IAAI,CAAA,EAAA,GAAA,cAAc,CAAC,IAAf,MAAmB,IAAnB,IAAmB,EAAA,KAAA,KAAA,CAAnB,GAAmB,KAAA,CAAnB,GAAmB,EAAA,CAAE,OAAzB,EAAkC;IAChC,SAAS,CAAC,YAAV,CAAuB,wBAAvB,EAAiD,EAAjD;EACD;;EAED,SAAS,CAAC,eAAV,CAA0B,uBAA1B;;EACA,IAAI,CAAA,EAAA,GAAA,cAAc,CAAC,IAAf,MAAmB,IAAnB,IAAmB,EAAA,KAAA,KAAA,CAAnB,GAAmB,KAAA,CAAnB,GAAmB,EAAA,CAAE,eAAzB,EAA0C;IACxC,SAAS,CAAC,YAAV,CAAuB,uBAAvB,EAAgD,EAAhD;EACD;;EAED,MAAM,CAAC,MAAP,CAAc,SAAS,CAAC,KAAxB,EAA+B;IAC7B,SAAS,EAAE,MAAM,GAAG,aAAa,CAAC,OAAO,CAAC,KAAzB,GAAiC,eAAe,CAAC,OAAO,CAAC,QAD7C;IAE7B,QAAQ,EAAE;EAFmB,CAA/B;AAID","sourcesContent":["import type { Placement, MiddlewareData, Strategy, Coords } from '@floating-ui/dom';\nimport {\n DATA_POSITIONING_ESCAPED,\n DATA_POSITIONING_HIDDEN,\n DATA_POSITIONING_INTERSECTING,\n DATA_POSITIONING_PLACEMENT,\n} from '../constants';\n\n/**\n * Writes all container element position updates after the position is computed\n */\nexport function writeContainerUpdates(options: {\n container: HTMLElement | null;\n placement: Placement;\n middlewareData: MiddlewareData;\n /**\n * Layer acceleration can disable subpixel rendering which causes slightly\n * blurry text on low PPI displays, so we want to use 2D transforms\n * instead\n */\n lowPPI: boolean;\n strategy: Strategy;\n coordinates: Coords;\n}) {\n const {\n container,\n placement,\n middlewareData,\n strategy,\n lowPPI,\n coordinates: { x, y },\n } = options;\n if (!container) {\n return;\n }\n container.setAttribute(DATA_POSITIONING_PLACEMENT, placement);\n container.removeAttribute(DATA_POSITIONING_INTERSECTING);\n if (middlewareData.intersectionObserver.intersecting) {\n container.setAttribute(DATA_POSITIONING_INTERSECTING, '');\n }\n\n container.removeAttribute(DATA_POSITIONING_ESCAPED);\n if (middlewareData.hide?.escaped) {\n container.setAttribute(DATA_POSITIONING_ESCAPED, '');\n }\n\n container.removeAttribute(DATA_POSITIONING_HIDDEN);\n if (middlewareData.hide?.referenceHidden) {\n container.setAttribute(DATA_POSITIONING_HIDDEN, '');\n }\n\n Object.assign(container.style, {\n transform: lowPPI ? `translate(${x}px, ${y}px)` : `translate3d(${x}px, ${y}px, 0)`,\n position: strategy,\n });\n}\n"],"sourceRoot":"../src/"}
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createPositionManager = void 0;
7
+
8
+ const dom_1 = /*#__PURE__*/require("@floating-ui/dom");
9
+
10
+ const utils_1 = /*#__PURE__*/require("./utils");
11
+ /**
12
+ * @internal
13
+ * @returns manager that handles positioning out of the react lifecycle
14
+ */
15
+
16
+
17
+ function createPositionManager(options) {
18
+ const {
19
+ container,
20
+ target,
21
+ arrow,
22
+ strategy,
23
+ middleware,
24
+ placement
25
+ } = options;
26
+
27
+ if (!target || !container) {
28
+ return {
29
+ updatePosition: () => undefined,
30
+ dispose: () => undefined
31
+ };
32
+ }
33
+
34
+ let isFirstUpdate = true;
35
+ const scrollParents = new Set();
36
+ const targetWindow = container.ownerDocument.defaultView; // When the container is first resolved, set position `fixed` to avoid scroll jumps.
37
+ // Without this scroll jumps can occur when the element is rendered initially and receives focus
38
+
39
+ Object.assign(container.style, {
40
+ position: 'fixed',
41
+ left: 0,
42
+ top: 0,
43
+ margin: 0
44
+ });
45
+
46
+ const forceUpdate = () => {
47
+ if (isFirstUpdate) {
48
+ scrollParents.add(utils_1.getScrollParent(container));
49
+
50
+ if (target instanceof HTMLElement) {
51
+ scrollParents.add(utils_1.getScrollParent(target));
52
+ }
53
+
54
+ scrollParents.forEach(scrollParent => {
55
+ scrollParent.addEventListener('scroll', updatePosition);
56
+ });
57
+ isFirstUpdate = false;
58
+ }
59
+
60
+ Object.assign(container.style, {
61
+ position: strategy
62
+ });
63
+ dom_1.computePosition(target, container, {
64
+ placement,
65
+ middleware,
66
+ strategy
67
+ }).then(({
68
+ x,
69
+ y,
70
+ middlewareData,
71
+ placement: computedPlacement
72
+ }) => {
73
+ utils_1.writeArrowUpdates({
74
+ arrow,
75
+ middlewareData
76
+ });
77
+ utils_1.writeContainerUpdates({
78
+ container,
79
+ middlewareData,
80
+ placement: computedPlacement,
81
+ coordinates: {
82
+ x,
83
+ y
84
+ },
85
+ lowPPI: ((targetWindow === null || targetWindow === void 0 ? void 0 : targetWindow.devicePixelRatio) || 1) <= 1,
86
+ strategy
87
+ });
88
+ }).catch(err => {
89
+ // https://github.com/floating-ui/floating-ui/issues/1845
90
+ // FIXME for node > 14
91
+ // node 15 introduces promise rejection which means that any components
92
+ // tests need to be `it('', async () => {})` otherwise there can be race conditions with
93
+ // JSDOM being torn down before this promise is resolved so globals like `window` and `document` don't exist
94
+ // Unless all tests that ever use `usePositioning` are turned into async tests, any logging during testing
95
+ // will actually be counter productive
96
+ if (process.env.NODE_ENV === 'development') {
97
+ // eslint-disable-next-line no-console
98
+ console.error('[usePositioning]: Failed to calculate position', err);
99
+ }
100
+ });
101
+ };
102
+
103
+ const updatePosition = utils_1.debounce(() => forceUpdate());
104
+
105
+ const dispose = () => {
106
+ if (targetWindow) {
107
+ targetWindow.removeEventListener('scroll', updatePosition);
108
+ targetWindow.removeEventListener('resize', updatePosition);
109
+ }
110
+
111
+ scrollParents.forEach(scrollParent => {
112
+ scrollParent.removeEventListener('scroll', updatePosition);
113
+ });
114
+ };
115
+
116
+ if (targetWindow) {
117
+ targetWindow.addEventListener('scroll', updatePosition);
118
+ targetWindow.addEventListener('resize', updatePosition);
119
+ } // Update the position on initialization
120
+
121
+
122
+ updatePosition();
123
+ return {
124
+ updatePosition,
125
+ dispose
126
+ };
127
+ }
128
+
129
+ exports.createPositionManager = createPositionManager;
130
+ //# sourceMappingURL=createPositionManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["createPositionManager.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,KAAA,gBAAA,OAAA,CAAA,kBAAA,CAAA;;AAGA,MAAA,OAAA,gBAAA,OAAA,CAAA,SAAA,CAAA;AA8BA;;;AAGG;;;AACH,SAAgB,qBAAhB,CAAsC,OAAtC,EAAqE;EACnE,MAAM;IAAE,SAAF;IAAa,MAAb;IAAqB,KAArB;IAA4B,QAA5B;IAAsC,UAAtC;IAAkD;EAAlD,IAAgE,OAAtE;;EACA,IAAI,CAAC,MAAD,IAAW,CAAC,SAAhB,EAA2B;IACzB,OAAO;MACL,cAAc,EAAE,MAAM,SADjB;MAEL,OAAO,EAAE,MAAM;IAFV,CAAP;EAID;;EAED,IAAI,aAAa,GAAG,IAApB;EACA,MAAM,aAAa,GAAqB,IAAI,GAAJ,EAAxC;EACA,MAAM,YAAY,GAAG,SAAS,CAAC,aAAV,CAAwB,WAA7C,CAXmE,CAanE;EACA;;EACA,MAAM,CAAC,MAAP,CAAc,SAAS,CAAC,KAAxB,EAA+B;IAAE,QAAQ,EAAE,OAAZ;IAAqB,IAAI,EAAE,CAA3B;IAA8B,GAAG,EAAE,CAAnC;IAAsC,MAAM,EAAE;EAA9C,CAA/B;;EAEA,MAAM,WAAW,GAAG,MAAK;IACvB,IAAI,aAAJ,EAAmB;MACjB,aAAa,CAAC,GAAd,CAAkB,OAAA,CAAA,eAAA,CAAgB,SAAhB,CAAlB;;MACA,IAAI,MAAM,YAAY,WAAtB,EAAmC;QACjC,aAAa,CAAC,GAAd,CAAkB,OAAA,CAAA,eAAA,CAAgB,MAAhB,CAAlB;MACD;;MAED,aAAa,CAAC,OAAd,C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AjC,EAA2C,cAA3C;IACD;;IAED,aAAa,CAAC,OAAd,CAAsB,YAAY,IAAG;MACnC,YAAY,CAAC,mBAAb,CAAiC,QAAjC,EAA2C,cAA3C;IACD,CAFD;EAGD,CATD;;EAWA,IAAI,YAAJ,EAAkB;IAChB,YAAY,CAAC,gBAAb,CAA8B,QAA9B,EAAwC,cAAxC;IACA,YAAY,CAAC,gBAAb,CAA8B,QAA9B,EAAwC,cAAxC;EACD,CA3EkE,CA6EnE;;;EACA,cAAc;EAEd,OAAO;IACL,cADK;IAEL;EAFK,CAAP;AAID;;AApFD,OAAA,CAAA,qBAAA,GAAA,qBAAA","sourcesContent":["import { computePosition } from '@floating-ui/dom';\nimport type { Middleware, Placement, Strategy } from '@floating-ui/dom';\nimport type { PositionManager, TargetElement } from './types';\nimport { debounce, writeArrowUpdates, writeContainerUpdates, getScrollParent } from './utils';\n\ninterface PositionManagerOptions {\n /**\n * The positioned element\n */\n container: HTMLElement;\n /**\n * Element that the container will be anchored to\n */\n target: TargetElement;\n /**\n * Arrow that points from the container to the target\n */\n arrow: HTMLElement | null;\n /**\n * The value of the css `position` property\n * @default absolute\n */\n strategy: Strategy;\n /**\n * [Floating UI middleware](https://floating-ui.com/docs/middleware)\n */\n middleware: Middleware[];\n /**\n * [Floating UI placement](https://floating-ui.com/docs/computePosition#placement)\n */\n placement?: Placement;\n}\n\n/**\n * @internal\n * @returns manager that handles positioning out of the react lifecycle\n */\nexport function createPositionManager(options: PositionManagerOptions): PositionManager {\n const { container, target, arrow, strategy, middleware, placement } = options;\n if (!target || !container) {\n return {\n updatePosition: () => undefined,\n dispose: () => undefined,\n };\n }\n\n let isFirstUpdate = true;\n const scrollParents: Set<HTMLElement> = new Set<HTMLElement>();\n const targetWindow = container.ownerDocument.defaultView;\n\n // When the container is first resolved, set position `fixed` to avoid scroll jumps.\n // Without this scroll jumps can occur when the element is rendered initially and receives focus\n Object.assign(container.style, { position: 'fixed', left: 0, top: 0, margin: 0 });\n\n const forceUpdate = () => {\n if (isFirstUpdate) {\n scrollParents.add(getScrollParent(container));\n if (target instanceof HTMLElement) {\n scrollParents.add(getScrollParent(target));\n }\n\n scrollParents.forEach(scrollParent => {\n scrollParent.addEventListener('scroll', updatePosition);\n });\n\n isFirstUpdate = false;\n }\n\n Object.assign(container.style, { position: strategy });\n computePosition(target, container, { placement, middleware, strategy })\n .then(({ x, y, middlewareData, placement: computedPlacement }) => {\n writeArrowUpdates({ arrow, middlewareData });\n writeContainerUpdates({\n container,\n middlewareData,\n placement: computedPlacement,\n coordinates: { x, y },\n lowPPI: (targetWindow?.devicePixelRatio || 1) <= 1,\n strategy,\n });\n })\n .catch(err => {\n // https://github.com/floating-ui/floating-ui/issues/1845\n // FIXME for node > 14\n // node 15 introduces promise rejection which means that any components\n // tests need to be `it('', async () => {})` otherwise there can be race conditions with\n // JSDOM being torn down before this promise is resolved so globals like `window` and `document` don't exist\n // Unless all tests that ever use `usePositioning` are turned into async tests, any logging during testing\n // will actually be counter productive\n if (process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.error('[usePositioning]: Failed to calculate position', err);\n }\n });\n };\n\n const updatePosition = debounce(() => forceUpdate());\n\n const dispose = () => {\n if (targetWindow) {\n targetWindow.removeEventListener('scroll', updatePosition);\n targetWindow.removeEventListener('resize', updatePosition);\n }\n\n scrollParents.forEach(scrollParent => {\n scrollParent.removeEventListener('scroll', updatePosition);\n });\n };\n\n if (targetWindow) {\n targetWindow.addEventListener('scroll', updatePosition);\n targetWindow.addEventListener('resize', updatePosition);\n }\n\n // Update the position on initialization\n updatePosition();\n\n return {\n updatePosition,\n dispose,\n };\n}\n"],"sourceRoot":"../src/"}