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

Block Editor: Optimize resizable preview hooks #22511

Merged
merged 3 commits into from
May 21, 2020
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
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