@wordpress/media-editor 0.16.0 → 0.17.2-next.v.202608281122.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 (44) hide show
  1. package/CHANGELOG.md +25 -1
  2. package/build/components/media-editor/index.cjs +188 -183
  3. package/build/components/media-editor/index.cjs.map +3 -3
  4. package/build/components/media-editor-canvas/index.cjs +0 -2
  5. package/build/components/media-editor-canvas/index.cjs.map +2 -2
  6. package/build/components/media-editor-crop-panel/index.cjs +2 -3
  7. package/build/components/media-editor-crop-panel/index.cjs.map +2 -2
  8. package/build/components/media-editor-modal/index.cjs +16 -14
  9. package/build/components/media-editor-modal/index.cjs.map +3 -3
  10. package/build-module/components/media-editor/index.mjs +192 -190
  11. package/build-module/components/media-editor/index.mjs.map +2 -2
  12. package/build-module/components/media-editor-canvas/index.mjs +0 -2
  13. package/build-module/components/media-editor-canvas/index.mjs.map +2 -2
  14. package/build-module/components/media-editor-crop-panel/index.mjs +2 -3
  15. package/build-module/components/media-editor-crop-panel/index.mjs.map +2 -2
  16. package/build-module/components/media-editor-modal/index.mjs +16 -14
  17. package/build-module/components/media-editor-modal/index.mjs.map +2 -2
  18. package/build-style/style-rtl.css +62 -95
  19. package/build-style/style.css +62 -95
  20. package/build-types/components/media-editor/index.d.ts +39 -11
  21. package/build-types/components/media-editor/index.d.ts.map +1 -1
  22. package/build-types/components/media-editor-canvas/index.d.ts +1 -4
  23. package/build-types/components/media-editor-canvas/index.d.ts.map +1 -1
  24. package/build-types/components/media-editor-crop-panel/index.d.ts +4 -11
  25. package/build-types/components/media-editor-crop-panel/index.d.ts.map +1 -1
  26. package/build-types/components/media-editor-modal/index.d.ts.map +1 -1
  27. package/global.d.ts +5 -0
  28. package/package.json +23 -22
  29. package/src/components/media-editor/index.tsx +381 -271
  30. package/src/components/media-editor/style.scss +136 -131
  31. package/src/components/media-editor-canvas/index.tsx +5 -9
  32. package/src/components/media-editor-crop-panel/index.tsx +4 -12
  33. package/src/components/media-editor-crop-panel/test/index.tsx +2 -10
  34. package/src/components/media-editor-modal/index.tsx +23 -12
  35. package/src/components/media-editor-modal/style.scss +14 -51
  36. package/src/image-editor/stories/rectangle-crop.story.tsx +15 -0
  37. package/tsconfig.build.json +26 -0
  38. package/tsconfig.build.tsbuildinfo +1 -0
  39. package/tsconfig.json +23 -16
  40. package/build-types/components/rotation-ruler/stories/index.story.d.ts +0 -8
  41. package/build-types/components/rotation-ruler/stories/index.story.d.ts.map +0 -1
  42. package/build-types/image-editor/stories/rectangle-crop.story.d.ts +0 -10
  43. package/build-types/image-editor/stories/rectangle-crop.story.d.ts.map +0 -1
  44. package/tsconfig.tsbuildinfo +0 -1
@@ -1,17 +1,20 @@
1
+ import clsx from 'clsx';
1
2
  import {
2
3
  Button,
3
4
  Spinner,
4
5
  __experimentalConfirmDialog as ConfirmDialog,
5
6
  } from '@wordpress/components';
6
- import { Stack, Tabs } from '@wordpress/ui';
7
+ import { Stack, Tabs, VisuallyHidden } from '@wordpress/ui';
7
8
  import { useViewportMatch } from '@wordpress/compose';
8
9
  import { useDispatch, useSelect } from '@wordpress/data';
9
10
  import { store as coreStore } from '@wordpress/core-data';
11
+ import { NavigableRegion } from '@wordpress/admin-ui';
10
12
  import {
13
+ createContext,
11
14
  createPortal,
12
15
  useCallback,
16
+ useContext,
13
17
  useEffect,
14
- useMemo,
15
18
  useRef,
16
19
  useState,
17
20
  } from '@wordpress/element';
@@ -24,20 +27,14 @@ import {
24
27
  } from '@wordpress/keycodes';
25
28
  import { SnackbarNotices, store as noticesStore } from '@wordpress/notices';
26
29
  import type { Field } from '@wordpress/dataviews';
27
- import {
28
- ComplementaryArea,
29
- InterfaceSkeleton,
30
- PinnedItems,
31
- // @ts-expect-error `@wordpress/interface` is not typed yet.
32
- } from '@wordpress/interface';
33
30
  import type { KeyboardEvent as ReactKeyboardEvent, ReactNode } from 'react';
34
31
  import { MediaEditorProvider } from '../media-editor-provider';
35
32
  import type { Media } from '../media-editor-provider';
36
33
  import MediaPreview from '../media-preview';
37
34
  import MediaEditorCanvas from '../media-editor-canvas';
38
35
  import MediaEditorFineRotation from '../media-editor-fine-rotation';
39
- import MediaEditorImageControls from '../media-editor-image-controls';
40
36
  import MediaEditorCropPanel from '../media-editor-crop-panel';
37
+ import MediaEditorImageControls from '../media-editor-image-controls';
41
38
  import MediaForm from '../media-form';
42
39
  import { getMediaTypeFromMimeType } from '../../utils';
43
40
  import { MediaEditorStateProvider, useMediaEditor } from '../../state';
@@ -60,26 +57,40 @@ const ATTACHMENT_EMBED_QUERY = { _embed: 'author,wp:attached-to' } as const;
60
57
 
61
58
  const PLACEMENT_CONTROL_IDLE_MS = 300;
62
59
 
