-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Blocks: Try new context API to simplify display of block controls #5029
Changes from all commits
966dab4
b89bd58
91c7185
9e37c97
39b2561
458b6f3
4e4404a
c650c18
8c95bb0
33d8a56
f47a184
fbb66c8
cf63e10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; |
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( { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel this context could also include what's current in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we can consolidate it in this PR using new Context API unless it breaks more tests ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 ) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this 👍 |
||
return ( props ) => ( | ||
<Consumer> | ||
{ ( { isSelected } ) => isSelected && ( | ||
<OriginalComponent { ...props } /> | ||
) } | ||
</Consumer> | ||
); | ||
}, 'ifBlockEditSelected' ); |
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have an improvement for this one in another PR:
https://github.com/WordPress/gutenberg/pull/6086/files#diff-f55821c6d9b2038d9e462f87c1c9e0feR6
I will extract and update this PR if it is going to be ready to merge sooner.