Skip to content

Commit

Permalink
Make the inserter in the site editor behave as a popover (#27502)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Dec 7, 2020
1 parent 85173ee commit 5c6f4d7
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 129 deletions.
213 changes: 102 additions & 111 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import KeyboardShortcuts from '../keyboard-shortcuts';
import GlobalStylesProvider from './global-styles-provider';
import NavigationSidebar from '../navigation-sidebar';
import URLQueryController from '../url-query-controller';
import PopoverWrapper from './popover-wrapper';

const interfaceLabels = {
secondarySidebar: __( 'Block Library' ),
Expand Down Expand Up @@ -97,13 +98,14 @@ function Editor() {
settings: getSettings(),
templateType: _templateType,
page: getPage(),
template: _templateType
? select( 'core' ).getEntityRecord(
'postType',
_templateType,
_entityId
)
: null,
template:
_templateType && _entityId
? select( 'core' ).getEntityRecord(
'postType',
_templateType,
_entityId
)
: null,
entityId: _entityId,
isNavigationOpen: isNavigationOpened(),
};
Expand Down Expand Up @@ -182,46 +184,39 @@ function Editor() {
<EntityProvider kind="root" type="site">
<EntityProvider
kind="postType"
type={ 'wp_template' }
id={
templateType === 'wp_template' ? entityId : null
}
type={ templateType }
id={ entityId }
>
<EntityProvider
kind="postType"
type="wp_template_part"
type="wp_global_styles"
id={
templateType === 'wp_template_part'
? entityId
: null
settings.__experimentalGlobalStylesUserEntityId
}
>
<EntityProvider
kind="postType"
type="wp_global_styles"
id={
settings.__experimentalGlobalStylesUserEntityId
}
>
<BlockContextProvider
value={ blockContext }
>
<FocusReturnProvider>
<GlobalStylesProvider
baseStyles={
settings.__experimentalGlobalStylesBaseStyles
}
>
<KeyboardShortcuts.Register />
<SidebarComplementaryAreaFills />
<InterfaceSkeleton
ref={ ref }
labels={ interfaceLabels }
drawer={
<NavigationSidebar />
}
secondarySidebar={
isInserterOpen ? (
<BlockContextProvider value={ blockContext }>
<FocusReturnProvider>
<GlobalStylesProvider
baseStyles={
settings.__experimentalGlobalStylesBaseStyles
}
>
<KeyboardShortcuts.Register />
<SidebarComplementaryAreaFills />
<InterfaceSkeleton
ref={ ref }
labels={ interfaceLabels }
drawer={ <NavigationSidebar /> }
secondarySidebar={
isInserterOpen ? (
<PopoverWrapper
className="edit-site-editor__inserter-panel-popover-wrapper"
onClose={ () =>
setIsInserterOpened(
false
)
}
>
<div className="edit-site-editor__inserter-panel">
<div className="edit-site-editor__inserter-panel-header">
<Button
Expand Down Expand Up @@ -250,79 +245,75 @@ function Editor() {
/>
</div>
</div>
) : null
}
sidebar={
sidebarIsOpened && (
<ComplementaryArea.Slot scope="core/edit-site" />
)
}
header={
<Header
openEntitiesSavedStates={
openEntitiesSavedStates
</PopoverWrapper>
) : null
}
sidebar={
sidebarIsOpened && (
<ComplementaryArea.Slot scope="core/edit-site" />
)
}
header={
<Header
openEntitiesSavedStates={
openEntitiesSavedStates
}
/>
}
content={
<div
className="edit-site-visual-editor"
style={ inlineStyles }
>
<Notices />
<Popover.Slot name="block-toolbar" />
{ template && (
<BlockEditor
setIsInserterOpen={
setIsInserterOpened
}
/>
) }
<KeyboardShortcuts />
</div>
}
actions={
<>
<EntitiesSavedStates
isOpen={
isEntitiesSavedStatesOpen
}
/>
}
content={
<div
className="edit-site-visual-editor"
style={
inlineStyles
close={
closeEntitiesSavedStates
}
>
<Notices />
<Popover.Slot name="block-toolbar" />
{ template && (
<BlockEditor
setIsInserterOpen={
setIsInserterOpened
/>
{ ! isEntitiesSavedStatesOpen && (
<div className="edit-site-editor__toggle-save-panel">
<Button
isSecondary
className="edit-site-editor__toggle-save-panel-button"
onClick={
openEntitiesSavedStates
}
/>
) }
<KeyboardShortcuts />
</div>
}
actions={
<>
<EntitiesSavedStates
isOpen={
isEntitiesSavedStatesOpen
}
close={
closeEntitiesSavedStates
}
/>
{ ! isEntitiesSavedStatesOpen && (
<div className="edit-site-editor__toggle-save-panel">
<Button
isSecondary
className="edit-site-editor__toggle-save-panel-button"
onClick={
openEntitiesSavedStates
}
aria-expanded={
false
}
>
{ __(
'Open save panel'
) }
</Button>
</div>
) }
</>
}
footer={
<BlockBreadcrumb />
}
/>
<Popover.Slot />
<PluginArea />
</GlobalStylesProvider>
</FocusReturnProvider>
</BlockContextProvider>
</EntityProvider>
aria-expanded={
false
}
>
{ __(
'Open save panel'
) }
</Button>
</div>
) }
</>
}
footer={ <BlockBreadcrumb /> }
/>
<Popover.Slot />
<PluginArea />
</GlobalStylesProvider>
</FocusReturnProvider>
</BlockContextProvider>
</EntityProvider>
</EntityProvider>
</EntityProvider>
Expand Down
57 changes: 57 additions & 0 deletions packages/edit-site/src/components/editor/popover-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* WordPress dependencies
*/
import {
withConstrainedTabbing,
withFocusReturn,
withFocusOutside,
} from '@wordpress/components';
import { Component } from '@wordpress/element';
import { ESCAPE } from '@wordpress/keycodes';

function stopPropagation( event ) {
event.stopPropagation();
}

const DetectOutside = withFocusOutside(
class extends Component {
handleFocusOutside( event ) {
this.props.onFocusOutside( event );
}

render() {
return this.props.children;
}
}
);

const FocusManaged = withConstrainedTabbing(
withFocusReturn( ( { children } ) => children )
);

export default function PopoverWrapper( { onClose, children, className } ) {
// Event handlers
const maybeClose = ( event ) => {
// Close on escape
if ( event.keyCode === ESCAPE && onClose ) {
event.stopPropagation();
onClose();
}
};

// Disable reason: this stops certain events from propagating outside of the component.
// - onMouseDown is disabled as this can cause interactions with other DOM elements
/* eslint-disable jsx-a11y/no-static-element-interactions */
return (
<div
className={ className }
onKeyDown={ maybeClose }
onMouseDown={ stopPropagation }
>
<DetectOutside onFocusOutside={ onClose }>
<FocusManaged>{ children }</FocusManaged>
</DetectOutside>
</div>
);
/* eslint-enable jsx-a11y/no-static-element-interactions */
}
11 changes: 11 additions & 0 deletions packages/edit-site/src/components/editor/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
background-color: $white;
}

// Ideally we don't need all these nested divs.
// Removing these requires a refactoring of the different a11y HoCs.
.edit-site-editor__inserter-panel-popover-wrapper {
&,
& > div,
& > div > div,
& > div > div > div {
height: 100%;
}
}

.edit-site-editor__inserter-panel {
height: 100%;
display: flex;
Expand Down
26 changes: 9 additions & 17 deletions packages/edit-site/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,21 @@ export default function Header( { openEntitiesSavedStates } ) {
'core/editor'
);

const _templateType = getTemplateType();
const _template = getEntityRecord(
'postType',
'wp_template',
getTemplateId()
);
const _templatePart = getEntityRecord(
'postType',
'wp_template_part',
getTemplatePartId()
);

const postType = getTemplateType();
const postId =
postType === 'wp_template' ? getTemplateId() : getTemplatePartId();
const record = getEntityRecord( 'postType', postType, postId );
const _entityTitle =
'wp_template' === _templateType
? getTemplateInfo( _template ).title
: _templatePart?.slug;
'wp_template' === postType
? getTemplateInfo( record ).title
: record?.slug;

return {
deviceType: __experimentalGetPreviewDeviceType(),
entityTitle: _entityTitle,
hasFixedToolbar: isFeatureActive( 'fixedToolbar' ),
template: _template,
templateType: _templateType,
template: record,
templateType: postType,
isInserterOpen: isInserterOpened(),
};
}, [] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ const NavigationPanel = ( { isOpen } ) => {
// from a separate component (such as document actions in the header).
const panelRef = useRef();
useEffect( () => {
panelRef.current.focus();
if ( isOpen ) {
panelRef.current.focus();
}
}, [ templatesActiveMenu ] );

return (
Expand Down

0 comments on commit 5c6f4d7

Please sign in to comment.