Skip to content

Commit 0af876e

Browse files
Patterns: Display all custom template part areas in sidebar nav
1 parent f6efbc2 commit 0af876e

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

packages/edit-site/src/components/page-patterns/use-patterns.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,8 @@ const templatePartToPattern = ( templatePart ) => ( {
3838
templatePart,
3939
} );
4040

41-
const templatePartCategories = [ 'header', 'footer', 'sidebar' ];
42-
const templatePartHasCategory = ( item, category ) => {
43-
if ( category === 'uncategorized' ) {
44-
return ! templatePartCategories.includes( item.templatePart.area );
45-
}
46-
47-
return item.templatePart.area === category;
48-
};
41+
const templatePartHasCategory = ( item, category ) =>
42+
item.templatePart.area === category;
4943

5044
const useTemplatePartsAsPatterns = (
5145
categoryId,

packages/edit-site/src/components/sidebar-navigation-screen-patterns/index.js

+6-14
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ import usePatternCategories from './use-pattern-categories';
2929
import useMyPatterns from './use-my-patterns';
3030
import useTemplatePartAreas from './use-template-part-areas';
3131

32-
const templatePartAreaLabels = {
33-
header: __( 'Headers' ),
34-
footer: __( 'Footers' ),
35-
sidebar: __( 'Sidebar' ),
36-
uncategorized: __( 'Uncategorized' ),
37-
};
38-
3932
export default function SidebarNavigationScreenPatterns() {
4033
const isMobileViewport = useViewportMatch( 'medium', '<' );
4134
const { categoryType, categoryId } = getQueryArgs( window.location.href );
@@ -111,18 +104,17 @@ export default function SidebarNavigationScreenPatterns() {
111104
{ hasTemplateParts && (
112105
<ItemGroup className="edit-site-sidebar-navigation-screen-patterns__group">
113106
{ Object.entries( templatePartAreas ).map(
114-
( [ area, parts ] ) => (
107+
( [
108+
area,
109+
{ label, templateParts },
110+
] ) => (
115111
<CategoryItem
116112
key={ area }
117-
count={ parts.length }
113+
count={ templateParts?.length }
118114
icon={ getTemplatePartIcon(
119115
area
120116
) }
121-
label={
122-
templatePartAreaLabels[
123-
area
124-
]
125-
}
117+
label={ label }
126118
id={ area }
127119
type="wp_template_part"
128120
isActive={

packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-template-part-areas.js

+31-9
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,41 @@
22
* WordPress dependencies
33
*/
44
import { useEntityRecords } from '@wordpress/core-data';
5+
import { useSelect } from '@wordpress/data';
6+
import { store as editorStore } from '@wordpress/editor';
57

6-
const getTemplatePartAreas = ( items ) => {
8+
const useTemplatePartsGroupedByArea = ( items ) => {
79
const allItems = items || [];
810

9-
const groupedByArea = allItems.reduce(
10-
( accumulator, item ) => {
11-
const key = accumulator[ item.area ] ? item.area : 'uncategorized';
12-
accumulator[ key ].push( item );
13-
return accumulator;
14-
},
15-
{ header: [], footer: [], sidebar: [], uncategorized: [] }
11+
const templatePartAreas = useSelect(
12+
( select ) =>
13+
select( editorStore ).__experimentalGetDefaultTemplatePartAreas(),
14+
[]
1615
);
1716

17+
// Create map of template areas ensuring that default areas are displayed before
18+
// any custom registered template part areas.
19+
const knownAreas = {
20+
header: {},
21+
footer: {},
22+
sidebar: {},
23+
uncategorized: {},
24+
};
25+
26+
templatePartAreas.forEach(
27+
( templatePartArea ) =>
28+
( knownAreas[ templatePartArea.area ] = {
29+
...templatePartArea,
30+
templateParts: [],
31+
} )
32+
);
33+
34+
const groupedByArea = allItems.reduce( ( accumulator, item ) => {
35+
const key = accumulator[ item.area ] ? item.area : 'uncategorized';
36+
accumulator[ key ].templateParts.push( item );
37+
return accumulator;
38+
}, knownAreas );
39+
1840
return groupedByArea;
1941
};
2042

@@ -28,6 +50,6 @@ export default function useTemplatePartAreas() {
2850
return {
2951
hasTemplateParts: templateParts ? !! templateParts.length : false,
3052
isLoading,
31-
templatePartAreas: getTemplatePartAreas( templateParts ),
53+
templatePartAreas: useTemplatePartsGroupedByArea( templateParts ),
3254
};
3355
}

0 commit comments

Comments
 (0)