Skip to content

Commit

Permalink
implement feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
chad1008 committed Jan 18, 2024
1 parent b87debe commit 908f0f7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
20 changes: 9 additions & 11 deletions packages/components/src/tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,20 @@ function Tabs( {
const currentItem = items.find( ( item ) => item.id === selectedId );
const activeElement = currentItem?.element?.ownerDocument.activeElement;

const tabsHasFocus =
activeElement &&
items.some( ( item ) => {
return activeElement === item.element;
} );
if (
! activeElement ||
! items.some( ( item ) => activeElement === item.element )
) {
return; // Return early if no tabs are focused.
}

const previousSelectedTabHadFocus =
typeof previousSelectedId === 'string' &&
previousSelectedId === activeElement?.id;

// If the previously selected tab had focus when the selection changed,
// move focus to the newly selected tab.
if (
tabsHasFocus &&
previousSelectedTabHadFocus &&
selectedId !== activeElement.id
) {
if ( previousSelectedTabHadFocus && selectedId !== activeElement.id ) {
move( selectedId );
return;
}
Expand All @@ -192,7 +190,7 @@ function Tabs( {
// selection changed, update the activeId to the currently focused tab.
// The activeId controls how arrow key navigation behaves. Keeping them
// in sync avoids confusion when navigating tabs with the keyboard.
if ( tabsHasFocus && ! previousSelectedTabHadFocus ) {
if ( ! previousSelectedTabHadFocus ) {
setActiveId( activeElement.id );
}
}, [
Expand Down
19 changes: 16 additions & 3 deletions packages/components/src/tabs/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,29 @@ const ControlledModeTemplate: StoryFn< typeof Tabs > = ( props ) => {
<DropdownMenu
controls={ [
{
onClick: () => setSelectedTabId( 'tab1' ),
onClick: () =>
setTimeout(
() => setSelectedTabId( 'tab1' ),
3000
),
title: 'Tab 1',
isActive: selectedTabId === 'tab1',
},
{
onClick: () => setSelectedTabId( 'tab2' ),
onClick: () =>
setTimeout(
() => setSelectedTabId( 'tab2' ),
3000
),
title: 'Tab 2',
isActive: selectedTabId === 'tab2',
},
{
onClick: () => setSelectedTabId( 'tab3' ),
onClick: () =>
setTimeout(
() => setSelectedTabId( 'tab3' ),
3000
),
title: 'Tab 3',
isActive: selectedTabId === 'tab3',
},
Expand All @@ -299,6 +311,7 @@ const ControlledModeTemplate: StoryFn< typeof Tabs > = ( props ) => {
export const ControlledMode = ControlledModeTemplate.bind( {} );
ControlledMode.args = {
selectedTabId: 'tab3',
selectOnMove: false,
};

const TabBecomesDisabledTemplate: StoryFn< typeof Tabs > = ( props ) => {
Expand Down

0 comments on commit 908f0f7

Please sign in to comment.