@wordpress/media-editor 0.10.1 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/build/components/media-editor/index.cjs +41 -51
  3. package/build/components/media-editor/index.cjs.map +3 -3
  4. package/build/components/media-editor-crop-panel/index.cjs +15 -45
  5. package/build/components/media-editor-crop-panel/index.cjs.map +3 -3
  6. package/build/components/media-editor-fine-rotation/index.cjs.map +2 -2
  7. package/build/components/media-editor-image-controls/index.cjs +230 -0
  8. package/build/components/media-editor-image-controls/index.cjs.map +7 -0
  9. package/build/components/media-form/index.cjs +12 -23
  10. package/build/components/media-form/index.cjs.map +3 -3
  11. package/build/image-editor/core/constants.cjs +3 -0
  12. package/build/image-editor/core/constants.cjs.map +2 -2
  13. package/build/image-editor/core/stencil-math.cjs +11 -2
  14. package/build/image-editor/core/stencil-math.cjs.map +2 -2
  15. package/build/image-editor/react/components/cropper.cjs +12 -7
  16. package/build/image-editor/react/components/cropper.cjs.map +2 -2
  17. package/build-module/components/media-editor/index.mjs +41 -51
  18. package/build-module/components/media-editor/index.mjs.map +2 -2
  19. package/build-module/components/media-editor-crop-panel/index.mjs +8 -51
  20. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  21. package/build-module/components/media-editor-fine-rotation/index.mjs.map +2 -2
  22. package/build-module/components/media-editor-image-controls/index.mjs +221 -0
  23. package/build-module/components/media-editor-image-controls/index.mjs.map +7 -0
  24. package/build-module/components/media-form/index.mjs +12 -13
  25. package/build-module/components/media-form/index.mjs.map +2 -2
  26. package/build-module/image-editor/core/constants.mjs +2 -0
  27. package/build-module/image-editor/core/constants.mjs.map +2 -2
  28. package/build-module/image-editor/core/stencil-math.mjs +9 -1
  29. package/build-module/image-editor/core/stencil-math.mjs.map +2 -2
  30. package/build-module/image-editor/react/components/cropper.mjs +12 -7
  31. package/build-module/image-editor/react/components/cropper.mjs.map +2 -2
  32. package/build-style/style-rtl.css +136 -47
  33. package/build-style/style.css +136 -46
  34. package/build-types/components/media-editor/index.d.ts +6 -7
  35. package/build-types/components/media-editor/index.d.ts.map +1 -1
  36. package/build-types/components/media-editor-crop-panel/index.d.ts +11 -6
  37. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  38. package/build-types/components/media-editor-fine-rotation/index.d.ts +4 -3
  39. package/build-types/components/media-editor-fine-rotation/index.d.ts.map +1 -1
  40. package/build-types/components/media-editor-image-controls/index.d.ts +46 -0
  41. package/build-types/components/media-editor-image-controls/index.d.ts.map +1 -0
  42. package/build-types/components/media-editor-toolbar/index.d.ts +24 -0
  43. package/build-types/components/media-editor-toolbar/index.d.ts.map +1 -0
  44. package/build-types/components/media-editor-transform-controls/index.d.ts +18 -5
  45. package/build-types/components/media-editor-transform-controls/index.d.ts.map +1 -1
  46. package/build-types/components/media-form/index.d.ts.map +1 -1
  47. package/build-types/image-editor/core/constants.d.ts +10 -0
  48. package/build-types/image-editor/core/constants.d.ts.map +1 -1
  49. package/build-types/image-editor/core/stencil-math.d.ts +16 -3
  50. package/build-types/image-editor/core/stencil-math.d.ts.map +1 -1
  51. package/build-types/image-editor/react/components/cropper.d.ts.map +1 -1
  52. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts +108 -0
  53. package/build-types/image-editor/react/hooks/use-cropper-state.d.ts.map +1 -0
  54. package/package.json +25 -20
  55. package/src/components/media-editor/index.tsx +69 -77
  56. package/src/components/media-editor/style.scss +124 -4
  57. package/src/components/media-editor-crop-panel/index.tsx +16 -62
  58. package/src/components/media-editor-crop-panel/test/index.tsx +25 -41
  59. package/src/components/media-editor-fine-rotation/index.tsx +4 -3
  60. package/src/components/media-editor-image-controls/index.tsx +262 -0
  61. package/src/components/media-editor-image-controls/style.scss +48 -0
  62. package/src/components/media-editor-image-controls/test/index.tsx +150 -0
  63. package/src/components/media-editor-modal/style.scss +32 -42
  64. package/src/components/media-form/index.tsx +17 -21
  65. package/src/components/media-form/style.scss +6 -0
  66. package/src/components/rotation-ruler/style.scss +10 -3
  67. package/src/image-editor/core/constants.ts +11 -0
  68. package/src/image-editor/core/stencil-math.ts +23 -0
  69. package/src/image-editor/core/test/stencil-math.ts +39 -0
  70. package/src/image-editor/react/components/cropper.scss +28 -17
  71. package/src/image-editor/react/components/cropper.tsx +17 -7
  72. package/src/style.scss +2 -1
  73. package/tsconfig.tsbuildinfo +1 -1
  74. package/build/components/media-editor-transform-controls/index.cjs +0 -84
  75. package/build/components/media-editor-transform-controls/index.cjs.map +0 -7
  76. package/build/components/media-form/sidebar-datetime-view.cjs +0 -53
  77. package/build/components/media-form/sidebar-datetime-view.cjs.map +0 -7
  78. package/build-module/components/media-editor-transform-controls/index.mjs +0 -68
  79. package/build-module/components/media-editor-transform-controls/index.mjs.map +0 -7
  80. package/build-module/components/media-form/sidebar-datetime-view.mjs +0 -32
  81. package/build-module/components/media-form/sidebar-datetime-view.mjs.map +0 -7
  82. package/src/components/media-editor-transform-controls/index.tsx +0 -70
  83. package/src/components/media-editor-transform-controls/style.scss +0 -8
  84. package/src/components/media-form/sidebar-datetime-view.tsx +0 -47
  85. package/src/components/media-form/test/sidebar-datetime-view.test.tsx +0 -96
