@wordpress/media-editor 0.8.0 → 0.8.1-next.v.202605131006.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 (217) hide show
  1. package/build/components/media-editor/index.cjs +560 -0
  2. package/build/components/media-editor/index.cjs.map +7 -0
  3. package/build/components/media-editor-canvas/index.cjs +14 -2
  4. package/build/components/media-editor-canvas/index.cjs.map +2 -2
  5. package/build/components/media-editor-crop-panel/index.cjs +34 -27
  6. package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
  7. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs +162 -0
  8. package/build/components/media-editor-keyboard-shortcuts-modal/index.cjs.map +7 -0
  9. package/build/components/media-editor-modal/index.cjs +42 -430
  10. package/build/components/media-editor-modal/index.cjs.map +3 -3
  11. package/build/components/media-editor-toolbar/index.cjs +143 -75
  12. package/build/components/media-editor-toolbar/index.cjs.map +3 -3
  13. package/build/components/media-form/index.cjs +3 -0
  14. package/build/components/media-form/index.cjs.map +2 -2
  15. package/build/components/rotation-ruler/index.cjs +197 -0
  16. package/build/components/rotation-ruler/index.cjs.map +7 -0
  17. package/build/components/rotation-ruler/use-ruler-drag.cjs +154 -0
  18. package/build/components/rotation-ruler/use-ruler-drag.cjs.map +7 -0
  19. package/build/hooks/use-crop-gesture-handlers.cjs +43 -0
  20. package/build/hooks/use-crop-gesture-handlers.cjs.map +7 -0
  21. package/build/image-editor/core/constants.cjs +9 -0
  22. package/build/image-editor/core/constants.cjs.map +2 -2
  23. package/build/image-editor/core/containment.cjs +7 -13
  24. package/build/image-editor/core/containment.cjs.map +2 -2
  25. package/build/image-editor/core/interaction-controller.cjs +43 -7
  26. package/build/image-editor/core/interaction-controller.cjs.map +2 -2
  27. package/build/image-editor/core/state.cjs +5 -1
  28. package/build/image-editor/core/state.cjs.map +2 -2
  29. package/build/image-editor/core/transform-style.cjs.map +1 -1
  30. package/build/image-editor/core/types.cjs.map +1 -1
  31. package/build/image-editor/core/viewport-state.cjs +50 -0
  32. package/build/image-editor/core/viewport-state.cjs.map +7 -0
  33. package/build/image-editor/react/components/cropper.cjs +192 -72
  34. package/build/image-editor/react/components/cropper.cjs.map +3 -3
  35. package/build/image-editor/react/components/overlays/dimming-overlay.cjs +5 -2
  36. package/build/image-editor/react/components/overlays/dimming-overlay.cjs.map +2 -2
  37. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs +16 -4
  38. package/build/image-editor/react/components/stencils/rectangle-stencil.cjs.map +2 -2
  39. package/build/image-editor/react/components/viewport-provider.cjs +51 -0
  40. package/build/image-editor/react/components/viewport-provider.cjs.map +7 -0
  41. package/build/image-editor/react/hooks/use-aria-announcer.cjs +53 -6
  42. package/build/image-editor/react/hooks/use-aria-announcer.cjs.map +2 -2
  43. package/build/image-editor/react/hooks/use-cropper-state.cjs +127 -8
  44. package/build/image-editor/react/hooks/use-cropper-state.cjs.map +2 -2
  45. package/build/image-editor/react/hooks/use-interaction.cjs +26 -5
  46. package/build/image-editor/react/hooks/use-interaction.cjs.map +2 -2
  47. package/build/image-editor/react/hooks/use-viewport-state.cjs +51 -0
  48. package/build/image-editor/react/hooks/use-viewport-state.cjs.map +7 -0
  49. package/build/image-editor/react/visually-hidden-style.cjs +41 -0
  50. package/build/image-editor/react/visually-hidden-style.cjs.map +7 -0
  51. package/build/private-apis.cjs +2 -0
  52. package/build/private-apis.cjs.map +2 -2
  53. package/build-module/components/media-editor/index.mjs +545 -0
  54. package/build-module/components/media-editor/index.mjs.map +7 -0
  55. package/build-module/components/media-editor-canvas/index.mjs +14 -2
  56. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  57. package/build-module/components/media-editor-crop-panel/index.mjs +35 -28
  58. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  59. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs +144 -0
  60. package/build-module/components/media-editor-keyboard-shortcuts-modal/index.mjs.map +7 -0
  61. package/build-module/components/media-editor-modal/index.mjs +46 -455
  62. package/build-module/components/media-editor-modal/index.mjs.map +2 -2
  63. package/build-module/components/media-editor-toolbar/index.mjs +137 -77
  64. package/build-module/components/media-editor-toolbar/index.mjs.map +2 -2
  65. package/build-module/components/media-form/index.mjs +3 -0
  66. package/build-module/components/media-form/index.mjs.map +2 -2
  67. package/build-module/components/rotation-ruler/index.mjs +166 -0
  68. package/build-module/components/rotation-ruler/index.mjs.map +7 -0
  69. package/build-module/components/rotation-ruler/use-ruler-drag.mjs +126 -0
  70. package/build-module/components/rotation-ruler/use-ruler-drag.mjs.map +7 -0
  71. package/build-module/hooks/use-crop-gesture-handlers.mjs +17 -0
  72. package/build-module/hooks/use-crop-gesture-handlers.mjs.map +7 -0
  73. package/build-module/image-editor/core/constants.mjs +6 -0
  74. package/build-module/image-editor/core/constants.mjs.map +2 -2
  75. package/build-module/image-editor/core/containment.mjs +6 -12
  76. package/build-module/image-editor/core/containment.mjs.map +2 -2
  77. package/build-module/image-editor/core/interaction-controller.mjs +50 -8
  78. package/build-module/image-editor/core/interaction-controller.mjs.map +2 -2
  79. package/build-module/image-editor/core/state.mjs +5 -1
  80. package/build-module/image-editor/core/state.mjs.map +2 -2
  81. package/build-module/image-editor/core/transform-style.mjs.map +1 -1
  82. package/build-module/image-editor/core/viewport-state.mjs +24 -0
  83. package/build-module/image-editor/core/viewport-state.mjs.map +7 -0
  84. package/build-module/image-editor/react/components/cropper.mjs +195 -73
  85. package/build-module/image-editor/react/components/cropper.mjs.map +3 -3
  86. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs +5 -2
  87. package/build-module/image-editor/react/components/overlays/dimming-overlay.mjs.map +2 -2
  88. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs +26 -5
  89. package/build-module/image-editor/react/components/stencils/rectangle-stencil.mjs.map +2 -2
  90. package/build-module/image-editor/react/components/viewport-provider.mjs +27 -0
  91. package/build-module/image-editor/react/components/viewport-provider.mjs.map +7 -0
  92. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs +59 -7
  93. package/build-module/image-editor/react/hooks/use-aria-announcer.mjs.map +2 -2
  94. package/build-module/image-editor/react/hooks/use-cropper-state.mjs +134 -9
  95. package/build-module/image-editor/react/hooks/use-cropper-state.mjs.map +2 -2
  96. package/build-module/image-editor/react/hooks/use-interaction.mjs +26 -5
  97. package/build-module/image-editor/react/hooks/use-interaction.mjs.map +2 -2
  98. package/build-module/image-editor/react/hooks/use-viewport-state.mjs +29 -0
  99. package/build-module/image-editor/react/hooks/use-viewport-state.mjs.map +7 -0
  100. package/build-module/image-editor/react/visually-hidden-style.mjs +16 -0
  101. package/build-module/image-editor/react/visually-hidden-style.mjs.map +7 -0
  102. package/build-module/private-apis.mjs +2 -0
  103. package/build-module/private-apis.mjs.map +2 -2
  104. package/build-style/style-rtl.css +254 -29
  105. package/build-style/style.css +254 -29
  106. package/build-types/components/media-editor/index.d.ts +34 -0
  107. package/build-types/components/media-editor/index.d.ts.map +1 -0
  108. package/build-types/components/media-editor-canvas/index.d.ts +10 -1
  109. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  110. package/build-types/components/media-editor-crop-panel/index.d.ts +1 -1
  111. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  112. package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts +6 -0
  113. package/build-types/components/media-editor-keyboard-shortcuts-modal/index.d.ts.map +1 -0
  114. package/build-types/components/media-editor-modal/index.d.ts +2 -2
  115. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  116. package/build-types/components/media-editor-provider/index.d.ts +1 -1
  117. package/build-types/components/media-editor-provider/index.d.ts.map +1 -1
  118. package/build-types/components/media-editor-toolbar/index.d.ts +4 -1
  119. package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -1
  120. package/build-types/components/media-form/index.d.ts +1 -1
  121. package/build-types/components/media-form/index.d.ts.map +1 -1
  122. package/build-types/components/rotation-ruler/index.d.ts +46 -0
  123. package/build-types/components/rotation-ruler/index.d.ts.map +1 -0
  124. package/build-types/components/rotation-ruler/stories/index.story.d.ts +14 -0
  125. package/build-types/components/rotation-ruler/stories/index.story.d.ts.map +1 -0
  126. package/build-types/components/rotation-ruler/use-ruler-drag.d.ts +61 -0
  127. package/build-types/components/rotation-ruler/use-ruler-drag.d.ts.map +1 -0
  128. package/build-types/hooks/use-crop-gesture-handlers.d.ts +42 -0
  129. package/build-types/hooks/use-crop-gesture-handlers.d.ts.map +1 -0
  130. package/build-types/image-editor/core/constants.d.ts +9 -0
  131. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  132. package/build-types/image-editor/core/containment.d.ts +11 -7
  133. package/build-types/image-editor/core/containment.d.ts.map +1 -1
  134. package/build-types/image-editor/core/interaction-controller.d.ts +14 -2
  135. package/build-types/image-editor/core/interaction-controller.d.ts.map +1 -1
  136. package/build-types/image-editor/core/state.d.ts.map +1 -1
  137. package/build-types/image-editor/core/transform-style.d.ts +1 -1
  138. package/build-types/image-editor/core/types.d.ts +25 -0
  139. package/build-types/image-editor/core/types.d.ts.map +1 -1
  140. package/build-types/image-editor/core/viewport-state.d.ts +7 -0
  141. package/build-types/image-editor/core/viewport-state.d.ts.map +1 -0
  142. package/build-types/image-editor/react/components/cropper-provider.d.ts +1 -1
  143. package/build-types/image-editor/react/components/cropper-provider.d.ts.map +1 -1
  144. package/build-types/image-editor/react/components/cropper.d.ts +2 -1
  145. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  146. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts +3 -13
  147. package/build-types/image-editor/react/components/overlays/dimming-overlay.d.ts.map +1 -1
  148. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts +1 -1
  149. package/build-types/image-editor/react/components/overlays/grid-overlay.d.ts.map +1 -1
  150. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts +1 -1
  151. package/build-types/image-editor/react/components/stencils/rectangle-stencil.d.ts.map +1 -1
  152. package/build-types/image-editor/react/components/viewport-provider.d.ts +9 -0
  153. package/build-types/image-editor/react/components/viewport-provider.d.ts.map +1 -0
  154. package/build-types/image-editor/react/hooks/use-aria-announcer.d.ts.map +1 -1
  155. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +29 -0
  156. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -1
  157. package/build-types/image-editor/react/hooks/use-interaction.d.ts +5 -2
  158. package/build-types/image-editor/react/hooks/use-interaction.d.ts.map +1 -1
  159. package/build-types/image-editor/react/hooks/use-viewport-state.d.ts +19 -0
  160. package/build-types/image-editor/react/hooks/use-viewport-state.d.ts.map +1 -0
  161. package/build-types/image-editor/react/visually-hidden-style.d.ts +6 -0
  162. package/build-types/image-editor/react/visually-hidden-style.d.ts.map +1 -0
  163. package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +1 -1
  164. package/build-types/private-apis.d.ts.map +1 -1
  165. package/build-types/store/actions.d.ts +3 -3
  166. package/build-types/store/actions.d.ts.map +1 -1
  167. package/package.json +16 -15
  168. package/src/components/media-editor/index.tsx +709 -0
  169. package/src/components/media-editor/style.scss +89 -0
  170. package/src/components/media-editor-canvas/index.tsx +24 -0
  171. package/src/components/media-editor-crop-panel/index.tsx +39 -27
  172. package/src/components/media-editor-crop-panel/test/index.tsx +109 -0
  173. package/src/components/media-editor-keyboard-shortcuts-modal/index.tsx +177 -0
  174. package/src/components/media-editor-keyboard-shortcuts-modal/style.scss +53 -0
  175. package/src/components/media-editor-modal/index.tsx +49 -589
  176. package/src/components/media-editor-modal/style.scss +13 -112
  177. package/src/components/media-editor-toolbar/index.tsx +122 -63
  178. package/src/components/media-editor-toolbar/style.scss +39 -11
  179. package/src/components/media-form/index.tsx +5 -0
  180. package/src/components/rotation-ruler/README.md +37 -0
  181. package/src/components/rotation-ruler/index.tsx +266 -0
  182. package/src/components/rotation-ruler/stories/index.story.tsx +54 -0
  183. package/src/components/rotation-ruler/style.scss +147 -0
  184. package/src/components/rotation-ruler/test/index.tsx +109 -0
  185. package/src/components/rotation-ruler/use-ruler-drag.ts +230 -0
  186. package/src/hooks/use-crop-gesture-handlers.ts +54 -0
  187. package/src/image-editor/core/constants.ts +12 -0
  188. package/src/image-editor/core/containment.ts +16 -28
  189. package/src/image-editor/core/interaction-controller.ts +66 -10
  190. package/src/image-editor/core/state.ts +15 -8
  191. package/src/image-editor/core/test/camera.ts +15 -26
  192. package/src/image-editor/core/test/interaction-controller.ts +158 -7
  193. package/src/image-editor/core/test/state.ts +21 -1
  194. package/src/image-editor/core/transform-style.ts +1 -1
  195. package/src/image-editor/core/types.ts +16 -0
  196. package/src/image-editor/core/viewport-state.ts +33 -0
  197. package/src/image-editor/docs/architecture.md +1 -1
  198. package/src/image-editor/docs/recipes.md +2 -2
  199. package/src/image-editor/react/components/cropper.scss +28 -0
  200. package/src/image-editor/react/components/cropper.tsx +268 -72
  201. package/src/image-editor/react/components/overlays/dimming-overlay.tsx +12 -12
  202. package/src/image-editor/react/components/stencils/rectangle-stencil.tsx +27 -8
  203. package/src/image-editor/react/components/stencils/test/rectangle-stencil.tsx +29 -3
  204. package/src/image-editor/react/components/test/cropper.tsx +294 -5
  205. package/src/image-editor/react/components/viewport-provider.tsx +39 -0
  206. package/src/image-editor/react/hooks/test/use-aria-announcer.ts +81 -0
  207. package/src/image-editor/react/hooks/test/use-cropper-state.ts +286 -0
  208. package/src/image-editor/react/hooks/test/use-interaction.ts +1 -0
  209. package/src/image-editor/react/hooks/use-aria-announcer.ts +68 -7
  210. package/src/image-editor/react/hooks/use-cropper-state.ts +230 -11
  211. package/src/image-editor/react/hooks/use-interaction.ts +36 -6
  212. package/src/image-editor/react/hooks/use-viewport-state.ts +48 -0
  213. package/src/image-editor/react/visually-hidden-style.ts +15 -0
  214. package/src/private-apis.ts +2 -0
  215. package/src/style.scss +3 -0
  216. package/tsconfig.json +1 -0
  217. package/tsconfig.tsbuildinfo +1 -1
