Skip to content

Commit

Permalink
use useRefEffect instead of the requestAnimationFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian committed Apr 30, 2024
1 parent 43cea9c commit c48a820
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
24 changes: 7 additions & 17 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
forwardRef,
useEffect,
useState,
useCallback,
useMemo,
useRef,
} from '@wordpress/element';
import { forwardRef, useState, useCallback, useMemo } from '@wordpress/element';
import { VisuallyHidden, SearchControl, Popover } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { useDebouncedInput } from '@wordpress/compose';
import { useDebouncedInput, useRefEffect } from '@wordpress/compose';

/**
* Internal dependencies
Expand Down Expand Up @@ -238,14 +231,11 @@ function InserterMenu(
setSelectedTab( value );
};

const searchRef = useRef();
useEffect( () => {
// We need to wait for the next frame to focus the search input
// becase the tabs component will try to focus the selected tab
window.requestAnimationFrame( () => {
searchRef?.current?.focus();
} );
}, [ selectedTab ] );
const searchRef = useRefEffect( ( element ) => {
if ( element ) {
element.focus();
}
} );

const inserterSearch = useMemo( () => {
if ( selectedTab === 'media' ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
useViewportMatch,
__experimentalUseDialog as useDialog,
} from '@wordpress/compose';
import { useCallback, useEffect, useRef } from '@wordpress/element';
import { useCallback, useRef } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

Expand All @@ -35,9 +35,6 @@ export default function InserterSidebar() {
} );

const libraryRef = useRef();
useEffect( () => {
libraryRef.current.focusSearch();
}, [] );

return (
<div
Expand Down

0 comments on commit c48a820

Please sign in to comment.