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

Experiment: Try block inspector controls toggle in block toolbar #65118

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
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
75 changes: 75 additions & 0 deletions packages/editor/src/hooks/block-toolbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { __unstableBlockToolbarLastItem as BlockToolbarLastItem } from '@wordpress/block-editor';
import { ToolbarGroup, ToolbarButton } from '@wordpress/components';
import { settings as settingsIcon } from '@wordpress/icons';
import { store as interfaceStore } from '@wordpress/interface';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

/**
* Override the default edit UI to include a new block inspector control for
* assigning a partial syncing controls to supported blocks in the pattern editor.
* Currently, only the `core/paragraph` block is supported.
*
* @param {Component} BlockEdit Original component.
*
* @return {Component} Wrapped component.
*/
const withBlockToolbar = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
return (
<>
<BlockEdit key="edit" { ...props } />
{ props.isSelected && <BlockSidebarToggle /> }
</>
);
Comment on lines +24 to +29
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If this ever went to production we'd always need to be careful about hooking into block edit because it can cause performance degradation if not done carefully with the right conditions. In this case the toolbar item will only render for the currently selected block. We may need other conditions.

},
'withBlockToolbar'
);

const BlockSidebarToggle = () => {
const { enableComplementaryArea, disableComplementaryArea } =
useDispatch( interfaceStore );

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

return (
<BlockToolbarLastItem>
<ToolbarGroup>
<ToolbarButton
icon={ settingsIcon }
label={ __( 'Toggle block settings' ) }
isPressed={ isSelected }
onClick={ () => {
if ( isSelected ) {
disableComplementaryArea(
'core',
'edit-post/block'
);
} else {
enableComplementaryArea(
'core',
'edit-post/block'
);
}
} }
/>
</ToolbarGroup>
</BlockToolbarLastItem>
);
};

addFilter(
'editor.BlockEdit',
'core/editor/with-block-toolbar',
withBlockToolbar
);
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-toolbar';
Loading