-
Notifications
You must be signed in to change notification settings - Fork 4k
/
hook.ts
217 lines (188 loc) · 5.83 KB
/
hook.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* WordPress dependencies
*/
import { useEffect, useMemo, useRef, useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import * as styles from '../styles';
import { useContextSystem, WordPressComponentProps } from '../../ui/context';
import { useCx } from '../../utils/hooks/use-cx';
import type {
ToolsPanelItem,
ToolsPanelMenuItemKey,
ToolsPanelMenuItems,
ToolsPanelMenuItemsConfig,
ToolsPanelProps,
} from '../types';
const generateMenuItems = ( {
panelItems,
shouldReset,
}: ToolsPanelMenuItemsConfig ) => {
const menuItems: ToolsPanelMenuItems = { default: {}, optional: {} };
panelItems.forEach( ( { hasValue, isShownByDefault, label } ) => {
const group = isShownByDefault ? 'default' : 'optional';
menuItems[ group ][ label ] = shouldReset ? false : hasValue();
} );
return menuItems;
};
export function useToolsPanel(
props: WordPressComponentProps< ToolsPanelProps, 'div' >
) {
const {
className,
resetAll,
panelId,
hasInnerWrapper,
shouldRenderPlaceholderItems,
...otherProps
} = useContextSystem( props, 'ToolsPanel' );
const isResetting = useRef( false );
const wasResetting = isResetting.current;
// `isResetting` is cleared via this hook to effectively batch together
// the resetAll task. Without this, the flag is cleared after the first
// control updates and forces a rerender with subsequent controls then
// believing they need to reset, unfortunately using stale data.
useEffect( () => {
if ( wasResetting ) {
isResetting.current = false;
}
}, [ wasResetting ] );
// Allow panel items to register themselves.
const [ panelItems, setPanelItems ] = useState< ToolsPanelItem[] >( [] );
const registerPanelItem = ( item: ToolsPanelItem ) => {
setPanelItems( ( items ) => [ ...items, item ] );
};
// Panels need to deregister on unmount to avoid orphans in menu state.
// This is an issue when panel items are being injected via SlotFills.
const deregisterPanelItem = ( label: string ) => {
// When switching selections between components injecting matching
// controls, e.g. both panels have a "padding" control, the
// deregistration of the first panel doesn't occur until after the
// registration of the next.
const index = panelItems.findIndex( ( item ) => item.label === label );
if ( index !== -1 ) {
setPanelItems( ( items ) => items.splice( index, 1 ) );
}
};
// Manage and share display state of menu items representing child controls.
const [ menuItems, setMenuItems ] = useState< ToolsPanelMenuItems >( {
default: {},
optional: {},
} );
// Setup menuItems state as panel items register themselves.
useEffect( () => {
const items = generateMenuItems( {
panelItems,
shouldReset: false,
} );
setMenuItems( items );
}, [ panelItems ] );
// Force a menu item to be checked.
// This is intended for use with default panel items. They are displayed
// separately to optional items and have different display states,
//.we need to update that when their value is customized.
const flagItemCustomization = (
label: string,
group: ToolsPanelMenuItemKey = 'default'
) => {
setMenuItems( {
...menuItems,
[ group ]: {
...menuItems[ group ],
[ label ]: true,
},
} );
};
// Whether all optional menu items are hidden or not must be tracked
// in order to later determine if the panel display is empty and handle
// conditional display of a plus icon to indicate the presence of further
// menu items.
const [
areAllOptionalControlsHidden,
setAreAllOptionalControlsHidden,
] = useState( false );
useEffect( () => {
if ( menuItems.optional ) {
const optionalItems = Object.entries( menuItems.optional );
const allControlsHidden =
optionalItems.length > 0 &&
! optionalItems.some( ( [ , isSelected ] ) => isSelected );
setAreAllOptionalControlsHidden( allControlsHidden );
}
}, [ menuItems.optional ] );
const cx = useCx();
const classes = useMemo( () => {
const hasDefaultMenuItems =
menuItems?.default && !! Object.keys( menuItems?.default ).length;
const wrapperStyle =
hasInnerWrapper && styles.ToolsPanelWithInnerWrapper;
const emptyStyle =
! hasDefaultMenuItems &&
areAllOptionalControlsHidden &&
styles.ToolsPanelHiddenInnerWrapper;
return cx( styles.ToolsPanel, wrapperStyle, emptyStyle, className );
}, [
className,
hasInnerWrapper,
menuItems,
areAllOptionalControlsHidden,
] );
// Toggle the checked state of a menu item which is then used to determine
// display of the item within the panel.
const toggleItem = ( label: string ) => {
const currentItem = panelItems.find( ( item ) => item.label === label );
if ( ! currentItem ) {
return;
}
const menuGroup = currentItem.isShownByDefault ? 'default' : 'optional';
const newMenuItems = {
...menuItems,
[ menuGroup ]: {
...menuItems[ menuGroup ],
[ label ]: ! menuItems[ menuGroup ][ label ],
},
};
setMenuItems( newMenuItems );
};
const getResetAllFilters = () => {
const filters: Array< () => void > = [];
panelItems.forEach( ( item ) => {
if ( item.resetAllFilter ) {
filters.push( item.resetAllFilter );
}
} );
return filters;
};
// Resets display of children and executes resetAll callback if available.
const resetAllItems = () => {
if ( typeof resetAll === 'function' ) {
isResetting.current = true;
resetAll( getResetAllFilters() );
}
// Turn off display of all non-default items.
const resetMenuItems = generateMenuItems( {
panelItems,
shouldReset: true,
} );
setMenuItems( resetMenuItems );
};
const panelContext = {
panelId,
menuItems,
registerPanelItem,
deregisterPanelItem,
flagItemCustomization,
areAllOptionalControlsHidden,
hasMenuItems: !! panelItems.length,
isResetting: isResetting.current,
shouldRenderPlaceholderItems,
};
return {
...otherProps,
panelContext,
resetAllItems,
toggleItem,
className: classes,
};
}