@@ -13,6 +13,8 @@ import {
13
13
  useMemo,
14
14
  useRef,
15
15
  useEffect,
16
+ useLayoutEffect,
17
+ useId,
16
18
  forwardRef,
17
19
  } from '@wordpress/element';
18
20
  import { __ } from '@wordpress/i18n';
@@ -28,14 +30,15 @@ import type {
28
30
  } from '../../core/types';
29
31
  import type { UseCropperStateReturn } from '../hooks/use-cropper-state';
30
32
  import { getImageFit } from '../../core/camera';
31
- import { getCropBounds } from '../../core/containment';
33
+ import { getImageCropBounds } from '../../core/containment';
32
34
  import { useInteraction } from '../hooks/use-interaction';
33
35
  import { useTransformStyle } from '../hooks/use-transform-style';
34
36
  import { useAriaAnnouncer } from '../hooks/use-aria-announcer';
35
37
  import { RectangleStencil } from './stencils/rectangle-stencil';
36
38
  import { DimmingOverlay } from './overlays/dimming-overlay';
37
39
  import { GridOverlay } from './overlays/grid-overlay';
38
- import './cropper.scss';
40
+ import { ViewportProvider, useViewport } from './viewport-provider';
41
+ import { VISUALLY_HIDDEN_STYLE } from '../visually-hidden-style';
39
42
 
