@fluentui/react-positioning 0.0.0-nightly-20221101-0424.1 → 0.0.0-nightly-20221103-0420.1

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.
package/CHANGELOG.json CHANGED
@@ -2,9 +2,9 @@
2
2
  "name": "@fluentui/react-positioning",
3
3
  "entries": [
4
4
  {
5
- "date": "Tue, 01 Nov 2022 04:31:16 GMT",
6
- "tag": "@fluentui/react-positioning_v0.0.0-nightly-20221101-0424.1",
7
- "version": "0.0.0-nightly-20221101-0424.1",
5
+ "date": "Thu, 03 Nov 2022 04:27:05 GMT",
6
+ "tag": "@fluentui/react-positioning_v0.0.0-nightly-20221103-0420.1",
7
+ "version": "0.0.0-nightly-20221103-0420.1",
8
8
  "comments": {
9
9
  "prerelease": [
10
10
  {
@@ -16,20 +16,61 @@
16
16
  {
17
17
  "author": "beachball",
18
18
  "package": "@fluentui/react-positioning",
19
- "comment": "Bump @fluentui/react-shared-contexts to v0.0.0-nightly-20221101-0424.1",
20
- "commit": "9743baf64ba7f7abe5279916628c8ac93b69b575"
19
+ "comment": "Bump @fluentui/react-shared-contexts to v0.0.0-nightly-20221103-0420.1",
20
+ "commit": "458da9a232ec82c72a8b412fc0281ccca0ba2f4d"
21
21
  },
22
22
  {
23
23
  "author": "beachball",
24
24
  "package": "@fluentui/react-positioning",
25
- "comment": "Bump @fluentui/react-theme to v0.0.0-nightly-20221101-0424.1",
26
- "commit": "9743baf64ba7f7abe5279916628c8ac93b69b575"
25
+ "comment": "Bump @fluentui/react-theme to v0.0.0-nightly-20221103-0420.1",
26
+ "commit": "458da9a232ec82c72a8b412fc0281ccca0ba2f4d"
27
27
  },
28
28
  {
29
29
  "author": "beachball",
30
30
  "package": "@fluentui/react-positioning",
31
- "comment": "Bump @fluentui/react-utilities to v0.0.0-nightly-20221101-0424.1",
32
- "commit": "9743baf64ba7f7abe5279916628c8ac93b69b575"
31
+ "comment": "Bump @fluentui/react-utilities to v0.0.0-nightly-20221103-0420.1",
32
+ "commit": "458da9a232ec82c72a8b412fc0281ccca0ba2f4d"
33
+ }
34
+ ]
35
+ }
36
+ },
37
+ {
38
+ "date": "Wed, 02 Nov 2022 11:57:57 GMT",
39
+ "tag": "@fluentui/react-positioning_v9.3.0",
40
+ "version": "9.3.0",
41
+ "comments": {
42
+ "minor": [
43
+ {
44
+ "author": "bernardo.sunderhus@gmail.com",
45
+ "package": "@fluentui/react-positioning",
46
+ "commit": "acbfdcac87c7626542838000f34dbf2283c10f4e",
47
+ "comment": "exposes new typings to avoid exposing internal methods"
48
+ },
49
+ {
50
+ "author": "lingfangao@hotmail.com",
51
+ "package": "@fluentui/react-positioning",
52
+ "commit": "393a7b169155997a97a6ce1ad36ee3eafc6f461b",
53
+ "comment": "feat: position updates are handled out of react lifecycle"
54
+ },
55
+ {
56
+ "author": "beachball",
57
+ "package": "@fluentui/react-positioning",
58
+ "comment": "Bump @fluentui/react-shared-contexts to v9.1.0",
59
+ "commit": "393a7b169155997a97a6ce1ad36ee3eafc6f461b"
60
+ },
61
+ {
62
+ "author": "beachball",
63
+ "package": "@fluentui/react-positioning",
64
+ "comment": "Bump @fluentui/react-utilities to v9.2.0",
65
+ "commit": "393a7b169155997a97a6ce1ad36ee3eafc6f461b"
66
+ }
67
+ ],
68
+ "patch": [
69
+ {
70
+ "author": "olfedias@microsoft.com",
71
+ "package": "@fluentui/react-positioning",
72
+ "commit": "c619ba066f11a8654e95375314824ba6b81b003e",
73
+ "comment": "chore: Update Griffel to latest version"
33
74
  }
34
75
  ]
35
76
  }
package/CHANGELOG.md CHANGED
@@ -1,20 +1,36 @@
1
1
  # Change Log - @fluentui/react-positioning
2
2
 
3
- This log was last generated on Tue, 01 Nov 2022 04:31:16 GMT and should not be manually modified.
3
+ This log was last generated on Thu, 03 Nov 2022 04:27:05 GMT and should not be manually modified.
4
4
 
5
5
  <!-- Start content -->
6
6
 
7
- ## [0.0.0-nightly-20221101-0424.1](https://github.com/microsoft/fluentui/tree/@fluentui/react-positioning_v0.0.0-nightly-20221101-0424.1)
7
+ ## [0.0.0-nightly-20221103-0420.1](https://github.com/microsoft/fluentui/tree/@fluentui/react-positioning_v0.0.0-nightly-20221103-0420.1)
8
8
 
9
- Tue, 01 Nov 2022 04:31:16 GMT
10
- [Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-positioning_v9.2.2..@fluentui/react-positioning_v0.0.0-nightly-20221101-0424.1)
9
+ Thu, 03 Nov 2022 04:27:05 GMT
10
+ [Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-positioning_v9.3.0..@fluentui/react-positioning_v0.0.0-nightly-20221103-0420.1)
11
11
 
12
12
  ### Changes
13
13
 
14
14
  - Release nightly v9 ([commit](https://github.com/microsoft/fluentui/commit/not available) by fluentui-internal@service.microsoft.com)
15
- - Bump @fluentui/react-shared-contexts to v0.0.0-nightly-20221101-0424.1 ([commit](https://github.com/microsoft/fluentui/commit/9743baf64ba7f7abe5279916628c8ac93b69b575) by beachball)
16
- - Bump @fluentui/react-theme to v0.0.0-nightly-20221101-0424.1 ([commit](https://github.com/microsoft/fluentui/commit/9743baf64ba7f7abe5279916628c8ac93b69b575) by beachball)
17
- - Bump @fluentui/react-utilities to v0.0.0-nightly-20221101-0424.1 ([commit](https://github.com/microsoft/fluentui/commit/9743baf64ba7f7abe5279916628c8ac93b69b575) by beachball)
15
+ - Bump @fluentui/react-shared-contexts to v0.0.0-nightly-20221103-0420.1 ([commit](https://github.com/microsoft/fluentui/commit/458da9a232ec82c72a8b412fc0281ccca0ba2f4d) by beachball)
16
+ - Bump @fluentui/react-theme to v0.0.0-nightly-20221103-0420.1 ([commit](https://github.com/microsoft/fluentui/commit/458da9a232ec82c72a8b412fc0281ccca0ba2f4d) by beachball)
17
+ - Bump @fluentui/react-utilities to v0.0.0-nightly-20221103-0420.1 ([commit](https://github.com/microsoft/fluentui/commit/458da9a232ec82c72a8b412fc0281ccca0ba2f4d) by beachball)
18
+
19
+ ## [9.3.0](https://github.com/microsoft/fluentui/tree/@fluentui/react-positioning_v9.3.0)
20
+
21
+ Wed, 02 Nov 2022 11:57:57 GMT
22
+ [Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-positioning_v9.2.2..@fluentui/react-positioning_v9.3.0)
23
+
24
+ ### Minor changes
25
+
26
+ - exposes new typings to avoid exposing internal methods ([PR #25407](https://github.com/microsoft/fluentui/pull/25407) by bernardo.sunderhus@gmail.com)
27
+ - feat: position updates are handled out of react lifecycle ([PR #25456](https://github.com/microsoft/fluentui/pull/25456) by lingfangao@hotmail.com)
28
+ - Bump @fluentui/react-shared-contexts to v9.1.0 ([PR #25456](https://github.com/microsoft/fluentui/pull/25456) by beachball)
29
+ - Bump @fluentui/react-utilities to v9.2.0 ([PR #25456](https://github.com/microsoft/fluentui/pull/25456) by beachball)
30
+
31
+ ### Patches
32
+
33
+ - chore: Update Griffel to latest version ([PR #25412](https://github.com/microsoft/fluentui/pull/25412) by olfedias@microsoft.com)
18
34
 
19
35
  ## [9.2.2](https://github.com/microsoft/fluentui/tree/@fluentui/react-positioning_v9.2.2)
20
36
 
package/dist/index.d.ts CHANGED
@@ -123,7 +123,7 @@ export declare type PositioningImperativeRef = {
123
123
  * Sets the target and updates positioning imperatively.
124
124
  * Useful for avoiding double renders with the target option.
125
125
  */
126
- setTarget: (target: HTMLElement | PositioningVirtualElement) => void;
126
+ setTarget: (target: TargetElement) => void;
127
127
  };
128
128
 
129
129
  declare interface PositioningOptions {
@@ -186,7 +186,7 @@ export declare interface PositioningProps extends Omit<PositioningOptions, 'posi
186
186
  /**
187
187
  * Manual override for the target element. Useful for scenarios where a component accepts user prop to override target
188
188
  */
189
- target?: HTMLElement | PositioningVirtualElement | null;
189
+ target?: TargetElement | null;
190
190
  }
191
191
 
192
192
  export declare type PositioningShorthand = PositioningProps | PositioningShorthandValue;
@@ -218,14 +218,12 @@ export declare function resolvePositioningShorthand(shorthand: PositioningShorth
218
218
 
219
219
  export declare type SetVirtualMouseTarget = (event: React_2.MouseEvent | MouseEvent | undefined | null) => void;
220
220
 
221
+ declare type TargetElement = HTMLElement | PositioningVirtualElement;
222
+
221
223
  /**
222
224
  * @internal
223
225
  */
224
- export declare function usePositioning(options: UsePositioningOptions): {
225
- targetRef: React_2.MutableRefObject<any>;
226
- containerRef: React_2.MutableRefObject<any>;
227
- arrowRef: React_2.MutableRefObject<any>;
228
- };
226
+ export declare function usePositioning(options: UsePositioningOptions): UsePositioningReturn;
229
227
 
230
228
  /**
231
229
  * @internal
@@ -245,4 +243,10 @@ declare interface UsePositioningOptions extends PositioningProps {
245
243
  enabled?: boolean;
246
244
  }
247
245
 
246
+ declare interface UsePositioningReturn {
247
+ targetRef: React_2.MutableRefObject<any>;
248
+ containerRef: React_2.MutableRefObject<any>;
249
+ arrowRef: React_2.MutableRefObject<any>;
250
+ }
251
+
248
252
  export { }
@@ -0,0 +1,119 @@
1
+ import { computePosition } from '@floating-ui/dom';
2
+ import { debounce, writeArrowUpdates, writeContainerUpdates, getScrollParent } from './utils';
3
+ /**
4
+ * @internal
5
+ * @returns manager that handles positioning out of the react lifecycle
6
+ */
7
+
8
+ export function createPositionManager(options) {
9
+ const {
10
+ container,
11
+ target,
12
+ arrow,
13
+ strategy,
14
+ middleware,
15
+ placement
16
+ } = options;
17
+
18
+ if (!target || !container) {
19
+ return {
20
+ updatePosition: () => undefined,
21
+ dispose: () => undefined
22
+ };
23
+ }
24
+
25
+ let isFirstUpdate = true;
26
+ const scrollParents = new Set();
27
+ const targetWindow = container.ownerDocument.defaultView; // When the container is first resolved, set position `fixed` to avoid scroll jumps.
28
+ // Without this scroll jumps can occur when the element is rendered initially and receives focus
29
+
30
+ Object.assign(container.style, {
31
+ position: 'fixed',
32
+ left: 0,
33
+ top: 0,
34
+ margin: 0
35
+ });
36
+
37
+ const forceUpdate = () => {
38
+ if (isFirstUpdate) {
39
+ scrollParents.add(getScrollParent(container));
40
+
41
+ if (target instanceof HTMLElement) {
42
+ scrollParents.add(getScrollParent(target));
43
+ }
44
+
45
+ scrollParents.forEach(scrollParent => {
46
+ scrollParent.addEventListener('scroll', updatePosition);
47
+ });
48
+ isFirstUpdate = false;
49
+ }
50
+
51
+ Object.assign(container.style, {
52
+ position: strategy
53
+ });
54
+ computePosition(target, container, {
55
+ placement,
56
+ middleware,
57
+ strategy
58
+ }).then(({
59
+ x,
60
+ y,
61
+ middlewareData,
62
+ placement: computedPlacement
63
+ }) => {
64
+ writeArrowUpdates({
65
+ arrow,
66
+ middlewareData
67
+ });
68
+ writeContainerUpdates({
69
+ container,
70
+ middlewareData,
71
+ placement: computedPlacement,
72
+ coordinates: {
73
+ x,
74
+ y
75
+ },
76
+ lowPPI: ((targetWindow === null || targetWindow === void 0 ? void 0 : targetWindow.devicePixelRatio) || 1) <= 1,
77
+ strategy
78
+ });
79
+ }).catch(err => {
80
+ // https://github.com/floating-ui/floating-ui/issues/1845
81
+ // FIXME for node > 14
82
+ // node 15 introduces promise rejection which means that any components
83
+ // tests need to be `it('', async () => {})` otherwise there can be race conditions with
84
+ // JSDOM being torn down before this promise is resolved so globals like `window` and `document` don't exist
85
+ // Unless all tests that ever use `usePositioning` are turned into async tests, any logging during testing
86
+ // will actually be counter productive
87
+ if (process.env.NODE_ENV === 'development') {
88
+ // eslint-disable-next-line no-console
89
+ console.error('[usePositioning]: Failed to calculate position', err);
90
+ }
91
+ });
92
+ };
93
+
94
+ const updatePosition = debounce(() => forceUpdate());
95
+
96
+ const dispose = () => {
97
+ if (targetWindow) {
98
+ targetWindow.removeEventListener('scroll', updatePosition);
99
+ targetWindow.removeEventListener('resize', updatePosition);
100
+ }
101
+
102
+ scrollParents.forEach(scrollParent => {
103
+ scrollParent.removeEventListener('scroll', updatePosition);
104
+ });
105
+ };
106
+
107
+ if (targetWindow) {
108
+ targetWindow.addEventListener('scroll', updatePosition);
109
+ targetWindow.addEventListener('resize', updatePosition);
110
+ } // Update the position on initialization
111
+
112
+
113
+ updatePosition();
114
+ return {
115
+ updatePosition,
116
+ dispose
117
+ };
118
+ }
119
+ //# sourceMappingURL=createPositionManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/createPositionManager.ts"],"names":[],"mappings":"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","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":""}
package/lib/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import * as React from 'react';\n\ntype Rect = {\n width: number;\n height: number;\n x: number;\n y: number;\n};\n\nexport type OffsetFunctionParam = {\n positionedRect: Rect;\n targetRect: Rect;\n position: Position;\n alignment?: Alignment;\n};\n\nexport type OffsetObject = { crossAxis?: number; mainAxis: number };\n\nexport type OffsetShorthand = number;\n\nexport type OffsetFunction = (param: OffsetFunctionParam) => OffsetObject | OffsetShorthand;\n\nexport type Offset = OffsetFunction | OffsetObject | OffsetShorthand;\n\nexport type Position = 'above' | 'below' | 'before' | 'after';\nexport type Alignment = 'top' | 'bottom' | 'start' | 'end' | 'center';\n\nexport type AutoSize = 'height' | 'height-always' | 'width' | 'width-always' | 'always' | boolean;\n\nexport type Boundary = HTMLElement | Array<HTMLElement> | 'clippingParents' | 'scrollParent' | 'window';\n\nexport type PositioningImperativeRef = {\n /**\n * Updates the position imperatively.\n * Useful when the position of the target changes from other factors than scrolling of window resize.\n */\n updatePosition: () => void;\n\n /**\n * Sets the target and updates positioning imperatively.\n * Useful for avoiding double renders with the target option.\n */\n setTarget: (target: HTMLElement | PositioningVirtualElement) => void;\n};\n\nexport type PositioningVirtualElement = {\n getBoundingClientRect: () => {\n x: number;\n y: number;\n top: number;\n left: number;\n bottom: number;\n right: number;\n width: number;\n height: number;\n };\n contextElement?: Element;\n};\n\nexport type SetVirtualMouseTarget = (event: React.MouseEvent | MouseEvent | undefined | null) => void;\n\nexport interface PositioningOptions {\n /** Alignment for the component. Only has an effect if used with the @see position option */\n align?: Alignment;\n\n /** The element which will define the boundaries of the positioned element for the flip behavior. */\n flipBoundary?: Boundary | null;\n\n /** The element which will define the boundaries of the positioned element for the overflow behavior. */\n overflowBoundary?: Boundary | null;\n\n /**\n * Position for the component. Position has higher priority than align. If position is vertical ('above' | 'below')\n * and align is also vertical ('top' | 'bottom') or if both position and align are horizontal ('before' | 'after'\n * and 'start' | 'end' respectively),\n * then provided value for 'align' will be ignored and 'center' will be used instead.\n */\n position?: Position;\n\n /**\n * Enables the position element to be positioned with 'fixed' (default value is position: 'absolute')\n * @default false\n */\n positionFixed?: boolean;\n\n /**\n * Lets you displace a positioned element from its reference element.\n * This can be useful if you need to apply some margin between them or if you need to fine tune the\n * position according to some custom logic.\n */\n offset?: Offset;\n\n /**\n * Defines padding between the corner of the popup element and the arrow.\n * Use to prevent the arrow from overlapping a rounded corner, for example.\n */\n arrowPadding?: number;\n\n /**\n * Applies max-height and max-width on the positioned element to fit it within the available space in viewport.\n * true enables this for both width and height when overflow happens.\n * 'always' applies `max-height`/`max-width` regardless of overflow.\n * 'height' applies `max-height` when overflow happens, and 'width' for `max-width`\n * `height-always` applies `max-height` regardless of overflow, and 'width-always' for always applying `max-width`\n */\n autoSize?: AutoSize;\n\n /**\n * Modifies position and alignment to cover the target\n */\n coverTarget?: boolean;\n\n /**\n * Disables automatic repositioning of the component; it will always be placed according to the values of `align` and\n * `position` props, regardless of the size of the component, the reference element or the viewport.\n */\n pinned?: boolean;\n\n /**\n * When the reference element or the viewport is outside viewport allows a positioned element to be fully in viewport.\n * \"all\" enables this behavior for all axis.\n */\n // eslint-disable-next-line @typescript-eslint/naming-convention\n unstable_disableTether?: boolean | 'all';\n}\n\nexport interface PositioningProps\n // \"positionFixed\" & \"unstable_disableTether\" are not exported as public API (yet)\n extends Omit<PositioningOptions, 'positionFixed' | 'unstable_disableTether'> {\n /** An imperative handle to Popper methods. */\n positioningRef?: React.Ref<PositioningImperativeRef>;\n\n /**\n * Manual override for the target element. Useful for scenarios where a component accepts user prop to override target\n */\n target?: HTMLElement | PositioningVirtualElement | null;\n}\n\nexport type PositioningShorthandValue =\n | 'above'\n | 'above-start'\n | 'above-end'\n | 'below'\n | 'below-start'\n | 'below-end'\n | 'before'\n | 'before-top'\n | 'before-bottom'\n | 'after'\n | 'after-top'\n | 'after-bottom';\n\nexport type PositioningShorthand = PositioningProps | PositioningShorthandValue;\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import * as React from 'react';\n\ntype Rect = {\n width: number;\n height: number;\n x: number;\n y: number;\n};\n\nexport type OffsetFunctionParam = {\n positionedRect: Rect;\n targetRect: Rect;\n position: Position;\n alignment?: Alignment;\n};\n\nexport type TargetElement = HTMLElement | PositioningVirtualElement;\n\n/**\n * @internal\n */\nexport interface UsePositioningOptions extends PositioningProps {\n /**\n * If false, does not position anything\n */\n enabled?: boolean;\n}\n\n/**\n * @internal\n */\nexport interface PositionManager {\n updatePosition: () => void;\n dispose: () => void;\n}\n\nexport interface UsePositioningReturn {\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\nexport type OffsetObject = { crossAxis?: number; mainAxis: number };\n\nexport type OffsetShorthand = number;\n\nexport type OffsetFunction = (param: OffsetFunctionParam) => OffsetObject | OffsetShorthand;\n\nexport type Offset = OffsetFunction | OffsetObject | OffsetShorthand;\n\nexport type Position = 'above' | 'below' | 'before' | 'after';\nexport type Alignment = 'top' | 'bottom' | 'start' | 'end' | 'center';\n\nexport type AutoSize = 'height' | 'height-always' | 'width' | 'width-always' | 'always' | boolean;\n\nexport type Boundary = HTMLElement | Array<HTMLElement> | 'clippingParents' | 'scrollParent' | 'window';\n\nexport type PositioningImperativeRef = {\n /**\n * Updates the position imperatively.\n * Useful when the position of the target changes from other factors than scrolling of window resize.\n */\n updatePosition: () => void;\n\n /**\n * Sets the target and updates positioning imperatively.\n * Useful for avoiding double renders with the target option.\n */\n setTarget: (target: TargetElement) => void;\n};\n\nexport type PositioningVirtualElement = {\n getBoundingClientRect: () => {\n x: number;\n y: number;\n top: number;\n left: number;\n bottom: number;\n right: number;\n width: number;\n height: number;\n };\n contextElement?: Element;\n};\n\nexport type SetVirtualMouseTarget = (event: React.MouseEvent | MouseEvent | undefined | null) => void;\n\nexport interface PositioningOptions {\n /** Alignment for the component. Only has an effect if used with the @see position option */\n align?: Alignment;\n\n /** The element which will define the boundaries of the positioned element for the flip behavior. */\n flipBoundary?: Boundary | null;\n\n /** The element which will define the boundaries of the positioned element for the overflow behavior. */\n overflowBoundary?: Boundary | null;\n\n /**\n * Position for the component. Position has higher priority than align. If position is vertical ('above' | 'below')\n * and align is also vertical ('top' | 'bottom') or if both position and align are horizontal ('before' | 'after'\n * and 'start' | 'end' respectively),\n * then provided value for 'align' will be ignored and 'center' will be used instead.\n */\n position?: Position;\n\n /**\n * Enables the position element to be positioned with 'fixed' (default value is position: 'absolute')\n * @default false\n */\n positionFixed?: boolean;\n\n /**\n * Lets you displace a positioned element from its reference element.\n * This can be useful if you need to apply some margin between them or if you need to fine tune the\n * position according to some custom logic.\n */\n offset?: Offset;\n\n /**\n * Defines padding between the corner of the popup element and the arrow.\n * Use to prevent the arrow from overlapping a rounded corner, for example.\n */\n arrowPadding?: number;\n\n /**\n * Applies max-height and max-width on the positioned element to fit it within the available space in viewport.\n * true enables this for both width and height when overflow happens.\n * 'always' applies `max-height`/`max-width` regardless of overflow.\n * 'height' applies `max-height` when overflow happens, and 'width' for `max-width`\n * `height-always` applies `max-height` regardless of overflow, and 'width-always' for always applying `max-width`\n */\n autoSize?: AutoSize;\n\n /**\n * Modifies position and alignment to cover the target\n */\n coverTarget?: boolean;\n\n /**\n * Disables automatic repositioning of the component; it will always be placed according to the values of `align` and\n * `position` props, regardless of the size of the component, the reference element or the viewport.\n */\n pinned?: boolean;\n\n /**\n * When the reference element or the viewport is outside viewport allows a positioned element to be fully in viewport.\n * \"all\" enables this behavior for all axis.\n */\n // eslint-disable-next-line @typescript-eslint/naming-convention\n unstable_disableTether?: boolean | 'all';\n}\n\nexport interface PositioningProps\n // \"positionFixed\" & \"unstable_disableTether\" are not exported as public API (yet)\n extends Omit<PositioningOptions, 'positionFixed' | 'unstable_disableTether'> {\n /** An imperative handle to Popper methods. */\n positioningRef?: React.Ref<PositioningImperativeRef>;\n\n /**\n * Manual override for the target element. Useful for scenarios where a component accepts user prop to override target\n */\n target?: TargetElement | null;\n}\n\nexport type PositioningShorthandValue =\n | 'above'\n | 'above-start'\n | 'above-end'\n | 'below'\n | 'below-start'\n | 'below-end'\n | 'before'\n | 'before-top'\n | 'before-bottom'\n | 'after'\n | 'after-top'\n | 'after-bottom';\n\nexport type PositioningShorthand = PositioningProps | PositioningShorthandValue;\n"]}
@@ -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