-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try: Sticky header and pagination on Patterns page (#52663)
* Try: Sticky header on Patterns page * Sticky pagination * Match the pagination height to the save button * Revert pagination buttons size * Use strict grid size * Remove default values that were set by mistake
- Loading branch information
Showing
5 changed files
with
247 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 9 additions & 124 deletions
133
packages/edit-site/src/components/page-patterns/grid.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,137 +1,22 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
__experimentalHStack as HStack, | ||
__experimentalText as Text, | ||
Button, | ||
} from '@wordpress/components'; | ||
import { useRef, useState, useMemo } from '@wordpress/element'; | ||
import { __, _x, _n, sprintf } from '@wordpress/i18n'; | ||
import { useAsyncList } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import GridItem from './grid-item'; | ||
|
||
const PAGE_SIZE = 20; | ||
|
||
function Pagination( { currentPage, numPages, changePage, totalItems } ) { | ||
return ( | ||
<HStack | ||
expanded={ false } | ||
spacing={ 3 } | ||
className="edit-site-patterns__grid-pagination" | ||
> | ||
<Text variant="muted"> | ||
{ | ||
// translators: %s: Total number of patterns. | ||
sprintf( | ||
// translators: %s: Total number of patterns. | ||
_n( '%s item', '%s items', totalItems ), | ||
totalItems | ||
) | ||
} | ||
</Text> | ||
<HStack expanded={ false } spacing={ 1 }> | ||
<Button | ||
variant="tertiary" | ||
onClick={ () => changePage( 1 ) } | ||
disabled={ currentPage === 1 } | ||
aria-label={ __( 'First page' ) } | ||
> | ||
« | ||
</Button> | ||
<Button | ||
variant="tertiary" | ||
onClick={ () => changePage( currentPage - 1 ) } | ||
disabled={ currentPage === 1 } | ||
aria-label={ __( 'Previous page' ) } | ||
> | ||
‹ | ||
</Button> | ||
</HStack> | ||
<Text variant="muted"> | ||
{ sprintf( | ||
// translators: %1$s: Current page number, %2$s: Total number of pages. | ||
_x( '%1$s of %2$s', 'paging' ), | ||
currentPage, | ||
numPages | ||
) } | ||
</Text> | ||
<HStack expanded={ false } spacing={ 1 }> | ||
<Button | ||
variant="tertiary" | ||
onClick={ () => changePage( currentPage + 1 ) } | ||
disabled={ currentPage === numPages } | ||
aria-label={ __( 'Next page' ) } | ||
> | ||
› | ||
</Button> | ||
<Button | ||
variant="tertiary" | ||
onClick={ () => changePage( numPages ) } | ||
disabled={ currentPage === numPages } | ||
aria-label={ __( 'Last page' ) } | ||
> | ||
» | ||
</Button> | ||
</HStack> | ||
</HStack> | ||
); | ||
} | ||
|
||
export default function Grid( { categoryId, items, ...props } ) { | ||
const [ currentPage, setCurrentPage ] = useState( 1 ); | ||
const gridRef = useRef(); | ||
const totalItems = items.length; | ||
const pageIndex = currentPage - 1; | ||
|
||
const list = useMemo( | ||
() => | ||
items.slice( | ||
pageIndex * PAGE_SIZE, | ||
pageIndex * PAGE_SIZE + PAGE_SIZE | ||
), | ||
[ pageIndex, items ] | ||
); | ||
|
||
const asyncList = useAsyncList( list, { step: 10 } ); | ||
|
||
if ( ! list?.length ) { | ||
if ( ! items?.length ) { | ||
return null; | ||
} | ||
|
||
const numPages = Math.ceil( items.length / PAGE_SIZE ); | ||
const changePage = ( page ) => { | ||
const scrollContainer = document.querySelector( '.edit-site-patterns' ); | ||
scrollContainer?.scrollTo( 0, 0 ); | ||
|
||
setCurrentPage( page ); | ||
}; | ||
|
||
return ( | ||
<> | ||
<ul | ||
role="listbox" | ||
className="edit-site-patterns__grid" | ||
{ ...props } | ||
ref={ gridRef } | ||
> | ||
{ asyncList.map( ( item ) => ( | ||
<GridItem | ||
key={ item.name } | ||
item={ item } | ||
categoryId={ categoryId } | ||
/> | ||
) ) } | ||
</ul> | ||
{ numPages > 1 && ( | ||
<Pagination | ||
{ ...{ currentPage, numPages, changePage, totalItems } } | ||
<ul role="listbox" className="edit-site-patterns__grid" { ...props }> | ||
{ items.map( ( item ) => ( | ||
<GridItem | ||
key={ item.name } | ||
item={ item } | ||
categoryId={ categoryId } | ||
/> | ||
) } | ||
</> | ||
) ) } | ||
</ul> | ||
); | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/edit-site/src/components/page-patterns/pagination.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
__experimentalHStack as HStack, | ||
__experimentalText as Text, | ||
Button, | ||
} from '@wordpress/components'; | ||
import { __, _x, _n, sprintf } from '@wordpress/i18n'; | ||
|
||
export default function Pagination( { | ||
currentPage, | ||
numPages, | ||
changePage, | ||
totalItems, | ||
} ) { | ||
return ( | ||
<HStack | ||
expanded={ false } | ||
spacing={ 3 } | ||
justify="flex-start" | ||
className="edit-site-patterns__grid-pagination" | ||
> | ||
<Text variant="muted"> | ||
{ | ||
// translators: %s: Total number of patterns. | ||
sprintf( | ||
// translators: %s: Total number of patterns. | ||
_n( '%s item', '%s items', totalItems ), | ||
totalItems | ||
) | ||
} | ||
</Text> | ||
<HStack expanded={ false } spacing={ 1 }> | ||
<Button | ||
variant="tertiary" | ||
onClick={ () => changePage( 1 ) } | ||
disabled={ currentPage === 1 } | ||
aria-label={ __( 'First page' ) } | ||
> | ||
« | ||
</Button> | ||
<Button | ||
variant="tertiary" | ||
onClick={ () => changePage( currentPage - 1 ) } | ||
disabled={ currentPage === 1 } | ||
aria-label={ __( 'Previous page' ) } | ||
> | ||
‹ | ||
</Button> | ||
</HStack> | ||
<Text variant="muted"> | ||
{ sprintf( | ||
// translators: %1$s: Current page number, %2$s: Total number of pages. | ||
_x( '%1$s of %2$s', 'paging' ), | ||
currentPage, | ||
numPages | ||
) } | ||
</Text> | ||
<HStack expanded={ false } spacing={ 1 }> | ||
<Button | ||
variant="tertiary" | ||
onClick={ () => changePage( currentPage + 1 ) } | ||
disabled={ currentPage === numPages } | ||
aria-label={ __( 'Next page' ) } | ||
> | ||
› | ||
</Button> | ||
<Button | ||
variant="tertiary" | ||
onClick={ () => changePage( numPages ) } | ||
disabled={ currentPage === numPages } | ||
aria-label={ __( 'Last page' ) } | ||
> | ||
» | ||
</Button> | ||
</HStack> | ||
</HStack> | ||
); | ||
} |
Oops, something went wrong.