-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Navigation: Add an edit menu button to the block controls
- Loading branch information
Showing
3 changed files
with
68 additions
and
55 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
packages/block-library/src/navigation/edit/edit-menu-toolbar-button.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
BlockControls, | ||
} from '@wordpress/block-editor'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { | ||
ToolbarButton, | ||
ToolbarGroup, | ||
} from '@wordpress/components'; | ||
import { store as interfaceStore } from '@wordpress/interface'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
const EditMenuToolbarButton = () => { | ||
// Blocks can be loaded into both post and site editors. | ||
// We use this to determine which editor we are in so that | ||
// we can determine which inspector controls to open. | ||
const { | ||
isPostEditor, | ||
} = useSelect( | ||
( select ) => { | ||
// eslint-disable-next-line @wordpress/data-no-store-string-literals | ||
const editorSelectors = select( 'core/editor' ); | ||
return { | ||
isPostEditor: !! editorSelectors.getEditedPostAttribute( 'type' ) | ||
}; | ||
} | ||
); | ||
const { enableComplementaryArea } = useDispatch( interfaceStore ); | ||
// eslint-disable-next-line @wordpress/data-no-store-string-literals | ||
const { openGeneralSidebar } = useDispatch( 'core/edit-post' ); | ||
const openBlockInspector = () => { | ||
if ( isPostEditor ) { | ||
openGeneralSidebar( 'edit-post/block' ); | ||
} else { | ||
enableComplementaryArea( 'core/edit-site', 'edit-site/block-inspector' ); | ||
} | ||
}; | ||
|
||
const isOffCanvasNavigationEditorEnabled = | ||
window?.__experimentalEnableOffCanvasNavigationEditor === true; | ||
|
||
if ( ! isOffCanvasNavigationEditorEnabled ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<BlockControls> | ||
<ToolbarGroup> | ||
<ToolbarButton | ||
className="components-toolbar__control" | ||
label={ __( 'Open list view' ) } | ||
onClick={ openBlockInspector } // TODO - focus the menu part | ||
> | ||
{ __( 'Edit menu' ) } | ||
</ToolbarButton> | ||
</ToolbarGroup> | ||
</BlockControls> | ||
); | ||
}; | ||
|
||
export default EditMenuToolbarButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters