Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/components/site-content-tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TabPanel } from '@wordpress/components';
import { useI18n } from '@wordpress/react-i18n';
import { useEffect, useRef, useState } from 'react';
import { ContentTabAssistant } from 'src/components/content-tab-assistant';
import { ContentTabImportExport } from 'src/components/content-tab-import-export';
import { ContentTabOverview } from 'src/components/content-tab-overview';
Expand All @@ -21,6 +22,24 @@ export function SiteContentTabs() {
const { tabs, selectedTab, setSelectedTab } = useContentTabs();
const { __ } = useI18n();

// Remount: Avoid focus loss on user tab changes (no remount),
// but remount on programmatic changes and site switches so initial tab/content state resets.
const [ keyCounter, setKeyCounter ] = useState( 0 );
const lastChangeWasUser = useRef( false );
const isFirstRender = useRef( true );

useEffect( () => {
if ( isFirstRender.current ) {
isFirstRender.current = false;
return;
}
if ( lastChangeWasUser.current ) {
lastChangeWasUser.current = false;
return;
}
setKeyCounter( ( k ) => k + 1 );
}, [ selectedTab ] );

if ( ! localSites.length ) {
return <EmptyStudio />;
}
Expand All @@ -44,9 +63,13 @@ export function SiteContentTabs() {
className={ `mt-6 h-full flex flex-col overflow-hidden ${ MIN_WIDTH_CLASS_TO_MEASURE }` }
tabs={ tabs }
orientation="horizontal"
onSelect={ ( tabName ) => setSelectedTab( tabName as TabName ) }
onSelect={ ( tabName ) => {
// Mark this as a user-initiated change so we don't remount
lastChangeWasUser.current = true;
setSelectedTab( tabName as TabName );
} }
initialTabName={ selectedTab }
key={ selectedTab + selectedSite.id }
key={ `${ selectedSite.id }-${ keyCounter }` }
>
{ ( { name } ) => (
<div
Expand Down