Skip to content

Commit

Permalink
Hide inserter pattern/media panel when there are no categories (#66246)
Browse files Browse the repository at this point in the history
* Hide inserter media panel when there are no categories

* Fix unstable reference

---------

Co-authored-by: Kai Hao <[email protected]>
  • Loading branch information
talldan and kevin940726 authored Oct 21, 2024
1 parent 1899810 commit 6ed62de
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { useState, useEffect } from '@wordpress/element';
import { useViewportMatch } from '@wordpress/compose';
import { Button, Spinner } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
Expand All @@ -24,6 +24,7 @@ function BlockPatternsTab( {
selectedCategory,
onInsert,
rootClientId,
setHasCategories,
children,
} ) {
const [ showPatternsExplorer, setShowPatternsExplorer ] = useState( false );
Expand All @@ -37,6 +38,10 @@ function BlockPatternsTab( {
[]
);

useEffect( () => {
setHasCategories( !! categories.length );
}, [ categories, setHasCategories ] );

if ( isResolvingPatterns ) {
return (
<div className="block-editor-inserter__patterns-loading">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { __ } from '@wordpress/i18n';
import { useViewportMatch } from '@wordpress/compose';
import { Button } from '@wordpress/components';
import { useCallback, useMemo } from '@wordpress/element';
import { useCallback, useEffect, useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -24,6 +24,7 @@ function MediaTab( {
rootClientId,
selectedCategory,
onSelectCategory,
setHasCategories,
onInsert,
children,
} ) {
Expand All @@ -49,6 +50,10 @@ function MediaTab( {
[ mediaCategories ]
);

useEffect( () => {
setHasCategories( !! categories.length );
}, [ categories, setHasCategories ] );

if ( ! categories.length ) {
return <InserterNoResults />;
}
Expand Down
8 changes: 7 additions & 1 deletion packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ function InserterMenu(
const [ patternFilter, setPatternFilter ] = useState( 'all' );
const [ selectedMediaCategory, setSelectedMediaCategory ] =
useState( null );

const [ hasCategories, setHasCategories ] = useState( true );
function getInitialTab() {
if ( __experimentalInitialTab ) {
return __experimentalInitialTab;
Expand Down Expand Up @@ -146,10 +148,12 @@ function InserterMenu(

const showPatternPanel =
selectedTab === 'patterns' &&
hasCategories &&
! delayedFilterValue &&
!! selectedPatternCategory;

const showMediaPanel = selectedTab === 'media' && !! selectedMediaCategory;
const showMediaPanel =
selectedTab === 'media' && !! selectedMediaCategory && hasCategories;

const inserterSearch = useMemo( () => {
if ( selectedTab === 'media' ) {
Expand Down Expand Up @@ -242,6 +246,7 @@ function InserterMenu(
onInsert={ onInsertPattern }
onSelectCategory={ onClickPatternCategory }
selectedCategory={ selectedPatternCategory }
setHasCategories={ setHasCategories }
>
{ showPatternPanel && (
<PatternCategoryPreviews
Expand Down Expand Up @@ -270,6 +275,7 @@ function InserterMenu(
selectedCategory={ selectedMediaCategory }
onSelectCategory={ setSelectedMediaCategory }
onInsert={ onInsert }
setHasCategories={ setHasCategories }
>
{ showMediaPanel && (
<MediaCategoryPanel
Expand Down

0 comments on commit 6ed62de

Please sign in to comment.