40
43
  /** Threshold for comparing normalized crop rect values. */
41
44
  const CROP_RECT_EPSILON = 1e-6;
@@ -99,6 +102,8 @@ export interface CropperProps {
99
102
  * When true, the crop area has resize handles and can be freely repositioned.
100
103
  */
101
104
  freeformCrop?: boolean;
105
+ /** Focus the crop area when the cropper mounts. */
106
+ focusOnMount?: boolean;
102
107
  /** Callback fired when the image is loaded. */
103
108
  onImageLoaded?: ( size: Size ) => void;
104
109
  /**
@@ -141,6 +146,7 @@ export interface CropperProps {
141
146
  * @param root0.maxZoom Maximum zoom level.
142
147
  * @param root0.aspectRatio Fixed aspect ratio (width/height).
143
148
  * @param root0.freeformCrop Enable resize handles.
149
+ * @param root0.focusOnMount Focus the crop area on mount.
144
150
  * @param root0.onImageLoaded Image load callback.
145
151
  * @param root0.onStateChange Every-frame state callback.
146
152
  * @param root0.onGestureStart Gesture boundary start.
@@ -160,6 +166,7 @@ function CropperInner(
160
166
  maxZoom,
161
167
  aspectRatio,
162
168
  freeformCrop = false,
169
+ focusOnMount = false,
163
170
  onImageLoaded,
164
171
  onStateChange,
165
172
  onGestureStart,
@@ -169,15 +176,59 @@ function CropperInner(
169
176
  ref: React.ForwardedRef< HTMLDivElement >
170
177
  ) {
171
178
  const { state, setImage, setCropRect, settleCrop } = controller;
179
+ const {
180
+ viewport: viewportState,
181
+ setViewportPan,
182
+ resetViewport,
183
+ } = useViewport();
172
184
  // Canvas measurement via ResizeObserver. The canvas is the inner
173
185
  // positioning context for image/stencil/handles — inset from the root
174
186
  // by the handle gutter, so crop math operates on the reduced box.
175
187
  const canvasRef = useRef< HTMLDivElement >( null );
188
+ const cropAreaDescriptionId = useId();
189
+ const [ isCropAreaFocused, setIsCropAreaFocused ] =
190
+ useState( focusOnMount );
191
+ const [ isFocusVisible, setIsFocusVisible ] = useState( false );
176
192
  const [ canvasSize, setCanvasSize ] = useState< Size >( {
177
193
  width: 0,
178
194
  height: 0,
179
195
  } );
180
196
 
197
+ useLayoutEffect( () => {
198
+ if ( focusOnMount ) {
199
+ canvasRef.current?.focus( { preventScroll: true } );
200
+ }
201
+ }, [ focusOnMount ] );
202
+
203
+ const handleCropAreaFocus = useCallback(
204
+ ( event: React.FocusEvent< HTMLDivElement > ) => {
205
+ if ( event.target === event.currentTarget ) {
206
+ setIsCropAreaFocused( true );
207
+ // Show the outline only when focus arrived via keyboard
208
+ // navigation. relatedTarget is null for pointer-initiated and
209
+ // cross-window focus, so it reliably excludes those cases
210
+ // without needing a separate ref or flag.
211
+ if (
212
+ event.relatedTarget !== null &&
213
+ event.currentTarget.matches( ':focus-visible' )
214
+ ) {
215
+ setIsFocusVisible( true );
216
+ }
217
+ }
218
+ },
219
+ []
220
+ );
221
+
222
+ const handleCropAreaBlur = useCallback(
223
+ ( event: React.FocusEvent< HTMLDivElement > ) => {
224
+ if ( event.target === event.currentTarget ) {
225
+ setIsCropAreaFocused( false );
226
+ setIsFocusVisible( false );
227
+ }
228
+ },
229
+ []
230
+ );
231
+
181
232
  useEffect( () => {
182
233
  const element = canvasRef.current;
183
234
  if ( ! element ) {
@@ -221,13 +272,16 @@ function CropperInner(
221
272
  [ canvasSize, naturalWidth, naturalHeight, state.rotation ]
222
273
  );
223
274
 
224
- // In fixed-crop mode, auto-size the crop rect to fill the visual area
225
- // while respecting the aspect ratio. The crop is always centered.
275
+ // In fixed-crop mode, auto-size the crop rect only when a fixed aspect
276
+ // ratio is selected. With "Free" selected, turning freeform handles off
277
+ // should preserve the user's current unconstrained crop.
226
278
  useEffect( () => {
227
279
  if (
228
280
  freeformCrop ||
229
281
  visualSize.width === 0 ||
230
- visualSize.height === 0
282
+ visualSize.height === 0 ||
283
+ ! aspectRatio ||
284
+ aspectRatio <= 0
231
285
  ) {
232
286
  return;
233
287
  }
@@ -266,7 +320,7 @@ function CropperInner(
266
320
  }, [ aspectRatio, freeformCrop, visualSize, setCropRect ] );
267
321
 
268
322
  // Compute the crop handle bounds from the actual image footprint.
269
- // Depends on the full state object because getCropBounds reads
323
+ // Depends on the full state object because getImageCropBounds reads
270
324
  // crop, zoom, rotation, flip, and image. React Compiler requires
271
325
  // the complete dependency; the computation is lightweight (a few
272
326
  // trig ops + 4 corner transforms).
@@ -274,8 +328,11 @@ function CropperInner(
274
328
  if ( ! state.image || elementSize.width === 0 ) {
275
329
  return undefined;
276
330
  }
277
- return getCropBounds( state, elementSize, visualSize, canvasSize );
278
- }, [ state, elementSize, visualSize, canvasSize ] );
331
+ return getImageCropBounds( state, elementSize, visualSize );
332
+ }, [ state, elementSize, visualSize ] );
333
+ const [ isResizing, setIsResizing ] = useState( false );
334
+ const isResizingRef = useRef( false );
335
+ const isSettlingRef = useRef( false );
279
336
 
280
337
  // Use the interaction hook for mouse, touch, and keyboard events.
281
338
  const {
@@ -291,6 +348,33 @@ function CropperInner(
291
348
  onGestureEnd,
292
349
  } );
293
350
 
351
+ // Compose focus-visibility tracking into the canvas event handlers.
352
+ // Kept as a spread rather than explicit props to avoid triggering
353
+ // jsx-a11y/no-noninteractive-element-interactions on role="group".
354
+ const canvasHandlers = {
355
+ ...handlers,
356
+ onPointerDown: ( event: React.PointerEvent< HTMLDivElement > ) => {
357
+ handlers.onPointerDown?.( event );
358
+ // Called after handlers so it lands last in the React batch —
359
+ // el.focus() inside the handler fires onFocus, which may
360
+ // otherwise set isFocusVisible back to true.
361
+ setIsFocusVisible( false );
362
+ },
363
+ onKeyDown: ( event: React.KeyboardEvent< HTMLDivElement > ) => {
364
+ // Guard against keydowns bubbling up from child handles
365
+ // (e.g. Tab between handles, unhandled keys). Modifier-only
366
+ // keypresses are excluded — they precede another key rather
367
+ // than indicating deliberate keyboard interaction on their own.
368
+ if (
369
+ event.target === event.currentTarget &&
370
+ ! [ 'Shift', 'Control', 'Alt', 'Meta' ].includes( event.key )
371
+ ) {
372
+ setIsFocusVisible( true );
373
+ }
374
+ handlers.onKeyDown?.( event );
375
+ },
376
+ };
377
+
294
378
  // Register wheel handler natively with { passive: false } so
295
379
  // preventDefault works. React's onWheel registers as passive. Bound
296
380
  // to the canvas (not the root) so pointer geometry inside the handler
@@ -300,11 +384,21 @@ function CropperInner(
300
384
  if ( ! el ) {
301
385
  return;
302
386
  }
303
- el.addEventListener( 'wheel', onWheelNative, {
387
+ const handleWheel = ( event: WheelEvent ) => {
388
+ // Block wheel zoom while resizing or settling — the stage CSS
389
+ // transform changes getBoundingClientRect() during this window,
390
+ // so focal-point math would resolve against the wrong center.
391
+ if ( isResizingRef.current || isSettlingRef.current ) {
392
+ event.preventDefault();
393
+ return;
394
+ }
395
+ onWheelNative( event );
396
+ };
397
+ el.addEventListener( 'wheel', handleWheel, {
304
398
  passive: false,
305
399
  } );
306
400
  return () => {
307
- el.removeEventListener( 'wheel', onWheelNative );
401
+ el.removeEventListener( 'wheel', handleWheel );
308
402
  };
309
403
  }, [ onWheelNative ] );
310
404
 
@@ -333,14 +427,45 @@ function CropperInner(
333
427
  [ src, setImage, onImageLoaded ]
334
428
  );
335
429
 
336
- /**
337
- * Handle crop rect changes from the stencil (during drag).
338
- */
339
430
  const handleCropChange = useCallback(
340
431
  ( rect: NormalizedRect ) => {
341
432
  setCropRect( rect );
433
+ // During a resize drag, pan the viewport so the handle stays
434
+ // visible even when the crop extends beyond the canvas edge.
435
+ if (
436
+ isResizingRef.current &&
437
+ visualSize.width > 0 &&
438
+ visualSize.height > 0
439
+ ) {
440
+ const offsetX = ( canvasSize.width - visualSize.width ) / 2;
441
+ const offsetY = ( canvasSize.height - visualSize.height ) / 2;
442
+ const rightOverflow = Math.max(
443
+ 0,
444
+ offsetX +
445
+ ( rect.x + rect.width ) * visualSize.width -
446
+ canvasSize.width
447
+ );
448
+ const leftOverflow = Math.max(
449
+ 0,
450
+ -( offsetX + rect.x * visualSize.width )
451
+ );
452
+ const bottomOverflow = Math.max(
453
+ 0,
454
+ offsetY +
455
+ ( rect.y + rect.height ) * visualSize.height -
456
+ canvasSize.height
457
+ );
458
+ const topOverflow = Math.max(
459
+ 0,
460
+ -( offsetY + rect.y * visualSize.height )
461
+ );
462
+ setViewportPan( {
463
+ x: -rightOverflow + leftOverflow,
464
+ y: -bottomOverflow + topOverflow,
465
+ } );
466
+ }
342
467
  },
343
- [ setCropRect ]
468
+ [ setCropRect, setViewportPan, canvasSize, visualSize ]
344
469
  );
345
470
 
346
471
  // Settling animation: brief linear transition after resize end.
@@ -355,7 +480,6 @@ function CropperInner(
355
480
  };
356
481
  }, [] );
