Skip to content

Commit

Permalink
Fix rendering for tests. Reduce Timeout from 250 -> 200 to improve UX.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Q committed Jun 30, 2020
1 parent 6e2e085 commit 239e07a
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/block-editor/src/components/block-toolbar/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ import { noop } from 'lodash';
import { useDispatch } from '@wordpress/data';
import { useState, useRef, useEffect, useCallback } from '@wordpress/element';

const { clearTimeout, setTimeout } = window;
const DEBOUNCE_TIMEOUT = 250;
const {
cancelAnimationFrame,
clearTimeout,
requestAnimationFrame,
setTimeout,
} = window;

const DEBOUNCE_TIMEOUT = 200;

/**
* Hook that creates a showMover state, as well as debounced show/hide callbacks.
Expand Down Expand Up @@ -173,6 +179,7 @@ export function useShowMoversGestures( {
*/
export function useToggleBlockHighlight( clientId ) {
const { toggleBlockHighlight } = useDispatch( 'core/block-editor' );
let requestAnimationFrameId;

const updateBlockHighlight = useCallback(
( isFocused ) => {
Expand All @@ -182,7 +189,20 @@ export function useToggleBlockHighlight( clientId ) {
);

useEffect( () => {
return () => updateBlockHighlight( false );
// On mount, we make sure to cancel any pending animation frame request
// that hasn't been completed yet. Components like NavigableToolbar may
// mount and unmount quickly.
if ( requestAnimationFrameId ) {
cancelAnimationFrame( requestAnimationFrameId );
}

return () => {
// Sequences state change to enable editor updates (e.g. cursor
// position) to render correctly.
requestAnimationFrameId = requestAnimationFrame( () => {
updateBlockHighlight( false );
} );
};
}, [ updateBlockHighlight ] );

return updateBlockHighlight;
Expand Down

0 comments on commit 239e07a

Please sign in to comment.