-
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.
Add: Block areas tabbed sidebar to the widgets screen (#22467)
- Loading branch information
1 parent
e2c7bb2
commit cfbce35
Showing
9 changed files
with
358 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
81 changes: 81 additions & 0 deletions
81
packages/edit-widgets/src/components/sidebar/block-areas.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,81 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { blockDefault } from '@wordpress/icons'; | ||
import { BlockIcon } from '@wordpress/block-editor'; | ||
import { Button } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
function BlockArea( { clientId } ) { | ||
const { name, selectedBlock } = useSelect( | ||
( select ) => { | ||
const { getBlockAttributes, getBlockSelectionStart } = select( | ||
'core/block-editor' | ||
); | ||
return { | ||
name: getBlockAttributes( clientId ).name, | ||
selectedBlock: getBlockSelectionStart(), | ||
}; | ||
}, | ||
[ clientId ] | ||
); | ||
const { selectBlock } = useDispatch( 'core/block-editor' ); | ||
const isSelected = selectedBlock === clientId; | ||
return ( | ||
<li> | ||
<Button | ||
aria-expanded={ isSelected } | ||
onClick={ | ||
isSelected | ||
? undefined | ||
: () => { | ||
selectBlock( clientId ); | ||
} | ||
} | ||
> | ||
{ name } | ||
<span className="edit-widgets-block-areas__edit"> | ||
{ __( 'Edit' ) } | ||
</span> | ||
</Button> | ||
</li> | ||
); | ||
} | ||
|
||
export default function BlockAreas() { | ||
const blockOrder = useSelect( ( select ) => { | ||
return select( 'core/block-editor' ).getBlockOrder(); | ||
} ); | ||
const hasBlockAreas = blockOrder.length > 0; | ||
return ( | ||
<> | ||
<div className="edit-widgets-block-areas"> | ||
<div className="edit-widgets-block-areas__top-container"> | ||
<BlockIcon icon={ blockDefault } /> | ||
<div> | ||
<p> | ||
{ __( | ||
'Block Areas (also known as "Widget Areas") are global parts in your site\'s layout that can accept blocks. These vary by theme, but are typically parts like your Sidebar or Footer.' | ||
) } | ||
</p> | ||
{ ! hasBlockAreas && ( | ||
<p> | ||
{ __( | ||
'Your theme does not contain block areas.' | ||
) } | ||
</p> | ||
) } | ||
</div> | ||
</div> | ||
{ hasBlockAreas && ( | ||
<ul> | ||
{ blockOrder.map( ( clientId ) => ( | ||
<BlockArea key={ clientId } clientId={ clientId } /> | ||
) ) } | ||
</ul> | ||
) } | ||
</div> | ||
</> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,22 +1,141 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { map } from 'lodash'; | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useEffect } from '@wordpress/element'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { ComplementaryArea } from '@wordpress/interface'; | ||
import { BlockInspector } from '@wordpress/block-editor'; | ||
import { cog } from '@wordpress/icons'; | ||
import { Button } from '@wordpress/components'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BlockAreas from './block-areas'; | ||
|
||
const CORE_WIDGET_COMPLEMENTARY_AREAS = { | ||
'edit-widgets/block-areas': __( 'Block areas' ), | ||
'edit-widgets/block-inspector': __( 'Block' ), | ||
}; | ||
|
||
function ComplementaryAreaHeader( { activeComplementaryArea } ) { | ||
const { enableComplementaryArea } = useDispatch( 'core/interface' ); | ||
return ( | ||
<ul> | ||
{ map( CORE_WIDGET_COMPLEMENTARY_AREAS, ( label, identifier ) => { | ||
const isActive = identifier === activeComplementaryArea; | ||
return ( | ||
<li key={ identifier }> | ||
<Button | ||
onClick={ () => | ||
enableComplementaryArea( | ||
'core/edit-widgets', | ||
identifier | ||
) | ||
} | ||
className={ classnames( | ||
'edit-widgets-sidebar__panel-tab', | ||
{ | ||
'is-active': isActive, | ||
} | ||
) } | ||
aria-label={ | ||
isActive | ||
? // translators: %s: sidebar label e.g: "Block areas". | ||
sprintf( __( '%s (selected)' ), label ) | ||
: label | ||
} | ||
data-label={ label } | ||
> | ||
{ label } | ||
</Button> | ||
</li> | ||
); | ||
} ) } | ||
</ul> | ||
); | ||
} | ||
|
||
export default function Sidebar() { | ||
const { enableComplementaryArea } = useDispatch( 'core/interface' ); | ||
const { | ||
currentArea, | ||
hasSelectedNonAreaBlock, | ||
isGeneralSidebarOpen, | ||
} = useSelect( ( select ) => { | ||
let activeArea = select( 'core/interface' ).getActiveComplementaryArea( | ||
'core/edit-widgets' | ||
); | ||
const isSidebarOpen = !! activeArea; | ||
const { getBlockSelectionStart, getBlockRootClientId } = select( | ||
'core/block-editor' | ||
); | ||
const selectionStart = getBlockSelectionStart(); | ||
if ( ! CORE_WIDGET_COMPLEMENTARY_AREAS[ activeArea ] ) { | ||
if ( ! selectionStart ) { | ||
activeArea = 'edit-widgets/block-areas'; | ||
} else { | ||
activeArea = 'edit-widgets/block-inspector'; | ||
} | ||
} | ||
return { | ||
currentArea: activeArea, | ||
hasSelectedNonAreaBlock: !! ( | ||
selectionStart && getBlockRootClientId( selectionStart ) | ||
), | ||
isGeneralSidebarOpen: isSidebarOpen, | ||
}; | ||
}, [] ); | ||
|
||
// currentArea, and isGeneralSidebarOpen are intentionally left out from the dependencies, | ||
// because we want to run the effect when a block is selected/unselected and not when the sidebar state changes. | ||
useEffect( () => { | ||
if ( | ||
hasSelectedNonAreaBlock && | ||
currentArea === 'edit-widgets/block-areas' && | ||
isGeneralSidebarOpen | ||
) { | ||
enableComplementaryArea( | ||
'core/edit-widgets', | ||
'edit-widgets/block-inspector' | ||
); | ||
} | ||
if ( | ||
! hasSelectedNonAreaBlock && | ||
currentArea === 'edit-widgets/block-inspector' && | ||
isGeneralSidebarOpen | ||
) { | ||
enableComplementaryArea( | ||
'core/edit-widgets', | ||
'edit-widgets/block-areas' | ||
); | ||
} | ||
}, [ hasSelectedNonAreaBlock, enableComplementaryArea ] ); | ||
|
||
return ( | ||
<ComplementaryArea | ||
className="edit-widgets-sidebar" | ||
smallScreenTitle={ __( 'Widget Areas' ) } | ||
header={ | ||
<ComplementaryAreaHeader | ||
activeComplementaryArea={ currentArea } | ||
/> | ||
} | ||
headerClassName="edit-widgets-sidebar__panel-tabs" | ||
scope="core/edit-widgets" | ||
complementaryAreaIdentifier="edit-widgets/block-inspector" | ||
title={ __( 'Block' ) } | ||
identifier={ currentArea } | ||
icon={ cog } | ||
> | ||
<BlockInspector showNoBlockSelectedMessage={ false } /> | ||
{ currentArea === 'edit-widgets/block-areas' && <BlockAreas /> } | ||
{ currentArea === 'edit-widgets/block-inspector' && ( | ||
<BlockInspector /> | ||
) } | ||
</ComplementaryArea> | ||
); | ||
} |
Oops, something went wrong.