63
- interface EditorTab {
60
+ /**
61
+ * Identifier for the details panel, and the panel the sidebar opens on before
62
+ * the user picks another. The sidebar tracks which panel is open rather than
63
+ * whether one is, so a further panel is a new id here and a new entry in the
64
+ * tab list — not a change to the state's shape.
65
+ */
66
+ const DETAILS_PANEL = 'details';
67
+
68
+ /** Identifier for the crop panel. Images only; other media has nothing to crop. */
69
+ const CROP_PANEL = 'crop';
70
+
71
+ interface MediaEditorTab {
64
72
  id: string;
65
73
  title: string;
66
- panel: JSX.Element;
74
+ render: () => JSX.Element;
67
75
  }
68
76
 
69
77
  export interface MediaEditorFrameProps {
70
78
  children: ReactNode;
71
- headerActions: ReactNode;
72
- footerActions: ReactNode;
73
79
  /**
74
- * Footer layout selector. Frames apply this to the footer container
75
- * as a modifier class. Tracks the sidebar-collapse breakpoint (`medium`).
76
- *
77
- * - `wide` — sidebar is a column; footer is a single row of History |
78
- * Cancel/Save (transform controls live in the Crop panel).
79
- * - `narrow` — sidebar collapsed; transform controls sit above a
80
- * History | Cancel/Save row.
80
+ * Whether the media being edited is an image. The history and transform
81
+ * clusters render nothing for other media types, so a frame uses this to
82
+ * decide whether the container it would put them in is worth rendering at
83
+ * all.
84
+ */
85
+ isImage: boolean;
86
+ /**
87
+ * `narrow` below the `small` breakpoint, where the frame's header has no
88
+ * room for the history cluster beside its other controls. A frame uses
89
+ * this to place that cluster, not to decide whether it renders —
90
+ * `HistoryActions` returns nothing on its own when the canvas is off
91
+ * screen.
81
92
  */
82
- footerLayout: 'wide' | 'narrow';
93
+ layout: 'wide' | 'narrow';
83
94
  onRequestClose: () => void;
84
95
  onKeyDown: ( event: ReactKeyboardEvent< HTMLElement > ) => void;
85
96
  shouldCloseOnClickOutside: boolean;
@@ -97,74 +108,131 @@ export interface MediaEditorProps {
97
108
  renderFrame: ( props: MediaEditorFrameProps ) => JSX.Element;
98
109
  noticesClassName?: string;
99
110
  noticesPortalElement?: Element | null;
100
- showCloseButton?: boolean;
101
111
  shouldCloseOnEsc?: boolean;
102
112
  }
103
113
 
104
114
  interface MediaEditorSidebarProps {
105
- tabs: EditorTab[];
106
- activeTabId?: string;
107
- onTabChange: ( tabId: string ) => void;
115
+ tabs: MediaEditorTab[];
116
+ /** The open panel's id. Always one of `tabs`. */
117
+ activeTab: string;
118
+ onSelectTab: ( tab: string ) => void;
108
119
  }
109
120
 
110
121
  function MediaEditorSidebar( {
111
122
  tabs,
112
- activeTabId,
113
- onTabChange,
123
+ activeTab,
124
+ onSelectTab,
114
125
  }: MediaEditorSidebarProps ) {
115
126
  return (
116
- <ComplementaryArea
117
- scope="media-editor"
118
- identifier="media-editor/details"
119
- title={ __( 'Details' ) }
120
- icon={ drawerRight }
121
- isActiveByDefault
127
+ <NavigableRegion
122
128
  className="media-editor__sidebar"
123
- panelClassName="media-editor__sidebar-panel"
124
- headerClassName="media-editor__sidebar-header"
125
- closeLabel={ __( 'Close media panel' ) }
126
- // Makes `Tabs.Root` the container, so the tab list passed as
127
- // `header` and the panels below share a subtree across the fill.
128
- render={
129
- <Tabs.Root
130
- value={ activeTabId }
131
- onValueChange={ ( value ) =>
132
- onTabChange( value as string )
133
- }
134
- />
135
- }
136
- header={
137
- <Tabs.List variant="minimal">
138
- { tabs.map( ( tab ) => (
139
- <Tabs.Tab key={ tab.id } value={ tab.id }>
140
- { tab.title }
141
- </Tabs.Tab>
142
- ) ) }
143
- </Tabs.List>
144
- }
129
+ ariaLabel={ __( 'Media settings' ) }
145
130
  >
146
- { tabs.map( ( tab ) => (
147
- <Tabs.Panel key={ tab.id } value={ tab.id } tabIndex={ -1 }>
148
- { tab.panel }
149
- </Tabs.Panel>
150
- ) ) }
151
- </ComplementaryArea>
131
+ { /* No visible heading and no close button: the strip below names
132
+ the open panel and the header's pressed toggle dismisses it.
133
+ The heading stays for screen readers, which read neither as
134
+ this region's title. */ }
135
+ <VisuallyHidden render={ <h2 /> }>
136
+ { __( 'Media settings' ) }
137
+ </VisuallyHidden>
138
+ <Tabs.Root
139
+ className="media-editor__tabs"
140
+ value={ activeTab }
141
+ onValueChange={ onSelectTab }
142
+ >
143
+ { /* `Tabs.List` sizes itself to its tabs (`width: fit-content`),
144
+ so the rule under the strip is drawn by this wrapper
145
+ instead, which spans the column. */ }
146
+ <div className="media-editor__tablist">
147
+ <Tabs.List variant="minimal">
148
+ { tabs.map( ( tab ) => (
149
+ <Tabs.Tab key={ tab.id } value={ tab.id }>
150
+ { tab.title }
151
+ </Tabs.Tab>
152
+ ) ) }
153
+ </Tabs.List>
154
+ </div>
155
+ { /* One `Tabs.Panel` per tab: the counts have to match or
156
+ `Tabs.Root` throws in dev. Only the active one paints
157
+ anything — the rest render nothing. */ }
158
+ { tabs.map( ( tab ) => (
159
+ <Tabs.Panel
160
+ key={ tab.id }
161
+ value={ tab.id }
162
+ tabIndex={ -1 }
163
+ className="media-editor__panel"
164
+ >
165
+ <Stack direction="column" gap="lg">
166
+ { tab.render() }
167
+ </Stack>
168
+ </Tabs.Panel>
169
+ ) ) }
170
+ </Tabs.Root>
171
+ </NavigableRegion>
152
172
  );
153
173
  }
154
174
 
155
- interface HeaderActionsProps {
156
- isSaving: boolean;
175
+ /**
176
+ * Everything the frame's action clusters need from the editor. Supplied by
177
+ * context rather than by props so the clusters can be module-level components:
178
+ * a component created during a render has a new identity on every pass, which
179
+ * remounts its subtree and drops focus mid-interaction.
180
+ */
181
+ interface MediaEditorFrameContextValue {
157
182
  isImage: boolean;
158
- showCloseButton?: boolean;
183
+ isSaving: boolean;
184
+ hasMedia: boolean;
185
+ hasChanges: boolean;
186
+ isUndoRedoDisabled: boolean;
187
+ aspectRatioPresets?: AspectRatioPreset[];
188
+ /** `true` above `large`, where the panel docks beside the canvas. */
189
+ isWide: boolean;
190
+ /** The open panel's id, or `null` when none is. */
191
+ activePanel: string | null;
192
+ /**
193
+ * Opens the sidebar on the last-shown panel, or closes it when one is
194
+ * already open.
195
+ */
196
+ onTogglePanel: () => void;
159
197
  onCancel: () => void;
198
+ onSave: () => void;
199
+ onReset: () => void;
160
200
  }
161
201
 
162
- function HeaderActions( {
163
- isSaving,
164
- isImage,
165
- showCloseButton = false,
166
- onCancel,
167
- }: HeaderActionsProps ) {
202
+ const MediaEditorFrameContext =
203
+ createContext< MediaEditorFrameContextValue | null >( null );
204
+
205
+ /**
206
+ * Hook to access the media editor's frame context.
207
+ *
208
+ * Must be used within a MediaEditor component.
209
+ *
210
+ * @return MediaEditorFrameContextValue the action clusters render from.
211
+ */
212
+ function useMediaEditorFrameContext(): MediaEditorFrameContextValue {
213
+ const context = useContext( MediaEditorFrameContext );
214
+ if ( ! context ) {
215
+ throw new Error(
216
+ 'useMediaEditorFrameContext must be used within MediaEditor'
217
+ );
218
+ }
219
+ return context;
220
+ }
221
+
222
+ export interface HeaderActionsProps {
223
+ /**
224
+ * Whether to include a Close button. Frames with a dismissal affordance of
225
+ * their own — a route's breadcrumbs, say — leave it out.
226
+ *
227
+ * @default false
228
+ */
229
+ showCloseButton?: boolean;
230
+ }
231
+
232
+ function HeaderActions( { showCloseButton = false }: HeaderActionsProps ) {
233
+ const { isImage, isSaving, onCancel, isWide, activePanel, onTogglePanel } =
234
+ useMediaEditorFrameContext();
235
+ const isPanelOpen = !! activePanel;
168
236
  const [ isShortcutsModalOpen, setIsShortcutsModalOpen ] = useState( false );
169
237
  return (
170
238
  <Stack
@@ -181,7 +249,21 @@ function HeaderActions( {
181
249
  onClick={ () => setIsShortcutsModalOpen( true ) }
182
250
  />
183
251
  ) }
184
- <PinnedItems.Slot scope="media-editor" />
252
+ { /* The sidebar holds more than one panel, so this opens and
253
+ closes the sidebar rather than naming a panel: reopening
254
+ returns to whichever tab was last showing, and the tab strip
255
+ inside switches between them. */ }
256
+ <Button
257
+ size="compact"
258
+ icon={ drawerRight }
259
+ label={ __( 'Media settings' ) }
260
+ isPressed={ isPanelOpen }
261
+ // Only a docked column really expands beside the canvas; below
262
+ // that the panel replaces the view, which the pressed state
263
+ // already describes.
264
+ aria-expanded={ isWide ? isPanelOpen : undefined }
265
+ onClick={ onTogglePanel }
266
+ />
185
267
  { showCloseButton && (
186
268
  <Button
187
269
  size="compact"
@@ -201,15 +283,9 @@ function HeaderActions( {
201
283
  );
202
284
  }
203
285
 
204
- interface HistoryActionsProps {
205
- isUndoRedoDisabled?: boolean;
206
- onReset: () => void;
207
- }
208
-
209
- function HistoryActions( {
210
- isUndoRedoDisabled = false,
211
- onReset,
212
- }: HistoryActionsProps ) {
286
+ function HistoryActions() {
287
+ const { isImage, isUndoRedoDisabled, onReset, isWide, activePanel } =
288
+ useMediaEditorFrameContext();
213
289
  const {
214
290
  reset,
215
291
  isDirty,
@@ -220,6 +296,17 @@ function HistoryActions( {
220
296
  beginGesture,
221
297
  endGesture,
222
298
  } = useMediaEditor();
299
+ // Reset, undo and redo act on the cropper, so they only appear where the
300
+ // canvas does. Non-image media has no edit history at all; below the dock
301
+ // breakpoint an open panel replaces the canvas, and leaving these behind
302
+ // would put an enabled, destructive Reset next to metadata fields it does
303
+ // not touch — clicking it would discard a crop the user cannot see.
304
+ // (Metadata edits have no undo of their own; a history spanning both is a
305
+ // larger change than hiding these.) Checked after the hooks above so the
306
+ // hook order stays stable across renders.
307
+ if ( ! isImage || ( ! isWide && !! activePanel ) ) {
308
+ return null;
309
+ }
223
310
  const handleUndo = () => {
224
311
  if ( isUndoRedoDisabled ) {
225
312
  return;
@@ -281,31 +368,30 @@ function HistoryActions( {
281
368
  );
282
369
  }
283
370
 
284
- interface FooterActionsProps {
285
- isSaving: boolean;
286
- hasMedia: boolean;
287
- hasChanges: boolean;
288
- onCancel: () => void;
289
- onSave: () => void;
371
+ export interface SaveActionsProps {
372
+ /**
373
+ * Button height. Footers take the 40px default; frames placing these in a
374
+ * page header pass `compact` to match the header's other controls.
375
+ *
376
+ * @default 'default'
377
+ */
378
+ size?: 'default' | 'compact';
290
379
  }
291
380
 
292
- function FooterActions( {
293
- isSaving,
294
- hasMedia,
295
- hasChanges,
296
- onCancel,
297
- onSave,
298
- }: FooterActionsProps ) {
381
+ function SaveActions( { size = 'default' }: SaveActionsProps ) {
382
+ const { isSaving, hasMedia, hasChanges, onCancel, onSave } =
383
+ useMediaEditorFrameContext();
299
384
  const saveDisabled = isSaving || ! hasMedia || ! hasChanges;
300
385
  return (
301
386
  <Stack
302
- className="media-editor__footer-actions"
387
+ className="media-editor__save-actions"
303
388
  justify="flex-end"
304
389
  align="center"
305
390
  gap="sm"
306
391
  >
307
392
  <Button
308
393
  __next40pxDefaultSize
394
+ size={ size }
309
395
  variant="tertiary"
310
396
  onClick={ onCancel }
311
397
  disabled={ isSaving }
@@ -315,6 +401,7 @@ function FooterActions( {
315
401
  </Button>
316
402
  <Button
317
403
  __next40pxDefaultSize
404
+ size={ size }
318
405
  variant="primary"
319
406
  onClick={ onSave }
320
407
  isBusy={ isSaving }
@@ -336,20 +423,56 @@ function MediaEditorContent( {
336
423
  renderFrame,
337
424
  noticesClassName = 'media-editor__snackbar',
338
425
  noticesPortalElement,
339
- showCloseButton = false,
340
426
  shouldCloseOnEsc = false,
341
427
  }: MediaEditorProps ) {
342
428
  const cropper = useMediaEditor();
343
- // The sidebar is a side column from the `small` breakpoint up and collapses
344
- // to an overlay below it — mirroring InterfaceSkeleton's behaviour, shifted
345
- // from `medium` to `small` (see the matching CSS overrides in style.scss).
346
- // Track that single breakpoint: in "panel mode" (≥ small) the
347
- // rotate/flip/zoom controls live in the Crop panel and the footer is just
348
- // History + Cancel/Save; below it the controls drop into the footer. (The
349
- // fine-rotation ruler always sits under the canvas.)
350
- const isPanelLayout = useViewportMatch( 'small' );
351
- const footerLayout: 'wide' | 'narrow' = isPanelLayout ? 'wide' : 'narrow';
352
-
429
+ // Width decides whether the settings panel docks beside the canvas or
430
+ // takes the whole body. It docks from `small` (600px): the modal is the
431
+ // viewport less a 16px margin either side, so a 320px panel and the
432
+ // canvas column's 2 x 24px gutters still leave the canvas ~200px there
433
+ // and ~380px by 782px. Tight, but a full-screen panel would have hidden
434
+ // the image altogether — you could not crop and preview at once.
435
+ //
436
+ // The same line moves the transform controls: docked, the Crop panel
437
+ // carries them; below it they fall back to a row under the canvas.
438
+ //
439
+ // The choice is not carried across openings: the modal is transient, and
440
+ // re-opening with the panel hidden on a wide screen is how #81487 read to
441
+ // people in the first place. Within one opening it does hold — see
442
+ // `hasChosenPanelRef` below.
443
+ const isWide = useViewportMatch( 'small' );
444
+ // `null` when nothing is open, otherwise the open panel's id.
445
+ const [ activePanel, setActivePanel ] = useState< string | null >(
446
+ isWide ? DETAILS_PANEL : null
447
+ );
448
+ // Width picks the starting panel, but only until the user picks one. After
449
+ // that their choice holds for the rest of the session: closing the panel
450
+ // and then resizing — or crossing the breakpoint by rotating a tablet —
451
+ // should not reopen something they just dismissed.
452
+ const hasChosenPanelRef = useRef( false );
453
+ // The panel the toggle reopens, and the one width picks until the user
454
+ // picks their own. Tracked separately from `hasChosenPanelRef` because
455
+ // the two answer different questions: whether the panel should be open,
456
+ // and which tab it should show.
457
+ const lastPanelRef = useRef( DETAILS_PANEL );
458
+ const hasChosenTabRef = useRef( false );
459
+ // Switching tabs says nothing about whether the panel should be open, so
460
+ // it leaves `hasChosenPanelRef` alone: picking Details on a wide screen
461
+ // and then dragging the window narrow should still hand the canvas the
462
+ // full width.
463
+ const selectPanel = useCallback( ( panel: string ) => {
464
+ hasChosenTabRef.current = true;
465
+ lastPanelRef.current = panel;
466
+ setActivePanel( panel );
467
+ }, [] );
468
+ const togglePanel = useCallback( () => {
469
+ hasChosenPanelRef.current = true;
470
+ setActivePanel( ( open ) => ( open ? null : lastPanelRef.current ) );
471
+ }, [] );
472
+ // Below `small` a frame's header cannot fit the history cluster alongside
473
+ // its own controls, so it moves that cluster elsewhere. The same width
474
+ // undocks the panel, so this is `isWide` rather than a second query.
475
+ const layout: 'wide' | 'narrow' = isWide ? 'wide' : 'narrow';
353
476
  const { media, hasEdits } = useSelect(
354
477
  ( select ) => {
355
478
  const {
@@ -436,14 +559,38 @@ function MediaEditorContent( {
436
559
 
437
560
  const mediaType = getMediaTypeFromMimeType( media?.mime_type ).type;
438
561
  const isImage = !! media && mediaType === 'image';
562
+
563
+ // Until the user picks a panel, follow the breakpoint: dragging a window
564
+ // narrow hands the canvas the full width instead of leaving a panel
565
+ // wedged beside it.
566
+ //
567
+ // Docked, the panel opens on Crop: the canvas is still beside it and
568
+ // cropping is what the editor is for. Below `small` an open panel covers
569
+ // the canvas, and crop controls with no crop in sight are not worth
570
+ // opening on, so Details leads there. Non-image media has no Crop tab at
571
+ // all (see `tabs`), so it always gets Details. `isImage` comes from a
572
+ // record that resolves after mount, which is why this runs in an effect
573
+ // rather than seeding `useState`.
574
+ useEffect( () => {
575
+ if ( hasChosenPanelRef.current && hasChosenTabRef.current ) {
576
+ return;
577
+ }
578
+ const defaultPanel = isWide && isImage ? CROP_PANEL : DETAILS_PANEL;
579
+ if ( ! hasChosenTabRef.current ) {
580
+ lastPanelRef.current = defaultPanel;
581
+ }
582
+ if ( ! hasChosenPanelRef.current ) {
583
+ setActivePanel( isWide ? lastPanelRef.current : null );
584
+ }
585
+ }, [ isWide, isImage ] );
586
+ // Only `resetCropOptions` is needed here, for the Reset button; the
587
+ // aspect-ratio members are read by `MediaEditorImageControls` itself.
439
588
  const {
440
589
  aspectRatioValue,
441
590
  setAspectRatioValue,
442
591
  aspectRatioOptions,
443
592
  resetCropOptions,
444
- } = useCropOptions( {
445
- aspectRatioPresets,
446
- } );
593
+ } = useCropOptions( { aspectRatioPresets } );
447
594
  const { isSaving, save: saveMediaEditor } = useSaveMediaEditor( {
448
595
  cropper,
449
596
  id,
@@ -452,59 +599,6 @@ function MediaEditorContent( {
452
599
  onSaved,
453
600
  } );
454
601
 
455
- const tabs = useMemo< EditorTab[] >( () => {
456
- const detailsTab: EditorTab = {
457
- id: 'details',
458
- title: __( 'Details' ),
459
- panel: (
460
- <Stack
461
- className="media-editor__panel"
462
- direction="column"
463
- gap="lg"
464
- >
465
- <MediaForm />
466
- </Stack>
467
- ),
468
- };
469
- if ( ! isImage ) {
470
- return [ detailsTab ];
471
- }
472
- return [
473
- {
474
- id: 'crop',
475
- title: __( 'Crop' ),
476
- panel: (
477
- <Stack
478
- className="media-editor__panel"
479
- direction="column"
480
- gap="lg"
481
- >
482
- <MediaEditorCropPanel
483
- aspectRatioValue={ aspectRatioValue }
484
- onAspectRatioChange={ setAspectRatioValue }
485
- aspectRatioOptions={ aspectRatioOptions }
486
- showTransformControls={ isPanelLayout }
487
- />
488
- </Stack>
489
- ),
490
- },
491
- detailsTab,
492
- ];
493
- }, [
494
- isImage,
495
- aspectRatioValue,
496
- setAspectRatioValue,
497
- aspectRatioOptions,
498
- isPanelLayout,
499
- ] );
500
-
501
- // Control the active tab from state here so the selection survives the
502
- // sidebar closing (which unmounts the `ComplementaryArea` Fill and its
503
- // tabs). Fall back to the first tab until one is picked, so images open on
504
- // Crop.
505
- const [ selectedTabId, setSelectedTabId ] = useState< string >();
506
- const activeTabId = selectedTabId ?? tabs[ 0 ]?.id;
507
-
508
602
  const handleChange = ( updates: Partial< Media > ) => {
509
603
  editEntityRecord( 'postType', 'attachment', id, updates );
510
604
  };
@@ -573,6 +667,45 @@ function MediaEditorContent( {
573
667
  />
574
668
  );
575
669
 
670
+ // Below `small` the panel is full screen, so an open one replaces the
671
+ // canvas and these never share the screen with the Crop panel's copies:
672
+ // this row is what reaches rotate/flip/zoom while the panel is closed,
673
+ // with aspect ratio joining them as a dropdown. From `small` up the Crop
674
+ // panel docks beside the canvas and carries them instead.
675
+ const imageControls =
676
+ isImage && ! isWide ? (
677
+ <MediaEditorImageControls
678
+ showAspectRatioControl
679
+ aspectRatioPresets={ aspectRatioPresets }
680
+ />
681
+ ) : null;
682
+
683
+ // Crop leads, since it is what the docked panel opens on. Non-image media
684
+ // has nothing to crop and gets Details alone, which is why the sidebar
685
+ // takes this list rather than deriving it.
686
+ const tabs: MediaEditorTab[] = [
687
+ ...( isImage
688
+ ? [
689
+ {
690
+ id: CROP_PANEL,
691
+ title: __( 'Crop' ),
692
+ render: () => (
693
+ <MediaEditorCropPanel
694
+ aspectRatioValue={ aspectRatioValue }
695
+ onAspectRatioChange={ setAspectRatioValue }
696
+ aspectRatioOptions={ aspectRatioOptions }
697
+ />
698
+ ),
699
+ },
700
+ ]
701
+ : [] ),
702
+ {
703
+ id: DETAILS_PANEL,
704
+ title: __( 'Details' ),
705
+ render: () => <MediaForm />,
706
+ },
707
+ ];
708
+
576
709
  const ruler = isImage ? (
577
710
  <MediaEditorFineRotation
578
711
  onPlacementControlInteraction={ signalPlacementControlInteraction }
@@ -591,52 +724,58 @@ function MediaEditorContent( {
591
724
  <Spinner />
592
725
  </div>
593
726
  ) : (
594
- <>
595
- <MediaEditorSidebar
596
- tabs={ tabs }
597
- activeTabId={ activeTabId }
598
- onTabChange={ setSelectedTabId }
599
- />
600
- <InterfaceSkeleton
601
- className="media-editor__skeleton"
602
- labels={ {
603
- body: isImage
727
+ <div
728
+ className={ clsx( 'media-editor__body', {
729
+ 'has-panel-open': !! activePanel,
730
+ } ) }
731
+ >
732
+ <NavigableRegion
733
+ className="media-editor__content"
734
+ ariaLabel={
735
+ isImage
604
736
  ? __( 'Image editor' )
605
- : __( 'Media preview' ),
606
- sidebar: __( 'Media details' ),
607
- } }
608
- content={
609
- <div className="media-editor__content">
610
- <div className="media-editor__canvas-area">
611
- { isImage ? (
612
- <MediaEditorCanvas
613
- focusOnMount
614
- isPlacementActive={
615
- isPlacementActive
616
- }
617
- onGestureStart={
618
- handleCanvasGestureStart
619
- }
620
- onGestureEnd={
621
- handleCanvasGestureEnd
622
- }
623
- />
624
- ) : (
625
- <MediaPreview />
626
- ) }
627
- </div>
628
- { isImage && (
629
- <div className="media-editor__canvas-toolbar">
630
- { ruler }
631
- </div>
632
- ) }
633
- </div>
634
- }
635
- sidebar={
636
- <ComplementaryArea.Slot scope="media-editor" />
737
+ : __( 'Media preview' )
637
738
  }
638
- />
639
- </>
739
+ >
740
+ <div className="media-editor__canvas-area">
741
+ { isImage ? (
742
+ <MediaEditorCanvas
743
+ isPlacementActive={ isPlacementActive }
744
+ onGestureStart={
745
+ handleCanvasGestureStart
746
+ }
747
+ onGestureEnd={ handleCanvasGestureEnd }
748
+ />
749
+ ) : (
750
+ <MediaPreview />
751
+ ) }
752
+ </div>
753
+ { isImage && (
754
+ <div className="media-editor__canvas-toolbar">
755
+ { ruler }
756
+ { imageControls }
757
+ </div>
758
+ ) }
759
+ </NavigableRegion>
760
+ { !! activePanel && (
761
+ <MediaEditorSidebar
762
+ tabs={ tabs }
763
+ // Swapping the edited media can drop the Crop
764
+ // tab out from under an open panel — saving a
765
+ // crop moves the editor to a new id, which may
766
+ // resolve as non-image. Fall back rather than
767
+ // hand `Tabs.Root` a value no tab matches.
768
+ activeTab={
769
+ tabs.some(
770
+ ( tab ) => tab.id === activePanel
771
+ )
772
+ ? activePanel
773
+ : DETAILS_PANEL
774
+ }
775
+ onSelectTab={ selectPanel }
776
+ />
777
+ ) }
778
+ </div>
640
779
  ) }
641
780
  </div>
642
781
  <ConfirmDialog
@@ -659,74 +798,38 @@ function MediaEditorContent( {
659
798
  </MediaEditorProvider>
660
799
  );
661
800
 
662
- const history = isImage ? (
663
- <HistoryActions
664
- isUndoRedoDisabled={ isCropInteractionActive }
665
- onReset={ resetCropOptions }
666
- />
667
- ) : null;
668
- const imageControls = isImage ? (
669
- <MediaEditorImageControls
670
- showAspectRatioControl
671
- aspectRatioPresets={ aspectRatioPresets }
672
- />
673
- ) : null;
674
- const actions = (
675
- <FooterActions
676
- isSaving={ isSaving }
677
- hasMedia={ !! media }
678
- hasChanges={ hasChanges }
679
- onCancel={ handleRequestClose }
680
- onSave={ saveMediaEditor }
681
- />
682
- );
683
-
684
- // The fine-rotation ruler always lives under the canvas (in
685
- // `media-editor__content`), never the footer, so it stays constrained to
686
- // the canvas column at every viewport. One JSX tree per layout; DOM order
687
- // matches visual order.
688
- let footerActions: ReactNode;
689
- if ( footerLayout === 'wide' ) {
690
- // Sidebar is a column: image controls live in the Crop panel, so
691
- // the footer is just History + Cancel/Save.
692
- footerActions = (
693
- <>
694
- { history }
695
- { actions }
696
- </>
697
- );
698
- } else {
699
- // Sidebar collapsed: the image controls drop into the footer.
700
- footerActions = (
701
- <>
702
- { imageControls }
703
- <div className="media-editor-modal__footer-row">
704
- { history }
705
- { actions }
706
- </div>
707
- </>
708
- );
709
- }
710
-
711
- return renderFrame( {
712
- children,
713
- headerActions: (
714
- <HeaderActions
715
- isSaving={ isSaving }
716
- isImage={ isImage }
717
- showCloseButton={ showCloseButton }
718
- onCancel={ handleRequestClose }
719
- />
720
- ),
721
- footerActions,
722
- footerLayout,
723
- onRequestClose: handleRequestClose,
724
- onKeyDown: handleKeyDown,
725
- shouldCloseOnClickOutside: ! hasChanges && ! isSaving,
801
+ // Rebuilt each render, like the elements it replaces: consumers re-render
802
+ // as they always did, but the cluster component types stay stable.
803
+ const contextValue: MediaEditorFrameContextValue = {
804
+ isImage,
726
805
  isSaving,
727
- hasChanges,
728
806
  hasMedia: !! media,
729
- } );
807
+ hasChanges,
808
+ isUndoRedoDisabled: isCropInteractionActive,
809
+ aspectRatioPresets,
810
+ isWide,
811
+ activePanel,
812
+ onTogglePanel: togglePanel,
813
+ onCancel: handleRequestClose,
814
+ onSave: saveMediaEditor,
815
+ onReset: resetCropOptions,
816
+ };
817
+
818
+ return (
819
+ <MediaEditorFrameContext.Provider value={ contextValue }>
820
+ { renderFrame( {
821
+ children,
822
+ isImage,
823
+ layout,
824
+ onRequestClose: handleRequestClose,
825
+ onKeyDown: handleKeyDown,
826
+ shouldCloseOnClickOutside: ! hasChanges && ! isSaving,
827
+ isSaving,
828
+ hasChanges,
829
+ hasMedia: !! media,
830
+ } ) }
831
+ </MediaEditorFrameContext.Provider>
832
+ );
730
833
  }
731
834
 
732
835
  export function MediaEditor( props: MediaEditorProps ) {
@@ -737,4 +840,11 @@ export function MediaEditor( props: MediaEditorProps ) {
737
840
  );
738
841
  }
739
842
 
843
+ // Attached to `MediaEditor` so frames import and arrange them, the way
844
+ // `DataViewsPicker` exposes its own sub-components. They read what they need
845
+ // from context, so a frame only chooses where they go and how they look.
846
+ MediaEditor.HeaderActions = HeaderActions;
847
+ MediaEditor.HistoryActions = HistoryActions;
848
+ MediaEditor.SaveActions = SaveActions;
849
+
740
850
  export default MediaEditor;