Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global styles: close stylebook if the editor canvas container slot is not filled #50086

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 12 additions & 3 deletions packages/edit-site/src/components/editor-canvas-container/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Children, cloneElement, useState, useMemo } from '@wordpress/element';
import {
Button,
privateApis as componentsPrivateApis,
__experimentalUseSlotFills as useSlotFills,
} from '@wordpress/components';
import { ESCAPE } from '@wordpress/keycodes';
import { __ } from '@wordpress/i18n';
Expand All @@ -25,7 +26,7 @@ import { store as editSiteStore } from '../../store';
*
* @return {string} Translated string corresponding to value of view. Default is ''.
*/
export function getEditorCanvasContainerTitle( view ) {
function getEditorCanvasContainerTitle( view ) {
switch ( view ) {
case 'style-book':
return __( 'Style Book' );
Expand All @@ -37,8 +38,11 @@ export function getEditorCanvasContainerTitle( view ) {
// Creates a private slot fill.
const { createPrivateSlotFill } = unlock( componentsPrivateApis );
const SLOT_FILL_NAME = 'EditSiteEditorCanvasContainerSlot';
const { Slot: EditorCanvasContainerSlot, Fill: EditorCanvasContainerFill } =
createPrivateSlotFill( SLOT_FILL_NAME );
const {
privateKey,
Slot: EditorCanvasContainerSlot,
Fill: EditorCanvasContainerFill,
} = createPrivateSlotFill( SLOT_FILL_NAME );

function EditorCanvasContainer( {
children,
Expand Down Expand Up @@ -110,6 +114,11 @@ function EditorCanvasContainer( {
</EditorCanvasContainerFill>
);
}
function useHasEditorCanvasContainer() {
const fills = useSlotFills( privateKey );
return !! fills?.length;
}

EditorCanvasContainer.Slot = EditorCanvasContainerSlot;
export default EditorCanvasContainer;
export { useHasEditorCanvasContainer, getEditorCanvasContainerTitle };
7 changes: 5 additions & 2 deletions packages/edit-site/src/components/header-edit-mode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ import UndoButton from './undo-redo/undo';
import RedoButton from './undo-redo/redo';
import DocumentActions from './document-actions';
import { store as editSiteStore } from '../../store';
import { getEditorCanvasContainerTitle } from '../editor-canvas-container';
import {
getEditorCanvasContainerTitle,
useHasEditorCanvasContainer,
} from '../editor-canvas-container';
import { unlock } from '../../private-apis';

const preventDefault = ( event ) => {
Expand Down Expand Up @@ -122,7 +125,7 @@ export default function HeaderEditMode() {
[ setIsListViewOpened, isListViewOpen ]
);

const hasDefaultEditorCanvasView = ! editorCanvasView;
const hasDefaultEditorCanvasView = ! useHasEditorCanvasContainer();

const isFocusMode = templateType === 'wp_template_part';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ 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 @@ -18,35 +17,25 @@ import { GlobalStylesMenuSlot } from '../global-styles/ui';
import { unlock } from '../../private-apis';

export default function GlobalStylesSidebar() {
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 { editorMode, editorCanvasView } = useSelect( ( select ) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change reinstates the file before #50081 was merged given that this PR uses @noisysocks's original method of checking for the existence of the slot fill to determine whether to display the header.

return {
editorMode: select( editSiteStore ).getEditorMode(),
editorCanvasView: unlock(
select( editSiteStore )
).getEditorCanvasContainerView(),
};
}, [] );
const { setEditorCanvasContainerView } = unlock(
useDispatch( editSiteStore )
);

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

const isStyleBookOpened = editorCanvasView === 'style-book';

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