Skip to content

Commit

Permalink
Style book: make the style book slot generic (#49973)
Browse files Browse the repository at this point in the history
* Initial commit:
- create new component slot to house style book and other editor views

* - adding state for editor canvas view container
- forwarding refs to first child

* - attempting to juggle refs so that we retain keyboard navigation and close functionality

* Remove comment that was the motivation behind this PR in the first place.

* Adding the label to the interactive element

* Make the slot private.

* Resetting default state of `editorCanvasContainerView` to `undefined`
  • Loading branch information
ramonjd authored Apr 26, 2023
1 parent bd8c936 commit 13a2d09
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 103 deletions.
16 changes: 8 additions & 8 deletions packages/edit-site/src/components/block-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import { store as editSiteStore } from '../../store';
import BackButton from './back-button';
import ResizableEditor from './resizable-editor';
import EditorCanvas from './editor-canvas';
import StyleBook from '../style-book';
import { unlock } from '../../private-apis';
import EditorCanvasContainer from '../editor-canvas-container';

const { ExperimentalBlockEditorProvider } = unlock( blockEditorPrivateApis );

Expand Down Expand Up @@ -165,19 +165,19 @@ export default function BlockEditor() {
<SidebarInspectorFill>
<BlockInspector />
</SidebarInspectorFill>
{ /* Potentially this could be a generic slot (e.g. EditorCanvas.Slot) if there are other uses for it. */ }
<StyleBook.Slot>
{ ( [ styleBook ] ) =>
styleBook ? (
<EditorCanvasContainer.Slot>
{ ( [ editorCanvasView ] ) =>
editorCanvasView ? (
<div className="edit-site-visual-editor is-focus-mode">
<ResizableEditor enableResizing>
{ styleBook }
{ editorCanvasView }
</ResizableEditor>
</div>
) : (
<BlockTools
className={ classnames( 'edit-site-visual-editor', {
'is-focus-mode': isTemplatePart || !! styleBook,
'is-focus-mode':
isTemplatePart || !! editorCanvasView,
'is-view-mode': isViewMode,
} ) }
__unstableContentRef={ contentRef }
Expand Down Expand Up @@ -211,7 +211,7 @@ export default function BlockEditor() {
</BlockTools>
)
}
</StyleBook.Slot>
</EditorCanvasContainer.Slot>
<ReusableBlocksMenuItems />
</ExperimentalBlockEditorProvider>
);
Expand Down
115 changes: 115 additions & 0 deletions packages/edit-site/src/components/editor-canvas-container/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* WordPress dependencies
*/
import { Children, cloneElement, useState, useMemo } from '@wordpress/element';
import {
Button,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { ESCAPE } from '@wordpress/keycodes';
import { __ } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import { closeSmall } from '@wordpress/icons';
import { useFocusOnMount, useFocusReturn } from '@wordpress/compose';

/**
* Internal dependencies
*/
import { unlock } from '../../private-apis';
import { store as editSiteStore } from '../../store';

/**
* Returns a translated string for the title of the editor canvas container.
*
* @param {string} view Editor canvas container view.
*
* @return {string} Translated string corresponding to value of view. Default is ''.
*/
export function getEditorCanvasContainerTitle( view ) {
switch ( view ) {
case 'style-book':
return __( 'Style Book' );
default:
return '';
}
}

// Creates a private slot fill.
const { createPrivateSlotFill } = unlock( componentsPrivateApis );
const SLOT_FILL_NAME = 'EditSiteEditorCanvasContainerSlot';
const { Slot: EditorCanvasContainerSlot, Fill: EditorCanvasContainerFill } =
createPrivateSlotFill( SLOT_FILL_NAME );

function EditorCanvasContainer( {
children,
closeButtonLabel,
onClose = () => {},
} ) {
const editorCanvasContainerView = useSelect(
( select ) =>
unlock( select( editSiteStore ) ).getEditorCanvasContainerView(),
[]
);
const [ isClosed, setIsClosed ] = useState( false );
const { setEditorCanvasContainerView } = unlock(
useDispatch( editSiteStore )
);
const focusOnMountRef = useFocusOnMount( 'firstElement' );
const sectionFocusReturnRef = useFocusReturn();
const title = useMemo(
() => getEditorCanvasContainerTitle( editorCanvasContainerView ),
[ editorCanvasContainerView ]
);
function onCloseContainer() {
onClose();
setEditorCanvasContainerView( undefined );
setIsClosed( true );
}

function closeOnEscape( event ) {
if ( event.keyCode === ESCAPE && ! event.defaultPrevented ) {
event.preventDefault();
onCloseContainer();
}
}

const childrenWithProps = Array.isArray( children )
? Children.map( children, ( child, index ) =>
index === 0
? cloneElement( child, {
ref: sectionFocusReturnRef,
} )
: child
)
: cloneElement( children, {
ref: sectionFocusReturnRef,
} );

if ( isClosed ) {
return null;
}

return (
<EditorCanvasContainerFill>
{ /* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */ }
<section
className="edit-site-editor-canvas-container"
ref={ focusOnMountRef }
onKeyDown={ closeOnEscape }
aria-label={ title }
>
<Button
className="edit-site-editor-canvas-container__close-button"
icon={ closeSmall }
label={ closeButtonLabel || __( 'Close' ) }
onClick={ onCloseContainer }
showTooltip={ false }
/>
{ childrenWithProps }
</section>
</EditorCanvasContainerFill>
);
}

EditorCanvasContainer.Slot = EditorCanvasContainerSlot;
export default EditorCanvasContainer;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.edit-site-editor-canvas-container {
background: $white; // Fallback color, overridden by JavaScript.
border-radius: $radius-block-ui;
bottom: 0;
left: 0;
overflow: hidden;
position: absolute;
right: 0;
top: 0;
transition: all 0.3s; // Match .block-editor-iframe__body transition.
}

.edit-site-editor-canvas-container__close-button {
position: absolute;
right: $grid-unit-10;
top: math.div($grid-unit-60 - $button-size, 2); // ( tab height - button size ) / 2
z-index: 1;
background: $white;
}
16 changes: 10 additions & 6 deletions packages/edit-site/src/components/global-styles/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import StyleBook from '../style-book';
import ScreenCSS from './screen-css';
import { unlock } from '../../private-apis';
import ScreenEffects from './screen-effects';
import { store as editSiteStore } from '../../store';

const SLOT_FILL_NAME = 'GlobalStylesMenu';
const { Slot: GlobalStylesMenuSlot, Fill: GlobalStylesMenuFill } =
Expand Down Expand Up @@ -239,7 +240,7 @@ function ContextScreens( { name, parentMenu = '', variation = '' } ) {
);
}

function GlobalStylesStyleBook( { onClose } ) {
function GlobalStylesStyleBook() {
const navigator = useNavigator();
const { path } = navigator.location;
return (
Expand All @@ -257,7 +258,6 @@ function GlobalStylesStyleBook( { onClose } ) {
// Now go to the selected block.
navigator.goTo( '/blocks/' + encodeURIComponent( blockName ) );
} }
onClose={ onClose }
/>
);
}
Expand Down Expand Up @@ -296,9 +296,13 @@ function GlobalStylesBlockLink() {
}, [ selectedBlockClientId, selectedBlockName, blockHasGlobalStyles ] );
}

function GlobalStylesUI( { isStyleBookOpened, onCloseStyleBook } ) {
function GlobalStylesUI() {
const blocks = getBlockTypes();

const editorCanvasContainerView = useSelect(
( select ) =>
unlock( select( editSiteStore ) ).getEditorCanvasContainerView(),
[]
);
return (
<NavigatorProvider
className="edit-site-global-styles-sidebar__navigator-provider"
Expand Down Expand Up @@ -343,8 +347,8 @@ function GlobalStylesUI( { isStyleBookOpened, onCloseStyleBook } ) {
/>
);
} ) }
{ isStyleBookOpened && (
<GlobalStylesStyleBook onClose={ onCloseStyleBook } />
{ 'style-book' === editorCanvasContainerView && (
<GlobalStylesStyleBook />
) }

<GlobalStylesActionMenu />
Expand Down
19 changes: 14 additions & 5 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,8 @@ import UndoButton from './undo-redo/undo';
import RedoButton from './undo-redo/redo';
import DocumentActions from './document-actions';
import { store as editSiteStore } from '../../store';
import { useHasStyleBook } from '../style-book';
import { getEditorCanvasContainerTitle } from '../editor-canvas-container';
import { unlock } from '../../private-apis';

const preventDefault = ( event ) => {
event.preventDefault();
Expand All @@ -56,6 +57,7 @@ export default function HeaderEditMode() {
blockEditorMode,
homeUrl,
showIconLabels,
editorCanvasView,
} = useSelect( ( select ) => {
const {
__experimentalGetPreviewDeviceType,
Expand Down Expand Up @@ -88,6 +90,9 @@ export default function HeaderEditMode() {
'core/edit-site',
'showIconLabels'
),
editorCanvasView: unlock(
select( editSiteStore )
).getEditorCanvasContainerView(),
};
}, [] );

Expand Down Expand Up @@ -117,7 +122,7 @@ export default function HeaderEditMode() {
[ setIsListViewOpened, isListViewOpen ]
);

const hasStyleBook = useHasStyleBook();
const hasDefaultEditorCanvasView = ! editorCanvasView;

const isFocusMode = templateType === 'wp_template_part';

Expand All @@ -138,7 +143,7 @@ export default function HeaderEditMode() {
'show-icon-labels': showIconLabels,
} ) }
>
{ ! hasStyleBook && (
{ hasDefaultEditorCanvasView && (
<NavigableToolbar
className="edit-site-header-edit-mode__start"
aria-label={ __( 'Document tools' ) }
Expand Down Expand Up @@ -223,12 +228,16 @@ export default function HeaderEditMode() {
) }

<div className="edit-site-header-edit-mode__center">
{ hasStyleBook ? __( 'Style Book' ) : <DocumentActions /> }
{ ! hasDefaultEditorCanvasView ? (
getEditorCanvasContainerTitle( editorCanvasView )
) : (
<DocumentActions />
) }
</div>

<div className="edit-site-header-edit-mode__end">
<div className="edit-site-header-edit-mode__actions">
{ ! isFocusMode && ! hasStyleBook && (
{ ! isFocusMode && hasDefaultEditorCanvasView && (
<div
className={ classnames(
'edit-site-header-edit-mode__preview-options',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import { FlexItem, FlexBlock, Flex, Button } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { styles, seen } from '@wordpress/icons';
import { useSelect } from '@wordpress/data';

import { useState, useEffect } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -15,19 +14,29 @@ import DefaultSidebar from './default-sidebar';
import { GlobalStylesUI } from '../global-styles';
import { store as editSiteStore } from '../../store';
import { GlobalStylesMenuSlot } from '../global-styles/ui';
import { unlock } from '../../private-apis';

export default function GlobalStylesSidebar() {
const [ isStyleBookOpened, setIsStyleBookOpened ] = useState( false );
const editorMode = useSelect(
( select ) => select( editSiteStore ).getEditorMode(),
[]
const { editorMode, editorCanvasView } = useSelect( ( select ) => {
return {
editorMode: select( editSiteStore ).getEditorMode(),
editorCanvasView: unlock(
select( editSiteStore )
).getEditorCanvasContainerView(),
};
}, [] );
const { setEditorCanvasContainerView } = unlock(
useDispatch( editSiteStore )
);

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

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

return (
<DefaultSidebar
className="edit-site-global-styles-sidebar"
Expand All @@ -47,9 +56,11 @@ export default function GlobalStylesSidebar() {
label={ __( 'Style Book' ) }
isPressed={ isStyleBookOpened }
disabled={ editorMode !== 'visual' }
onClick={ () => {
setIsStyleBookOpened( ! isStyleBookOpened );
} }
onClick={ () =>
setEditorCanvasContainerView(
isStyleBookOpened ? undefined : 'style-book'
)
}
/>
</FlexItem>
<FlexItem>
Expand All @@ -58,10 +69,7 @@ export default function GlobalStylesSidebar() {
</Flex>
}
>
<GlobalStylesUI
isStyleBookOpened={ isStyleBookOpened }
onCloseStyleBook={ () => setIsStyleBookOpened( false ) }
/>
<GlobalStylesUI />
</DefaultSidebar>
);
}
Loading

0 comments on commit 13a2d09

Please sign in to comment.