-
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
Slot/Fill: Add a constrained version #14408
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
packages/block-editor/src/components/inspector-controls/index.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 |
---|---|---|
@@ -1,17 +1,17 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createSlotFill } from '@wordpress/components'; | ||
import { createConstrainedSlotFill } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { ifBlockEditSelected } from '../block-edit/context'; | ||
|
||
const { Fill, Slot } = createSlotFill( 'InspectorControls' ); | ||
const { Fill, Slot, Provider } = createConstrainedSlotFill(); | ||
|
||
const InspectorControls = ifBlockEditSelected( Fill ); | ||
|
||
InspectorControls.Slot = Slot; | ||
InspectorControls.Provider = Provider; | ||
|
||
export default InspectorControls; |
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,106 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { | ||
filter, | ||
isFunction, | ||
isString, | ||
negate, | ||
findIndex, | ||
} from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
createContext, | ||
useReducer, | ||
useContext, | ||
useEffect, | ||
cloneElement, | ||
Children, | ||
isEmptyElement, | ||
Fragment, | ||
} from '@wordpress/element'; | ||
import { withInstanceId } from '@wordpress/compose'; | ||
|
||
export function createConstrainedSlotFill() { | ||
const Context = createContext( [ [], () => {} ] ); | ||
|
||
const initialFills = []; | ||
function reducer( state, action ) { | ||
switch ( action.type ) { | ||
case 'remove': | ||
return filter( state, ( fill ) => fill.key !== action.key ); | ||
case 'add': { | ||
const index = findIndex( state, ( fill ) => fill.key === action.key ); | ||
if ( index === -1 ) { | ||
return [ | ||
...state, | ||
{ key: action.key, children: action.children }, | ||
]; | ||
} | ||
return [ | ||
...state.slice( 0, index ), | ||
{ key: action.key, children: action.children }, | ||
...state.slice( index + 1 ), | ||
]; | ||
} | ||
default: | ||
throw new Error(); | ||
} | ||
} | ||
|
||
function Provider( { children } ) { | ||
const context = useReducer( reducer, initialFills ); | ||
|
||
return ( | ||
<Context.Provider value={ context }> | ||
{ children } | ||
</Context.Provider> | ||
); | ||
} | ||
|
||
function Slot( { children, fillProps = {} } ) { | ||
const [ fills ] = useContext( Context ); | ||
|
||
const normalizedFills = fills.map( ( fill ) => { | ||
const { children: fillChildren, key } = fill; | ||
const element = isFunction( fillChildren ) ? fillChildren( fillProps ) : fillChildren; | ||
return Children.map( element, ( child, childIndex ) => { | ||
if ( ! child || isString( child ) ) { | ||
return child; | ||
} | ||
|
||
const childKey = `${ key }---${ child.key || childIndex }`; | ||
return cloneElement( child, { key: childKey } ); | ||
} ); | ||
} ).filter( | ||
// In some cases fills are rendered only when some conditions apply. | ||
// This ensures that we only use non-empty fills when rendering, i.e., | ||
// it allows us to render wrappers only when the fills are actually present. | ||
negate( isEmptyElement ) | ||
); | ||
|
||
return ( | ||
<Fragment> | ||
{ isFunction( children ) ? children( normalizedFills ) : normalizedFills } | ||
</Fragment> | ||
); | ||
} | ||
|
||
const Fill = withInstanceId( ( { children, instanceId } ) => { | ||
const [ , dispatch ] = useContext( Context ); | ||
useEffect( () => { | ||
dispatch( { type: 'add', key: instanceId, children } ); | ||
return () => dispatch( { type: 'remove', key: instanceId } ); | ||
}, [ instanceId, children ] ); | ||
return null; | ||
} ); | ||
|
||
return { | ||
Provider, | ||
Slot, | ||
Fill, | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
While I realize it's not needed (because it's "guaranteed" to be static), convention seems to be to include
dispatch
as a dependency here. Should we included it along with[ instanceId, children ]
?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.
right 👍 Seems like a good thing to add.