@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.
Files changed (168) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/build/draggable/index.js +3 -6
  3. package/build/draggable/index.js.map +1 -1
  4. package/build/index.js +6 -0
  5. package/build/index.js.map +1 -1
  6. package/build/menu-item/index.js +4 -3
  7. package/build/menu-item/index.js.map +1 -1
  8. package/build/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js +10 -3
  9. package/build/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js.map +1 -1
  10. package/build/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js +12 -3
  11. package/build/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js.map +1 -1
  12. package/build/mobile/bottom-sheet/sub-sheet/index.native.js +4 -1
  13. package/build/mobile/bottom-sheet/sub-sheet/index.native.js.map +1 -1
  14. package/build/mobile/color-settings/index.native.js +3 -1
  15. package/build/mobile/color-settings/index.native.js.map +1 -1
  16. package/build/mobile/color-settings/picker-screen.native.js +3 -1
  17. package/build/mobile/color-settings/picker-screen.native.js.map +1 -1
  18. package/build/mobile/image/index.native.js +3 -1
  19. package/build/mobile/image/index.native.js.map +1 -1
  20. package/build/mobile/keyboard-avoiding-view/index.ios.js +3 -1
  21. package/build/mobile/keyboard-avoiding-view/index.ios.js.map +1 -1
  22. package/build/mobile/link-picker/index.native.js +3 -1
  23. package/build/mobile/link-picker/index.native.js.map +1 -1
  24. package/build/mobile/link-picker/link-picker-results.native.js +3 -1
  25. package/build/mobile/link-picker/link-picker-results.native.js.map +1 -1
  26. package/build/mobile/link-picker/link-picker-screen.native.js +3 -1
  27. package/build/mobile/link-picker/link-picker-screen.native.js.map +1 -1
  28. package/build/mobile/link-settings/index.native.js +24 -6
  29. package/build/mobile/link-settings/index.native.js.map +1 -1
  30. package/build/mobile/link-settings/link-settings-screen.native.js +3 -1
  31. package/build/mobile/link-settings/link-settings-screen.native.js.map +1 -1
  32. package/build/mobile/segmented-control/index.native.js +6 -2
  33. package/build/mobile/segmented-control/index.native.js.map +1 -1
  34. package/build/mobile/utils/use-unit-converter-to-mobile.native.js +6 -2
  35. package/build/mobile/utils/use-unit-converter-to-mobile.native.js.map +1 -1
  36. package/build/notice/index.native.js +15 -19
  37. package/build/notice/index.native.js.map +1 -1
  38. package/build/notice/list.native.js +2 -3
  39. package/build/notice/list.native.js.map +1 -1
  40. package/build/palette-edit/index.js +1 -1
  41. package/build/palette-edit/index.js.map +1 -1
  42. package/build/popover/index.js +29 -32
  43. package/build/popover/index.js.map +1 -1
  44. package/build/popover/limit-shift.js +145 -0
  45. package/build/popover/limit-shift.js.map +1 -0
  46. package/build/popover/utils.js +55 -15
  47. package/build/popover/utils.js.map +1 -1
  48. package/build/tools-panel/styles.js +27 -12
  49. package/build/tools-panel/styles.js.map +1 -1
  50. package/build/tools-panel/tools-panel-header/component.js +19 -6
  51. package/build/tools-panel/tools-panel-header/component.js.map +1 -1
  52. package/build/tools-panel/tools-panel-header/hook.js +4 -0
  53. package/build/tools-panel/tools-panel-header/hook.js.map +1 -1
  54. package/build-module/draggable/index.js +2 -5
  55. package/build-module/draggable/index.js.map +1 -1
  56. package/build-module/index.js +1 -1
  57. package/build-module/index.js.map +1 -1
  58. package/build-module/menu-item/index.js +4 -3
  59. package/build-module/menu-item/index.js.map +1 -1
  60. package/build-module/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js +10 -3
  61. package/build-module/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js.map +1 -1
  62. package/build-module/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js +12 -3
  63. package/build-module/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js.map +1 -1
  64. package/build-module/mobile/bottom-sheet/sub-sheet/index.native.js +4 -1
  65. package/build-module/mobile/bottom-sheet/sub-sheet/index.native.js.map +1 -1
  66. package/build-module/mobile/color-settings/index.native.js +3 -1
  67. package/build-module/mobile/color-settings/index.native.js.map +1 -1
  68. package/build-module/mobile/color-settings/picker-screen.native.js +3 -1
  69. package/build-module/mobile/color-settings/picker-screen.native.js.map +1 -1
  70. package/build-module/mobile/image/index.native.js +3 -1
  71. package/build-module/mobile/image/index.native.js.map +1 -1
  72. package/build-module/mobile/keyboard-avoiding-view/index.ios.js +3 -1
  73. package/build-module/mobile/keyboard-avoiding-view/index.ios.js.map +1 -1
  74. package/build-module/mobile/link-picker/index.native.js +3 -1
  75. package/build-module/mobile/link-picker/index.native.js.map +1 -1
  76. package/build-module/mobile/link-picker/link-picker-results.native.js +3 -1
  77. package/build-module/mobile/link-picker/link-picker-results.native.js.map +1 -1
  78. package/build-module/mobile/link-picker/link-picker-screen.native.js +3 -1
  79. package/build-module/mobile/link-picker/link-picker-screen.native.js.map +1 -1
  80. package/build-module/mobile/link-settings/index.native.js +24 -6
  81. package/build-module/mobile/link-settings/index.native.js.map +1 -1
  82. package/build-module/mobile/link-settings/link-settings-screen.native.js +3 -1
  83. package/build-module/mobile/link-settings/link-settings-screen.native.js.map +1 -1
  84. package/build-module/mobile/segmented-control/index.native.js +6 -2
  85. package/build-module/mobile/segmented-control/index.native.js.map +1 -1
  86. package/build-module/mobile/utils/use-unit-converter-to-mobile.native.js +6 -2
  87. package/build-module/mobile/utils/use-unit-converter-to-mobile.native.js.map +1 -1
  88. package/build-module/notice/index.native.js +16 -21
  89. package/build-module/notice/index.native.js.map +1 -1
  90. package/build-module/notice/list.native.js +3 -3
  91. package/build-module/notice/list.native.js.map +1 -1
  92. package/build-module/palette-edit/index.js +1 -1
  93. package/build-module/palette-edit/index.js.map +1 -1
  94. package/build-module/popover/index.js +31 -35
  95. package/build-module/popover/index.js.map +1 -1
  96. package/build-module/popover/limit-shift.js +136 -0
  97. package/build-module/popover/limit-shift.js.map +1 -0
  98. package/build-module/popover/utils.js +55 -15
  99. package/build-module/popover/utils.js.map +1 -1
  100. package/build-module/tools-panel/styles.js +23 -12
  101. package/build-module/tools-panel/styles.js.map +1 -1
  102. package/build-module/tools-panel/tools-panel-header/component.js +19 -7
  103. package/build-module/tools-panel/tools-panel-header/component.js.map +1 -1
  104. package/build-module/tools-panel/tools-panel-header/hook.js +4 -0
  105. package/build-module/tools-panel/tools-panel-header/hook.js.map +1 -1
  106. package/build-style/style-rtl.css +13 -2
  107. package/build-style/style.css +13 -2
  108. package/build-types/draggable/index.d.ts.map +1 -1
  109. package/build-types/external-link/styles/external-link-styles.d.ts +1 -1
  110. package/build-types/focal-point-picker/styles/focal-point-style.d.ts +3 -3
  111. package/build-types/menu-item/index.d.ts.map +1 -1
  112. package/build-types/popover/index.d.ts.map +1 -1
  113. package/build-types/popover/limit-shift.d.ts +87 -0
  114. package/build-types/popover/limit-shift.d.ts.map +1 -0
  115. package/build-types/popover/test/index.d.ts +2 -0
  116. package/build-types/popover/test/index.d.ts.map +1 -0
  117. package/build-types/popover/types.d.ts +1 -1
  118. package/build-types/popover/types.d.ts.map +1 -1
  119. package/build-types/popover/utils.d.ts.map +1 -1
  120. package/build-types/range-control/types.d.ts +0 -32
  121. package/build-types/range-control/types.d.ts.map +1 -1
  122. package/build-types/spinner/index.d.ts +1 -1
  123. package/build-types/tools-panel/styles.d.ts +6 -0
  124. package/build-types/tools-panel/styles.d.ts.map +1 -1
  125. package/build-types/tools-panel/tools-panel-header/component.d.ts.map +1 -1
  126. package/build-types/tools-panel/tools-panel-header/hook.d.ts +1 -0
  127. package/build-types/tools-panel/tools-panel-header/hook.d.ts.map +1 -1
  128. package/build-types/tools-panel/types.d.ts +1 -0
  129. package/build-types/tools-panel/types.d.ts.map +1 -1
  130. package/package.json +17 -17
  131. package/src/draggable/index.js +2 -5
  132. package/src/index.js +1 -1
  133. package/src/menu-item/README.md +7 -0
  134. package/src/menu-item/index.js +11 -5
  135. package/src/menu-item/style.scss +1 -0
  136. package/src/menu-item/test/index.js +36 -0
  137. package/src/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js +9 -0
  138. package/src/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js +11 -0
  139. package/src/mobile/bottom-sheet/sub-sheet/index.native.js +3 -0
  140. package/src/mobile/color-settings/index.native.js +3 -0
  141. package/src/mobile/color-settings/picker-screen.native.js +3 -0
  142. package/src/mobile/image/index.native.js +3 -0
  143. package/src/mobile/keyboard-avoiding-view/index.ios.js +3 -0
  144. package/src/mobile/link-picker/index.native.js +3 -0
  145. package/src/mobile/link-picker/link-picker-results.native.js +3 -0
  146. package/src/mobile/link-picker/link-picker-screen.native.js +3 -0
  147. package/src/mobile/link-settings/index.native.js +18 -0
  148. package/src/mobile/link-settings/link-settings-screen.native.js +3 -0
  149. package/src/mobile/segmented-control/index.native.js +6 -0
  150. package/src/mobile/utils/use-unit-converter-to-mobile.native.js +6 -0
  151. package/src/notice/index.native.js +17 -20
  152. package/src/notice/list.native.js +7 -3
  153. package/src/palette-edit/index.js +1 -1
  154. package/src/placeholder/style.scss +1 -1
  155. package/src/popover/index.tsx +26 -42
  156. package/src/popover/limit-shift.ts +205 -0
  157. package/src/popover/test/index.tsx +230 -0
  158. package/src/popover/types.ts +1 -0
  159. package/src/popover/utils.ts +58 -16
  160. package/src/range-control/types.ts +0 -33
  161. package/src/tools-panel/stories/index.js +27 -0
  162. package/src/tools-panel/styles.ts +28 -1
  163. package/src/tools-panel/tools-panel-header/component.tsx +12 -5
  164. package/src/tools-panel/tools-panel-header/hook.ts +5 -0
  165. package/src/tools-panel/types.ts +1 -0
  166. package/tsconfig.tsbuildinfo +1 -1
  167. package/src/popover/test/__snapshots__/index.js.snap +0 -34
  168. 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
