-
Notifications
You must be signed in to change notification settings - Fork 4k
/
provider.tsx
131 lines (112 loc) · 3.1 KB
/
provider.tsx
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* WordPress dependencies
*/
import type { Component } from '@wordpress/element';
/**
* Internal dependencies
*/
import SlotFillContext from './context';
import type {
BaseFillObject,
BaseSlotFillContext,
SlotComponentProps,
} from './types';
import { useState } from '@wordpress/element';
/**
* External dependencies
*/
import type { ReactNode } from 'react';
export function createSlotRegistory(): BaseSlotFillContext {
const slots: Record< string, Component< SlotComponentProps > > = {};
const fills: Record< string, BaseFillObject[] > = {};
let listeners: Array< () => void > = [];
function registerSlot(
name: string,
slot: Component< SlotComponentProps >
) {
const previousSlot = slots[ name ];
slots[ name ] = slot;
triggerListeners();
// Sometimes the fills are registered after the initial render of slot
// But before the registerSlot call, we need to rerender the slot.
forceUpdateSlot( name );
// If a new instance of a slot is being mounted while another with the
// same name exists, force its update _after_ the new slot has been
// assigned into the instance, such that its own rendering of children
// will be empty (the new Slot will subsume all fills for this name).
if ( previousSlot ) {
previousSlot.forceUpdate();
}
}
function registerFill( name: string, instance: BaseFillObject ) {
fills[ name ] = [ ...( fills[ name ] || [] ), instance ];
forceUpdateSlot( name );
}
function unregisterSlot(
name: string,
instance: Component< SlotComponentProps >
) {
// If a previous instance of a Slot by this name unmounts, do nothing,
// as the slot and its fills should only be removed for the current
// known instance.
if ( slots[ name ] !== instance ) {
return;
}
delete slots[ name ];
triggerListeners();
}
function unregisterFill( name: string, instance: BaseFillObject ) {
fills[ name ] =
fills[ name ]?.filter( ( fill ) => fill !== instance ) ?? [];
forceUpdateSlot( name );
}
function getSlot(
name: string
): Component< SlotComponentProps > | undefined {
return slots[ name ];
}
function getFills(
name: string,
slotInstance: Component< SlotComponentProps >
): BaseFillObject[] {
// Fills should only be returned for the current instance of the slot
// in which they occupy.
if ( slots[ name ] !== slotInstance ) {
return [];
}
return fills[ name ];
}
function forceUpdateSlot( name: string ) {
const slot = getSlot( name );
if ( slot ) {
slot.forceUpdate();
}
}
function triggerListeners() {
listeners.forEach( ( listener ) => listener() );
}
function subscribe( listener: () => void ) {
listeners.push( listener );
return () => {
listeners = listeners.filter( ( l ) => l !== listener );
};
}
return {
registerSlot,
unregisterSlot,
registerFill,
unregisterFill,
getSlot,
getFills,
subscribe,
};
}
export function SlotFillProvider( { children }: { children: ReactNode } ) {
const [ contextValue ] = useState( createSlotRegistory );
return (
<SlotFillContext.Provider value={ contextValue }>
{ children }
</SlotFillContext.Provider>
);
}
export default SlotFillProvider;