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

Add block sidebar toggle to block settings menu #65185

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
62 changes: 62 additions & 0 deletions packages/editor/src/hooks/block-settings-inspector-toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { __unstableBlockSettingsMenuFirstItem as BlockSettingsMenuFirstItem } from '@wordpress/block-editor';
import { MenuItem } from '@wordpress/components';
import { store as interfaceStore } from '@wordpress/interface';
import { useDispatch, useSelect } from '@wordpress/data';
import { __, isRTL } from '@wordpress/i18n';
import { drawerLeft, drawerRight } from '@wordpress/icons';

/**
* Override the default edit UI to include a new block options menu item
* to toggle the block's inspector controls open.
*
* @param {Component} BlockEdit Original component.
*
* @return {Component} Wrapped component.
*/
const withBlockSettingsInspectorToggle = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
return (
<>
<BlockEdit key="edit" { ...props } />
{ props.isSelected && <BlockInspectorToggle /> }
</>
);
},
'withBlockSettingsInspectorToggle'
);

const BlockInspectorToggle = () => {
const { enableComplementaryArea } = useDispatch( interfaceStore );

const isOpen = useSelect(
( select ) =>
select( interfaceStore ).getActiveComplementaryArea( 'core' ) ===
'edit-post/block',
[]
);

return (
<BlockSettingsMenuFirstItem>
<MenuItem
onClick={ () => {
enableComplementaryArea( 'core', 'edit-post/block' );
} }
icon={ isRTL() ? drawerLeft : drawerRight }
aria-disabled={ isOpen }
>
{ __( 'Block settings' ) }
</MenuItem>
</BlockSettingsMenuFirstItem>
);
};

addFilter(
'editor.BlockEdit',
'core/editor/with-block-settings-inspector-toggle',
withBlockSettingsInspectorToggle
);
1 change: 1 addition & 0 deletions packages/editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ import './custom-sources-backwards-compatibility';
import './default-autocompleters';
import './media-upload';
import './pattern-overrides';
import './block-settings-inspector-toggle';
Loading