From 13415b61b52bf957bae8003d5a61d406fe769179 Mon Sep 17 00:00:00 2001 From: chad1008 <13856531+chad1008@users.noreply.github.com> Date: Fri, 2 Feb 2024 11:25:20 -0500 Subject: [PATCH 1/2] add requestAnimationFrame --- packages/components/src/tabs/index.tsx | 34 ++++++++++++++------------ 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/components/src/tabs/index.tsx b/packages/components/src/tabs/index.tsx index e8e9bff76b3e92..4573f7a6968df9 100644 --- a/packages/components/src/tabs/index.tsx +++ b/packages/components/src/tabs/index.tsx @@ -164,23 +164,25 @@ function Tabs( { return; } - const focusedElement = - items?.[ 0 ]?.element?.ownerDocument.activeElement; - - if ( - ! focusedElement || - ! items.some( ( item ) => focusedElement === item.element ) - ) { - return; // Return early if no tabs are focused. - } + requestAnimationFrame( () => { + const focusedElement = + items?.[ 0 ]?.element?.ownerDocument.activeElement; + + if ( + ! focusedElement || + ! items.some( ( item ) => focusedElement === item.element ) + ) { + return; // Return early if no tabs are focused. + } - // If, after ariakit re-computes the active tab, that tab doesn't match - // the currently focused tab, then we force an update to ariakit to avoid - // any mismatches, especially when navigating to previous/next tab with - // arrow keys. - if ( activeId !== focusedElement.id ) { - setActiveId( focusedElement.id ); - } + // If, after ariakit re-computes the active tab, that tab doesn't match + // the currently focused tab, then we force an update to ariakit to avoid + // any mismatches, especially when navigating to previous/next tab with + // arrow keys. + if ( activeId !== focusedElement.id ) { + setActiveId( focusedElement.id ); + } + } ); }, [ activeId, isControlled, items, setActiveId ] ); const contextValue = useMemo( From 5c3b9b1f453a080d9cc9f8a6d8271a04ed54de7f Mon Sep 17 00:00:00 2001 From: chad1008 <13856531+chad1008@users.noreply.github.com> Date: Fri, 2 Feb 2024 11:47:31 -0500 Subject: [PATCH 2/2] changelog --- packages/components/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index 44e453668955a3..25e473a331eafa 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -22,6 +22,7 @@ - `Guide`, `Modal`: Restore accent color themability ([#58098](https://github.com/WordPress/gutenberg/pull/58098)). - `DropdownMenuV2`: Restore accent color themability ([#58130](https://github.com/WordPress/gutenberg/pull/58130)). - `Tabs`: improve controlled mode focus handling and keyboard navigation ([#57696](https://github.com/WordPress/gutenberg/pull/57696)). +- `Tabs`: prevent internal focus from updating too early ([#58625](https://github.com/WordPress/gutenberg/pull/58625)). - Expand theming support in the `COLORS` variable object ([#58097](https://github.com/WordPress/gutenberg/pull/58097)). ### Enhancements