Skip to content

Commit

Permalink
Block Editor: Optimize resizable preview hooks (#22511)
Browse files Browse the repository at this point in the history
* Block Editor: Move useResizeCanvas to folder by name

* Block Editor: Avoid simulated media query transforms except when previewing

* Block Editor: Avoid resize event listener except when previewing
  • Loading branch information
aduth authored May 21, 2020
1 parent 574a77c commit 53cec2f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
5 changes: 3 additions & 2 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,13 @@ Undocumented declaration.

<a name="useSimulatedMediaQuery" href="#useSimulatedMediaQuery">#</a> **useSimulatedMediaQuery**

Function that manipulates media queries from stylesheets to simulate a given viewport width.
Function that manipulates media queries from stylesheets to simulate a given
viewport width.

_Parameters_

- _marker_ `string`: CSS selector string defining start and end of manipulable styles.
- _width_ `number`: Viewport width to simulate.
- _width_ `?number`: Viewport width to simulate. If provided null, the stylesheets will not be modified.

<a name="Warning" href="#Warning">#</a> **Warning**

Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export { default as withColorContext } from './color-palette/with-color-context'
export { default as __experimentalBlockSettingsMenuFirstItem } from './block-settings-menu/block-settings-menu-first-item';
export { default as __experimentalInserterMenuExtension } from './inserter-menu-extension';
export { default as __experimentalPreviewOptions } from './preview-options';
export { default as __experimentalUseResizeCanvas } from './resize-canvas';
export { default as __experimentalUseResizeCanvas } from './use-resize-canvas';
export { default as BlockInspector } from './block-inspector';
export { default as BlockList } from './block-list';
export { Block as __experimentalBlock } from './block-list/block-wrapper';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ export default function useResizeCanvas( deviceType ) {
const [ actualWidth, updateActualWidth ] = useState( window.innerWidth );

useEffect( () => {
if ( deviceType === 'Desktop' ) {
return;
}

const resizeListener = () => updateActualWidth( window.innerWidth );
window.addEventListener( 'resize', resizeListener );

return () => {
window.removeEventListener( 'resize', resizeListener );
};
} );
}, [ deviceType ] );

const getCanvasWidth = ( device ) => {
let deviceWidth = 0;
let deviceWidth;

switch ( device ) {
case 'Tablet':
Expand All @@ -38,7 +42,7 @@ export default function useResizeCanvas( deviceType ) {
deviceWidth = 360;
break;
default:
deviceWidth = 2000;
return null;
}

return deviceWidth < actualWidth ? deviceWidth : actualWidth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,20 @@ function replaceMediaQueryWithWidthEvaluation( ruleText, widthValue ) {
}

/**
* Function that manipulates media queries from stylesheets to simulate a given viewport width.
* Function that manipulates media queries from stylesheets to simulate a given
* viewport width.
*
* @param {string} marker CSS selector string defining start and end of manipulable styles.
* @param {number} width Viewport width to simulate.
* @param {string} marker CSS selector string defining start and end of
* manipulable styles.
* @param {number?} width Viewport width to simulate. If provided null, the
* stylesheets will not be modified.
*/
export default function useSimulatedMediaQuery( marker, width ) {
useEffect( () => {
if ( ! width ) {
return;
}

const styleSheets = getStyleSheetsThatMatchHostname();
const originalStyles = [];
styleSheets.forEach( ( styleSheet, styleSheetIndex ) => {
Expand Down

0 comments on commit 53cec2f

Please sign in to comment.