@@ -1,33 +1,17 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { RangeControl, SelectControl } from '@wordpress/components';
4
+ import { SelectControl } from '@wordpress/components';
5
5
  import { Stack, VisuallyHidden } from '@wordpress/ui';
6
- import { __, sprintf } from '@wordpress/i18n';
6
+ import { __ } from '@wordpress/i18n';
7
7
 
8
8
  /**
9
9
  * Internal dependencies
10
10
  */
11
- import { useMediaEditor } from '../../state';
12
- import {
13
- useCropGestureHandlers,
14
- CROP_CONTROL_ATTR,
15
- } from '../../hooks/use-crop-gesture-handlers';
16
- import { MAX_ZOOM } from '../../image-editor/core/constants';
17
- import { getMinZoom } from '../../image-editor/core/containment';
11
+ import { CROP_CONTROL_ATTR } from '../../hooks/use-crop-gesture-handlers';
12
+ import MediaEditorImageControls from '../media-editor-image-controls';
18
13
  import type { AspectRatioPreset } from '../../image-editor/core/constants';
19
14
 
20
- const ZOOM_PERCENTAGE_SCALE = 100;
21
- const MAX_ZOOM_PERCENTAGE = MAX_ZOOM * ZOOM_PERCENTAGE_SCALE;
22
-
23
- function getZoomPercentageForDisplay( zoom: number ): number {
24
- return Math.round( zoom * ZOOM_PERCENTAGE_SCALE );
25
- }
26
-
27
- function getMinZoomPercentageForDisplay( zoom: number ): number {
28
- return Math.ceil( zoom * ZOOM_PERCENTAGE_SCALE );
29
- }
30
-
31
15
  export interface MediaEditorCropPanelProps {
32
16
  /**
33
17
  * Selected aspect-ratio preset value as a string (so it round-trips
@@ -37,33 +21,32 @@ export interface MediaEditorCropPanelProps {
37
21
  aspectRatioValue: string;
38
22
  /** Setter for the aspect-ratio preset value. */
39
23
  onAspectRatioChange: ( value: string ) => void;
40
- /** Signal that a placement-oriented control is being adjusted. */
41
- onPlacementControlInteraction?: () => void;
42
24
  /** Aspect-ratio presets to display in the selector. */
43
25
  aspectRatioOptions: AspectRatioPreset[];
26
+ /**
27
+ * When `true`, render the rotate/flip/zoom image controls at the top of
28
+ * the panel. Used on wide viewports where the footer no longer carries
29
+ * them.
30
+ */
31
+ showTransformControls?: boolean;
44
32
  }
