Block Patterns tab: Handle receiving the category name as a string#68806
Block Patterns tab: Handle receiving the category name as a string#68806carolinan wants to merge 3 commits into
Conversation
|
Size Change: +35 B (0%) Total Size: 1.84 MB
ℹ️ View Unchanged
|
|
Even if the modal is used instead, there is still a problem with passing any category to the pattern preview in an unexpected format. |
| const [ selectedPatternCategory, setSelectedPatternCategory ] = useState( | ||
| __experimentalInitialCategory | ||
| ); |
There was a problem hiding this comment.
The above logic can be inlined here.
diff --git packages/block-editor/src/components/inserter/menu.js packages/block-editor/src/components/inserter/menu.js
index f1e387c5a59..caec6adef78 100644
--- packages/block-editor/src/components/inserter/menu.js
+++ packages/block-editor/src/components/inserter/menu.js
@@ -68,9 +68,9 @@ function InserterMenu(
const [ filterValue, setFilterValue, delayedFilterValue ] =
useDebouncedInput( __experimentalFilterValue );
const [ hoveredItem, setHoveredItem ] = useState( null );
- const [ selectedPatternCategory, setSelectedPatternCategory ] = useState(
- __experimentalInitialCategory
- );
+ const [ selectedPatternCategory, setSelectedPatternCategory ] = useState( {
+ name: __experimentalInitialCategory,
+ } );
const [ patternFilter, setPatternFilter ] = useState( 'all' );
const [ selectedMediaCategory, setSelectedMediaCategory ] =
useState( null );There was a problem hiding this comment.
I am not sure I understand. Should the check for the string format be removed? Right now in Gutenberg, the requesting components passes a string, or: I only found two places where a category name is passed at all. But the expected received format is still meant to be an object, no? So it needs to handle both string and object.
There was a problem hiding this comment.
Currently, only selectedPatternCategory and selectedMediaCategory values are expected to be objects with the name property. The remaining code can use string value.
Inlining object as initial state value can simplify things, IMO.
const [ selectedPatternCategory, setSelectedPatternCategory ] = useState( {
name: __experimentalInitialCategory,
} );There was a problem hiding this comment.
But it assumes __experimentalInitialCategory is always a string? If it is already an object it will be placed inside name
There was a problem hiding this comment.
If it is truly never and object, then shouldn't the block patterns tab and the preview be updated to use strings?
There was a problem hiding this comment.
Correct. The __experimentalInitialCategory should always be a string, but pattern tabs will use an object internally because of how the Tabs component works.
P.S. This is probably unimportant now, considering we will revert the feature - #68736 (comment).
Edit: But we'll have to deal with this issue anyway, so let's keep this open.
|
The missing fallback for invalid categories - I think this predates changes from #66836 and was only highlighted. While I would like to see it resolved, it isn't a blocker for this PR. |
|
@carolinan, should we remove this from the WP 6.8 project board in favor of #69081? |
Ok |

What?
This started as a bug fix related to the page creation flow, where the
starter contentpattern category was meant to show when you create a new page.But it turned out the problem was not only with the patterns that are meant to show on page creation, but with passing any category name to the block patterns tab as a string.
That is why there is no separate issue opened for this PR.
Partial for #68736
Alternative to #68801
In the pattern category preview, the expected format of the category is an object with a
nameandlabelparameter.However, in the components that pass the category to the previewer, the category is a string.
The
ZoomOutModeInserterButtonpasses the value "all" -and this is not recognized as a category by the previewer.The previewer is expecting the name parameter with the value "allPatterns":
https://github.com/WordPress/gutenberg/blob/trunk/packages/block-editor/src/components/inserter/block-patterns-tab/utils.js#L18
The
StartPageOptionspasses the value "core/starter-content" and the previewer is expecting thenameparameter with this value.So what this PR does is:
name, so that the previewer receives the correct identifier for the categoryTesting Instructions
Activate a block theme that has starter content patterns, for example Twenty Twenty-Five.
Create a new page and observe which pattern category that is shown in the inserter sidebar.
Edit a template and observe which pattern category that is shown when adding a pattern in zoom out mode.