- function onRemoveNotice( id ) {
29
- removeNotice( id );
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={ () => {
@@ -234,5 +234,5 @@
234
234
  width: 100%;
235
235
  height: 100%;
236
236
  stroke: currentColor;
237
- stroke-dasharray: 3;
237
+ opacity: 0.25;
238
238
  }
@@ -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
- offsetMiddleware( ( { placement: currentPlacement } ) => {
292
- if ( ! frameOffsetRef.current ) {
293
- return offsetRef.current;
294
- }
295
-
296
- const isTopBottomPlacement =
297
- currentPlacement.includes( 'top' ) ||
298
- currentPlacement.includes( 'bottom' );
299
-
300
- // The main axis should represent the gap between the
301
- // floating element and the reference element. The cross
302
- // axis is always perpendicular to the main axis.
303
- const mainAxis = isTopBottomPlacement ? 'y' : 'x';
304
- const crossAxis = mainAxis === 'x' ? 'y' : 'x';
305
-
306
- // When the popover is before the reference, subtract the offset,
307
- // of the main axis else add it.
308
- const hasBeforePlacement =
309
- currentPlacement.includes( 'top' ) ||
310
- currentPlacement.includes( 'left' );
311
- const mainAxisModifier = hasBeforePlacement ? -1 : 1;
312
-
313
- return {
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: limitShift(),
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
+ } );
@@ -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
  /**
@@ -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
- const [ x, y, z ] = position.split( ' ' );
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