Skip to content

Commit b4887bf

Browse files
authored
SlotFill: several code cleanups (#50098)
* Fill: simplify component by using useContext * SlotFillProvider: remove unused and unexposed hasFills method * Slot: remove unused bindNode method * Fill: remove unused portal code (moved to bubblesVirtually version) * Improve export of useSlot
1 parent 18e3283 commit b4887bf

File tree

4 files changed

+5
-40
lines changed

4 files changed

+5
-40
lines changed

packages/components/src/slot-fill/fill.js

+4-26
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
/**
44
* WordPress dependencies
55
*/
6-
import { createPortal, useLayoutEffect, useRef } from '@wordpress/element';
6+
import { useContext, useLayoutEffect, useRef } from '@wordpress/element';
77

88
/**
99
* Internal dependencies
1010
*/
1111
import SlotFillContext from './context';
1212
import useSlot from './use-slot';
1313

14-
function FillComponent( { name, children, registerFill, unregisterFill } ) {
14+
export default function Fill( { name, children } ) {
15+
const { registerFill, unregisterFill } = useContext( SlotFillContext );
1516
const slot = useSlot( name );
1617

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

54-
if ( ! slot || ! slot.node ) {
55-
return null;
56-
}
57-
58-
// If a function is passed as a child, provide it with the fillProps.
59-
if ( typeof children === 'function' ) {
60-
children = children( slot.props.fillProps );
61-
}
62-
63-
return createPortal( children, slot.node );
55+
return null;
6456
}
65-
66-
const Fill = ( props ) => (
67-
<SlotFillContext.Consumer>
68-
{ ( { registerFill, unregisterFill } ) => (
69-
<FillComponent
70-
{ ...props }
71-
registerFill={ registerFill }
72-
unregisterFill={ unregisterFill }
73-
/>
74-
) }
75-
</SlotFillContext.Consumer>
76-
);
77-
78-
export default Fill;

packages/components/src/slot-fill/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import BubblesVirtuallyFill from './bubbles-virtually/fill';
1313
import BubblesVirtuallySlot from './bubbles-virtually/slot';
1414
import BubblesVirtuallySlotFillProvider from './bubbles-virtually/slot-fill-provider';
1515
import SlotFillProvider from './provider';
16-
import useSlot from './bubbles-virtually/use-slot';
16+
export { default as useSlot } from './bubbles-virtually/use-slot';
1717
export { default as useSlotFills } from './bubbles-virtually/use-slot-fills';
1818

1919
export function Fill( props ) {
@@ -65,5 +65,3 @@ export const createPrivateSlotFill = ( name ) => {
6565

6666
return { privateKey, ...privateSlotFill };
6767
};
68-
69-
export { useSlot };

packages/components/src/slot-fill/provider.js

-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export default class SlotFillProvider extends Component {
1919
this.unregisterFill = this.unregisterFill.bind( this );
2020
this.getSlot = this.getSlot.bind( this );
2121
this.getFills = this.getFills.bind( this );
22-
this.hasFills = this.hasFills.bind( this );
2322
this.subscribe = this.subscribe.bind( this );
2423

2524
this.slots = {};
@@ -32,7 +31,6 @@ export default class SlotFillProvider extends Component {
3231
unregisterFill: this.unregisterFill,
3332
getSlot: this.getSlot,
3433
getFills: this.getFills,
35-
hasFills: this.hasFills,
3634
subscribe: this.subscribe,
3735
};
3836
}
@@ -91,10 +89,6 @@ export default class SlotFillProvider extends Component {
9189
return this.fills[ name ];
9290
}
9391

94-
hasFills( name ) {
95-
return this.fills[ name ] && !! this.fills[ name ].length;
96-
}
97-
9892
forceUpdateSlot( name ) {
9993
const slot = this.getSlot( name );
10094

packages/components/src/slot-fill/slot.js

-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class SlotComponent extends Component {
2929
super( ...arguments );
3030

3131
this.isUnmounted = false;
32-
this.bindNode = this.bindNode.bind( this );
3332
}
3433

3534
componentDidMount() {
@@ -53,10 +52,6 @@ class SlotComponent extends Component {
5352
}
5453
}
5554

56-
bindNode( node ) {
57-
this.node = node;
58-
}
59-
6055
forceUpdate() {
6156
if ( this.isUnmounted ) {
6257
return;

0 commit comments

Comments
 (0)