From ade84f8e0894fcf5d11faa0f69e4560e2e333db9 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Tue, 15 Oct 2024 16:07:02 +0200 Subject: [PATCH] Patterns inserter: Make sure first tab is focused even if there isn't a selected tab --- .../components/inserter/category-tabs/index.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/components/inserter/category-tabs/index.js b/packages/block-editor/src/components/inserter/category-tabs/index.js index 2641e2a20c40f..7b6baaab398f8 100644 --- a/packages/block-editor/src/components/inserter/category-tabs/index.js +++ b/packages/block-editor/src/components/inserter/category-tabs/index.js @@ -6,6 +6,7 @@ import { privateApis as componentsPrivateApis, __unstableMotion as motion, } from '@wordpress/components'; +import { useState, useEffect } from '@wordpress/element'; /** * Internal dependencies @@ -31,10 +32,22 @@ function CategoryTabs( { const previousSelectedCategory = usePrevious( selectedCategory ); + const selectedTabId = selectedCategory ? selectedCategory.name : null; + const [ activeTabId, setActiveId ] = useState(); + const firstTabId = categories?.[ 0 ]?.name; + useEffect( () => { + // If there is no active tab, make the first tab the active tab, so that + // when focus is moved to the tablist, the first tab will be focused + // despite not being selected + if ( selectedTabId === null && ! activeTabId && firstTabId ) { + setActiveId( firstTabId ); + } + }, [ selectedTabId, activeTabId, firstTabId, setActiveId ] ); + return ( { // Pass the full category object @@ -44,6 +57,8 @@ function CategoryTabs( { ) ); } } + activeTabId={ activeTabId } + onActiveTabIdChange={ setActiveId } > { categories.map( ( category ) => (