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

[Patterns]: Reorder pattern categories #47760

Merged
merged 1 commit into from
Feb 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ import BlockPatternList from '../block-patterns-list';
import PatternsExplorerModal from './block-patterns-explorer/explorer';
import MobileTabNavigation from './mobile-tab-navigation';

// Preffered order of pattern categories. Any other categories should
// be at the bottom without any re-ordering.
const patternCategoriesOrder = [
'featured',
'posts',
'text',
'gallery',
'call-to-action',
'banner',
'header',
'footer',
];

function usePatternsCategories( rootClientId ) {
const [ allPatterns, allCategories ] = usePatternsState(
undefined,
Expand Down Expand Up @@ -56,17 +69,27 @@ function usePatternsCategories( rootClientId ) {
)
)
.sort( ( { name: currentName }, { name: nextName } ) => {
// The pattern categories should be ordered as follows:
// 1. The categories from `patternCategoriesOrder` in that specific order should be at the top.
// 2. The rest categories should be at the bottom without any re-ordering.
if (
! [ currentName, nextName ].some( ( categoryName ) =>
[ 'featured', 'text' ].includes( categoryName )
patternCategoriesOrder.includes( categoryName )
)
) {
return 0;
}
// Move `featured` category to the top and `text` to the bottom.
return currentName === 'featured' || nextName === 'text'
? -1
: 1;
if (
[ currentName, nextName ].every( ( categoryName ) =>
patternCategoriesOrder.includes( categoryName )
)
) {
return (
patternCategoriesOrder.indexOf( currentName ) -
patternCategoriesOrder.indexOf( nextName )
);
}
return patternCategoriesOrder.includes( currentName ) ? -1 : 1;
} );

if (
Expand Down