45
33
 
46
34
  /**
47
- * Sidebar panel for crop-shape controls. The tactile verbs (rotate, flip)
48
- * live in the bottom toolbar instead.
35
+ * Sidebar panel for crop controls. Renders the aspect-ratio selector, plus the
36
+ * rotate/flip and zoom controls on wide viewports (these move to the footer
37
+ * toolbar when the sidebar collapses).
49
38
  * @param props
50
39
  * @param props.aspectRatioValue
51
40
  * @param props.onAspectRatioChange
52
- * @param props.onPlacementControlInteraction
53
41
  * @param props.aspectRatioOptions
42
+ * @param props.showTransformControls
54
43
  */
55
44
  export default function MediaEditorCropPanel( {
56
45
  aspectRatioValue,
57
46
  onAspectRatioChange,
58
- onPlacementControlInteraction,
59
47
  aspectRatioOptions,
48
+ showTransformControls = false,
60
49
  }: MediaEditorCropPanelProps ) {
61
- const { state, setZoom } = useMediaEditor();
62
- const zoomGestureHandlers = useCropGestureHandlers();
63
- const minZoom = getMinZoom( state );
64
- const zoomPercentage = getZoomPercentageForDisplay( state.zoom );
65
- const minZoomPercentage = getMinZoomPercentageForDisplay( minZoom );
66
-
67
50
  return (
68
51
  // Tag the whole panel as a crop-control region so the modal's
69
52
  // Cmd+Z handler doesn't mistake the SelectControl input for a
@@ -76,6 +59,7 @@ export default function MediaEditorCropPanel( {
76
59
  <VisuallyHidden render={ <h2 /> }>
77
60
  { __( 'Crop options' ) }
78
61
  </VisuallyHidden>
62
+ { showTransformControls && <MediaEditorImageControls withLabels /> }
79
63
  <SelectControl
80
64
  __next40pxDefaultSize
81
65
  label={ __( 'Aspect ratio' ) }
@@ -86,36 +70,6 @@ export default function MediaEditorCropPanel( {
86
70
  value: preset.value.toString(),
87
71
  } ) ) }
88
72
  />
89
- <div role="presentation" { ...zoomGestureHandlers }>
90
- <RangeControl
91
- __next40pxDefaultSize
92
- label={ __( 'Zoom (%)' ) }
93
- min={ minZoomPercentage }
94
- max={ MAX_ZOOM_PERCENTAGE }
95
- step={ 1 }
96
- shiftStep={ 10 }
97
- value={ zoomPercentage }
98
- onChange={ ( value ) => {
99
- onPlacementControlInteraction?.();
100
- setZoom(
101
- typeof value === 'number'
102
- ? value / ZOOM_PERCENTAGE_SCALE
103
- : minZoom
104
- );
105
- } }
106
- renderTooltipContent={ ( value ) => {
107
- const percentage =
108
- typeof value === 'number'
109
- ? value
110
- : minZoomPercentage;
111
- return sprintf(
112
- /* translators: %d: zoom level as a percentage. */
113
- __( '%d%%' ),
114
- Math.round( percentage )
115
- );
116
- } }
117
- />
118
- </div>
119
73
  </Stack>
120
74
  );
121
75
  }
@@ -8,7 +8,7 @@ import { fireEvent, render, screen } from '@testing-library/react';
8
8
  */
9
9
  import MediaEditorCropPanel from '..';
10
10
  import type { MediaEditorCropPanelProps } from '..';
11
- import { MediaEditorStateProvider, useMediaEditor } from '../../../state';
11
+ import { MediaEditorStateProvider } from '../../../state';
12
12
  import type { CropperState } from '../../../image-editor';
13
13
 
