@wordpress/components 21.0.2 → 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 (255) hide show
  1. package/CHANGELOG.md +31 -4
  2. package/build/autocomplete/get-default-use-items.js +3 -3
  3. package/build/autocomplete/get-default-use-items.js.map +1 -1
  4. package/build/box-control/unit-control.js +0 -1
  5. package/build/box-control/unit-control.js.map +1 -1
  6. package/build/button/index.js +5 -1
  7. package/build/button/index.js.map +1 -1
  8. package/build/draggable/index.js +3 -6
  9. package/build/draggable/index.js.map +1 -1
  10. package/build/drop-zone/index.js +7 -1
  11. package/build/drop-zone/index.js.map +1 -1
  12. package/build/higher-order/with-filters/index.js +1 -1
  13. package/build/higher-order/with-filters/index.js.map +1 -1
  14. package/build/index.js +6 -0
  15. package/build/index.js.map +1 -1
  16. package/build/menu-item/index.js +4 -3
  17. package/build/menu-item/index.js.map +1 -1
  18. package/build/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js +10 -3
  19. package/build/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js.map +1 -1
  20. package/build/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js +15 -6
  21. package/build/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js.map +1 -1
  22. package/build/mobile/bottom-sheet/sub-sheet/index.native.js +4 -1
  23. package/build/mobile/bottom-sheet/sub-sheet/index.native.js.map +1 -1
  24. package/build/mobile/color-settings/index.native.js +3 -1
  25. package/build/mobile/color-settings/index.native.js.map +1 -1
  26. package/build/mobile/color-settings/picker-screen.native.js +3 -1
  27. package/build/mobile/color-settings/picker-screen.native.js.map +1 -1
  28. package/build/mobile/image/index.native.js +3 -1
  29. package/build/mobile/image/index.native.js.map +1 -1
  30. package/build/mobile/keyboard-avoiding-view/index.ios.js +3 -1
  31. package/build/mobile/keyboard-avoiding-view/index.ios.js.map +1 -1
  32. package/build/mobile/link-picker/index.native.js +3 -1
  33. package/build/mobile/link-picker/index.native.js.map +1 -1
  34. package/build/mobile/link-picker/link-picker-results.native.js +6 -4
  35. package/build/mobile/link-picker/link-picker-results.native.js.map +1 -1
  36. package/build/mobile/link-picker/link-picker-screen.native.js +3 -1
  37. package/build/mobile/link-picker/link-picker-screen.native.js.map +1 -1
  38. package/build/mobile/link-settings/index.native.js +24 -6
  39. package/build/mobile/link-settings/index.native.js.map +1 -1
  40. package/build/mobile/link-settings/link-settings-screen.native.js +3 -1
  41. package/build/mobile/link-settings/link-settings-screen.native.js.map +1 -1
  42. package/build/mobile/segmented-control/index.native.js +6 -2
  43. package/build/mobile/segmented-control/index.native.js.map +1 -1
  44. package/build/mobile/utils/use-unit-converter-to-mobile.native.js +6 -2
  45. package/build/mobile/utils/use-unit-converter-to-mobile.native.js.map +1 -1
  46. package/build/notice/index.native.js +15 -19
  47. package/build/notice/index.native.js.map +1 -1
  48. package/build/notice/list.native.js +2 -3
  49. package/build/notice/list.native.js.map +1 -1
  50. package/build/palette-edit/index.js +1 -1
  51. package/build/palette-edit/index.js.map +1 -1
  52. package/build/popover/index.js +29 -32
  53. package/build/popover/index.js.map +1 -1
  54. package/build/popover/limit-shift.js +145 -0
  55. package/build/popover/limit-shift.js.map +1 -0
  56. package/build/popover/utils.js +55 -15
  57. package/build/popover/utils.js.map +1 -1
  58. package/build/range-control/input-range.js +1 -15
  59. package/build/range-control/input-range.js.map +1 -1
  60. package/build/range-control/tooltip.js +1 -1
  61. package/build/range-control/tooltip.js.map +1 -1
  62. package/build/range-control/utils.js +2 -58
  63. package/build/range-control/utils.js.map +1 -1
  64. package/build/tools-panel/styles.js +27 -12
  65. package/build/tools-panel/styles.js.map +1 -1
  66. package/build/tools-panel/tools-panel-header/component.js +19 -6
  67. package/build/tools-panel/tools-panel-header/component.js.map +1 -1
  68. package/build/tools-panel/tools-panel-header/hook.js +4 -0
  69. package/build/tools-panel/tools-panel-header/hook.js.map +1 -1
  70. package/build/tree-grid/index.js +13 -8
  71. package/build/tree-grid/index.js.map +1 -1
  72. package/build/unit-control/index.js +2 -2
  73. package/build/unit-control/index.js.map +1 -1
  74. package/build/unit-control/index.native.js +10 -2
  75. package/build/unit-control/index.native.js.map +1 -1
  76. package/build/unit-control/styles/unit-control-styles.js +21 -32
  77. package/build/unit-control/styles/unit-control-styles.js.map +1 -1
  78. package/build/utils/hooks/use-controlled-state.js +2 -3
  79. package/build/utils/hooks/use-controlled-state.js.map +1 -1
  80. package/build-module/autocomplete/get-default-use-items.js +1 -1
  81. package/build-module/autocomplete/get-default-use-items.js.map +1 -1
  82. package/build-module/box-control/unit-control.js +0 -1
  83. package/build-module/box-control/unit-control.js.map +1 -1
  84. package/build-module/button/index.js +5 -1
  85. package/build-module/button/index.js.map +1 -1
  86. package/build-module/draggable/index.js +2 -5
  87. package/build-module/draggable/index.js.map +1 -1
  88. package/build-module/drop-zone/index.js +7 -1
  89. package/build-module/drop-zone/index.js.map +1 -1
  90. package/build-module/higher-order/with-filters/index.js +2 -2
  91. package/build-module/higher-order/with-filters/index.js.map +1 -1
  92. package/build-module/index.js +1 -1
  93. package/build-module/index.js.map +1 -1
  94. package/build-module/menu-item/index.js +4 -3
  95. package/build-module/menu-item/index.js.map +1 -1
  96. package/build-module/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js +10 -3
  97. package/build-module/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js.map +1 -1
  98. package/build-module/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js +13 -4
  99. package/build-module/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js.map +1 -1
  100. package/build-module/mobile/bottom-sheet/sub-sheet/index.native.js +4 -1
  101. package/build-module/mobile/bottom-sheet/sub-sheet/index.native.js.map +1 -1
  102. package/build-module/mobile/color-settings/index.native.js +3 -1
  103. package/build-module/mobile/color-settings/index.native.js.map +1 -1
  104. package/build-module/mobile/color-settings/picker-screen.native.js +3 -1
  105. package/build-module/mobile/color-settings/picker-screen.native.js.map +1 -1
  106. package/build-module/mobile/image/index.native.js +3 -1
  107. package/build-module/mobile/image/index.native.js.map +1 -1
  108. package/build-module/mobile/keyboard-avoiding-view/index.ios.js +3 -1
  109. package/build-module/mobile/keyboard-avoiding-view/index.ios.js.map +1 -1
  110. package/build-module/mobile/link-picker/index.native.js +3 -1
  111. package/build-module/mobile/link-picker/index.native.js.map +1 -1
  112. package/build-module/mobile/link-picker/link-picker-results.native.js +4 -2
  113. package/build-module/mobile/link-picker/link-picker-results.native.js.map +1 -1
  114. package/build-module/mobile/link-picker/link-picker-screen.native.js +3 -1
  115. package/build-module/mobile/link-picker/link-picker-screen.native.js.map +1 -1
  116. package/build-module/mobile/link-settings/index.native.js +24 -6
  117. package/build-module/mobile/link-settings/index.native.js.map +1 -1
  118. package/build-module/mobile/link-settings/link-settings-screen.native.js +3 -1
  119. package/build-module/mobile/link-settings/link-settings-screen.native.js.map +1 -1
  120. package/build-module/mobile/segmented-control/index.native.js +6 -2
  121. package/build-module/mobile/segmented-control/index.native.js.map +1 -1
  122. package/build-module/mobile/utils/use-unit-converter-to-mobile.native.js +6 -2
  123. package/build-module/mobile/utils/use-unit-converter-to-mobile.native.js.map +1 -1
  124. package/build-module/notice/index.native.js +16 -21
  125. package/build-module/notice/index.native.js.map +1 -1
  126. package/build-module/notice/list.native.js +3 -3
  127. package/build-module/notice/list.native.js.map +1 -1
  128. package/build-module/palette-edit/index.js +1 -1
  129. package/build-module/palette-edit/index.js.map +1 -1
  130. package/build-module/popover/index.js +31 -35
  131. package/build-module/popover/index.js.map +1 -1
  132. package/build-module/popover/limit-shift.js +136 -0
  133. package/build-module/popover/limit-shift.js.map +1 -0
  134. package/build-module/popover/utils.js +55 -15
  135. package/build-module/popover/utils.js.map +1 -1
  136. package/build-module/range-control/input-range.js +1 -14
  137. package/build-module/range-control/input-range.js.map +1 -1
  138. package/build-module/range-control/tooltip.js +1 -1
  139. package/build-module/range-control/tooltip.js.map +1 -1
  140. package/build-module/range-control/utils.js +2 -57
  141. package/build-module/range-control/utils.js.map +1 -1
  142. package/build-module/tools-panel/styles.js +23 -12
  143. package/build-module/tools-panel/styles.js.map +1 -1
  144. package/build-module/tools-panel/tools-panel-header/component.js +19 -7
  145. package/build-module/tools-panel/tools-panel-header/component.js.map +1 -1
  146. package/build-module/tools-panel/tools-panel-header/hook.js +4 -0
  147. package/build-module/tools-panel/tools-panel-header/hook.js.map +1 -1
  148. package/build-module/tree-grid/index.js +13 -8
  149. package/build-module/tree-grid/index.js.map +1 -1
  150. package/build-module/unit-control/index.js +2 -2
  151. package/build-module/unit-control/index.js.map +1 -1
  152. package/build-module/unit-control/index.native.js +10 -2
  153. package/build-module/unit-control/index.native.js.map +1 -1
  154. package/build-module/unit-control/styles/unit-control-styles.js +20 -31
  155. package/build-module/unit-control/styles/unit-control-styles.js.map +1 -1
  156. package/build-module/utils/hooks/use-controlled-state.js +3 -4
  157. package/build-module/utils/hooks/use-controlled-state.js.map +1 -1
  158. package/build-style/style-rtl.css +13 -2
  159. package/build-style/style.css +13 -2
  160. package/build-types/button/index.d.ts.map +1 -1
  161. package/build-types/draggable/index.d.ts.map +1 -1
  162. package/build-types/drop-zone/index.d.ts.map +1 -1
  163. package/build-types/external-link/styles/external-link-styles.d.ts +1 -1
  164. package/build-types/focal-point-picker/styles/focal-point-style.d.ts +3 -3
  165. package/build-types/menu-item/index.d.ts.map +1 -1
  166. package/build-types/popover/index.d.ts.map +1 -1
  167. package/build-types/popover/limit-shift.d.ts +87 -0
  168. package/build-types/popover/limit-shift.d.ts.map +1 -0
  169. package/build-types/popover/stories/e2e/index.d.ts +8 -0
  170. package/build-types/popover/stories/e2e/index.d.ts.map +1 -0
  171. package/build-types/popover/test/index.d.ts +2 -0
  172. package/build-types/popover/test/index.d.ts.map +1 -0
  173. package/build-types/popover/types.d.ts +1 -1
  174. package/build-types/popover/types.d.ts.map +1 -1
  175. package/build-types/popover/utils.d.ts.map +1 -1
  176. package/build-types/range-control/input-range.d.ts.map +1 -1
  177. package/build-types/range-control/stories/index.d.ts.map +1 -1
  178. package/build-types/range-control/types.d.ts +0 -34
  179. package/build-types/range-control/types.d.ts.map +1 -1
  180. package/build-types/range-control/utils.d.ts +1 -12
  181. package/build-types/range-control/utils.d.ts.map +1 -1
  182. package/build-types/spinner/index.d.ts +1 -1
  183. package/build-types/tools-panel/styles.d.ts +6 -0
  184. package/build-types/tools-panel/styles.d.ts.map +1 -1
  185. package/build-types/tools-panel/tools-panel-header/component.d.ts.map +1 -1
  186. package/build-types/tools-panel/tools-panel-header/hook.d.ts +1 -0
  187. package/build-types/tools-panel/tools-panel-header/hook.d.ts.map +1 -1
  188. package/build-types/tools-panel/types.d.ts +1 -0
  189. package/build-types/tools-panel/types.d.ts.map +1 -1
  190. package/build-types/unit-control/styles/unit-control-styles.d.ts +1 -4
  191. package/build-types/unit-control/styles/unit-control-styles.d.ts.map +1 -1
  192. package/build-types/utils/hooks/use-controlled-state.d.ts.map +1 -1
  193. package/package.json +17 -17
  194. package/src/autocomplete/get-default-use-items.js +1 -1
  195. package/src/box-control/unit-control.js +0 -1
  196. package/src/button/index.js +7 -1
  197. package/src/button/test/index.js +36 -0
  198. package/src/color-palette/test/__snapshots__/index.js.snap +169 -1106
  199. package/src/color-palette/test/index.js +107 -61
  200. package/src/draggable/index.js +2 -5
  201. package/src/drop-zone/index.tsx +4 -1
  202. package/src/font-size-picker/stories/e2e/index.js +47 -0
  203. package/src/higher-order/with-filters/index.js +2 -2
  204. package/src/index.js +1 -1
  205. package/src/menu-item/README.md +7 -0
  206. package/src/menu-item/index.js +11 -5
  207. package/src/menu-item/style.scss +1 -0
  208. package/src/menu-item/test/index.js +36 -0
  209. package/src/mobile/bottom-sheet/bottom-sheet-navigation/navigation-container.native.js +9 -0
  210. package/src/mobile/bottom-sheet/bottom-sheet-navigation/navigation-screen.native.js +12 -2
  211. package/src/mobile/bottom-sheet/sub-sheet/index.native.js +3 -0
  212. package/src/mobile/color-settings/index.native.js +3 -0
  213. package/src/mobile/color-settings/picker-screen.native.js +3 -0
  214. package/src/mobile/image/index.native.js +3 -0
  215. package/src/mobile/keyboard-avoiding-view/index.ios.js +3 -0
  216. package/src/mobile/link-picker/index.native.js +3 -0
  217. package/src/mobile/link-picker/link-picker-results.native.js +4 -1
  218. package/src/mobile/link-picker/link-picker-screen.native.js +3 -0
  219. package/src/mobile/link-settings/index.native.js +18 -0
  220. package/src/mobile/link-settings/link-settings-screen.native.js +3 -0
  221. package/src/mobile/segmented-control/index.native.js +6 -0
  222. package/src/mobile/utils/use-unit-converter-to-mobile.native.js +6 -0
  223. package/src/notice/index.native.js +17 -20
  224. package/src/notice/list.native.js +7 -3
  225. package/src/palette-edit/index.js +1 -1
  226. package/src/placeholder/style.scss +1 -1
  227. package/src/popover/index.tsx +26 -42
  228. package/src/popover/limit-shift.ts +205 -0
  229. package/src/popover/stories/e2e/index.tsx +25 -0
  230. package/src/popover/test/index.tsx +230 -0
  231. package/src/popover/types.ts +1 -0
  232. package/src/popover/utils.ts +58 -16
  233. package/src/range-control/README.md +6 -7
  234. package/src/range-control/input-range.tsx +1 -21
  235. package/src/range-control/stories/index.tsx +2 -1
  236. package/src/range-control/tooltip.tsx +1 -1
  237. package/src/range-control/types.ts +0 -35
  238. package/src/range-control/utils.ts +3 -75
  239. package/src/tools-panel/stories/index.js +27 -0
  240. package/src/tools-panel/styles.ts +28 -1
  241. package/src/tools-panel/tools-panel-header/component.tsx +12 -5
  242. package/src/tools-panel/tools-panel-header/hook.ts +5 -0
  243. package/src/tools-panel/types.ts +1 -0
  244. package/src/tree-grid/index.js +23 -14
  245. package/src/tree-grid/test/__snapshots__/cell.js.snap +21 -17
  246. package/src/tree-grid/test/__snapshots__/index.js.snap +1 -1
  247. package/src/unit-control/index.native.js +8 -0
  248. package/src/unit-control/index.tsx +2 -2
  249. package/src/unit-control/styles/unit-control-styles.ts +0 -20
  250. package/src/unit-control/test/index.tsx +6 -3
  251. package/src/utils/hooks/use-controlled-state.js +9 -6
  252. package/tsconfig.tsbuildinfo +1 -1
  253. package/src/popover/test/__snapshots__/index.js.snap +0 -34
  254. package/src/popover/test/index.js +0 -164
  255. package/src/unit-control/test/__snapshots__/index.tsx.snap +0 -31
