Skip to content
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

SlotFill: several code cleanups #50098

Merged
merged 5 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 4 additions & 26 deletions packages/components/src/slot-fill/fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
/**
* WordPress dependencies
*/
import { createPortal, useLayoutEffect, useRef } from '@wordpress/element';
import { useContext, useLayoutEffect, useRef } from '@wordpress/element';

/**
* Internal dependencies
*/
import SlotFillContext from './context';
import useSlot from './use-slot';

function FillComponent( { name, children, registerFill, unregisterFill } ) {
export default function Fill( { name, children } ) {
const { registerFill, unregisterFill } = useContext( SlotFillContext );
const slot = useSlot( name );

const ref = useRef( {
Expand Down Expand Up @@ -51,28 +52,5 @@ function FillComponent( { name, children, registerFill, unregisterFill } ) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ name ] );

if ( ! slot || ! slot.node ) {
return null;
}

// If a function is passed as a child, provide it with the fillProps.
if ( typeof children === 'function' ) {
children = children( slot.props.fillProps );
}

return createPortal( children, slot.node );
return null;
}

const Fill = ( props ) => (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why we had this in the first place.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FillComponent used to be a class component, wrapped in a context consumer component, and then it was refactored to functional with hooks in #15541. But the PR kept the context consumer wrapper, it didn't migrate it to useContext.

<SlotFillContext.Consumer>
{ ( { registerFill, unregisterFill } ) => (
<FillComponent
{ ...props }
registerFill={ registerFill }
unregisterFill={ unregisterFill }
/>
) }
</SlotFillContext.Consumer>
);

export default Fill;
4 changes: 1 addition & 3 deletions packages/components/src/slot-fill/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import BubblesVirtuallyFill from './bubbles-virtually/fill';
import BubblesVirtuallySlot from './bubbles-virtually/slot';
import BubblesVirtuallySlotFillProvider from './bubbles-virtually/slot-fill-provider';
import SlotFillProvider from './provider';
import useSlot from './bubbles-virtually/use-slot';
export { default as useSlot } from './bubbles-virtually/use-slot';
export { default as useSlotFills } from './bubbles-virtually/use-slot-fills';

export function Fill( props ) {
Expand Down Expand Up @@ -65,5 +65,3 @@ export const createPrivateSlotFill = ( name ) => {

return { privateKey, ...privateSlotFill };
};

export { useSlot };
6 changes: 0 additions & 6 deletions packages/components/src/slot-fill/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export default class SlotFillProvider extends Component {
this.unregisterFill = this.unregisterFill.bind( this );
this.getSlot = this.getSlot.bind( this );
this.getFills = this.getFills.bind( this );
this.hasFills = this.hasFills.bind( this );
this.subscribe = this.subscribe.bind( this );

this.slots = {};
Expand All @@ -32,7 +31,6 @@ export default class SlotFillProvider extends Component {
unregisterFill: this.unregisterFill,
getSlot: this.getSlot,
getFills: this.getFills,
hasFills: this.hasFills,
subscribe: this.subscribe,
};
}
Expand Down Expand Up @@ -91,10 +89,6 @@ export default class SlotFillProvider extends Component {
return this.fills[ name ];
}

hasFills( name ) {
return this.fills[ name ] && !! this.fills[ name ].length;
}

forceUpdateSlot( name ) {
const slot = this.getSlot( name );

Expand Down
5 changes: 0 additions & 5 deletions packages/components/src/slot-fill/slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class SlotComponent extends Component {
super( ...arguments );

this.isUnmounted = false;
this.bindNode = this.bindNode.bind( this );
}

componentDidMount() {
Expand All @@ -53,10 +52,6 @@ class SlotComponent extends Component {
}
}

bindNode( node ) {
this.node = node;
}

forceUpdate() {
if ( this.isUnmounted ) {
return;
Expand Down