14
14
  function setupCropPanel(
@@ -29,31 +29,13 @@ function setupCropPanel(
29
29
  render(
30
30
  <MediaEditorStateProvider initialCropperState={ initialCropperState }>
31
31
  <MediaEditorCropPanel { ...props } />
32
- <CurrentZoomValue />
33
32
  </MediaEditorStateProvider>
34
33
  );
35
34
 
36
35
  return props;
37
36
  }
38
37
 
39
- function CurrentZoomValue() {
40
- const { state } = useMediaEditor();
41
-
42
- return <output data-testid="current-zoom">{ state.zoom }</output>;
43
- }
44
-
45
38
  describe( 'MediaEditorCropPanel', () => {
46
- it( 'renders crop shape controls before zoom controls', () => {
47
- setupCropPanel();
48
-
49
- const aspectRatio = screen.getByLabelText( 'Aspect ratio' );
50
- const zoom = screen.getByRole( 'slider', { name: 'Zoom (%)' } );
51
-
52
- expect( aspectRatio.compareDocumentPosition( zoom ) ).toBe(
53
- Node.DOCUMENT_POSITION_FOLLOWING
54
- );
55
- } );
56
-
57
39
  it( 'passes selected aspect ratio changes to the caller', () => {
58
40
  const controls = setupCropPanel( {
59
41
  aspectRatioValue: '1',
@@ -69,34 +51,36 @@ describe( 'MediaEditorCropPanel', () => {
69
51
  ).toBe( '0' );
70
52
  } );
71
53
 
72
- it( 'displays zoom as a percentage without changing cropper state', () => {
73
- setupCropPanel( {}, { zoom: 3.749999999999999 } );
54
+ it( 'omits the image controls by default', () => {
55
+ setupCropPanel();
74
56
 
75
- const zoomInput = screen.getByRole( 'spinbutton', {
76
- name: 'Zoom (%)',
77
- } );
57
+ expect( screen.queryByText( 'Rotate' ) ).not.toBeInTheDocument();
58
+ expect( screen.queryByText( 'Flip' ) ).not.toBeInTheDocument();
59
+ expect( screen.queryByText( 'Zoom' ) ).not.toBeInTheDocument();
60
+ } );
78
61
 
79
- expect( zoomInput ).toHaveValue( 375 );
80
- expect( screen.getByTestId( 'current-zoom' ) ).toHaveTextContent(
81
- '3.749999999999999'
82
- );
62
+ it( 'renders rotate, flip and zoom controls when showTransformControls is set', () => {
63
+ setupCropPanel( { showTransformControls: true } );
64
+
65
+ expect( screen.getByText( 'Rotate' ) ).toBeInTheDocument();
66
+ expect( screen.getByText( 'Flip' ) ).toBeInTheDocument();
67
+ expect( screen.getByText( 'Zoom' ) ).toBeInTheDocument();
68
+ expect(
69
+ screen.getByRole( 'button', { name: 'Rotate 90° clockwise' } )
70
+ ).toBeInTheDocument();
71
+ expect(
72
+ screen.getByRole( 'button', { name: 'Zoom in' } )
73
+ ).toBeInTheDocument();
83
74
  } );
84
75
 
85
- it( 'converts percentage input back to the cropper zoom multiplier', () => {
86
- const controls = setupCropPanel( {
87
- onPlacementControlInteraction: jest.fn(),
88
- } );
76
+ it( 'renders the image controls above the aspect-ratio selector', () => {
77
+ setupCropPanel( { showTransformControls: true } );
89
78
 
90
- fireEvent.change(
91
- screen.getByRole( 'spinbutton', { name: 'Zoom (%)' } ),
92
- {
93
- target: { value: '250' },
94
- }
95
- );
79
+ const rotate = screen.getByText( 'Rotate' );
80
+ const aspectRatio = screen.getByLabelText( 'Aspect ratio' );
96
81
 
97
- expect( screen.getByTestId( 'current-zoom' ) ).toHaveTextContent(
98
- '2.5'
82
+ expect( rotate.compareDocumentPosition( aspectRatio ) ).toBe(
83
+ Node.DOCUMENT_POSITION_FOLLOWING
99
84
  );
100
- expect( controls.onPlacementControlInteraction ).toHaveBeenCalled();
101
85
  } );
102
86
  } );
@@ -17,9 +17,10 @@ export interface MediaEditorFineRotationProps {
17
17
  }
18
18
 
19
19
  /**
20
- * Fine-rotation slider for the media-editor footer. Lives separately from
21
- * the snap-rotate / flip buttons so the footer can place the two halves on
22
- * different rows at intermediate viewport widths.
20
+ * Fine-rotation slider for the media editor. It renders under the canvas so it
21
+ * stays constrained to the canvas column at all viewport sizes. Lives
22
+ * separately from the snap-rotate / flip buttons so the two can be placed
23
+ * independently per viewport.
23
24
  *
24
25
  * @param props
25
26
  * @param props.onPlacementControlInteraction
@@ -0,0 +1,262 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import {
5
+ Button,
6
+ DropdownMenu,
7
+ MenuGroup,
8
+ MenuItem,
9
+ } from '@wordpress/components';
10
+ import { __ } from '@wordpress/i18n';
11
+ import {
12
+ aspectRatio as aspectRatioIcon,
13
+ check,
14
+ rotateLeft,
15
+ rotateRight,
16
+ flipHorizontal,
17
+ flipVertical,
18
+ lineSolid,
19
+ plus,
20
+ } from '@wordpress/icons';
21
+
22
+ /**
23
+ * Internal dependencies
24
+ */
25
+ import { useMediaEditor } from '../../state';
26
+ import {
27
+ MAX_ZOOM,
28
+ type AspectRatioPreset,
29
+ } from '../../image-editor/core/constants';
30
+ import { getMinZoom } from '../../image-editor/core/containment';
31
+ import { useCropOptions } from '../media-editor/use-crop-options';
32
+
33
+ /**
34
+ * Default zoom factor per +/- click. Zooming is geometric, so a fixed
35
+ * multiplier — rather than an additive step — makes each click feel like the
36
+ * same amount of zoom at any level (an additive `+0.1` is a 10% jump at 1× but
37
+ * a ~1% nudge at 10×). `1.2` is ~+20% per click. Overridable via the
38
+ * `zoomFactor` prop.
39
+ */
40
+ export const DEFAULT_ZOOM_FACTOR = 1.2;
41
+
42
+ export interface MediaEditorImageControlsProps {
43
+ /**
44
+ * When `true`, render rotate, flip and zoom as labelled groups — the Crop
45
+ * panel layout used on wide viewports. When `false` (default), render a
46
+ * single flat row of icon buttons — the footer layout used at narrower
47
+ * widths.
48
+ */
49
+ withLabels?: boolean;
50
+ /**
51
+ * When `true`, include an aspect-ratio dropdown in the flat toolbar.
52
+ * Omitted from the labelled panel layout because the Crop panel already
53
+ * renders the full aspect-ratio select control.
54
+ */
55
+ showAspectRatioControl?: boolean;
56
+ /** Optional caller-supplied aspect-ratio presets. */
57
+ aspectRatioPresets?: AspectRatioPreset[];
58
+ /**
59
+ * Multiplier applied to the zoom per +/- click — zoom in multiplies by it,
60
+ * zoom out divides by it. Must be greater than 1. Defaults to
61
+ * `DEFAULT_ZOOM_FACTOR` (1.2).
62
+ */
63
+ zoomFactor?: number;
64
+ }
65
+
66
+ /**
67
+ * Image editing controls placed independently per viewport: on wide viewports
68
+ * rotate/flip/zoom render inside the Crop panel (`withLabels`), and at narrower
69
+ * widths they fall back into the footer toolbar (flat row), where the
70
+ * aspect-ratio dropdown can also be shown.
71
+ *
72
+ * @param props
73
+ * @param props.withLabels
74
+ * @param props.showAspectRatioControl
75
+ * @param props.aspectRatioPresets
76
+ * @param props.zoomFactor
77
+ */
78
+ export default function MediaEditorImageControls( {
79
+ withLabels = false,
80
+ showAspectRatioControl = false,
81
+ aspectRatioPresets,
82
+ zoomFactor = DEFAULT_ZOOM_FACTOR,
83
+ }: MediaEditorImageControlsProps ) {
84
+ const { state, setFlip, snapRotate90, setZoom } = useMediaEditor();
85
+ const { aspectRatioValue, setAspectRatioValue, aspectRatioOptions } =
86
+ useCropOptions( { aspectRatioPresets } );
87
+ const hasAspectRatioControl = ! withLabels && showAspectRatioControl;
88
+ const minZoom = getMinZoom( state );
89
+ const zoomByFactor = ( factor: number ) => {
90
+ setZoom(
91
+ Math.min( MAX_ZOOM, Math.max( minZoom, state.zoom * factor ) )
92
+ );
93
+ };
94
+
95
+ const rotateButtons = (
96
+ <>
97
+ <Button
98
+ size="compact"
99
+ icon={ rotateLeft }
100
+ label={ __( 'Rotate 90° counter-clockwise' ) }
101
+ showTooltip
102
+ onClick={ () => snapRotate90( -1 ) }
103
+ />
104
+ <Button
105
+ size="compact"
106
+ icon={ rotateRight }
107
+ label={ __( 'Rotate 90° clockwise' ) }
108
+ showTooltip
109
+ onClick={ () => snapRotate90( 1 ) }
110
+ />
111
+ </>
112
+ );
113
+
114
+ const flipButtons = (
115
+ <>
116
+ <Button
117
+ size="compact"
118
+ icon={ flipHorizontal }
119
+ label={ __( 'Flip horizontal' ) }
120
+ showTooltip
121
+ isPressed={ state.flip.horizontal }
122
+ onClick={ () =>
123
+ setFlip( {
124
+ horizontal: ! state.flip.horizontal,
125
+ vertical: state.flip.vertical,
126
+ } )
127
+ }
128
+ />
129
+ <Button
130
+ size="compact"
131
+ icon={ flipVertical }
132
+ label={ __( 'Flip vertical' ) }
133
+ showTooltip
134
+ isPressed={ state.flip.vertical }
135
+ onClick={ () =>
136
+ setFlip( {
137
+ horizontal: state.flip.horizontal,
138
+ vertical: ! state.flip.vertical,
139
+ } )
140
+ }
141
+ />
142
+ </>
143
+ );
144
+
145
+ const zoomButtons = (
146
+ <>
147
+ <Button
148
+ size="compact"
149
+ icon={ plus }
150
+ label={ __( 'Zoom in' ) }
151
+ showTooltip
152
+ disabled={ state.zoom >= MAX_ZOOM }
153
+ accessibleWhenDisabled
154
+ onClick={ () => zoomByFactor( zoomFactor ) }
155
+ />
156
+ <Button
157
+ size="compact"
158
+ icon={ lineSolid }
159
+ label={ __( 'Zoom out' ) }
160
+ showTooltip
161
+ disabled={ state.zoom <= minZoom }
162
+ accessibleWhenDisabled
163
+ onClick={ () => zoomByFactor( 1 / zoomFactor ) }
164
+ />
165
+ </>
166
+ );
167
+
168
+ const aspectRatioDropdown = hasAspectRatioControl ? (
169
+ <DropdownMenu
170
+ icon={ aspectRatioIcon }
171
+ label={ __( 'Aspect ratio' ) }
172
+ popoverProps={ { placement: 'top' } }
173
+ toggleProps={ { size: 'compact' } }
174
+ >
175
+ { ( { onClose } ) => (
176
+ <MenuGroup label={ __( 'Aspect ratio' ) }>
177
+ { aspectRatioOptions.map( ( preset ) => {
178
+ const value = preset.value.toString();
179
+ const isSelected = value === aspectRatioValue;
180
+ return (
181
+ <MenuItem
182
+ key={ value }
183
+ role="menuitemradio"
184
+ isSelected={ isSelected }
185
+ icon={ isSelected ? check : undefined }
186
+ onClick={ () => {
187
+ setAspectRatioValue( value );
188
+ onClose();
189
+ } }
190
+ >
191
+ { preset.label }
192
+ </MenuItem>
193
+ );
194
+ } ) }
195
+ </MenuGroup>
196
+ ) }
197
+ </DropdownMenu>
198
+ ) : null;
199
+
200
+ if ( withLabels ) {
201
+ return (
202
+ <div className="media-editor-image-controls is-stacked">
203
+ <div className="media-editor-image-controls__transforms">
204
+ <div
205
+ className="media-editor-image-controls__group"
206
+ role="group"
207
+ aria-label={ __( 'Rotate' ) }
208
+ >
209
+ <span
210
+ className="media-editor-image-controls__label"
211
+ aria-hidden="true"
212
+ >
213
+ { __( 'Rotate' ) }
214
+ </span>
215
+ <div className="media-editor-image-controls__buttons">
216
+ { rotateButtons }
217
+ </div>
218
+ </div>
219
+ <div
220
+ className="media-editor-image-controls__group"
221
+ role="group"
222
+ aria-label={ __( 'Flip' ) }
223
+ >
224
+ <span
225
+ className="media-editor-image-controls__label"
226
+ aria-hidden="true"
227
+ >
228
+ { __( 'Flip' ) }
229
+ </span>
230
+ <div className="media-editor-image-controls__buttons">
231
+ { flipButtons }
232
+ </div>
233
+ </div>
234
+ </div>
235
+ <div
236
+ className="media-editor-image-controls__group"
237
+ role="group"
238
+ aria-label={ __( 'Zoom' ) }
239
+ >
240
+ <span
241
+ className="media-editor-image-controls__label"
242
+ aria-hidden="true"
243
+ >
244
+ { __( 'Zoom' ) }
245
+ </span>
246
+ <div className="media-editor-image-controls__buttons">
247
+ { zoomButtons }
248
+ </div>
249
+ </div>
250
+ </div>
251
+ );
252
+ }
253
+
254
+ return (
255
+ <div className="media-editor-image-controls">
256
+ { rotateButtons }
257
+ { flipButtons }
258
+ { zoomButtons }
259
+ { aspectRatioDropdown }
260
+ </div>
261
+ );
262
+ }
@@ -0,0 +1,48 @@
1
+ @use "@wordpress/base-styles/variables" as *;
2
+ @use "@wordpress/base-styles/colors" as *;
3
+
4
+ .media-editor-image-controls {
5
+ display: flex;
6
+ flex-wrap: nowrap;
7
+ align-items: center;
8
+ gap: $grid-unit-10;
9
+
10
+ // Stacked layout used inside the Crop panel on wide viewports: the Rotate
11
+ // and Flip groups sit side by side (rotate left, flip right) in a row, with
12
+ // the Zoom group beneath them. The aspect-ratio select renders separately
13
+ // below in the Crop panel.
14
+ &.is-stacked {
15
+ flex-direction: column;
16
+ align-items: flex-start;
17
+ gap: $grid-unit-30;
18
+ }
19
+
20
+ &__transforms {
21
+ display: flex;
22
+ gap: $grid-unit-60;
23
+ }
24
+
25
+ &__group {
26
+ display: flex;
27
+ flex-direction: column;
28
+ gap: $grid-unit-05;
29
+ }
30
+
31
+ // Match the uppercase 11px heading the panel's other controls use for
32
+ // their labels (Zoom, Aspect ratio) so the groups read as section
33
+ // headings. Mirrors `baseLabelTypography` from `@wordpress/components`.
34
+ &__label {
35
+ font-size: 11px;
36
+ font-weight: 500;
37
+ line-height: 1.4;
38
+ text-transform: uppercase;
39
+ }
40
+
41
+ &__buttons {
42
+ display: flex;
43
+ gap: $grid-unit-10;
44
+ // Small leading inset so the leftmost (rotate) button's focus ring
45
+ // isn't clipped by the sidebar's `overflow: hidden`.
46
+ padding-inline: 2px 0;
47
+ }
48
+ }
@@ -0,0 +1,150 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { fireEvent, render, screen, waitFor } from '@testing-library/react';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import MediaEditorImageControls from '..';
10
+ import type { MediaEditorImageControlsProps } from '..';
11
+ import { MediaEditorStateProvider, useMediaEditor } from '../../../state';
12
+ import type { CropperState } from '../../../image-editor';
13
+ import { MAX_ZOOM } from '../../../image-editor/core/constants';
14
+
15
+ function setup(
16
+ props: MediaEditorImageControlsProps = {},
17
+ initialCropperState?: Partial< CropperState >
18
+ ) {
19
+ render(
20
+ <MediaEditorStateProvider initialCropperState={ initialCropperState }>
21
+ <MediaEditorImageControls { ...props } />
22
+ <CurrentState />
23
+ </MediaEditorStateProvider>
24
+ );
25
+ }
26
+
27
+ function CurrentState() {
28
+ const { state, cropOptions } = useMediaEditor();
29
+
30
+ return (
31
+ <>
32
+ <output data-testid="current-aspect-ratio">
33
+ { cropOptions.aspectRatioValue }
34
+ </output>
35
+ <output data-testid="current-zoom">{ state.zoom }</output>
36
+ </>
37
+ );
38
+ }
39
+
40
+ describe( 'MediaEditorImageControls', () => {
41
+ it( 'renders a flat row of rotate, flip and zoom buttons by default', () => {
42
+ setup();
43
+
44
+ expect(
45
+ screen.getByRole( 'button', {
46
+ name: 'Rotate 90° counter-clockwise',
47
+ } )
48
+ ).toBeInTheDocument();
49
+ expect(
50
+ screen.getByRole( 'button', { name: 'Flip horizontal' } )
51
+ ).toBeInTheDocument();
52
+ expect(
53
+ screen.getByRole( 'button', { name: 'Zoom in' } )
54
+ ).toBeInTheDocument();
55
+ expect(
56
+ screen.getByRole( 'button', { name: 'Zoom out' } )
57
+ ).toBeInTheDocument();
58
+
59
+ // No visible group labels in the flat (footer) layout.
60
+ expect( screen.queryByText( 'Rotate' ) ).not.toBeInTheDocument();
61
+ expect( screen.queryByText( 'Flip' ) ).not.toBeInTheDocument();
62
+ expect( screen.queryByText( 'Zoom' ) ).not.toBeInTheDocument();
63
+ } );
64
+
65
+ it( 'renders labelled rotate, flip and zoom groups when withLabels is set', () => {
66
+ setup( { withLabels: true } );
67
+
68
+ expect( screen.getByText( 'Rotate' ) ).toBeInTheDocument();
69
+ expect( screen.getByText( 'Flip' ) ).toBeInTheDocument();
70
+ expect( screen.getByText( 'Zoom' ) ).toBeInTheDocument();
71
+ expect(
72
+ screen.getByRole( 'button', { name: 'Rotate 90° clockwise' } )
73
+ ).toBeInTheDocument();
74
+ expect(
75
+ screen.getByRole( 'button', { name: 'Zoom in' } )
76
+ ).toBeInTheDocument();
77
+ } );
78
+
79
+ it( 'zoom in multiplies the zoom and zoom out divides it', () => {
80
+ setup( {}, { zoom: 2 } );
81
+
82
+ fireEvent.click( screen.getByRole( 'button', { name: 'Zoom in' } ) );
83
+ const zoomedIn = Number(
84
+ screen.getByTestId( 'current-zoom' ).textContent
85
+ );
86
+ expect( zoomedIn ).toBeGreaterThan( 2 );
87
+
88
+ fireEvent.click( screen.getByRole( 'button', { name: 'Zoom out' } ) );
89
+ expect(
90
+ Number( screen.getByTestId( 'current-zoom' ).textContent )
91
+ ).toBeLessThan( zoomedIn );
92
+ } );
93
+
94
+ it( 'honors a custom zoomFactor', () => {
95
+ setup( { zoomFactor: 2 }, { zoom: 2 } );
96
+
97
+ fireEvent.click( screen.getByRole( 'button', { name: 'Zoom in' } ) );
98
+
99
+ // A factor of 2 from zoom 2 lands at 4 (within [minZoom, MAX_ZOOM]).
100
+ expect(
101
+ Number( screen.getByTestId( 'current-zoom' ).textContent )
102
+ ).toBe( 4 );
103
+ } );
104
+
105
+ it( 'disables Zoom in at the maximum zoom', () => {
106
+ setup( {}, { zoom: MAX_ZOOM } );
107
+ // Buttons use `accessibleWhenDisabled`, so the disabled state is
108
+ // expressed via aria-disabled, not the `disabled` attribute.
109
+ expect(
110
+ screen.getByRole( 'button', { name: 'Zoom in' } )
111
+ ).toHaveAttribute( 'aria-disabled', 'true' );
112
+ } );
113
+
114
+ it( 'disables Zoom out at the minimum zoom', () => {
115
+ setup();
116
+ expect(
117
+ screen.getByRole( 'button', { name: 'Zoom out' } )
118
+ ).toHaveAttribute( 'aria-disabled', 'true' );
119
+ } );
120
+
121
+ it( 'renders an aspect ratio dropdown in the flat toolbar when enabled', async () => {
122
+ setup( {
123
+ showAspectRatioControl: true,
124
+ } );
125
+
126
+ fireEvent.click(
127
+ screen.getByRole( 'button', { name: 'Aspect ratio' } )
128
+ );
129
+ fireEvent.click(
130
+ screen.getByRole( 'menuitemradio', { name: 'Square (1:1)' } )
131
+ );
132
+
133
+ await waitFor( () =>
134
+ expect(
135
+ screen.getByTestId( 'current-aspect-ratio' )
136
+ ).toHaveTextContent( '1' )
137
+ );
138
+ } );
139
+
140
+ it( 'omits the aspect ratio dropdown from the labelled panel layout', () => {
141
+ setup( {
142
+ withLabels: true,
143
+ showAspectRatioControl: true,
144
+ } );
145
+
146
+ expect(
147
+ screen.queryByRole( 'button', { name: 'Aspect ratio' } )
148
+ ).not.toBeInTheDocument();
149
+ } );
150
+ } );