Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export type BlockVisibilityViewport = 'desktop' | 'tablet' | 'mobile';
/**
* The choices for the block visibility.
*
* Duplicated in packages/editor/src/components/preview-dropdown/index.js (choices array)
* and packages/edit-site/src/components/block-editor/use-viewport-sync.js
* (VALID_DEVICE_TYPES). Update all three when adding new viewport types.
* Duplicated in packages/editor/src/components/preview-dropdown/index.js (choices
* array), packages/edit-site/src/components/block-editor/viewport.js and
* packages/boot/src/components/canvas/viewport.ts (VALID_VIEWPORTS). Update all
* four when adding new viewport types.
*
* @todo create a single source of truth for the viewport types.
*/
Expand Down
4 changes: 3 additions & 1 deletion packages/boot/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
### Bug Fixes

- Canvas: Show the site's front end where a route names no entity and the theme has no block templates to resolve one from, instead of an editor with nothing open. Was previously handled by the home route alone, leaving `/identity` broken on a classic theme ([#81749](https://github.com/WordPress/gutenberg/pull/81749)).
- Canvas: Open an entity at the width its `viewport` param asks for through the editor provider, so the width comes from the theme's breakpoints rather than the defaults ([#81750](https://github.com/WordPress/gutenberg/pull/81750)).
- Canvas: Stop writing the current width into the `viewport` search param when leaving an entity. The param says where an entity opens; a width set from the device preview is view state and no longer follows the user between entities ([#81750](https://github.com/WordPress/gutenberg/pull/81750)).
- Canvas: Do not offer to edit a trashed entity from its preview ([#81632](https://github.com/WordPress/gutenberg/pull/81632)).
- Translate the click-to-edit label on a previewed canvas, and name the action rather than the gesture ([#81620](https://github.com/WordPress/gutenberg/pull/81620)).

### Enhancements

- Contain a failure to the surface it happened in, so a stage, inspector or canvas that throws leaves the rest of the screen working ([#81622](https://github.com/WordPress/gutenberg/pull/81622)).
- Warn before leaving the page with unsaved changes ([#81625](https://github.com/WordPress/gutenberg/pull/81625)).
- Keep the editor's device preview in step with a `viewport` search param, so an entity that asks to be edited at a particular width — a navigation overlay meant for mobile — opens there and restores the previous width on the way back ([#81617](https://github.com/WordPress/gutenberg/pull/81617)).
- Open an entity at the width its `viewport` search param asks for, so a navigation overlay meant for mobile is edited at mobile width ([#81617](https://github.com/WordPress/gutenberg/pull/81617)).
- Add a `stage` visibility function to a route's config, mirroring `inspector`, so a route can hide its stage and leave the canvas to fill the surfaces area.
- Pass the editor's entity navigation callbacks from the canvas, so a template part or synced pattern can be opened from the block inspector and navigated back out of, restoring the block that was selected ([#81590](https://github.com/WordPress/gutenberg/pull/81590)).
- Add `registerEntityLinks` and `getEntityLink`, so an application declares where each post type is listed and edited and the canvas resolves every link through them ([#81590](https://github.com/WordPress/gutenberg/pull/81590)).
Expand Down
15 changes: 12 additions & 3 deletions packages/boot/src/components/canvas/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect, useMemo } from '@wordpress/element';
import { Spinner } from '@wordpress/components';
import { useNavigate } from '@wordpress/route';
import { useNavigate, useSearch } from '@wordpress/route';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
Expand All @@ -11,7 +11,7 @@ import SitePreview from './site-preview';
import useNavigateToEntityRecord, {
useActionPerformed,
} from './use-navigate-to-entity-record';
import useViewportSync from './use-viewport-sync';
import { getDeviceType } from './viewport';

interface CanvasProps {
canvas: CanvasData;
Expand All @@ -31,7 +31,15 @@ export default function Canvas( { canvas }: CanvasProps ) {
useNavigateToEntityRecord();
const onActionPerformed = useActionPerformed( canvas.postType );

useViewportSync();
/*
* An entity can be asked to be edited at a particular width — a navigation
* overlay meant for mobile — and carries the width it was left at when
* returned to. Anything else opens at the default.
*/
const viewport = useSearch( {
strict: false,
select: ( search ) => ( search as { viewport?: string } ).viewport,
} ) as string | undefined;

/*
* Where clicking a previewed canvas goes, resolved the same way the editor
Expand Down Expand Up @@ -159,6 +167,7 @@ export default function Canvas( { canvas }: CanvasProps ) {
settings={ settings }
backButton={ backButton }
onActionPerformed={ onActionPerformed }
initialViewport={ getDeviceType( viewport ) }
/>
</div>
{ canvas.isPreview && editLink && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
privateApis as routePrivateApis,
} from '@wordpress/route';
import { store as bootStore } from '../../store';
import { DEFAULT_DEVICE_TYPE } from './use-viewport-sync';
import { isValidViewport } from './viewport';
import { unlock } from '../../lock-unlock';

const { useCanGoBack, useRouter } = unlock( routePrivateApis );
Expand All @@ -35,8 +35,6 @@ interface NavigateParams {
viewport?: string;
}

const VALID_VIEWPORTS = [ 'desktop', 'tablet', 'mobile' ];

/**
* Builds the editor's entity navigation callbacks.
*
Expand Down Expand Up @@ -81,38 +79,26 @@ export default function useNavigateToEntityRecord() {
) as { selection?: { selectionStart?: { clientId?: string } } };
const selectedBlock = edits?.selection?.selectionStart?.clientId;

/*
* An entity can ask to be edited at a particular width — a
* navigation overlay meant for mobile. Leave the width this entity
* is being viewed at behind, so returning restores it.
*/
const requestedViewport = params.viewport?.toLowerCase();
const hasRequestedViewport =
!! requestedViewport &&
VALID_VIEWPORTS.includes( requestedViewport );
const currentViewport = hasRequestedViewport
? (
(
registry.select( editorStore ) as any
).getDeviceType() ?? DEFAULT_DEVICE_TYPE
).toLowerCase()
: undefined;

if ( selectedBlock || currentViewport ) {
if ( selectedBlock ) {
navigate( {
search: ( previous: Record< string, unknown > ) => ( {
...previous,
...( selectedBlock ? { selectedBlock } : {} ),
// The default needs no entry; its absence means it.
...( currentViewport &&
currentViewport !== DEFAULT_DEVICE_TYPE.toLowerCase()
? { viewport: currentViewport }
: {} ),
selectedBlock,
} ),
replace: true,
} );
}

/*
* An entity can ask to be edited at a particular width — a
* navigation overlay meant for mobile. The width is where the entity
* opens, not something the entity being left carries away: a width
* set from the device preview is view state, and an entity that asks
* for none opens at the default.
*/
const requestedViewport = params.viewport?.toLowerCase();
const hasRequestedViewport = isValidViewport( requestedViewport );

/*
* `focusMode` marks the entity as one navigated into rather than
* opened directly, which is what offers the way back out. The search
Expand Down
39 changes: 0 additions & 39 deletions packages/boot/src/components/canvas/use-viewport-sync.ts

This file was deleted.

34 changes: 34 additions & 0 deletions packages/boot/src/components/canvas/viewport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const DEFAULT_DEVICE_TYPE = 'Desktop';

/*
* Lowercase in the URL, PascalCase in the editor store. Duplicated in
* packages/edit-site/src/components/block-editor/viewport.js, in the editor's
* preview-dropdown choices, and in the block editor's block-visibility
* constants. Update all four when adding a viewport type.
*/
const VALID_VIEWPORTS = [ 'desktop', 'tablet', 'mobile' ];

/**
* Whether a viewport is one an entity can be asked to be edited at.
*
* @param viewport Viewport, as written in the URL.
* @return Whether the editor has a width for it.
*/
export function isValidViewport( viewport?: string ): viewport is string {
return !! viewport && VALID_VIEWPORTS.includes( viewport.toLowerCase() );
}

/**
* The device type a `viewport` search param names.
*
* @param viewport Viewport, as written in the URL.
* @return Device type, Desktop for a viewport the editor has no width for.
*/
export function getDeviceType( viewport?: string ) {
if ( ! isValidViewport( viewport ) ) {
return DEFAULT_DEVICE_TYPE;
}

const value = viewport.toLowerCase();
return value.charAt( 0 ).toUpperCase() + value.slice( 1 );
}
4 changes: 4 additions & 0 deletions packages/edit-site/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

- Stop rendering `EditorKeyboardShortcutsRegister`, which the editor provider now renders itself ([#81580](https://github.com/WordPress/gutenberg/pull/81580)).

### Bug Fixes

- Open an entity at the width its `viewport` param asks for through the editor provider rather than a sync effect, so the width comes from the theme's breakpoints rather than the defaults, and stop writing the current width back into the param when leaving an entity ([#81750](https://github.com/WordPress/gutenberg/pull/81750)).

## 7.2.0 (2026-08-12)

### Bug Fixes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { useSelect, useRegistry } from '@wordpress/data';
import { useRegistry } from '@wordpress/data';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useCallback } from '@wordpress/element';
import { addQueryArgs } from '@wordpress/url';
import { store as coreStore } from '@wordpress/core-data';
import { store as editorStore } from '@wordpress/editor';
import { unlock } from '../../lock-unlock';
import { DEFAULT_DEVICE_TYPE } from './use-viewport-sync';
import { isValidViewport } from './viewport';

const { useHistory, useLocation } = unlock( routerPrivateApis );

const VALID_VIEWPORTS = [ 'desktop', 'tablet', 'mobile' ];
/**
* Hook to handle navigation to entity records.
*
Expand All @@ -20,10 +19,6 @@ export default function useNavigateToEntityRecord() {
const location = useLocation();
const { query, path } = location;
const registry = useRegistry();
const currentDeviceType = useSelect(
( select ) => select( editorStore ).getDeviceType(),
[]
);

const onNavigateToEntityRecord = useCallback(
( params ) => {
Expand All @@ -49,37 +44,21 @@ export default function useNavigateToEntityRecord() {
// Store the selected block in the URL for restoration when navigating back.
if ( externalClientId ) {
urlUpdates.selectedBlock = externalClientId;
history.navigate( addQueryArgs( path, urlUpdates ), {
replace: true,
} );
}

// Save the current viewport for when we navigate back (e.g. from overlay editor).
// Omit viewport from URL when it's the default to keep URLs clean.
// The width an entity is asked to be edited at is where it opens,
// not something the entity being left carries away: a width set from
// the device preview is view state, and an entity that asks for none
// opens at the default.
const requestedViewport =
typeof params.viewport === 'string'
? params.viewport.toLowerCase()
: undefined;
const isValidRequestedViewport =
VALID_VIEWPORTS.includes( requestedViewport );

if ( isValidRequestedViewport ) {
const currentViewportLower = (
currentDeviceType || DEFAULT_DEVICE_TYPE
).toLowerCase();
if (
currentViewportLower === DEFAULT_DEVICE_TYPE.toLowerCase()
) {
delete urlUpdates.viewport;
} else {
urlUpdates.viewport = currentViewportLower;
}
}

const hasUpdatesToSave =
externalClientId || isValidRequestedViewport;
if ( hasUpdatesToSave ) {
history.navigate( addQueryArgs( path, urlUpdates ), {
replace: true,
} );
}
isValidViewport( requestedViewport );

// Navigate to the new entity record
const queryArgs = {
Expand All @@ -96,7 +75,7 @@ export default function useNavigateToEntityRecord() {

history.navigate( url );
},
[ history, path, query, registry, currentDeviceType ]
[ history, path, query, registry ]
);

return onNavigateToEntityRecord;
Expand Down

This file was deleted.

34 changes: 34 additions & 0 deletions packages/edit-site/src/components/block-editor/viewport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export const DEFAULT_DEVICE_TYPE = 'Desktop';

/*
* Lowercase in the URL, PascalCase in the editor store. Duplicated in
* packages/boot/src/components/canvas/viewport.ts, in the editor's
* preview-dropdown choices, and in the block editor's block-visibility
* constants. Update all four when adding a viewport type.
*/
const VALID_VIEWPORTS = [ 'desktop', 'tablet', 'mobile' ];

/**
* Whether a viewport is one an entity can be asked to be edited at.
*
* @param {string} [viewport] Viewport, as written in the URL.
* @return {boolean} Whether the editor has a width for it.
*/
export function isValidViewport( viewport ) {
return !! viewport && VALID_VIEWPORTS.includes( viewport.toLowerCase() );
}

/**
* The device type a `viewport` query param names.
*
* @param {string} [viewport] Viewport, as written in the URL.
* @return {string} Device type, Desktop for a viewport the editor has no width for.
*/
export function getDeviceType( viewport ) {
if ( ! isValidViewport( viewport ) ) {
return DEFAULT_DEVICE_TYPE;
}

const value = viewport.toLowerCase();
return value.charAt( 0 ).toUpperCase() + value.slice( 1 );
}
Loading
Loading