Skip to content

Commit

Permalink
frame resizer centered
Browse files Browse the repository at this point in the history
  • Loading branch information
SaxonF authored and mtias committed Apr 24, 2023
1 parent 1c19781 commit d51b541
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 108 deletions.
127 changes: 20 additions & 107 deletions packages/edit-site/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
__unstableMotion as motion,
__unstableAnimatePresence as AnimatePresence,
__unstableUseNavigateRegions as useNavigateRegions,
ResizableBox,
} from '@wordpress/components';
import {
useReducedMotion,
Expand All @@ -38,7 +37,7 @@ import getIsListPage from '../../utils/get-is-list-page';
import Header from '../header-edit-mode';
import useInitEditedEntityFromURL from '../sync-state-with-url/use-init-edited-entity-from-url';
import SiteHub from '../site-hub';
import ResizeHandle from '../block-editor/resize-handle';
import ResizableFrame from '../resizable-frame';
import useSyncCanvasModeWithURL from '../sync-state-with-url/use-sync-canvas-mode-with-url';
import { unlock } from '../../private-apis';
import SavePanel from '../save-panel';
Expand All @@ -47,17 +46,6 @@ import KeyboardShortcutsGlobal from '../keyboard-shortcuts/global';
import { useCommands } from '../../hooks/commands';

const ANIMATION_DURATION = 0.5;
const emptyResizeHandleStyles = {
position: undefined,
userSelect: undefined,
cursor: undefined,
width: undefined,
height: undefined,
top: undefined,
right: undefined,
bottom: undefined,
left: undefined,
};

export default function Layout() {
// This ensures the edited entity id and type are initialized properly.
Expand Down Expand Up @@ -87,6 +75,7 @@ export default function Layout() {
select( preferencesStore ).get( 'fixedToolbar' ),
};
}, [] );
const isEditing = canvasMode === 'edit';
const navigateRegionsProps = useNavigateRegions( {
previous: previousShortcut,
next: nextShortcut,
Expand All @@ -98,25 +87,17 @@ export default function Layout() {
( isMobileViewport && ! isListPage ) ||
( ! isMobileViewport && ( canvasMode === 'view' || ! isEditorPage ) );
const showCanvas =
( isMobileViewport && isEditorPage && canvasMode === 'edit' ) ||
( isMobileViewport && isEditorPage && isEditing ) ||
! isMobileViewport ||
! isEditorPage;
const showFrame =
( ! isEditorPage && ! isMobileViewport ) ||
( ! isMobileViewport && isEditorPage && canvasMode === 'view' );
const isFullCanvas =
( isMobileViewport && isListPage ) ||
( isEditorPage && canvasMode === 'edit' );
( isMobileViewport && isListPage ) || ( isEditorPage && isEditing );
const [ canvasResizer, canvasSize ] = useResizeObserver();
const [ fullResizer, fullSize ] = useResizeObserver();
const [ forcedWidth, setForcedWidth ] = useState( null );
const [ isResizing, setIsResizing ] = useState( false );
const isResizingEnabled = ! isMobileViewport && canvasMode === 'view';
const defaultSidebarWidth = isMobileViewport ? '100vw' : 360;
let canvasWidth = isResizing ? '100%' : fullSize.width;
if ( showFrame && ! isResizing ) {
canvasWidth = canvasSize.width - canvasPadding;
}
const [ fullResizer ] = useResizeObserver();
const [ isResizing ] = useState( false );

// Synchronizing the URL with the store value of canvasMode happens in an effect
// This condition ensures the component is only rendered after the synchronization happens
Expand All @@ -139,15 +120,15 @@ export default function Layout() {
navigateRegionsProps.className,
{
'is-full-canvas': isFullCanvas,
'is-edit-mode': canvasMode === 'edit',
'is-edit-mode': isEditing,
'has-fixed-toolbar': hasFixedToolbar,
}
) }
>
<SiteHub ref={ hubRef } className="edit-site-layout__hub" />

<AnimatePresence initial={ false }>
{ isEditorPage && canvasMode === 'edit' && (
{ isEditorPage && isEditing && (
<NavigableRegion
className="edit-site-layout__header"
ariaLabel={ __( 'Editor top bar' ) }
Expand All @@ -169,16 +150,15 @@ export default function Layout() {
ease: 'easeOut',
} }
>
{ canvasMode === 'edit' && <Header /> }
{ isEditing && <Header /> }
</NavigableRegion>
) }
</AnimatePresence>

<div className="edit-site-layout__content">
<AnimatePresence initial={ false }>
{ showSidebar && (
<ResizableBox
as={ motion.div }
<motion.div
initial={ {
opacity: 0,
} }
Expand All @@ -190,86 +170,35 @@ export default function Layout() {
} }
transition={ {
type: 'tween',
duration:
disableMotion || isResizing
? 0
: ANIMATION_DURATION,
duration: ANIMATION_DURATION,
ease: 'easeOut',
} }
size={ {
height: '100%',
width:
isResizingEnabled && forcedWidth
? forcedWidth
: defaultSidebarWidth,
} }
className="edit-site-layout__sidebar"
enable={ {
right: isResizingEnabled,
} }
onResizeStop={ ( event, direction, elt ) => {
setForcedWidth( elt.clientWidth );
setIsResizing( false );
} }
onResizeStart={ () => {
setIsResizing( true );
} }
onResize={ ( event, direction, elt ) => {
// This is a performance optimization
// We set the width imperatively to avoid re-rendering
// the whole component while resizing.
hubRef.current.style.width =
elt.clientWidth - 48 + 'px';
} }
handleComponent={ {
right: (
<ResizeHandle
direction="right"
variation="separator"
resizeWidthBy={ ( delta ) => {
setForcedWidth(
( forcedWidth ??
defaultSidebarWidth ) +
delta
);
} }
/>
),
} }
handleClasses={ undefined }
handleStyles={ {
right: emptyResizeHandleStyles,
} }
minWidth={ isResizingEnabled ? 320 : undefined }
maxWidth={
isResizingEnabled && fullSize
? fullSize.width - 360
: undefined
}
>
<NavigableRegion
ariaLabel={ __( 'Navigation sidebar' ) }
>
<Sidebar />
</NavigableRegion>
</ResizableBox>
</motion.div>
) }
</AnimatePresence>

<SavePanel />

{ showCanvas && (
<div
<motion.div
className={ classnames(
'edit-site-layout__canvas-container',
{
'is-resizing': isResizing,
}
) }
style={ {
animate={ {
paddingTop: showFrame ? canvasPadding : 0,
paddingBottom: showFrame ? canvasPadding : 0,
} }
transition={ { duration: ANIMATION_DURATION } }
>
{ canvasResizer }
{ !! canvasSize.width && (
Expand Down Expand Up @@ -301,34 +230,18 @@ export default function Layout() {
ease: 'easeOut',
} }
>
<motion.div
style={ {
position: 'absolute',
top: 0,
left: 0,
bottom: 0,
} }
initial={ false }
animate={ {
width: canvasWidth,
} }
transition={ {
type: 'tween',
duration:
disableMotion || isResizing
? 0
: ANIMATION_DURATION,
ease: 'easeOut',
} }
<ResizableFrame
startWidth={ canvasSize.width }
isFull={ isEditing }
>
<ErrorBoundary>
{ isEditorPage && <Editor /> }
{ isListPage && <ListPage /> }
</ErrorBoundary>
</motion.div>
</ResizableFrame>
</motion.div>
) }
</div>
</motion.div>
) }
</div>
</div>
Expand Down
4 changes: 3 additions & 1 deletion packages/edit-site/src/components/layout/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@
left: 0;
bottom: 0;
width: 100%;

display: flex;
justify-content: center;
align-items: center;
& > div {
color: $gray-900;
background: $white;
Expand Down
Loading

0 comments on commit d51b541

Please sign in to comment.