@@ -37,6 +37,9 @@ const LinkSettingsScreen = ( props ) => {
37
37
  urlValue={ inputValue }
38
38
  />
39
39
  );
40
+ // Disable reason: deferring this refactor to the native team.
41
+ // see https://github.com/WordPress/gutenberg/pull/41166
42
+ // eslint-disable-next-line react-hooks/exhaustive-deps
40
43
  }, [ props, inputValue, navigation, route ] );
41
44
  };
42
45
 
@@ -74,12 +74,18 @@ const SegmentedControls = ( {
74
74
  useEffect( () => {
75
75
  setActiveSegmentIndex( selectedSegmentIndex );
76
76
  segmentHandler( segments[ selectedSegmentIndex ] );
77
+ // Disable reason: deferring this refactor to the native team.
78
+ // see https://github.com/WordPress/gutenberg/pull/41166
79
+ // eslint-disable-next-line react-hooks/exhaustive-deps
77
80
  }, [] );
78
81
 
79
82
  useEffect( () => {
80
83
  positionAnimationValue.setValue(
81
84
  calculateEndValue( activeSegmentIndex )
82
85
  );
86
+ // Disable reason: deferring this refactor to the native team.
87
+ // see https://github.com/WordPress/gutenberg/pull/41166
88
+ // eslint-disable-next-line react-hooks/exhaustive-deps
83
89
  }, [ segmentsDimensions ] );
84
90
 
85
91
  const containerStyle = usePreferredColorSchemeStyle(
@@ -78,6 +78,9 @@ const useConvertUnitToMobile = ( value, unit ) => {
78
78
  return () => {
79
79
  dimensionsChangeSubscription.remove();
80
80
  };
81
+ // Disable reason: deferring this refactor to the native team.
82
+ // see https://github.com/WordPress/gutenberg/pull/41166
83
+ // eslint-disable-next-line react-hooks/exhaustive-deps
81
84
  }, [] );
82
85
 
83
86
  const onDimensionsChange = useCallback( ( { window } ) => {
@@ -94,6 +97,9 @@ const useConvertUnitToMobile = ( value, unit ) => {
94
97
  valueToConvert,
95
98
  valueUnit
96
99
  );
100
+ // Disable reason: deferring this refactor to the native team.
101
+ // see https://github.com/WordPress/gutenberg/pull/41166
102
+ // eslint-disable-next-line react-hooks/exhaustive-deps
97
103
  }, [ windowSizes, value, unit ] );
98
104
  };
99
105
 
@@ -14,7 +14,7 @@ import { BlurView } from '@react-native-community/blur';
14
14
  /**
15
15
  * WordPress dependencies
16
16
  */
17
- import { useEffect, useRef, Platform } from '@wordpress/element';
17
+ import { useEffect, useRef, useCallback, Platform } from '@wordpress/element';
18
18
  import { usePreferredColorSchemeStyle } from '@wordpress/compose';
19
19
 
20
20
  /**
@@ -30,40 +30,37 @@ const Notice = ( { onNoticeHidden, content, id, status } ) => {
30
30
  const timer = useRef( null );
31
31
 
32
32
  useEffect( () => {
33
- startAnimation();
34
-
35
- return () => {
36
- clearTimeout( timer?.current );
37
- };
38
- }, [] );
39
-
40
- function onHide() {
33
+ // start animation
41
34
  Animated.timing( animationValue, {
42
- toValue: 0,
43
- duration: 150,
35
+ toValue: 1,
36
+ duration: 300,
44
37
  useNativeDriver: true,
45
38
  easing: Easing.out( Easing.quad ),
46
39
  } ).start( ( { finished } ) => {
47
40
  if ( finished && onNoticeHidden ) {
48
- onNoticeHidden( id );
41
+ timer.current = setTimeout( () => {
42
+ onHide();
43
+ }, HIDE_TIMER );
49
44
  }
50
45
  } );
51
- }
52
46
 
53
- function startAnimation() {
47
+ return () => {
48
+ clearTimeout( timer?.current );
49
+ };
50
+ }, [ animationValue, onHide, onNoticeHidden ] );
51
+
52
+ const onHide = useCallback( () => {
54
53
  Animated.timing( animationValue, {
55
- toValue: 1,
56
- duration: 300,
54
+ toValue: 0,
55
+ duration: 150,
57
56
  useNativeDriver: true,
58
57
  easing: Easing.out( Easing.quad ),
59
58
  } ).start( ( { finished } ) => {
60
59
  if ( finished && onNoticeHidden ) {
61
- timer.current = setTimeout( () => {
62
- onHide();
63
- }, HIDE_TIMER );
60
+ onNoticeHidden( id );
64
61
  }
65
62
  } );
66
- }
63
+ }, [ animationValue, onNoticeHidden, id ] );
67
64
 
68
65
  const noticeSolidStyles = usePreferredColorSchemeStyle(
69
66
  styles.noticeSolid,
@@ -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,25 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { useState } from '@wordpress/element';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import Popover from '../..';
10
+
11
+ export default {
12
+ title: 'Components/Popover',
13
+ component: Popover,
14
+ };
15
+
16
+ export const Default = () => {
17
+ const [ isVisible, setIsVisible ] = useState( false );
18
+
19
+ return (
20
+ <button onClick={ () => setIsVisible( ( state ) => ! state ) }>
21
+ Toggle Popover!
22
+ { isVisible && <Popover>Popover is toggled!</Popover> }
23
+ </button>
24
+ );
25
+ };