-
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.
Blocks: Try new context API to simplify display of block controls (#5029
) * Blocks: Refactor block controls to use block edit context * Docs: Update docs to reflect the change in how block controls are handled * Blocks: Add isSelected handling to InspectorAdvancedControls * Blocks: Replace getWrapperDisplayName with its new alternative * Blocks: Use createSlotFill for BlockControls * Blocks: Introduce BlockFormatControls component * Blocks: Make sure that RichText controls are only displayed when block is selected This makes it backward compatible with the existing core blocks * Blocks: Update test helper to work with block edit context * Blocks: Make sure that EditContextProvider is only rerendered when isSelected changes * Blocks: Merge Provider with BlockEdit * Edit Post: Add information for developers about `isSelected` changes introduced * Blocks: Improve state handling for BlockEdit context * Blocks: Addressed feedback related to BlockEditContext changes
- Loading branch information
Showing
19 changed files
with
247 additions
and
117 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Toolbar, Fill } from '@wordpress/components'; | ||
|
||
export default function BlockControls( { controls, children } ) { | ||
return ( | ||
<Fill name="Block.Toolbar"> | ||
<Toolbar controls={ controls } /> | ||
{ children } | ||
</Fill> | ||
); | ||
} | ||
import { createSlotFill, Toolbar } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { ifBlockEditSelected } from '../block-edit/context'; | ||
|
||
const Fill = createSlotFill( 'BlockControls' ); | ||
const { Slot } = Fill; | ||
|
||
const BlockControlsFill = ( { controls, children } ) => ( | ||
<Fill> | ||
<Toolbar controls={ controls } /> | ||
{ children } | ||
</Fill> | ||
); | ||
|
||
const BlockControls = ifBlockEditSelected( BlockControlsFill ); | ||
|
||
BlockControls.Slot = Slot; | ||
|
||
export default BlockControls; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { createContext, createHigherOrderComponent } from '@wordpress/element'; | ||
|
||
const { Consumer, Provider } = createContext( { | ||
isSelected: true, | ||
} ); | ||
|
||
export { Provider as BlockEditContextProvider }; | ||
|
||
/** | ||
* A Higher Order Component used to inject BlockEdit context to the | ||
* wrapped component. | ||
* | ||
* @param {Component} OriginalComponent Component to wrap. | ||
* | ||
* @return {Component} Component which has BlockEdit context injected. | ||
*/ | ||
export const withBlockEditContext = createHigherOrderComponent( ( OriginalComponent ) => { | ||
return ( props ) => ( | ||
<Consumer> | ||
{ ( context ) => ( | ||
<OriginalComponent | ||
{ ...props } | ||
blockEditContext={ context } | ||
/> | ||
) } | ||
</Consumer> | ||
); | ||
}, 'withBlockEditContext' ); | ||
|
||
/** | ||
* A Higher Order Component used to render conditionally the wrapped | ||
* component only when the BlockEdit has selected state set. | ||
* | ||
* @param {Component} OriginalComponent Component to wrap. | ||
* | ||
* @return {Component} Component which renders only when the BlockEdit is selected. | ||
*/ | ||
export const ifBlockEditSelected = createHigherOrderComponent( ( OriginalComponent ) => { | ||
return ( props ) => ( | ||
<Consumer> | ||
{ ( { isSelected } ) => isSelected && ( | ||
<OriginalComponent { ...props } /> | ||
) } | ||
</Consumer> | ||
); | ||
}, 'ifBlockEditSelected' ); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createSlotFill } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { ifBlockEditSelected } from '../block-edit/context'; | ||
|
||
const Fill = createSlotFill( 'BlockFormatControls' ); | ||
const { Slot } = Fill; | ||
|
||
const BlockFormatControls = ifBlockEditSelected( Fill ); | ||
|
||
BlockFormatControls.Slot = Slot; | ||
|
||
export default BlockFormatControls; |
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
Oops, something went wrong.