Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
106 changes: 7 additions & 99 deletions packages/block-library/src/tabs/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {
BlockContextProvider,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useSelect, useDispatch } from '@wordpress/data';
import { useMemo, useEffect, useRef } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { useMemo, useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import Controls from './controls';
import useTabMenuSync from './use-tab-menu-sync';

const TABS_TEMPLATE = [
[
Expand Down Expand Up @@ -74,13 +75,11 @@ function Edit( {
}
}, [] ); // eslint-disable-line react-hooks/exhaustive-deps

const { removeBlock } = useDispatch( blockEditorStore );

/**
* Construct a list of core/tab blocks, used to create tabs-list context.
* Also select menu items with their anchors for anchor-based deletion sync.
*/
const { tabs, menuItems } = useSelect(
const { tabs, menuItems, tabPanelClientId, tabsMenuClientId } = useSelect(
( select ) => {
const { getBlocks } = select( blockEditorStore );
const innerBlocks = getBlocks( clientId );
Expand Down Expand Up @@ -109,105 +108,14 @@ function Edit( {
anchor: b.attributes.anchor ?? '',
} ) )
: [],
tabPanelClientId: tabPanel?.clientId ?? null,
tabsMenuClientId: tabsMenu?.clientId ?? null,
};
},
[ clientId ]
);

/**
* Keep tabs and menu items in sync when either is deleted directly (e.g.
* via the Backspace key or List View).
*
* TODO: This effect only handles deletions. The two lists can get out of
* sync in other cases: if a user pastes a core/tab block into the tab-panel
* (or duplicates one), no corresponding tabs-menu-item is created; if a
* user drags and drops a tabs-menu-item, the tab panel is not copied with
* it. We should extend this effect to handle insertions, detecting when
* tabs.length > menuItems.length and inserting the missing menu
* item(s) at the correct index.
*/
const prevSyncStateRef = useRef( null );
useEffect( () => {
const currentTabs = tabs.map( ( tab ) => ( {
clientId: tab.clientId,
anchor: tab.attributes.anchor ?? '',
} ) );

if ( prevSyncStateRef.current === null ) {
prevSyncStateRef.current = {
tabs: currentTabs,
menuItems: [ ...menuItems ],
};
return;
}

const { tabs: prevTabs, menuItems: prevMenuItems } =
prevSyncStateRef.current;

const tabsRemoved = currentTabs.length < prevTabs.length;
const menuItemsRemoved = menuItems.length < prevMenuItems.length;

// Update snapshot to the current state.
// Snapshot is updated eagerly; post-removal mutations keep it consistent
// so the next effect invocation sees a stable baseline.
prevSyncStateRef.current = {
tabs: currentTabs,
menuItems: [ ...menuItems ],
};

// Lists are in sync, nothing changed, or toolbar already removed both.
if (
( ! tabsRemoved && ! menuItemsRemoved ) ||
( tabsRemoved && menuItemsRemoved )
) {
return;
}

const currentTabIds = new Set( currentTabs.map( ( t ) => t.clientId ) );
const currentMenuItemIds = new Set(
menuItems.map( ( m ) => m.clientId )
);

if ( tabsRemoved ) {
prevTabs.forEach( ( prevTab ) => {
if ( currentTabIds.has( prevTab.clientId ) ) {
return;
}
const expectedMenuAnchor = prevTab.anchor
? `${ prevTab.anchor }-button`
: null;
const menuItemToRemove = expectedMenuAnchor
? menuItems.find( ( m ) => m.anchor === expectedMenuAnchor )
: null;
if ( menuItemToRemove ) {
removeBlock( menuItemToRemove.clientId, false );
prevSyncStateRef.current.menuItems =
prevSyncStateRef.current.menuItems.filter(
( m ) => m.clientId !== menuItemToRemove.clientId
);
}
} );
} else {
prevMenuItems.forEach( ( prevItem ) => {
if ( currentMenuItemIds.has( prevItem.clientId ) ) {
return;
}
const expectedTabAnchor =
prevItem.anchor?.replace( /-button$/, '' ) ?? '';
const tabToRemove = tabs.find(
( tab ) =>
( tab.attributes.anchor ?? '' ) === expectedTabAnchor
);
if ( tabToRemove ) {
removeBlock( tabToRemove.clientId, false );
prevSyncStateRef.current.tabs =
prevSyncStateRef.current.tabs.filter(
( t ) => t.clientId !== tabToRemove.clientId
);
}
} );
}
}, [ tabs, menuItems, removeBlock ] );
useTabMenuSync( { tabs, menuItems, tabPanelClientId, tabsMenuClientId } );

/**
* Memoize context value to prevent unnecessary re-renders.
Expand Down
Loading
Loading