357
482
 
358
- const [ isResizing, setIsResizing ] = useState( false );
359
483
  const isInteractiveGrid = showGrid === 'interactive';
360
484
  const showInteractiveGrid =
361
485
  isInteractiveGrid &&
@@ -366,32 +490,54 @@ function CropperInner(
366
490
  * arrow keys pan the image rather than resize.
367
491
  */
368
492
  const handleEscape = useCallback( () => {
493
+ // Escape is always a keyboard action, so show the outline immediately
494
+ // rather than relying on the focus handler's :focus-visible check.
495
+ setIsFocusVisible( true );
369
496
  canvasRef.current?.focus( { preventScroll: true } );
370
497
  }, [] );
371
498
 
372
499
  const handleResizeStart = useCallback( () => {
500
+ isResizingRef.current = true;
373
501
  setIsResizing( true );
502
+ // Clear any in-flight settle so transitions don't apply during the
503
+ // new drag (rapid successive resizes would otherwise inherit the
504
+ // previous settle animation).
505
+ clearTimeout( settleTimerRef.current );
506
+ isSettlingRef.current = false;
507
+ setSettling( false );
508
+ resetViewport();
374
509
  onGestureStart?.();
375
- }, [ onGestureStart ] );
510
+ }, [ onGestureStart, resetViewport ] );
376
511
 
