Skip to content

Commit

Permalink
Simplify logic for preview selection and keep in line with editorMode
Browse files Browse the repository at this point in the history
  • Loading branch information
jeryj committed Aug 2, 2024
1 parent afb0b24 commit 4858356
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions packages/editor/src/components/preview-dropdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,43 +127,28 @@ export default function PreviewDropdown( { forceIsAutosaveable, disabled } ) {
},
];

/**
* The selected choice.
*
* @type {Object}
*/
const previewValue = editorMode === 'zoom-out' ? 'ZoomOut' : deviceType;
let selectedChoice = choices.find(
( choice ) => choice.value === previewValue
);

/**
* If no selected choice is found, default to the first
*/
if ( ! selectedChoice ) {
selectedChoice = choices[ 0 ];
}

/**
* Handles the selection of a device type.
*
* @param {string} value The device type.
*/
const onSelect = ( value ) => {
setDeviceType( value );
let newEditorMode = originalEditingMode.current;

if ( value === 'ZoomOut' ) {
newEditorMode = 'zoom-out';
setDeviceType( 'Desktop' );
} else {
setDeviceType( value );
}

__unstableSetEditorMode( newEditorMode );
};

const getText = () => {
if (
deviceType === 'ZoomOut' ||
editorMode === 'zoom-out' // This can happen if zoom out is enabled from by other means - like the patterns tab.
) {
if ( editorMode === 'zoom-out' ) {
return __( '50%' );
}

Expand Down Expand Up @@ -193,7 +178,7 @@ export default function PreviewDropdown( { forceIsAutosaveable, disabled } ) {
<MenuGroup>
<MenuItemsChoice
choices={ choices }
value={ selectedChoice.value }
value={ previewValue }
onSelect={ onSelect }
/>
</MenuGroup>
Expand Down

0 comments on commit 4858356

Please sign in to comment.