Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/edit-site/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@ export default function Layout() {
! isEditorLoading
}
isFullWidth={ isEditing }
defaultSize={ {
width:
canvasSize.width -
24 /* $canvas-padding */,
height: canvasSize.height,
} }
isOversized={
isResizableFrameOversized
}
Expand Down
35 changes: 10 additions & 25 deletions packages/edit-site/src/components/resizable-frame/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useState, useRef, useEffect } from '@wordpress/element';
import { useState, useRef } from '@wordpress/element';
import {
ResizableBox,
Tooltip,
Expand Down Expand Up @@ -82,6 +82,8 @@ function ResizableFrame( {
setIsOversized,
isReady,
children,
/** The default (unresized) width/height of the frame, based on the space availalbe in the viewport. */
defaultSize,
innerContentStyle,
} ) {
const [ frameSize, setFrameSize ] = useState( INITIAL_FRAME_SIZE );
Expand All @@ -95,22 +97,13 @@ function ResizableFrame( {
[]
);
const { setCanvasMode } = unlock( useDispatch( editSiteStore ) );
const initialAspectRatioRef = useRef( null );
// The width of the resizable frame on initial render.
const initialComputedWidthRef = useRef( null );
const FRAME_TRANSITION = { type: 'tween', duration: isResizing ? 0 : 0.5 };
const frameRef = useRef( null );
const resizableHandleHelpId = useInstanceId(
ResizableFrame,
'edit-site-resizable-frame-handle-help'
);

// Remember frame dimensions on initial render.
useEffect( () => {
const { offsetWidth, offsetHeight } = frameRef.current.resizable;
initialComputedWidthRef.current = offsetWidth;
initialAspectRatioRef.current = offsetWidth / offsetHeight;
}, [] );
const defaultAspectRatio = defaultSize.width / defaultSize.height;

const handleResizeStart = ( _event, _direction, ref ) => {
// Remember the starting width so we don't have to get `ref.offsetWidth` on
Expand All @@ -126,7 +119,7 @@ function ResizableFrame( {
const maxDoubledDelta =
delta.width < 0 // is shrinking
? deltaAbs
: ( initialComputedWidthRef.current - startingWidth ) / 2;
: ( defaultSize.width - startingWidth ) / 2;
const deltaToDouble = Math.min( deltaAbs, maxDoubledDelta );
const doubleSegment = deltaAbs === 0 ? 0 : deltaToDouble / deltaAbs;
const singleSegment = 1 - doubleSegment;
Expand All @@ -135,17 +128,14 @@ function ResizableFrame( {

const updatedWidth = startingWidth + delta.width;

setIsOversized( updatedWidth > initialComputedWidthRef.current );
setIsOversized( updatedWidth > defaultSize.width );

// Width will be controlled by the library (via `resizeRatio`),
// so we only need to update the height.
setFrameSize( {
height: isOversized
? '100%'
: calculateNewHeight(
updatedWidth,
initialAspectRatioRef.current
),
: calculateNewHeight( updatedWidth, defaultAspectRatio ),
} );
};

Expand Down Expand Up @@ -186,15 +176,12 @@ function ResizableFrame( {
FRAME_MIN_WIDTH,
frameRef.current.resizable.offsetWidth + delta
),
initialComputedWidthRef.current
defaultSize.width
);

setFrameSize( {
width: newWidth,
height: calculateNewHeight(
newWidth,
initialAspectRatioRef.current
),
height: calculateNewHeight( newWidth, defaultAspectRatio ),
} );
};

Expand Down Expand Up @@ -291,9 +278,7 @@ function ResizableFrame( {
undefined
}
aria-valuemin={ FRAME_MIN_WIDTH }
aria-valuemax={
initialComputedWidthRef.current
}
aria-valuemax={ defaultSize.width }
onKeyDown={ handleResizableHandleKeyDown }
initial="hidden"
exit="hidden"
Expand Down