-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
350 lines (333 loc) · 8.79 KB
/
index.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import {
Button,
Panel,
Slot,
Fill,
__unstableMotion as motion,
__unstableAnimatePresence as AnimatePresence,
} from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { check, starEmpty, starFilled } from '@wordpress/icons';
import { useEffect, useRef, useState } from '@wordpress/element';
import { store as viewportStore } from '@wordpress/viewport';
import { store as preferencesStore } from '@wordpress/preferences';
import {
useReducedMotion,
useViewportMatch,
usePrevious,
} from '@wordpress/compose';
/**
* Internal dependencies
*/
import ComplementaryAreaHeader from '../complementary-area-header';
import ComplementaryAreaMoreMenuItem from '../complementary-area-more-menu-item';
import ComplementaryAreaToggle from '../complementary-area-toggle';
import withComplementaryAreaContext from '../complementary-area-context';
import PinnedItems from '../pinned-items';
import { store as interfaceStore } from '../../store';
const ANIMATION_DURATION = 0.3;
function ComplementaryAreaSlot( { scope, ...props } ) {
return <Slot name={ `ComplementaryArea/${ scope }` } { ...props } />;
}
const SIDEBAR_WIDTH = 280;
const variants = {
open: { width: SIDEBAR_WIDTH },
closed: { width: 0 },
mobileOpen: { width: '100vw' },
};
function ComplementaryAreaFill( {
activeArea,
isActive,
scope,
children,
className,
id,
} ) {
const disableMotion = useReducedMotion();
const isMobileViewport = useViewportMatch( 'medium', '<' );
// This is used to delay the exit animation to the next tick.
// The reason this is done is to allow us to apply the right transition properties
// When we switch from an open sidebar to another open sidebar.
// we don't want to animate in this case.
const previousActiveArea = usePrevious( activeArea );
const previousIsActive = usePrevious( isActive );
const [ , setState ] = useState( {} );
useEffect( () => {
setState( {} );
}, [ isActive ] );
const transition = {
type: 'tween',
duration:
disableMotion ||
isMobileViewport ||
( !! previousActiveArea &&
!! activeArea &&
activeArea !== previousActiveArea )
? 0
: ANIMATION_DURATION,
ease: [ 0.6, 0, 0.4, 1 ],
};
return (
<Fill name={ `ComplementaryArea/${ scope }` }>
<AnimatePresence initial={ false }>
{ ( previousIsActive || isActive ) && (
<motion.div
variants={ variants }
initial="closed"
animate={ isMobileViewport ? 'mobileOpen' : 'open' }
exit="closed"
transition={ transition }
className="interface-complementary-area__fill"
>
<div
id={ id }
className={ className }
style={ {
width: isMobileViewport
? '100vw'
: SIDEBAR_WIDTH,
} }
>
{ children }
</div>
</motion.div>
) }
</AnimatePresence>
</Fill>
);
}
function useAdjustComplementaryListener(
scope,
identifier,
activeArea,
isActive,
isSmall
) {
const previousIsSmall = useRef( false );
const shouldOpenWhenNotSmall = useRef( false );
const { enableComplementaryArea, disableComplementaryArea } =
useDispatch( interfaceStore );
useEffect( () => {
// If the complementary area is active and the editor is switching from
// a big to a small window size.
if ( isActive && isSmall && ! previousIsSmall.current ) {
disableComplementaryArea( scope );
// Flag the complementary area to be reopened when the window size
// goes from small to big.
shouldOpenWhenNotSmall.current = true;
} else if (
// If there is a flag indicating the complementary area should be
// enabled when we go from small to big window size and we are going
// from a small to big window size.
shouldOpenWhenNotSmall.current &&
! isSmall &&
previousIsSmall.current
) {
// Remove the flag indicating the complementary area should be
// enabled.
shouldOpenWhenNotSmall.current = false;
enableComplementaryArea( scope, identifier );
} else if (
// If the flag is indicating the current complementary should be
// reopened but another complementary area becomes active, remove
// the flag.
shouldOpenWhenNotSmall.current &&
activeArea &&
activeArea !== identifier
) {
shouldOpenWhenNotSmall.current = false;
}
if ( isSmall !== previousIsSmall.current ) {
previousIsSmall.current = isSmall;
}
}, [
isActive,
isSmall,
scope,
identifier,
activeArea,
disableComplementaryArea,
enableComplementaryArea,
] );
}
function ComplementaryArea( {
children,
className,
closeLabel = __( 'Close plugin' ),
identifier,
header,
headerClassName,
icon,
isPinnable = true,
panelClassName,
scope,
name,
smallScreenTitle,
title,
toggleShortcut,
isActiveByDefault,
} ) {
// This state is used to delay the rendering of the Fill
// until the initial effect runs.
// This prevents the animation from running on mount if
// the complementary area is active by default.
const [ isReady, setIsReady ] = useState( false );
const {
isLoading,
isActive,
isPinned,
activeArea,
isSmall,
isLarge,
showIconLabels,
} = useSelect(
( select ) => {
const {
getActiveComplementaryArea,
isComplementaryAreaLoading,
isItemPinned,
} = select( interfaceStore );
const { get } = select( preferencesStore );
const _activeArea = getActiveComplementaryArea( scope );
return {
isLoading: isComplementaryAreaLoading( scope ),
isActive: _activeArea === identifier,
isPinned: isItemPinned( scope, identifier ),
activeArea: _activeArea,
isSmall: select( viewportStore ).isViewportMatch( '< medium' ),
isLarge: select( viewportStore ).isViewportMatch( 'large' ),
showIconLabels: get( 'core', 'showIconLabels' ),
};
},
[ identifier, scope ]
);
useAdjustComplementaryListener(
scope,
identifier,
activeArea,
isActive,
isSmall
);
const {
enableComplementaryArea,
disableComplementaryArea,
pinItem,
unpinItem,
} = useDispatch( interfaceStore );
useEffect( () => {
// Set initial visibility: For large screens, enable if it's active by
// default. For small screens, always initially disable.
if ( isActiveByDefault && activeArea === undefined && ! isSmall ) {
enableComplementaryArea( scope, identifier );
} else if ( activeArea === undefined && isSmall ) {
disableComplementaryArea( scope, identifier );
}
setIsReady( true );
}, [
activeArea,
isActiveByDefault,
scope,
identifier,
isSmall,
enableComplementaryArea,
disableComplementaryArea,
] );
if ( ! isReady ) {
return;
}
return (
<>
{ isPinnable && (
<PinnedItems scope={ scope }>
{ isPinned && (
<ComplementaryAreaToggle
scope={ scope }
identifier={ identifier }
isPressed={
isActive && ( ! showIconLabels || isLarge )
}
aria-expanded={ isActive }
aria-disabled={ isLoading }
label={ title }
icon={ showIconLabels ? check : icon }
showTooltip={ ! showIconLabels }
variant={ showIconLabels ? 'tertiary' : undefined }
size="compact"
/>
) }
</PinnedItems>
) }
{ name && isPinnable && (
<ComplementaryAreaMoreMenuItem
target={ name }
scope={ scope }
icon={ icon }
>
{ title }
</ComplementaryAreaMoreMenuItem>
) }
<ComplementaryAreaFill
activeArea={ activeArea }
isActive={ isActive }
className={ clsx( 'interface-complementary-area', className ) }
scope={ scope }
id={ identifier.replace( '/', ':' ) }
>
<ComplementaryAreaHeader
className={ headerClassName }
closeLabel={ closeLabel }
onClose={ () => disableComplementaryArea( scope ) }
smallScreenTitle={ smallScreenTitle }
toggleButtonProps={ {
label: closeLabel,
size: 'small',
shortcut: toggleShortcut,
scope,
identifier,
} }
>
{ header || (
<>
<h2 className="interface-complementary-area-header__title">
{ title }
</h2>
{ isPinnable && (
<Button
className="interface-complementary-area__pin-unpin-item"
icon={ isPinned ? starFilled : starEmpty }
label={
isPinned
? __( 'Unpin from toolbar' )
: __( 'Pin to toolbar' )
}
onClick={ () =>
( isPinned ? unpinItem : pinItem )(
scope,
identifier
)
}
isPressed={ isPinned }
aria-expanded={ isPinned }
size="compact"
/>
) }
</>
) }
</ComplementaryAreaHeader>
<Panel className={ panelClassName }>{ children }</Panel>
</ComplementaryAreaFill>
</>
);
}
const ComplementaryAreaWrapped =
withComplementaryAreaContext( ComplementaryArea );
ComplementaryAreaWrapped.Slot = ComplementaryAreaSlot;
export default ComplementaryAreaWrapped;