@wordpress/components 21.1.0 → 21.1.2-next.4d3b314fd5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/build/draggable/index.js +3 -6
- package/build/draggable/index.js.map +1 -1
- package/build/index.js +6 -0
- package/build/index.js.map +1 -1
- package/build/menu-item/index.js +4 -3
- package/build/menu-item/index.js.map +1 -1
- package/build/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js +10 -3
- package/build/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js.map +1 -1
- package/build/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js +12 -3
- package/build/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js.map +1 -1
- package/build/mobile/bottom-sheet/sub-sheet/index.native.js +4 -1
- package/build/mobile/bottom-sheet/sub-sheet/index.native.js.map +1 -1
- package/build/mobile/color-settings/index.native.js +3 -1
- package/build/mobile/color-settings/index.native.js.map +1 -1
- package/build/mobile/color-settings/picker-screen.native.js +3 -1
- package/build/mobile/color-settings/picker-screen.native.js.map +1 -1
- package/build/mobile/image/index.native.js +3 -1
- package/build/mobile/image/index.native.js.map +1 -1
- package/build/mobile/keyboard-avoiding-view/index.ios.js +3 -1
- package/build/mobile/keyboard-avoiding-view/index.ios.js.map +1 -1
- package/build/mobile/link-picker/index.native.js +3 -1
- package/build/mobile/link-picker/index.native.js.map +1 -1
- package/build/mobile/link-picker/link-picker-results.native.js +3 -1
- package/build/mobile/link-picker/link-picker-results.native.js.map +1 -1
- package/build/mobile/link-picker/link-picker-screen.native.js +3 -1
- package/build/mobile/link-picker/link-picker-screen.native.js.map +1 -1
- package/build/mobile/link-settings/index.native.js +24 -6
- package/build/mobile/link-settings/index.native.js.map +1 -1
- package/build/mobile/link-settings/link-settings-screen.native.js +3 -1
- package/build/mobile/link-settings/link-settings-screen.native.js.map +1 -1
- package/build/mobile/segmented-control/index.native.js +6 -2
- package/build/mobile/segmented-control/index.native.js.map +1 -1
- package/build/mobile/utils/use-unit-converter-to-mobile.native.js +6 -2
- package/build/mobile/utils/use-unit-converter-to-mobile.native.js.map +1 -1
- package/build/notice/index.native.js +15 -19
- package/build/notice/index.native.js.map +1 -1
- package/build/notice/list.native.js +2 -3
- package/build/notice/list.native.js.map +1 -1
- package/build/palette-edit/index.js +1 -1
- package/build/palette-edit/index.js.map +1 -1
- package/build/popover/index.js +29 -32
- package/build/popover/index.js.map +1 -1
- package/build/popover/limit-shift.js +145 -0
- package/build/popover/limit-shift.js.map +1 -0
- package/build/popover/utils.js +55 -15
- package/build/popover/utils.js.map +1 -1
- package/build/tools-panel/styles.js +27 -12
- package/build/tools-panel/styles.js.map +1 -1
- package/build/tools-panel/tools-panel-header/component.js +19 -6
- package/build/tools-panel/tools-panel-header/component.js.map +1 -1
- package/build/tools-panel/tools-panel-header/hook.js +4 -0
- package/build/tools-panel/tools-panel-header/hook.js.map +1 -1
- package/build-module/draggable/index.js +2 -5
- package/build-module/draggable/index.js.map +1 -1
- package/build-module/index.js +1 -1
- package/build-module/index.js.map +1 -1
- package/build-module/menu-item/index.js +4 -3
- package/build-module/menu-item/index.js.map +1 -1
- package/build-module/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js +10 -3
- package/build-module/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js.map +1 -1
- package/build-module/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js +12 -3
- package/build-module/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js.map +1 -1
- package/build-module/mobile/bottom-sheet/sub-sheet/index.native.js +4 -1
- package/build-module/mobile/bottom-sheet/sub-sheet/index.native.js.map +1 -1
- package/build-module/mobile/color-settings/index.native.js +3 -1
- package/build-module/mobile/color-settings/index.native.js.map +1 -1
- package/build-module/mobile/color-settings/picker-screen.native.js +3 -1
- package/build-module/mobile/color-settings/picker-screen.native.js.map +1 -1
- package/build-module/mobile/image/index.native.js +3 -1
- package/build-module/mobile/image/index.native.js.map +1 -1
- package/build-module/mobile/keyboard-avoiding-view/index.ios.js +3 -1
- package/build-module/mobile/keyboard-avoiding-view/index.ios.js.map +1 -1
- package/build-module/mobile/link-picker/index.native.js +3 -1
- package/build-module/mobile/link-picker/index.native.js.map +1 -1
- package/build-module/mobile/link-picker/link-picker-results.native.js +3 -1
- package/build-module/mobile/link-picker/link-picker-results.native.js.map +1 -1
- package/build-module/mobile/link-picker/link-picker-screen.native.js +3 -1
- package/build-module/mobile/link-picker/link-picker-screen.native.js.map +1 -1
- package/build-module/mobile/link-settings/index.native.js +24 -6
- package/build-module/mobile/link-settings/index.native.js.map +1 -1
- package/build-module/mobile/link-settings/link-settings-screen.native.js +3 -1
- package/build-module/mobile/link-settings/link-settings-screen.native.js.map +1 -1
- package/build-module/mobile/segmented-control/index.native.js +6 -2
- package/build-module/mobile/segmented-control/index.native.js.map +1 -1
- package/build-module/mobile/utils/use-unit-converter-to-mobile.native.js +6 -2
- package/build-module/mobile/utils/use-unit-converter-to-mobile.native.js.map +1 -1
- package/build-module/notice/index.native.js +16 -21
- package/build-module/notice/index.native.js.map +1 -1
- package/build-module/notice/list.native.js +3 -3
- package/build-module/notice/list.native.js.map +1 -1
- package/build-module/palette-edit/index.js +1 -1
- package/build-module/palette-edit/index.js.map +1 -1
- package/build-module/popover/index.js +31 -35
- package/build-module/popover/index.js.map +1 -1
- package/build-module/popover/limit-shift.js +136 -0
- package/build-module/popover/limit-shift.js.map +1 -0
- package/build-module/popover/utils.js +55 -15
- package/build-module/popover/utils.js.map +1 -1
- package/build-module/tools-panel/styles.js +23 -12
- package/build-module/tools-panel/styles.js.map +1 -1
- package/build-module/tools-panel/tools-panel-header/component.js +19 -7
- package/build-module/tools-panel/tools-panel-header/component.js.map +1 -1
- package/build-module/tools-panel/tools-panel-header/hook.js +4 -0
- package/build-module/tools-panel/tools-panel-header/hook.js.map +1 -1
- package/build-style/style-rtl.css +13 -2
- package/build-style/style.css +13 -2
- package/build-types/draggable/index.d.ts.map +1 -1
- package/build-types/external-link/styles/external-link-styles.d.ts +1 -1
- package/build-types/focal-point-picker/styles/focal-point-style.d.ts +3 -3
- package/build-types/menu-item/index.d.ts.map +1 -1
- package/build-types/popover/index.d.ts.map +1 -1
- package/build-types/popover/limit-shift.d.ts +87 -0
- package/build-types/popover/limit-shift.d.ts.map +1 -0
- package/build-types/popover/test/index.d.ts +2 -0
- package/build-types/popover/test/index.d.ts.map +1 -0
- package/build-types/popover/types.d.ts +1 -1
- package/build-types/popover/types.d.ts.map +1 -1
- package/build-types/popover/utils.d.ts.map +1 -1
- package/build-types/range-control/types.d.ts +0 -32
- package/build-types/range-control/types.d.ts.map +1 -1
- package/build-types/spinner/index.d.ts +1 -1
- package/build-types/tools-panel/styles.d.ts +6 -0
- package/build-types/tools-panel/styles.d.ts.map +1 -1
- package/build-types/tools-panel/tools-panel-header/component.d.ts.map +1 -1
- package/build-types/tools-panel/tools-panel-header/hook.d.ts +1 -0
- package/build-types/tools-panel/tools-panel-header/hook.d.ts.map +1 -1
- package/build-types/tools-panel/types.d.ts +1 -0
- package/build-types/tools-panel/types.d.ts.map +1 -1
- package/package.json +17 -17
- package/src/draggable/index.js +2 -5
- package/src/index.js +1 -1
- package/src/menu-item/README.md +7 -0
- package/src/menu-item/index.js +11 -5
- package/src/menu-item/style.scss +1 -0
- package/src/menu-item/test/index.js +36 -0
- package/src/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js +9 -0
- package/src/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js +11 -0
- package/src/mobile/bottom-sheet/sub-sheet/index.native.js +3 -0
- package/src/mobile/color-settings/index.native.js +3 -0
- package/src/mobile/color-settings/picker-screen.native.js +3 -0
- package/src/mobile/image/index.native.js +3 -0
- package/src/mobile/keyboard-avoiding-view/index.ios.js +3 -0
- package/src/mobile/link-picker/index.native.js +3 -0
- package/src/mobile/link-picker/link-picker-results.native.js +3 -0
- package/src/mobile/link-picker/link-picker-screen.native.js +3 -0
- package/src/mobile/link-settings/index.native.js +18 -0
- package/src/mobile/link-settings/link-settings-screen.native.js +3 -0
- package/src/mobile/segmented-control/index.native.js +6 -0
- package/src/mobile/utils/use-unit-converter-to-mobile.native.js +6 -0
- package/src/notice/index.native.js +17 -20
- package/src/notice/list.native.js +7 -3
- package/src/palette-edit/index.js +1 -1
- package/src/placeholder/style.scss +1 -1
- package/src/popover/index.tsx +26 -42
- package/src/popover/limit-shift.ts +205 -0
- package/src/popover/test/index.tsx +230 -0
- package/src/popover/types.ts +1 -0
- package/src/popover/utils.ts +58 -16
- package/src/range-control/types.ts +0 -33
- package/src/tools-panel/stories/index.js +27 -0
- package/src/tools-panel/styles.ts +28 -1
- package/src/tools-panel/tools-panel-header/component.tsx +12 -5
- package/src/tools-panel/tools-panel-header/hook.ts +5 -0
- package/src/tools-panel/types.ts +1 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/src/popover/test/__snapshots__/index.js.snap +0 -34
- package/src/popover/test/index.js +0 -164
|
@@ -14,6 +14,7 @@ import { store as noticesStore } from '@wordpress/notices';
|
|
|
14
14
|
*/
|
|
15
15
|
import Notice from './';
|
|
16
16
|
import styles from './style.scss';
|
|
17
|
+
import { useCallback } from '@wordpress/element';
|
|
17
18
|
|
|
18
19
|
function NoticeList() {
|
|
19
20
|
const { notices } = useSelect( ( select ) => {
|
|
@@ -25,9 +26,12 @@ function NoticeList() {
|
|
|
25
26
|
|
|
26
27
|
const { removeNotice } = useDispatch( noticesStore );
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
const onRemoveNotice = useCallback(
|
|
30
|
+
( id ) => {
|
|
31
|
+
removeNotice( id );
|
|
32
|
+
},
|
|
33
|
+
[ removeNotice ]
|
|
34
|
+
);
|
|
31
35
|
|
|
32
36
|
if ( ! notices.length ) {
|
|
33
37
|
return null;
|
|
@@ -316,7 +316,7 @@ export default function PaletteEdit( {
|
|
|
316
316
|
<PaletteHStackHeader>
|
|
317
317
|
<PaletteHeading>{ paletteLabel }</PaletteHeading>
|
|
318
318
|
<PaletteActionsContainer>
|
|
319
|
-
{ isEditing && (
|
|
319
|
+
{ hasElements && isEditing && (
|
|
320
320
|
<DoneButton
|
|
321
321
|
isSmall
|
|
322
322
|
onClick={ () => {
|
package/src/popover/index.tsx
CHANGED
|
@@ -10,9 +10,9 @@ import {
|
|
|
10
10
|
autoUpdate,
|
|
11
11
|
arrow,
|
|
12
12
|
offset as offsetMiddleware,
|
|
13
|
-
limitShift,
|
|
14
13
|
size,
|
|
15
14
|
Middleware,
|
|
15
|
+
MiddlewareArguments,
|
|
16
16
|
} from '@floating-ui/react-dom';
|
|
17
17
|
// eslint-disable-next-line no-restricted-imports
|
|
18
18
|
import {
|
|
@@ -34,7 +34,6 @@ import {
|
|
|
34
34
|
useMemo,
|
|
35
35
|
useState,
|
|
36
36
|
useCallback,
|
|
37
|
-
useEffect,
|
|
38
37
|
} from '@wordpress/element';
|
|
39
38
|
import {
|
|
40
39
|
useViewportMatch,
|
|
@@ -65,6 +64,7 @@ import type {
|
|
|
65
64
|
PopoverAnchorRefReference,
|
|
66
65
|
PopoverAnchorRefTopBottom,
|
|
67
66
|
} from './types';
|
|
67
|
+
import { limitShift as customLimitShift } from './limit-shift';
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
70
|
* Name of slot in which popover should fill.
|
|
@@ -281,42 +281,31 @@ const UnforwardedPopover = (
|
|
|
281
281
|
* https://floating-ui.com/docs/react-dom#variables-inside-middleware-functions.
|
|
282
282
|
*/
|
|
283
283
|
const frameOffsetRef = useRef( getFrameOffset( referenceOwnerDocument ) );
|
|
284
|
-
/**
|
|
285
|
-
* Store the offset prop in a ref, due to constraints with floating-ui:
|
|
286
|
-
* https://floating-ui.com/docs/react-dom#variables-inside-middleware-functions.
|
|
287
|
-
*/
|
|
288
|
-
const offsetRef = useRef( offsetProp );
|
|
289
284
|
|
|
290
285
|
const middleware = [
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
mainAxis:
|
|
315
|
-
offsetRef.current +
|
|
316
|
-
frameOffsetRef.current[ mainAxis ] * mainAxisModifier,
|
|
317
|
-
crossAxis: frameOffsetRef.current[ crossAxis ],
|
|
318
|
-
};
|
|
319
|
-
} ),
|
|
286
|
+
// Custom middleware which adjusts the popover's position by taking into
|
|
287
|
+
// account the offset of the anchor's iframe (if any) compared to the page.
|
|
288
|
+
{
|
|
289
|
+
name: 'frameOffset',
|
|
290
|
+
fn( { x, y }: MiddlewareArguments ) {
|
|
291
|
+
if ( ! frameOffsetRef.current ) {
|
|
292
|
+
return {
|
|
293
|
+
x,
|
|
294
|
+
y,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return {
|
|
299
|
+
x: x + frameOffsetRef.current.x,
|
|
300
|
+
y: y + frameOffsetRef.current.y,
|
|
301
|
+
data: {
|
|
302
|
+
// This will be used in the customLimitShift() function.
|
|
303
|
+
amount: frameOffsetRef.current,
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
offsetMiddleware( offsetProp ),
|
|
320
309
|
computedFlipProp ? flipMiddleware() : undefined,
|
|
321
310
|
computedResizeProp
|
|
322
311
|
? size( {
|
|
@@ -339,7 +328,7 @@ const UnforwardedPopover = (
|
|
|
339
328
|
shouldShift
|
|
340
329
|
? shiftMiddleware( {
|
|
341
330
|
crossAxis: true,
|
|
342
|
-
limiter:
|
|
331
|
+
limiter: customLimitShift(),
|
|
343
332
|
padding: 1, // Necessary to avoid flickering at the edge of the viewport.
|
|
344
333
|
} )
|
|
345
334
|
: undefined,
|
|
@@ -395,11 +384,6 @@ const UnforwardedPopover = (
|
|
|
395
384
|
} ),
|
|
396
385
|
} );
|
|
397
386
|
|
|
398
|
-
useEffect( () => {
|
|
399
|
-
offsetRef.current = offsetProp;
|
|
400
|
-
update();
|
|
401
|
-
}, [ offsetProp, update ] );
|
|
402
|
-
|
|
403
387
|
const arrowCallbackRef = useCallback(
|
|
404
388
|
( node ) => {
|
|
405
389
|
arrowRef.current = node;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type {
|
|
5
|
+
Axis,
|
|
6
|
+
Coords,
|
|
7
|
+
Placement,
|
|
8
|
+
Side,
|
|
9
|
+
MiddlewareArguments,
|
|
10
|
+
} from '@floating-ui/react-dom';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Parts of this source were derived and modified from `floating-ui`,
|
|
14
|
+
* released under the MIT license.
|
|
15
|
+
*
|
|
16
|
+
* https://github.com/floating-ui/floating-ui
|
|
17
|
+
*
|
|
18
|
+
* Copyright (c) 2021 Floating UI contributors
|
|
19
|
+
*
|
|
20
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
21
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
22
|
+
* in the Software without restriction, including without limitation the rights
|
|
23
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
24
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
25
|
+
* furnished to do so, subject to the following conditions:
|
|
26
|
+
*
|
|
27
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
28
|
+
* copies or substantial portions of the Software.
|
|
29
|
+
*
|
|
30
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
31
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
32
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
33
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
34
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
35
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
36
|
+
* SOFTWARE.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Custom limiter function for the `shift` middleware.
|
|
41
|
+
* This function is mostly identical default `limitShift` from ``@floating-ui`;
|
|
42
|
+
* the only difference is that, when computing the min/max shift limits, it
|
|
43
|
+
* also takes into account the iframe offset that is added by the
|
|
44
|
+
* custom "frameOffset" middleware.
|
|
45
|
+
*
|
|
46
|
+
* All unexported types and functions are also from the `@floating-ui` library,
|
|
47
|
+
* and have been copied to this file for convenience.
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
type LimitShiftOffset =
|
|
51
|
+
| ( ( args: MiddlewareArguments ) =>
|
|
52
|
+
| number
|
|
53
|
+
| {
|
|
54
|
+
/**
|
|
55
|
+
* Offset the limiting of the axis that runs along the alignment of the
|
|
56
|
+
* floating element.
|
|
57
|
+
*/
|
|
58
|
+
mainAxis?: number;
|
|
59
|
+
/**
|
|
60
|
+
* Offset the limiting of the axis that runs along the side of the
|
|
61
|
+
* floating element.
|
|
62
|
+
*/
|
|
63
|
+
crossAxis?: number;
|
|
64
|
+
} )
|
|
65
|
+
| number
|
|
66
|
+
| {
|
|
67
|
+
/**
|
|
68
|
+
* Offset the limiting of the axis that runs along the alignment of the
|
|
69
|
+
* floating element.
|
|
70
|
+
*/
|
|
71
|
+
mainAxis?: number;
|
|
72
|
+
/**
|
|
73
|
+
* Offset the limiting of the axis that runs along the side of the
|
|
74
|
+
* floating element.
|
|
75
|
+
*/
|
|
76
|
+
crossAxis?: number;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
type LimitShiftOptions = {
|
|
80
|
+
/**
|
|
81
|
+
* Offset when limiting starts. `0` will limit when the opposite edges of the
|
|
82
|
+
* reference and floating elements are aligned.
|
|
83
|
+
* - positive = start limiting earlier
|
|
84
|
+
* - negative = start limiting later
|
|
85
|
+
*/
|
|
86
|
+
offset: LimitShiftOffset;
|
|
87
|
+
/**
|
|
88
|
+
* Whether to limit the axis that runs along the alignment of the floating
|
|
89
|
+
* element.
|
|
90
|
+
*/
|
|
91
|
+
mainAxis: boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Whether to limit the axis that runs along the side of the floating element.
|
|
94
|
+
*/
|
|
95
|
+
crossAxis: boolean;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
function getSide( placement: Placement ): Side {
|
|
99
|
+
return placement.split( '-' )[ 0 ] as Side;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function getMainAxisFromPlacement( placement: Placement ): Axis {
|
|
103
|
+
return [ 'top', 'bottom' ].includes( getSide( placement ) ) ? 'x' : 'y';
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function getCrossAxis( axis: Axis ): Axis {
|
|
107
|
+
return axis === 'x' ? 'y' : 'x';
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export const limitShift = (
|
|
111
|
+
options: Partial< LimitShiftOptions > = {}
|
|
112
|
+
): {
|
|
113
|
+
options: Partial< LimitShiftOffset >;
|
|
114
|
+
fn: ( middlewareArguments: MiddlewareArguments ) => Coords;
|
|
115
|
+
} => ( {
|
|
116
|
+
options,
|
|
117
|
+
fn( middlewareArguments ) {
|
|
118
|
+
const { x, y, placement, rects, middlewareData } = middlewareArguments;
|
|
119
|
+
const {
|
|
120
|
+
offset = 0,
|
|
121
|
+
mainAxis: checkMainAxis = true,
|
|
122
|
+
crossAxis: checkCrossAxis = true,
|
|
123
|
+
} = options;
|
|
124
|
+
|
|
125
|
+
const coords = { x, y };
|
|
126
|
+
const mainAxis = getMainAxisFromPlacement( placement );
|
|
127
|
+
const crossAxis = getCrossAxis( mainAxis );
|
|
128
|
+
|
|
129
|
+
let mainAxisCoord = coords[ mainAxis ];
|
|
130
|
+
let crossAxisCoord = coords[ crossAxis ];
|
|
131
|
+
|
|
132
|
+
const rawOffset =
|
|
133
|
+
typeof offset === 'function'
|
|
134
|
+
? offset( middlewareArguments )
|
|
135
|
+
: offset;
|
|
136
|
+
const computedOffset =
|
|
137
|
+
typeof rawOffset === 'number'
|
|
138
|
+
? { mainAxis: rawOffset, crossAxis: 0 }
|
|
139
|
+
: { mainAxis: 0, crossAxis: 0, ...rawOffset };
|
|
140
|
+
|
|
141
|
+
// At the moment of writing, this is the only difference
|
|
142
|
+
// with the `limitShift` function from `@floating-ui`.
|
|
143
|
+
// This offset needs to be added to all min/max limits
|
|
144
|
+
// in order to make the shift-limiting work as expected.
|
|
145
|
+
const additionalFrameOffset = {
|
|
146
|
+
x: 0,
|
|
147
|
+
y: 0,
|
|
148
|
+
...middlewareData.frameOffset?.amount,
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
if ( checkMainAxis ) {
|
|
152
|
+
const len = mainAxis === 'y' ? 'height' : 'width';
|
|
153
|
+
const limitMin =
|
|
154
|
+
rects.reference[ mainAxis ] -
|
|
155
|
+
rects.floating[ len ] +
|
|
156
|
+
computedOffset.mainAxis +
|
|
157
|
+
additionalFrameOffset[ mainAxis ];
|
|
158
|
+
const limitMax =
|
|
159
|
+
rects.reference[ mainAxis ] +
|
|
160
|
+
rects.reference[ len ] -
|
|
161
|
+
computedOffset.mainAxis +
|
|
162
|
+
additionalFrameOffset[ mainAxis ];
|
|
163
|
+
|
|
164
|
+
if ( mainAxisCoord < limitMin ) {
|
|
165
|
+
mainAxisCoord = limitMin;
|
|
166
|
+
} else if ( mainAxisCoord > limitMax ) {
|
|
167
|
+
mainAxisCoord = limitMax;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if ( checkCrossAxis ) {
|
|
172
|
+
const len = mainAxis === 'y' ? 'width' : 'height';
|
|
173
|
+
const isOriginSide = [ 'top', 'left' ].includes(
|
|
174
|
+
getSide( placement )
|
|
175
|
+
);
|
|
176
|
+
const limitMin =
|
|
177
|
+
rects.reference[ crossAxis ] -
|
|
178
|
+
rects.floating[ len ] +
|
|
179
|
+
( isOriginSide
|
|
180
|
+
? middlewareData.offset?.[ crossAxis ] ?? 0
|
|
181
|
+
: 0 ) +
|
|
182
|
+
( isOriginSide ? 0 : computedOffset.crossAxis ) +
|
|
183
|
+
additionalFrameOffset[ crossAxis ];
|
|
184
|
+
const limitMax =
|
|
185
|
+
rects.reference[ crossAxis ] +
|
|
186
|
+
rects.reference[ len ] +
|
|
187
|
+
( isOriginSide
|
|
188
|
+
? 0
|
|
189
|
+
: middlewareData.offset?.[ crossAxis ] ?? 0 ) -
|
|
190
|
+
( isOriginSide ? computedOffset.crossAxis : 0 ) +
|
|
191
|
+
additionalFrameOffset[ crossAxis ];
|
|
192
|
+
|
|
193
|
+
if ( crossAxisCoord < limitMin ) {
|
|
194
|
+
crossAxisCoord = limitMin;
|
|
195
|
+
} else if ( crossAxisCoord > limitMax ) {
|
|
196
|
+
crossAxisCoord = limitMax;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
[ mainAxis ]: mainAxisCoord,
|
|
202
|
+
[ crossAxis ]: crossAxisCoord,
|
|
203
|
+
} as Coords;
|
|
204
|
+
},
|
|
205
|
+
} );
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { render, screen } from '@testing-library/react';
|
|
5
|
+
import type { CSSProperties } from 'react';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* WordPress dependencies
|
|
9
|
+
*/
|
|
10
|
+
import { useState } from '@wordpress/element';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Internal dependencies
|
|
14
|
+
*/
|
|
15
|
+
import { positionToPlacement, placementToMotionAnimationProps } from '../utils';
|
|
16
|
+
import Popover from '..';
|
|
17
|
+
import type { PopoverProps } from '../types';
|
|
18
|
+
|
|
19
|
+
type PositionToPlacementTuple = [
|
|
20
|
+
NonNullable< PopoverProps[ 'position' ] >,
|
|
21
|
+
NonNullable< PopoverProps[ 'placement' ] >
|
|
22
|
+
];
|
|
23
|
+
type PlacementToAnimationOriginTuple = [
|
|
24
|
+
NonNullable< PopoverProps[ 'placement' ] >,
|
|
25
|
+
number,
|
|
26
|
+
number
|
|
27
|
+
];
|
|
28
|
+
type PlacementToInitialTranslationTuple = [
|
|
29
|
+
NonNullable< PopoverProps[ 'placement' ] >,
|
|
30
|
+
'translateY' | 'translateX',
|
|
31
|
+
CSSProperties[ 'translate' ]
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
// There's no matching `placement` for 'middle center' positions,
|
|
35
|
+
// fallback to 'bottom' (same as `floating-ui`'s default.)
|
|
36
|
+
const FALLBACK_FOR_MIDDLE_CENTER_POSITIONS = 'bottom';
|
|
37
|
+
|
|
38
|
+
const ALL_POSITIONS_TO_EXPECTED_PLACEMENTS: PositionToPlacementTuple[] = [
|
|
39
|
+
// Format: [yAxis]
|
|
40
|
+
[ 'middle', FALLBACK_FOR_MIDDLE_CENTER_POSITIONS ],
|
|
41
|
+
[ 'bottom', 'bottom' ],
|
|
42
|
+
[ 'top', 'top' ],
|
|
43
|
+
// Format: [yAxis] [xAxis]
|
|
44
|
+
[ 'middle left', 'left' ],
|
|
45
|
+
[ 'middle center', FALLBACK_FOR_MIDDLE_CENTER_POSITIONS ],
|
|
46
|
+
[ 'middle right', 'right' ],
|
|
47
|
+
[ 'bottom left', 'bottom-end' ],
|
|
48
|
+
[ 'bottom center', 'bottom' ],
|
|
49
|
+
[ 'bottom right', 'bottom-start' ],
|
|
50
|
+
[ 'top left', 'top-end' ],
|
|
51
|
+
[ 'top center', 'top' ],
|
|
52
|
+
[ 'top right', 'top-start' ],
|
|
53
|
+
// Format: [yAxis] [xAxis] [corner]
|
|
54
|
+
[ 'middle left left', 'left' ],
|
|
55
|
+
[ 'middle left right', 'left' ],
|
|
56
|
+
[ 'middle left bottom', 'left-end' ],
|
|
57
|
+
[ 'middle left top', 'left-start' ],
|
|
58
|
+
[ 'middle center left', FALLBACK_FOR_MIDDLE_CENTER_POSITIONS ],
|
|
59
|
+
[ 'middle center right', FALLBACK_FOR_MIDDLE_CENTER_POSITIONS ],
|
|
60
|
+
[ 'middle center bottom', FALLBACK_FOR_MIDDLE_CENTER_POSITIONS ],
|
|
61
|
+
[ 'middle center top', FALLBACK_FOR_MIDDLE_CENTER_POSITIONS ],
|
|
62
|
+
[ 'middle right left', 'right' ],
|
|
63
|
+
[ 'middle right right', 'right' ],
|
|
64
|
+
[ 'middle right bottom', 'right-end' ],
|
|
65
|
+
[ 'middle right top', 'right-start' ],
|
|
66
|
+
[ 'bottom left left', 'bottom-end' ],
|
|
67
|
+
[ 'bottom left right', 'bottom-end' ],
|
|
68
|
+
[ 'bottom left bottom', 'bottom-end' ],
|
|
69
|
+
[ 'bottom left top', 'bottom-end' ],
|
|
70
|
+
[ 'bottom center left', 'bottom' ],
|
|
71
|
+
[ 'bottom center right', 'bottom' ],
|
|
72
|
+
[ 'bottom center bottom', 'bottom' ],
|
|
73
|
+
[ 'bottom center top', 'bottom' ],
|
|
74
|
+
[ 'bottom right left', 'bottom-start' ],
|
|
75
|
+
[ 'bottom right right', 'bottom-start' ],
|
|
76
|
+
[ 'bottom right bottom', 'bottom-start' ],
|
|
77
|
+
[ 'bottom right top', 'bottom-start' ],
|
|
78
|
+
[ 'top left left', 'top-end' ],
|
|
79
|
+
[ 'top left right', 'top-end' ],
|
|
80
|
+
[ 'top left bottom', 'top-end' ],
|
|
81
|
+
[ 'top left top', 'top-end' ],
|
|
82
|
+
[ 'top center left', 'top' ],
|
|
83
|
+
[ 'top center right', 'top' ],
|
|
84
|
+
[ 'top center bottom', 'top' ],
|
|
85
|
+
[ 'top center top', 'top' ],
|
|
86
|
+
[ 'top right left', 'top-start' ],
|
|
87
|
+
[ 'top right right', 'top-start' ],
|
|
88
|
+
[ 'top right bottom', 'top-start' ],
|
|
89
|
+
[ 'top right top', 'top-start' ],
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
describe( 'Popover', () => {
|
|
93
|
+
describe( 'Component', () => {
|
|
94
|
+
describe( 'basic behavior', () => {
|
|
95
|
+
it( 'should render content', () => {
|
|
96
|
+
render( <Popover>Hello</Popover> );
|
|
97
|
+
|
|
98
|
+
expect( screen.getByText( 'Hello' ) ).toBeInTheDocument();
|
|
99
|
+
} );
|
|
100
|
+
|
|
101
|
+
it( 'should forward additional props to portaled element', () => {
|
|
102
|
+
render( <Popover role="tooltip">Hello</Popover> );
|
|
103
|
+
|
|
104
|
+
expect( screen.getByRole( 'tooltip' ) ).toBeInTheDocument();
|
|
105
|
+
} );
|
|
106
|
+
} );
|
|
107
|
+
|
|
108
|
+
describe( 'anchor', () => {
|
|
109
|
+
it( 'should render correctly when anchor is provided', () => {
|
|
110
|
+
const PopoverWithAnchor = ( args: PopoverProps ) => {
|
|
111
|
+
// Use internal state instead of a ref to make sure that the component
|
|
112
|
+
// re-renders when the popover's anchor updates.
|
|
113
|
+
const [ anchor, setAnchor ] =
|
|
114
|
+
useState< HTMLParagraphElement | null >( null );
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<div>
|
|
118
|
+
<p ref={ setAnchor }>Anchor</p>
|
|
119
|
+
<Popover { ...args } anchor={ anchor } />
|
|
120
|
+
</div>
|
|
121
|
+
);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
render(
|
|
125
|
+
<PopoverWithAnchor>Popover content</PopoverWithAnchor>
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
expect(
|
|
129
|
+
screen.getByText( 'Popover content' )
|
|
130
|
+
).toBeInTheDocument();
|
|
131
|
+
} );
|
|
132
|
+
} );
|
|
133
|
+
|
|
134
|
+
describe( 'focus behavior', () => {
|
|
135
|
+
it( 'should focus the popover by default when opened', () => {
|
|
136
|
+
render( <Popover>Popover content</Popover> );
|
|
137
|
+
|
|
138
|
+
expect(
|
|
139
|
+
screen.getByText( 'Popover content' ).parentElement
|
|
140
|
+
).toHaveFocus();
|
|
141
|
+
} );
|
|
142
|
+
|
|
143
|
+
it( 'should allow focus-on-open behavior to be disabled', () => {
|
|
144
|
+
render(
|
|
145
|
+
<Popover focusOnMount={ false }>Popover content</Popover>
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
expect( document.body ).toHaveFocus();
|
|
149
|
+
} );
|
|
150
|
+
} );
|
|
151
|
+
} );
|
|
152
|
+
|
|
153
|
+
describe( 'positionToPlacement', () => {
|
|
154
|
+
it.each( ALL_POSITIONS_TO_EXPECTED_PLACEMENTS )(
|
|
155
|
+
'converts `%s` to `%s`',
|
|
156
|
+
( inputPosition, expectedPlacement ) => {
|
|
157
|
+
expect( positionToPlacement( inputPosition ) ).toEqual(
|
|
158
|
+
expectedPlacement
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
);
|
|
162
|
+
} );
|
|
163
|
+
|
|
164
|
+
describe( 'placementToMotionAnimationProps', () => {
|
|
165
|
+
describe( 'animation origin', () => {
|
|
166
|
+
it.each( [
|
|
167
|
+
[ 'top', 0.5, 1 ],
|
|
168
|
+
[ 'top-start', 0, 1 ],
|
|
169
|
+
[ 'top-end', 1, 1 ],
|
|
170
|
+
[ 'right', 0, 0.5 ],
|
|
171
|
+
[ 'right-start', 0, 0 ],
|
|
172
|
+
[ 'right-end', 0, 1 ],
|
|
173
|
+
[ 'bottom', 0.5, 0 ],
|
|
174
|
+
[ 'bottom-start', 0, 0 ],
|
|
175
|
+
[ 'bottom-end', 1, 0 ],
|
|
176
|
+
[ 'left', 1, 0.5 ],
|
|
177
|
+
[ 'left-start', 1, 0 ],
|
|
178
|
+
[ 'left-end', 1, 1 ],
|
|
179
|
+
] as PlacementToAnimationOriginTuple[] )(
|
|
180
|
+
'for the `%s` placement computes an animation origin of (%d, %d)',
|
|
181
|
+
( inputPlacement, expectedOriginX, expectedOriginY ) => {
|
|
182
|
+
expect(
|
|
183
|
+
placementToMotionAnimationProps( inputPlacement )
|
|
184
|
+
).toEqual(
|
|
185
|
+
expect.objectContaining( {
|
|
186
|
+
style: expect.objectContaining( {
|
|
187
|
+
originX: expectedOriginX,
|
|
188
|
+
originY: expectedOriginY,
|
|
189
|
+
} ),
|
|
190
|
+
} )
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
} );
|
|
195
|
+
describe( 'initial translation', () => {
|
|
196
|
+
it.each( [
|
|
197
|
+
[ 'top', 'translateY', '2em' ],
|
|
198
|
+
[ 'top-start', 'translateY', '2em' ],
|
|
199
|
+
[ 'top-end', 'translateY', '2em' ],
|
|
200
|
+
[ 'right', 'translateX', '-2em' ],
|
|
201
|
+
[ 'right-start', 'translateX', '-2em' ],
|
|
202
|
+
[ 'right-end', 'translateX', '-2em' ],
|
|
203
|
+
[ 'bottom', 'translateY', '-2em' ],
|
|
204
|
+
[ 'bottom-start', 'translateY', '-2em' ],
|
|
205
|
+
[ 'bottom-end', 'translateY', '-2em' ],
|
|
206
|
+
[ 'left', 'translateX', '2em' ],
|
|
207
|
+
[ 'left-start', 'translateX', '2em' ],
|
|
208
|
+
[ 'left-end', 'translateX', '2em' ],
|
|
209
|
+
] as PlacementToInitialTranslationTuple[] )(
|
|
210
|
+
'for the `%s` placement computes an initial `%s` of `%s',
|
|
211
|
+
(
|
|
212
|
+
inputPlacement,
|
|
213
|
+
expectedTranslationProp,
|
|
214
|
+
expectedTranslationValue
|
|
215
|
+
) => {
|
|
216
|
+
expect(
|
|
217
|
+
placementToMotionAnimationProps( inputPlacement )
|
|
218
|
+
).toEqual(
|
|
219
|
+
expect.objectContaining( {
|
|
220
|
+
initial: expect.objectContaining( {
|
|
221
|
+
[ expectedTranslationProp ]:
|
|
222
|
+
expectedTranslationValue,
|
|
223
|
+
} ),
|
|
224
|
+
} )
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
} );
|
|
229
|
+
} );
|
|
230
|
+
} );
|
package/src/popover/types.ts
CHANGED
|
@@ -121,6 +121,7 @@ export type PopoverProps = {
|
|
|
121
121
|
* _Note: this prop is deprecated. Use the `placement` prop instead._
|
|
122
122
|
*/
|
|
123
123
|
position?:
|
|
124
|
+
| `${ PositionYAxis }`
|
|
124
125
|
| `${ PositionYAxis } ${ PositionXAxis }`
|
|
125
126
|
| `${ PositionYAxis } ${ PositionXAxis } ${ PositionCorner }`;
|
|
126
127
|
/**
|
package/src/popover/utils.ts
CHANGED
|
@@ -14,6 +14,62 @@ import type {
|
|
|
14
14
|
PopoverAnchorRefTopBottom,
|
|
15
15
|
} from './types';
|
|
16
16
|
|
|
17
|
+
const POSITION_TO_PLACEMENT: Record<
|
|
18
|
+
NonNullable< PopoverProps[ 'position' ] >,
|
|
19
|
+
NonNullable< PopoverProps[ 'placement' ] >
|
|
20
|
+
> = {
|
|
21
|
+
bottom: 'bottom',
|
|
22
|
+
top: 'top',
|
|
23
|
+
'middle left': 'left',
|
|
24
|
+
'middle right': 'right',
|
|
25
|
+
'bottom left': 'bottom-end',
|
|
26
|
+
'bottom center': 'bottom',
|
|
27
|
+
'bottom right': 'bottom-start',
|
|
28
|
+
'top left': 'top-end',
|
|
29
|
+
'top center': 'top',
|
|
30
|
+
'top right': 'top-start',
|
|
31
|
+
'middle left left': 'left',
|
|
32
|
+
'middle left right': 'left',
|
|
33
|
+
'middle left bottom': 'left-end',
|
|
34
|
+
'middle left top': 'left-start',
|
|
35
|
+
'middle right left': 'right',
|
|
36
|
+
'middle right right': 'right',
|
|
37
|
+
'middle right bottom': 'right-end',
|
|
38
|
+
'middle right top': 'right-start',
|
|
39
|
+
'bottom left left': 'bottom-end',
|
|
40
|
+
'bottom left right': 'bottom-end',
|
|
41
|
+
'bottom left bottom': 'bottom-end',
|
|
42
|
+
'bottom left top': 'bottom-end',
|
|
43
|
+
'bottom center left': 'bottom',
|
|
44
|
+
'bottom center right': 'bottom',
|
|
45
|
+
'bottom center bottom': 'bottom',
|
|
46
|
+
'bottom center top': 'bottom',
|
|
47
|
+
'bottom right left': 'bottom-start',
|
|
48
|
+
'bottom right right': 'bottom-start',
|
|
49
|
+
'bottom right bottom': 'bottom-start',
|
|
50
|
+
'bottom right top': 'bottom-start',
|
|
51
|
+
'top left left': 'top-end',
|
|
52
|
+
'top left right': 'top-end',
|
|
53
|
+
'top left bottom': 'top-end',
|
|
54
|
+
'top left top': 'top-end',
|
|
55
|
+
'top center left': 'top',
|
|
56
|
+
'top center right': 'top',
|
|
57
|
+
'top center bottom': 'top',
|
|
58
|
+
'top center top': 'top',
|
|
59
|
+
'top right left': 'top-start',
|
|
60
|
+
'top right right': 'top-start',
|
|
61
|
+
'top right bottom': 'top-start',
|
|
62
|
+
'top right top': 'top-start',
|
|
63
|
+
// `middle`/`middle center [corner?]` positions are associated to a fallback
|
|
64
|
+
// `bottom` placement because there aren't any corresponding placement values.
|
|
65
|
+
middle: 'bottom',
|
|
66
|
+
'middle center': 'bottom',
|
|
67
|
+
'middle center bottom': 'bottom',
|
|
68
|
+
'middle center left': 'bottom',
|
|
69
|
+
'middle center right': 'bottom',
|
|
70
|
+
'middle center top': 'bottom',
|
|
71
|
+
};
|
|
72
|
+
|
|
17
73
|
/**
|
|
18
74
|
* Converts the `Popover`'s legacy "position" prop to the new "placement" prop
|
|
19
75
|
* (used by `floating-ui`).
|
|
@@ -23,22 +79,8 @@ import type {
|
|
|
23
79
|
*/
|
|
24
80
|
export const positionToPlacement = (
|
|
25
81
|
position: NonNullable< PopoverProps[ 'position' ] >
|
|
26
|
-
): NonNullable< PopoverProps[ 'placement' ] > =>
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if ( [ 'top', 'bottom' ].includes( x ) ) {
|
|
30
|
-
let suffix = '';
|
|
31
|
-
if ( ( !! z && z === 'left' ) || y === 'right' ) {
|
|
32
|
-
suffix = '-start';
|
|
33
|
-
} else if ( ( !! z && z === 'right' ) || y === 'left' ) {
|
|
34
|
-
suffix = '-end';
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return ( x + suffix ) as NonNullable< PopoverProps[ 'placement' ] >;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return y as NonNullable< PopoverProps[ 'placement' ] >;
|
|
41
|
-
};
|
|
82
|
+
): NonNullable< PopoverProps[ 'placement' ] > =>
|
|
83
|
+
POSITION_TO_PLACEMENT[ position ] ?? 'bottom';
|
|
42
84
|
|
|
43
85
|
/**
|
|
44
86
|
* @typedef AnimationOrigin
|