Skip to content

Commit

Permalink
ResizableFrame: Account for window resizing (#52697)
Browse files Browse the repository at this point in the history
* ResizableFrame: Account for window resizing

* Don't memoize

---------

Co-authored-by: Robert Anderson <robert@noisysocks.com>
  • Loading branch information
2 people authored and ramonjd committed Jul 25, 2023
1 parent b24d47c commit 59b5d2e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
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

0 comments on commit 59b5d2e

Please sign in to comment.