Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further bugfixes for 6.3 RC2 #52915

Merged
merged 15 commits into from
Jul 25, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
ResizableFrame: Account for window resizing (#52697)
* 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
commit 59b5d2eb7136bc1f9bf83b7ed866043398d15999
6 changes: 6 additions & 0 deletions packages/edit-site/src/components/layout/index.js
Original file line number Diff line number Diff line change
@@ -340,6 +340,12 @@ export default function Layout() {
! isEditorLoading
}
isFullWidth={ isEditing }
defaultSize={ {
width:
canvasSize.width -
24 /* $canvas-padding */,
height: canvasSize.height,
} }
isOversized={
isResizableFrameOversized
}
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
@@ -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,
@@ -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 );
@@ -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
@@ -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;
@@ -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 ),
} );
};

@@ -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 ),
} );
};

@@ -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"