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

Tiled gallery: Use requestAnimationFrame for resize handling #29016

Merged
merged 1 commit into from
Dec 3, 2018
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
1 change: 0 additions & 1 deletion client/gutenberg/extensions/tiled-gallery/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ export const LAYOUT_STYLES = [
];
export const LAYOUTS = [ 'rectangular', 'columns', 'square', 'circle' ];
export const MAX_COLUMNS = 20;
export const RESIZE_RATE_IN_MS = 200;
export const TILE_MARGIN = 2;
28 changes: 18 additions & 10 deletions client/gutenberg/extensions/tiled-gallery/gallery-grid.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
*/
import { Component, Fragment, createRef } from '@wordpress/element';
import classnames from 'classnames';
import { defer, throttle } from 'lodash';
import { defer } from 'lodash';
import ResizeObserver from 'resize-observer-polyfill';

/**
* Internal dependencies
*/
import { DEFAULT_GALLERY_WIDTH, RESIZE_RATE_IN_MS } from './constants';
import { DEFAULT_GALLERY_WIDTH } from './constants';
import { getLayout } from './layouts';

class TiledGalleryGrid extends Component {
Expand All @@ -22,14 +22,22 @@ class TiledGalleryGrid extends Component {
// Create a ref to store the wrapper DOM element for ResizeObserver
wrapper = createRef();

throttleOnResize = throttle( entries => {
for ( const entry of entries ) {
const { width } = entry.contentRect;
if ( width && width !== this.state.width ) {
this.setWidth( width );
}
pendingRaf = null;

handleResize = entries => {
if ( this.pendingRaf ) {
cancelAnimationFrame( this.pendingRaf );
}
}, RESIZE_RATE_IN_MS );
this.pendingRaf = requestAnimationFrame( () => {
this.pendingRaf = null;
for ( const entry of entries ) {
const { width } = entry.contentRect;
if ( width && width !== this.state.width ) {
this.setWidth( width );
}
}
} );
};

setWidth( width ) {
this.setState( { width } );
Expand All @@ -39,7 +47,7 @@ class TiledGalleryGrid extends Component {
this.deferredMount = defer( () => {
// ResizeObserver has checks for `window` & `document`:
// it does nothing if those are not available.
this.observer = new ResizeObserver( this.throttleOnResize );
this.observer = new ResizeObserver( this.handleResize );
this.observer.observe( this.wrapper.current.parentNode );
} );
}
Expand Down
43 changes: 24 additions & 19 deletions client/gutenberg/extensions/tiled-gallery/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
/**
* External dependencies
*/
import { throttle } from 'lodash';
import ResizeObserver from 'resize-observer-polyfill';

/**
* Internal dependencies
*/
import { RESIZE_RATE_IN_MS, DEFAULT_GALLERY_WIDTH } from './constants';
import { DEFAULT_GALLERY_WIDTH } from './constants';
import { getActiveStyleName, getLayout } from './layouts';

/**
Expand Down Expand Up @@ -50,25 +49,31 @@ const resizeGallery = ( { galleryNode, width, columns, layout } ) => {
};

/**
* Throttled resize event
* Compares gallery width to its previous width to decide if to resize it.
* @param {Array<ResizeObserverEntry>} entries Resized entries
*/
const throttleOnResize = throttle( entries => {
for ( const entry of entries ) {
const { width } = entry.contentRect;
// Don't resize if width didn't chance
if ( width !== entry.target.getAttribute( 'data-width' ) ) {
// Store width for later comparison
entry.target.setAttribute( 'data-width', width );
resizeGallery( {
columns: parseInt( entry.target.getAttribute( 'data-columns' ), 10 ),
galleryNode: entry.target,
layout: getActiveStyleName( entry.target.className ),
width,
} );
}
function handleResize( entries ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would really like to get all the resize handling using the same code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, that would be good 👍

if ( handleResize.pendingRaf ) {
cancelAnimationFrame( handleResize.pendingRaf );
}
}, RESIZE_RATE_IN_MS );
handleResize.pendingRaf = requestAnimationFrame( () => {
handleResize.pendingRaf = null;
for ( const entry of entries ) {
const { width } = entry.contentRect;
// Don't resize if width didn't chance
if ( width !== entry.target.getAttribute( 'data-width' ) ) {
// Store width for later comparison
entry.target.setAttribute( 'data-width', width );
resizeGallery( {
columns: parseInt( entry.target.getAttribute( 'data-columns' ), 10 ),
galleryNode: entry.target,
layout: getActiveStyleName( entry.target.className ),
width,
} );
}
}
} );
}

/**
* Get different galleries on the page
Expand All @@ -89,7 +94,7 @@ const observeGalleries = () => {
return;
}

const observer = new ResizeObserver( throttleOnResize );
const observer = new ResizeObserver( handleResize );

galleries.forEach( gallery => {
// Observe only if gallery has child nodes
Expand Down