Skip to content

Commit

Permalink
Group templates in sidebar list (#57711)
Browse files Browse the repository at this point in the history
* Group templates in sidebar list

* address feedback

* Section heading styling

---------

Co-authored-by: James Koster <james@jameskoster.co.uk>
  • Loading branch information
ntsekouras and jameskoster authored Jan 11, 2024
1 parent 8599e15 commit 47026ab
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import {
__experimentalItemGroup as ItemGroup,
__experimentalItem as Item,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useEntityRecords } from '@wordpress/core-data';
Expand All @@ -30,20 +31,11 @@ const TemplateItem = ( { postType, postId, ...props } ) => {

export default function SidebarNavigationScreenTemplates() {
const isMobileViewport = useViewportMatch( 'medium', '<' );

const { records: templates, isResolving: isLoading } = useEntityRecords(
'postType',
TEMPLATE_POST_TYPE,
{
per_page: -1,
}
);

const sortedTemplates = templates ? [ ...templates ] : [];
sortedTemplates.sort( ( a, b ) =>
a.title.rendered.localeCompare( b.title.rendered )
{ per_page: -1 }
);

const browseAllLink = useLink( { path: '/wp_template/all' } );
const canCreate = ! isMobileViewport;
return (
Expand All @@ -66,24 +58,7 @@ export default function SidebarNavigationScreenTemplates() {
<>
{ isLoading && __( 'Loading templates…' ) }
{ ! isLoading && (
<ItemGroup>
{ ! templates?.length && (
<Item>{ __( 'No templates found' ) }</Item>
) }
{ sortedTemplates.map( ( template ) => (
<TemplateItem
postType={ TEMPLATE_POST_TYPE }
postId={ template.id }
key={ template.id }
withChevron
>
{ decodeEntities(
template.title?.rendered ||
template.slug
) }
</TemplateItem>
) ) }
</ItemGroup>
<SidebarTemplatesList templates={ templates } />
) }
</>
}
Expand All @@ -97,3 +72,85 @@ export default function SidebarNavigationScreenTemplates() {
/>
);
}

function TemplatesGroup( { title, templates } ) {
return (
<ItemGroup>
{ !! title && (
<Item className="edit-site-sidebar-navigation-screen-templates__templates-group-title">
{ title }
</Item>
) }
{ templates.map( ( template ) => (
<TemplateItem
postType={ TEMPLATE_POST_TYPE }
postId={ template.id }
key={ template.id }
withChevron
>
{ decodeEntities(
template.title?.rendered || template.slug
) }
</TemplateItem>
) ) }
</ItemGroup>
);
}
function SidebarTemplatesList( { templates } ) {
if ( ! templates?.length ) {
return (
<ItemGroup>
<Item>{ __( 'No templates found' ) }</Item>
</ItemGroup>
);
}
const sortedTemplates = templates ? [ ...templates ] : [];
sortedTemplates.sort( ( a, b ) =>
a.title.rendered.localeCompare( b.title.rendered )
);
const { hierarchyTemplates, customTemplates, ...plugins } =
sortedTemplates.reduce(
( accumulator, template ) => {
const {
original_source: originalSource,
author_text: authorText,
} = template;
if ( originalSource === 'plugin' ) {
if ( ! accumulator[ authorText ] ) {
accumulator[ authorText ] = [];
}
accumulator[ authorText ].push( template );
} else if ( template.is_custom ) {
accumulator.customTemplates.push( template );
} else {
accumulator.hierarchyTemplates.push( template );
}
return accumulator;
},
{ hierarchyTemplates: [], customTemplates: [] }
);
return (
<VStack spacing={ 3 }>
{ !! hierarchyTemplates.length && (
<TemplatesGroup templates={ hierarchyTemplates } />
) }
{ !! customTemplates.length && (
<TemplatesGroup
title={ __( 'Custom' ) }
templates={ customTemplates }
/>
) }
{ Object.entries( plugins ).map(
( [ plugin, pluginTemplates ] ) => {
return (
<TemplatesGroup
key={ plugin }
title={ plugin }
templates={ pluginTemplates }
/>
);
}
) }
</VStack>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.edit-site-sidebar-navigation-screen-templates__templates-group-title.components-item {
text-transform: uppercase;
color: $gray-200;
// 6px right padding to align with + button
padding: $grid-unit-30 6px $grid-unit-20 $grid-unit-20;
border-top: 1px solid $gray-800;
font-size: 11px;
font-weight: 500;
}
1 change: 1 addition & 0 deletions packages/edit-site/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
@import "./components/sidebar-navigation-screen-details-footer/style.scss";
@import "./components/sidebar-navigation-screen-navigation-menu/style.scss";
@import "./components/sidebar-navigation-screen-page/style.scss";
@import "./components/sidebar-navigation-screen-templates/style.scss";
@import "components/sidebar-navigation-screen-details-panel/style.scss";
@import "./components/sidebar-navigation-screen-pattern/style.scss";
@import "./components/sidebar-navigation-screen-patterns/style.scss";
Expand Down

0 comments on commit 47026ab

Please sign in to comment.