377
512
  /**
378
- * Handle resize end — settle the crop rect (re-center, fill height).
513
+ * Handle resize end — settle the crop rect (re-center, fill height)
514
+ * and reset the viewport pan back to neutral.
379
515
  */
380
516
  const handleResizeEnd = useCallback( () => {
517
+ isResizingRef.current = false;
381
518
  setIsResizing( false );
519
+ isSettlingRef.current = true;
382
520
  setSettling( true );
521
+ // Reset viewport pan first so it transitions back to zero in sync
522
+ // with the settle animation on the image.
523
+ resetViewport();
383
524
  settleCrop();
384
525
  onGestureEnd?.();
385
526
  clearTimeout( settleTimerRef.current );
386
527
  settleTimerRef.current = setTimeout( () => {
528
+ isSettlingRef.current = false;
387
529
  setSettling( false );
388
530
  }, 200 );
389
- }, [ settleCrop, onGestureEnd ] );
531
+ }, [ settleCrop, onGestureEnd, resetViewport ] );
390
532
 
391
- const imageTransition =
392
- settling || isZooming ? 'transform 150ms linear' : undefined;
533
+ let imageTransition: string | undefined;
534
+ if ( settling ) {
535
+ imageTransition = 'transform 200ms ease-out';
536
+ } else if ( isZooming ) {
537
+ imageTransition = 'transform 150ms linear';
538
+ }
393
539
  const settleStencilTransition = settling
