Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components: replace TabPanel with Tabs in the Block Inserter #56918

Merged
merged 6 commits into from Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 9 additions & 14 deletions packages/block-editor/src/components/inserter/menu.js
Expand Up @@ -191,17 +191,13 @@ function InserterMenu(
]
);

const getCurrentTab = useCallback(
( tab ) => {
if ( tab.name === 'blocks' ) {
return blocksTab;
} else if ( tab.name === 'patterns' ) {
return patternsTab;
} else if ( tab.name === 'media' ) {
return mediaTab;
}
},
[ blocksTab, patternsTab, mediaTab ]
const inserterTabsContents = useMemo(
() => ( {
blocks: blocksTab,
patterns: patternsTab,
media: mediaTab,
} ),
[ blocksTab, mediaTab, patternsTab ]
);

const searchRef = useRef();
Expand Down Expand Up @@ -275,9 +271,8 @@ function InserterMenu(
showMedia={ showMedia }
prioritizePatterns={ prioritizePatterns }
onSelect={ handleSetSelectedTab }
>
{ getCurrentTab }
</InserterTabs>
tabsContents={ inserterTabsContents }
/>
) }
{ ! delayedFilterValue && ! showAsTabs && (
<div className="block-editor-inserter__no-tab-container">
Expand Down
8 changes: 4 additions & 4 deletions packages/block-editor/src/components/inserter/style.scss
Expand Up @@ -61,7 +61,7 @@ $block-inserter-tabs-height: 44px;
.block-editor-inserter__popover .block-editor-inserter__menu {
margin: -$grid-unit-15;

.block-editor-inserter__tabs .components-tab-panel__tabs {
.block-editor-inserter__tabs div[role="tablist"] {
top: $grid-unit-10 + $grid-unit-20 + $grid-unit-60 - $grid-unit-15;
}

Expand Down Expand Up @@ -118,10 +118,10 @@ $block-inserter-tabs-height: 44px;
flex-direction: column;
overflow: hidden;

.components-tab-panel__tabs {
div[role="tablist"] {
border-bottom: $border-width solid $gray-300;

.components-tab-panel__tabs-item {
button[role="tab"] {
flex-grow: 1;
margin-bottom: -$border-width;
&[id$="reusable"] {
Expand All @@ -133,7 +133,7 @@ $block-inserter-tabs-height: 44px;
}
}

.components-tab-panel__tab-content {
div[role="tabpanel"] {
display: flex;
flex-grow: 1;
flex-direction: column;
Expand Down
38 changes: 29 additions & 9 deletions packages/block-editor/src/components/inserter/tabs.js
Expand Up @@ -2,9 +2,16 @@
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { TabPanel } from '@wordpress/components';
import { privateApis as componentsPrivateApis } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';

const { Tabs } = unlock( componentsPrivateApis );

const blocksTab = {
name: 'blocks',
/* translators: Blocks tab title in the block inserter. */
Expand All @@ -23,11 +30,11 @@ const mediaTab = {
};

function InserterTabs( {
children,
showPatterns = false,
showMedia = false,
onSelect,
prioritizePatterns,
tabsContents,
} ) {
const tabs = useMemo( () => {
const tempTabs = [];
Expand All @@ -45,13 +52,26 @@ function InserterTabs( {
}, [ prioritizePatterns, showPatterns, showMedia ] );

return (
<TabPanel
className="block-editor-inserter__tabs"
tabs={ tabs }
onSelect={ onSelect }
>
{ children }
</TabPanel>
<div className="block-editor-inserter__tabs">
<Tabs onSelect={ onSelect }>
<Tabs.TabList>
{ tabs.map( ( tab ) => (
<Tabs.Tab key={ tab.name } tabId={ tab.name }>
{ tab.title }
</Tabs.Tab>
) ) }
</Tabs.TabList>
{ tabs.map( ( tab ) => (
<Tabs.TabPanel
key={ tab.name }
tabId={ tab.name }
focusable={ false }
>
{ tabsContents[ tab.name ] }
</Tabs.TabPanel>
) ) }
</Tabs>
</div>
);
}

Expand Down
5 changes: 4 additions & 1 deletion packages/e2e-test-utils/src/inserter.js
Expand Up @@ -86,7 +86,10 @@ export async function selectGlobalInserterTab( label ) {
}

const activeTab = await page.waitForSelector(
'.block-editor-inserter__tabs button.is-active'
// Targeting a class name is necessary here, because there are likely
// two implementations of the `Tabs` component visible to this test, and
// we want to confirm that it's waiting for the correct one.
'.block-editor-inserter__tabs [role="tab"][aria-selected="true"]'
);

const activeTabLabel = await page.evaluate(
Expand Down