Skip to content

Commit

Permalink
Global styles: close stylebook if the global styles side bar is not o…
Browse files Browse the repository at this point in the history
…pen (#50081)

* In the global styles sidebar, check that the sidebar is open. If not, close any editor canvas views.
This commit checks that the global styles side bar is open or if the editor is visual. If either is false then we programmatically close the style book.

* Removing unused var and renaming variables to be more clear
  • Loading branch information
ramonjd authored Apr 26, 2023
1 parent 004f36c commit 0824845
Showing 1 changed file with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { __ } from '@wordpress/i18n';
import { styles, seen } from '@wordpress/icons';
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { store as interfaceStore } from '@wordpress/interface';

/**
* Internal dependencies
Expand All @@ -17,25 +18,35 @@ import { GlobalStylesMenuSlot } from '../global-styles/ui';
import { unlock } from '../../private-apis';

export default function GlobalStylesSidebar() {
const { editorMode, editorCanvasView } = useSelect( ( select ) => {
return {
editorMode: select( editSiteStore ).getEditorMode(),
editorCanvasView: unlock(
select( editSiteStore )
).getEditorCanvasContainerView(),
};
}, [] );
const { shouldClearCanvasContainerView, isStyleBookOpened } = useSelect(
( select ) => {
const { getActiveComplementaryArea } = select( interfaceStore );
const _isVisualEditorMode =
'visual' === select( editSiteStore ).getEditorMode();

return {
isStyleBookOpened:
'style-book' ===
unlock(
select( editSiteStore )
).getEditorCanvasContainerView(),
shouldClearCanvasContainerView:
'edit-site/global-styles' !==
getActiveComplementaryArea( 'core/edit-site' ) ||
! _isVisualEditorMode,
};
},
[]
);

const { setEditorCanvasContainerView } = unlock(
useDispatch( editSiteStore )
);

useEffect( () => {
if ( editorMode !== 'visual' ) {
if ( shouldClearCanvasContainerView ) {
setEditorCanvasContainerView( undefined );
}
}, [ editorMode ] );

const isStyleBookOpened = editorCanvasView === 'style-book';
}, [ shouldClearCanvasContainerView ] );

return (
<DefaultSidebar
Expand All @@ -55,7 +66,7 @@ export default function GlobalStylesSidebar() {
icon={ seen }
label={ __( 'Style Book' ) }
isPressed={ isStyleBookOpened }
disabled={ editorMode !== 'visual' }
disabled={ shouldClearCanvasContainerView }
onClick={ () =>
setEditorCanvasContainerView(
isStyleBookOpened ? undefined : 'style-book'
Expand Down

0 comments on commit 0824845

Please sign in to comment.