394
- ? 'left 150ms linear, top 150ms linear, width 150ms linear, height 150ms linear'
540
+ ? 'left 200ms ease-out, top 200ms ease-out, width 200ms ease-out, height 200ms ease-out'
395
541
  : undefined;
396
542
 
397
543
  // Compute the image's CSS style.
@@ -413,6 +559,24 @@ function CropperInner(
413
559
  };
414
560
  }, [ canvasSize, elementSize, transformString, imageTransition ] );
415
561
 
562
+ // Viewport pan CSS transform for the stage div. Applied during resize
563
+ // drags to keep handles visible when the crop extends past the canvas edge.
564
+ // Transitions back to zero during the settle animation.
565
+ // will-change promotes the stage to its own compositor layer while the
566
+ // transform is active, keeping per-frame pan updates off the main thread.
567
+ const settleTransition = settling ? 'transform 200ms ease-out' : undefined;
568
+ const willChange = isResizing || settling ? 'transform' : undefined;
569
+ let stageStyle: React.CSSProperties | undefined;
570
+ if ( viewportState.pan.x !== 0 || viewportState.pan.y !== 0 ) {
571
+ stageStyle = {
572
+ transform: `translate(${ viewportState.pan.x }px, ${ viewportState.pan.y }px)`,
573
+ transition: settleTransition,
574
+ willChange,
575
+ };
576
+ } else if ( settling ) {
577
+ stageStyle = { transition: settleTransition, willChange };
578
+ }
579
+
416
580
  // Forward the root element to the consumer's ref.
