-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
fill.js
39 lines (32 loc) · 976 Bytes
/
fill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* WordPress dependencies
*/
import { useRef, useState, useEffect, createPortal } from '@wordpress/element';
/**
* Internal dependencies
*/
import useSlot from './use-slot';
function useForceUpdate() {
const [ , setState ] = useState( {} );
return () => setState( {} );
}
export default function Fill( { name, children } ) {
const slot = useSlot( name );
const ref = useRef( { rerender: useForceUpdate() } );
useEffect( () => {
// We register fills so we can keep track of their existance.
// Some Slot implementations need to know if there're already fills
// registered so they can choose to render themselves or not.
slot.registerFill( ref );
return () => {
slot.unregisterFill( ref );
};
}, [ slot.registerFill, slot.unregisterFill ] );
if ( ! slot.ref || ! slot.ref.current ) {
return null;
}
if ( typeof children === 'function' ) {
children = children( slot.fillProps );
}
return createPortal( children, slot.ref.current );
}