-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathuse-slot.ts
40 lines (36 loc) · 1017 Bytes
/
use-slot.ts
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
40
/**
* WordPress dependencies
*/
import { useMemo, useContext } from '@wordpress/element';
/**
* Internal dependencies
*/
import SlotFillContext from './slot-fill-context';
import type {
SlotFillBubblesVirtuallyFillRef,
SlotFillBubblesVirtuallySlotRef,
FillProps,
SlotKey,
} from '../types';
import { useObservableValue } from './observable-map';
export default function useSlot( name: SlotKey ) {
const registry = useContext( SlotFillContext );
const slot = useObservableValue( registry.slots, name );
const api = useMemo(
() => ( {
updateSlot: ( fillProps: FillProps ) =>
registry.updateSlot( name, fillProps ),
unregisterSlot: ( ref: SlotFillBubblesVirtuallySlotRef ) =>
registry.unregisterSlot( name, ref ),
registerFill: ( ref: SlotFillBubblesVirtuallyFillRef ) =>
registry.registerFill( name, ref ),
unregisterFill: ( ref: SlotFillBubblesVirtuallyFillRef ) =>
registry.unregisterFill( name, ref ),
} ),
[ name, registry ]
);
return {
...slot,
...api,
};
}