417
581
  const setContainerRef = useCallback(
418
582
  ( element: HTMLDivElement | null ) => {
@@ -455,72 +619,96 @@ function CropperInner(
455
619
  isInteractiveGrid &&
456
620
  'wp-media-editor-image-editor__canvas--grid-interactive',
457
621
  showInteractiveGrid &&
458
- 'wp-media-editor-image-editor__canvas--show-grid'
622
+ 'wp-media-editor-image-editor__canvas--show-grid',
623
+ settling &&
624
+ 'wp-media-editor-image-editor__canvas--settling',
625
+ // Show the keyboard focus outline only when the canvas
626
+ // itself (not a child handle) has keyboard focus.
627
+ isFocusVisible &&
628
+ isCropAreaFocused &&
629
+ 'wp-media-editor-image-editor__canvas--focus-visible'
459
630
  ) }
460
631
  tabIndex={ 0 }
461
632
  role="group"
462
- aria-label={ __( 'Image editor' ) }
463
- { ...handlers }
633
+ aria-label={ __( 'Crop area' ) }
634
+ aria-describedby={
635
+ isCropAreaFocused ? cropAreaDescriptionId : undefined
636
+ }
637
+ onFocus={ handleCropAreaFocus }
638
+ onBlur={ handleCropAreaBlur }
639
+ { ...canvasHandlers }
464
640
  >
465
- { /* The image layer */ }
466
- <img
467
- className="wp-media-editor-image-editor__image"
468
- src={ src }
469
- alt=""
470
- onLoad={ handleImageLoad }
471
- style={ imageStyle }
472
- draggable={ false }
473
- />
474
-
475
- { /* Dimming overlay outside the crop area */ }
476
- { showDimming && (
477
- <DimmingOverlay
478
- cropRect={ state.cropRect }
479
- containerSize={ canvasSize }
480
- imageSize={ visualSize }
641
+ <div
642
+ id={ cropAreaDescriptionId }
643
+ style={ VISUALLY_HIDDEN_STYLE }
644
+ >
645
+ { __(
646
+ 'When this area is focused, use arrow keys to move the image and plus or minus to zoom. Tab to resize handles and controls.'
647
+ ) }
648
+ </div>
649
+ { /*
650
+ * The stage is an inner full-size div that receives the
651
+ * viewport pan CSS transform. Keeping the transform here
652
+ * (rather than on the canvas) means the canvas always stays
653
+ * at its natural position, so the root div's background is
654
+ * never exposed when the stage is panned during a resize drag.
655
+ */ }
656
+ <div
657
+ className="wp-media-editor-image-editor__stage"
658
+ data-testid="cropper-stage"
659
+ style={ stageStyle }
660
+ >
661
+ { /* The image layer */ }
662
+ <img
663
+ className="wp-media-editor-image-editor__image"
664
+ src={ src }
665
+ alt=""
666
+ onLoad={ handleImageLoad }
667
+ style={ imageStyle }
668
+ draggable={ false }
481
669
  />
482
- ) }
483
670
 
484
- { /* The stencil (crop area with handles) */ }
485
- <StencilComponent
486
- cropRect={ state.cropRect }
487
- containerSize={ canvasSize }
488
- imageSize={ visualSize }
489
- onCropChange={ handleCropChange }
490
- onResizeStart={ handleResizeStart }
491
- onResizeEnd={ handleResizeEnd }
492
- onEscape={ handleEscape }
493
- aspectRatio={ aspectRatio }
494
- freeformCrop={ freeformCrop }
495
- stencilTransition={ settleStencilTransition }
496
- cropBounds={ cropBounds }
497
- />
498
-
499
- { /* Rule-of-thirds grid */ }
500
- { ( showGrid === true || isInteractiveGrid ) && (
501
- <GridOverlay
671
+ { /* Dimming overlay outside the crop area */ }
672
+ { showDimming && (
673
+ <DimmingOverlay
674
+ cropRect={ state.cropRect }
675
+ containerSize={ canvasSize }
676
+ imageSize={ visualSize }
677
+ transition={ settleStencilTransition }
678
+ />
679
+ ) }
680
+
681
+ { /* The stencil (crop area with handles) */ }
682
+ <StencilComponent
502
683
  cropRect={ state.cropRect }
503
684
  containerSize={ canvasSize }
504
685
  imageSize={ visualSize }
686
+ onCropChange={ handleCropChange }
687
+ onResizeStart={ handleResizeStart }
688
+ onResizeEnd={ handleResizeEnd }
689
+ onEscape={ handleEscape }
690
+ aspectRatio={ aspectRatio }
691
+ freeformCrop={ freeformCrop }
692
+ stencilTransition={ settleStencilTransition }
693
+ cropBounds={ cropBounds }
505
694
  />
506
- ) }
695
+
696
+ { /* Rule-of-thirds grid */ }
697
+ { ( showGrid === true || isInteractiveGrid ) && (
698
+ <GridOverlay
699
+ cropRect={ state.cropRect }
700
+ containerSize={ canvasSize }
701
+ imageSize={ visualSize }
702
+ />
703
+ ) }
704
+ </div>
507
705
 
508
706
  { /* ARIA live region for screen reader announcements */ }
509
707
  <div
510
708
  aria-live="polite"
511
709
  aria-atomic="true"
512
710
  className="wp-media-editor-image-editor__aria-live"
513
- style={ {
514
- position: 'absolute',
515
- width: 1,
516
- height: 1,
517
- padding: 0,
518
- margin: -1,
519
- overflow: 'hidden',
520
- clip: 'rect(0, 0, 0, 0)',
521
- whiteSpace: 'nowrap',
522
- border: 0,
523
- } }
711
+ style={ VISUALLY_HIDDEN_STYLE }
524
712
  >
525
713
  { ariaMessage }
526
714
  </div>
@@ -529,6 +717,14 @@ function CropperInner(
529
717
  );
530
718
  }
531
719
 
