-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
245 lines (232 loc) · 6.56 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
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import {
__unstableMotion as motion,
__unstableAnimatePresence as AnimatePresence,
__unstableUseNavigateRegions as useNavigateRegions,
SlotFillProvider,
} from '@wordpress/components';
import {
useReducedMotion,
useViewportMatch,
useResizeObserver,
usePrevious,
} from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { useState, useRef, useEffect } from '@wordpress/element';
import { CommandMenu } from '@wordpress/commands';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import {
EditorSnackbars,
UnsavedChangesWarning,
privateApis as editorPrivateApis,
} from '@wordpress/editor';
import { privateApis as coreCommandsPrivateApis } from '@wordpress/core-commands';
import { privateApis as routerPrivateApis } from '@wordpress/router';
/**
* Internal dependencies
*/
import ErrorBoundary from '../error-boundary';
import { default as SiteHub, SiteHubMobile } from '../site-hub';
import ResizableFrame from '../resizable-frame';
import { unlock } from '../../lock-unlock';
import SaveKeyboardShortcut from '../save-keyboard-shortcut';
import { useIsSiteEditorLoading } from './hooks';
import useMovingAnimation from './animation';
import SidebarContent from '../sidebar';
import SaveHub from '../save-hub';
import SavePanel from '../save-panel';
const { useCommands } = unlock( coreCommandsPrivateApis );
const { useGlobalStyle } = unlock( blockEditorPrivateApis );
const { NavigableRegion, GlobalStylesProvider } = unlock( editorPrivateApis );
const { useLocation } = unlock( routerPrivateApis );
const ANIMATION_DURATION = 0.3;
function Layout() {
const { query, name: routeKey, areas, widths } = useLocation();
const { canvas = 'view' } = query;
useCommands();
const isMobileViewport = useViewportMatch( 'medium', '<' );
const toggleRef = useRef();
const navigateRegionsProps = useNavigateRegions();
const disableMotion = useReducedMotion();
const [ canvasResizer, canvasSize ] = useResizeObserver();
const isEditorLoading = useIsSiteEditorLoading();
const [ isResizableFrameOversized, setIsResizableFrameOversized ] =
useState( false );
const animationRef = useMovingAnimation( {
triggerAnimationOnChange: routeKey + '-' + canvas,
} );
const [ backgroundColor ] = useGlobalStyle( 'color.background' );
const [ gradientValue ] = useGlobalStyle( 'color.gradient' );
const previousCanvaMode = usePrevious( canvas );
useEffect( () => {
if ( previousCanvaMode === 'edit' ) {
toggleRef.current?.focus();
}
// Should not depend on the previous canvas mode value but the next.
}, [ canvas ] );
return (
<>
<UnsavedChangesWarning />
<CommandMenu />
{ canvas === 'view' && <SaveKeyboardShortcut /> }
<div
{ ...navigateRegionsProps }
ref={ navigateRegionsProps.ref }
className={ clsx(
'edit-site-layout',
navigateRegionsProps.className,
{
'is-full-canvas': canvas === 'edit',
}
) }
>
<div className="edit-site-layout__content">
{ /*
The NavigableRegion must always be rendered and not use
`inert` otherwise `useNavigateRegions` will fail.
*/ }
{ ( ! isMobileViewport || ! areas.mobile ) && (
<NavigableRegion
ariaLabel={ __( 'Navigation' ) }
className="edit-site-layout__sidebar-region"
>
<AnimatePresence>
{ canvas === 'view' && (
<motion.div
initial={ { opacity: 0 } }
animate={ { opacity: 1 } }
exit={ { opacity: 0 } }
transition={ {
type: 'tween',
duration:
// Disable transition in mobile to emulate a full page transition.
disableMotion ||
isMobileViewport
? 0
: ANIMATION_DURATION,
ease: 'easeOut',
} }
className="edit-site-layout__sidebar"
>
<SiteHub
ref={ toggleRef }
isTransparent={
isResizableFrameOversized
}
/>
<SidebarContent
shouldAnimate={
routeKey !== 'styles-view'
}
routeKey={ routeKey }
>
{ areas.sidebar }
</SidebarContent>
<SaveHub />
<SavePanel />
</motion.div>
) }
</AnimatePresence>
</NavigableRegion>
) }
<EditorSnackbars />
{ isMobileViewport && areas.mobile && (
<div className="edit-site-layout__mobile">
{ canvas !== 'edit' && (
<SidebarContent routeKey={ routeKey }>
<SiteHubMobile
ref={ toggleRef }
isTransparent={
isResizableFrameOversized
}
/>
</SidebarContent>
) }
{ areas.mobile }
</div>
) }
{ ! isMobileViewport &&
areas.content &&
canvas !== 'edit' && (
<div
className="edit-site-layout__area"
style={ {
maxWidth: widths?.content,
} }
>
{ areas.content }
</div>
) }
{ ! isMobileViewport && areas.edit && canvas !== 'edit' && (
<div
className="edit-site-layout__area"
style={ {
maxWidth: widths?.edit,
} }
>
{ areas.edit }
</div>
) }
{ ! isMobileViewport && areas.preview && (
<div className="edit-site-layout__canvas-container">
{ canvasResizer }
{ !! canvasSize.width && (
<div
className={ clsx(
'edit-site-layout__canvas',
{
'is-right-aligned':
isResizableFrameOversized,
}
) }
ref={ animationRef }
>
<ErrorBoundary>
<ResizableFrame
isReady={ ! isEditorLoading }
isFullWidth={ canvas === 'edit' }
defaultSize={ {
width:
canvasSize.width -
24 /* $canvas-padding */,
height: canvasSize.height,
} }
isOversized={
isResizableFrameOversized
}
setIsOversized={
setIsResizableFrameOversized
}
innerContentStyle={ {
background:
gradientValue ??
backgroundColor,
} }
>
{ areas.preview }
</ResizableFrame>
</ErrorBoundary>
</div>
) }
</div>
) }
</div>
</div>
</>
);
}
export default function LayoutWithGlobalStylesProvider( props ) {
return (
<SlotFillProvider>
<GlobalStylesProvider>
<Layout { ...props } />
</GlobalStylesProvider>
</SlotFillProvider>
);
}