-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
fill.js
104 lines (97 loc) · 2.52 KB
/
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
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
/**
* WordPress dependencies
*/
import {
__experimentalStyleProvider as StyleProvider,
__experimentalToolsPanelContext as ToolsPanelContext,
} from '@wordpress/components';
import warning from '@wordpress/warning';
import deprecated from '@wordpress/deprecated';
import { useEffect, useContext } from '@wordpress/element';
/**
* Internal dependencies
*/
import {
useBlockEditContext,
mayDisplayControlsKey,
} from '../block-edit/context';
import groups from './groups';
export default function InspectorControlsFill( {
children,
group = 'default',
__experimentalGroup,
resetAllFilter,
} ) {
if ( __experimentalGroup ) {
deprecated(
'`__experimentalGroup` property in `InspectorControlsFill`',
{
since: '6.2',
version: '6.4',
alternative: '`group`',
}
);
group = __experimentalGroup;
}
const context = useBlockEditContext();
const Fill = groups[ group ]?.Fill;
if ( ! Fill ) {
warning( `Unknown InspectorControls group "${ group }" provided.` );
return null;
}
if ( ! context[ mayDisplayControlsKey ] ) {
return null;
}
return (
<StyleProvider document={ document }>
<Fill>
{ ( fillProps ) => {
return (
<ToolsPanelInspectorControl
fillProps={ fillProps }
children={ children }
resetAllFilter={ resetAllFilter }
/>
);
} }
</Fill>
</StyleProvider>
);
}
function RegisterResetAll( { resetAllFilter, children } ) {
const { registerResetAllFilter, deregisterResetAllFilter } =
useContext( ToolsPanelContext );
useEffect( () => {
if (
resetAllFilter &&
registerResetAllFilter &&
deregisterResetAllFilter
) {
registerResetAllFilter( resetAllFilter );
return () => {
deregisterResetAllFilter( resetAllFilter );
};
}
}, [ resetAllFilter, registerResetAllFilter, deregisterResetAllFilter ] );
return children;
}
function ToolsPanelInspectorControl( { children, resetAllFilter, fillProps } ) {
// `fillProps.forwardedContext` is an array of context provider entries, provided by slot,
// that should wrap the fill markup.
const { forwardedContext = [] } = fillProps;
// Children passed to InspectorControlsFill will not have
// access to any React Context whose Provider is part of
// the InspectorControlsSlot tree. So we re-create the
// Provider in this subtree.
const innerMarkup = (
<RegisterResetAll resetAllFilter={ resetAllFilter }>
{ children }
</RegisterResetAll>
);
return forwardedContext.reduce(
( inner, [ Provider, props ] ) => (
<Provider { ...props }>{ inner }</Provider>
),
innerMarkup
);
}