532
- export const Cropper = forwardRef< HTMLDivElement, CropperProps >(
720
+ const CropperInnerWithRef = forwardRef< HTMLDivElement, CropperProps >(
533
721
  CropperInner
534
722
  );
723
+
724
+ export const Cropper = forwardRef< HTMLDivElement, CropperProps >(
725
+ ( props, ref ) => (
726
+ <ViewportProvider>
727
+ <CropperInnerWithRef { ...props } ref={ ref } />
728
+ </ViewportProvider>
729
+ )
730
+ );
@@ -13,24 +13,17 @@ interface DimmingOverlayProps {
13
13
  containerSize: Size;
14
14
  /** The rendered image dimensions in pixels within the container. */
15
15
  imageSize: Size;
16
+ /** CSS transition string, e.g. during settle animation. */
17
+ transition?: string;
16
18
  }
17
19
 
18
- /**
19
- * Renders a semi-transparent overlay outside the crop rectangle.
20
- *
21
- * Uses the box-shadow approach: a div matching the crop rect position
22
- * with a large box-shadow that dims everything outside it.
23
- *
24
- * @param props Component props.
25
- * @param props.cropRect The crop rectangle in normalized coordinates.
26
- * @param props.containerSize The container element dimensions in pixels.
27
- * @param props.imageSize The rendered image dimensions in pixels.
28
- * @return The dimming overlay element.
29
- */
20
+ // Renders a semi-transparent overlay outside the crop rectangle using a
21
+ // box-shadow that dims everything outside the crop rect position.
30
22
  export function DimmingOverlay( {
31
23
  cropRect,
32
24
  containerSize,
33
25
  imageSize,
26
+ transition,
34
27
  }: DimmingOverlayProps ) {
35
28
  if ( containerSize.width === 0 || containerSize.height === 0 ) {
36
29
  return null;
@@ -43,6 +36,12 @@ export function DimmingOverlay( {
43
36
  const width = cropRect.width * imageSize.width;
44
37
  const height = cropRect.height * imageSize.height;
45
38
 
39
+ // Compose any incoming transition with the stylesheet's box-shadow
40
+ // transition so the drag-dimming effect isn't overwritten.
41
+ const composedTransition = transition
42
+ ? `${ transition }, box-shadow 0.15s ease`
43
+ : undefined;
44
+
46
45
  return (
47
46
  <div
48
47
  className="wp-media-editor-image-editor__dimming"
@@ -51,6 +50,7 @@ export function DimmingOverlay( {
51
50
  top,
52
51
  width,
53
52
  height,
53
+ transition: composedTransition,
54
54
  } }
55
55
  />
56
56
  );
@@ -1,13 +1,23 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { useCallback, useEffect, useMemo, useRef } from '@wordpress/element';
4
+ import {
5
+ useCallback,
6
+ useEffect,
7
+ useId,
8
+ useMemo,
9
+ useRef,
10
+ } from '@wordpress/element';
5
11
  import { __ } from '@wordpress/i18n';
6
12
 
7
13
  /**
8
14
  * Internal dependencies
9
15
  */
10
16
  import type { StencilProps, NormalizedRect } from '../../../core/types';
17
+ import {
18
+ DEFAULT_KEYBOARD_STEP,
19
+ KEYBOARD_SHIFT_STEP_MULTIPLIER,
20
+ } from '../../../core/constants';
11
21
  import {
12
22
  computeFreeResizeRect,
13
23
  computeLockedResizeRect,
@@ -16,6 +26,7 @@ import {
16
26
  type CropBounds,
17
27
  type ResizeDragState,
18
28
  } from '../../../core/stencil-math';
29
+ import { VISUALLY_HIDDEN_STYLE } from '../../visually-hidden-style';
19
30
 
20
31
  /**
21
32
  * Corner handle positions only — used when aspect ratio is locked.
@@ -65,12 +76,6 @@ function getHandleLabel( pos: HandlePosition ): string {
65
76
  }
66
77
  }
67
78
 
68
- /** Fine step for keyboard-driven handle resize, in normalized coordinates. */
69
- const KEYBOARD_STEP = 0.01;
70
-
71
- /** Coarse step when Shift is held — 10× the fine step. */
72
- const KEYBOARD_STEP_SHIFT = 0.1;
73
-
74
79
  /** Delay before keyboard resize triggers settle (ms). */
75
80
  const KEYBOARD_SETTLE_DELAY = 500;
76
81
 
@@ -129,6 +134,7 @@ export function RectangleStencil( {
129
134
  );
130
135
  const keyboardSettleTimerRef = useRef< ReturnType< typeof setTimeout > >();
131
136
  const keyboardResizeActiveRef = useRef( false );
137
+ const resizeHandleDescriptionId = useId();
132
138
  const hasLockedRatio = !! ( aspectRatio && aspectRatio > 0 );
133
139
 
134
140
  // Clear the pending keyboard settle timer on unmount so it can't
@@ -397,7 +403,9 @@ export function RectangleStencil( {
397
403
  }, KEYBOARD_SETTLE_DELAY );
398
404
  };
399
405
 
400
- const step = event.shiftKey ? KEYBOARD_STEP_SHIFT : KEYBOARD_STEP;
406
+ const step = event.shiftKey
407
+ ? DEFAULT_KEYBOARD_STEP * KEYBOARD_SHIFT_STEP_MULTIPLIER
408
+ : DEFAULT_KEYBOARD_STEP;
401
409
 
402
410
  // Determine the normalized delta from the arrow key.
403
411
  let dx = 0;
@@ -477,6 +485,16 @@ export function RectangleStencil( {
477
485
  transition: stencilTransition,
478
486
  } }
479
487
  >
488
+ { freeformCrop && (
489
+ <div
490
+ id={ resizeHandleDescriptionId }
491
+ style={ VISUALLY_HIDDEN_STYLE }
492
+ >
493
+ { __(
494
+ 'Use arrow keys to resize the crop area. Hold Shift for larger steps.'
495
+ ) }
496
+ </div>
497
+ ) }
480
498
  { /* The crop rectangle border. pointer-events: none is set in
481
499
  CSS so clicks pass through to the container for panning. */ }
482
500
  <div
@@ -506,6 +524,7 @@ export function RectangleStencil( {
506
524
  onTouchStart={ ( event ) => event.stopPropagation() }
507
525
  onKeyDown={ ( event ) => handleKeyDown( pos, event ) }
508
526
  aria-label={ getHandleLabel( pos ) }
527
+ aria-describedby={ resizeHandleDescriptionId }
509
528
  />
510
529
  ) ) }
511
530
  </div>