Skip to content

Commit

Permalink
Popover: clean up requestAnimationFrame (#19771)
Browse files Browse the repository at this point in the history
* Popover: clean up requestAnimationFrame

* Cancel the last request before a new request
  • Loading branch information
ellatrix authored Jan 23, 2020
1 parent 89c2b44 commit dcb08f3
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,6 @@ const Popover = ( {

// Height may still adjust between now and the next tick.
const timeoutId = window.setTimeout( refresh );
const refreshOnAnimationFrame = () => {
window.requestAnimationFrame( refresh );
};

/*
* There are sometimes we need to reposition or resize the popover that
Expand All @@ -373,6 +370,13 @@ const Popover = ( {
*/
const intervalHandle = window.setInterval( refresh, 500 );

let rafId;

const refreshOnAnimationFrame = () => {
window.cancelAnimationFrame( rafId );
rafId = window.requestAnimationFrame( refresh );
};

// Sometimes a click trigger a layout change that affects the popover
// position. This is an opportunity to immediately refresh rather than
// at the interval.
Expand All @@ -398,6 +402,7 @@ const Popover = ( {
window.removeEventListener( 'resize', refresh );
window.removeEventListener( 'scroll', refresh, true );
window.addEventListener( 'click', refreshOnAnimationFrame );
window.cancelAnimationFrame( rafId );

if ( observer ) {
observer.disconnect();
Expand Down

0 comments on commit dcb08f3

Please sign in to comment.