Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/global-styles-ui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Element screens: add an "Overridden by" list of the block types whose own styles override the element, linking to each block's screen ([#84671](https://github.com/WordPress/gutenberg/pull/84671)).
- Show icons for elements in the Blocks & Elements list, and rename Input, Select and Button (element) to Form input, Form select and Form button ([#85363](https://github.com/WordPress/gutenberg/pull/85363)).
- Font Library: Paginate installed fonts in the Library tab, like in the Install tab ([#86063](https://github.com/WordPress/gutenberg/pull/86063)).

## 2.0.0 (2026-10-20)

Expand Down
116 changes: 10 additions & 106 deletions packages/global-styles-ui/src/font-library/font-collection.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
useContext,
useEffect,
useState,
useMemo,
createInterpolateElement,
} from '@wordpress/element';
import { useContext, useEffect, useState, useMemo } from '@wordpress/element';
import {
__experimentalSpacer as Spacer,
__experimentalText as WCText,
Expand All @@ -23,17 +17,10 @@ import {
// eslint-disable-next-line @wordpress/use-recommended-components -- Intentional early adoption of the new Menu, pending WordPress/gutenberg#76135.
Menu,
Progress,
Select,
} from '@wordpress/ui';
import { debounce } from '@wordpress/compose';
import { sprintf, __, _x, isRTL } from '@wordpress/i18n';
import {
moreVertical,
next,
previous,
chevronLeft,
chevronRight,
} from '@wordpress/icons';
import { __, _x, isRTL } from '@wordpress/i18n';
import { moreVertical, chevronLeft, chevronRight } from '@wordpress/icons';
import { useEntityRecord } from '@wordpress/core-data';
import type {
FontCollection as FontCollectionType,
Expand All @@ -43,6 +30,7 @@ import type {
} from '@wordpress/core-data';
import { FontLibraryContext } from './context';
import FontCard from './font-card';
import Pagination, { getFontsPerPage } from './pagination';
import filterFonts from './utils/filter-fonts';
import { toggleFont } from './utils/toggleFont';
import {
Expand All @@ -61,7 +49,6 @@ const DEFAULT_CATEGORY = {
};

const LOCAL_STORAGE_ITEM = 'wp-font-library-google-fonts-permission';
const MIN_WINDOW_HEIGHT = 500;

function FontCollection( { slug }: { slug: string } ) {
const requiresPermission = slug === 'google-fonts';
Expand Down Expand Up @@ -136,10 +123,7 @@ function FontCollection( { slug }: { slug: string } ) {
[ collectionFonts, filters ]
);

// NOTE: The height of the font library modal unavailable to use for rendering font family items is roughly 417px
// The height of each font family item is 61px.
const windowHeight = Math.max( window.innerHeight, MIN_WINDOW_HEIGHT );
const pageSize = Math.floor( ( windowHeight - 417 ) / 61 );
const pageSize = getFontsPerPage();
const totalPages = Math.ceil( fonts.length / pageSize );
const itemsStart = ( page - 1 ) * pageSize;
const itemsLimit = page * pageSize;
Expand Down Expand Up @@ -546,91 +530,11 @@ function FontCollection( { slug }: { slug: string } ) {
justify="end"
spacing={ 6 }
>
<HStack
justify="flex-start"
expanded={ false }
spacing={ 1 }
className="font-library__page-selection"
>
{ createInterpolateElement(
sprintf(
// translators: 1: Current page number, 2: Total number of pages.
_x(
'<div>Page</div>%1$s<div>of %2$d</div>',
'paging'
),
'<CurrentPage />',
totalPages
),
{
div: <div aria-hidden />,
// @ts-expect-error — Tag injected via sprintf argument, not visible in format string.
CurrentPage: (
<Select.Root
value={ page.toString() }
onValueChange={ ( newPage ) => {
if ( newPage !== null ) {
setPage(
parseInt( newPage )
);
}
} }
>
<Select.Trigger
className="font-library__page-selection-trigger"
size="small"
variant="minimal"
aria-label={ __(
'Current page'
) }
/>
<Select.Popup width="content">
{ [
...Array( totalPages ),
].map( ( e, i ) => {
const value = (
i + 1
).toString();
return (
<Select.Item
key={ value }
value={ value }
size="small"
>
<Select.ItemLabel>
{ value }
</Select.ItemLabel>
</Select.Item>
);
} ) }
</Select.Popup>
</Select.Root>
),
}
) }
</HStack>
<HStack expanded={ false } spacing={ 1 }>
<Button
onClick={ () => setPage( page - 1 ) }
disabled={ page === 1 }
accessibleWhenDisabled
label={ __( 'Previous page' ) }
icon={ isRTL() ? next : previous }
showTooltip
size="compact"
tooltipPosition="top"
/>
<Button
onClick={ () => setPage( page + 1 ) }
disabled={ page === totalPages }
accessibleWhenDisabled
label={ __( 'Next page' ) }
icon={ isRTL() ? previous : next }
showTooltip
size="compact"
tooltipPosition="top"
/>
</HStack>
<Pagination
page={ page }
totalPages={ totalPages }
onChange={ setPage }
/>
</HStack>
) }
</>
Expand Down
55 changes: 51 additions & 4 deletions packages/global-styles-ui/src/font-library/installed-fonts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
import type { FontFamily } from '@wordpress/core-data';
import { FontLibraryContext } from './context';
import FontCard from './font-card';
import Pagination, { getFontsPerPage } from './pagination';
import LibraryFontVariant from './library-font-variant';
import { sortFontFaces } from './utils/sort-font-faces';
import {
Expand Down Expand Up @@ -130,6 +131,27 @@ function InstalledFonts() {
)
: [];

// Theme and custom fonts are paginated as one list, theme fonts first.
const [ page, setPage ] = useState( 1 );
const pageSize = getFontsPerPage();
const totalPages = Math.ceil(
( baseThemeFonts.length + baseCustomFonts.length ) / pageSize
);
const itemsStart = ( page - 1 ) * pageSize;
const itemsLimit = page * pageSize;
const pageThemeFonts = baseThemeFonts.slice( itemsStart, itemsLimit );
const pageCustomFonts = baseCustomFonts.slice(
Math.max( itemsStart - baseThemeFonts.length, 0 ),
Math.max( itemsLimit - baseThemeFonts.length, 0 )
);

// Go back a page when the last font of the last page is deleted.
useEffect( () => {
if ( totalPages > 0 && page > totalPages ) {
setPage( totalPages );
}
}, [ page, totalPages ] );

const customFontFamilyId =
libraryFontSelected?.source === 'custom' && libraryFontSelected?.id;

Expand Down Expand Up @@ -304,7 +326,7 @@ function InstalledFonts() {
{ __( 'No fonts installed.' ) }
</WCText>
) }
{ baseThemeFonts.length > 0 && (
{ pageThemeFonts.length > 0 && (
<VStack>
<h2 className="font-library__fonts-title">
{
Expand All @@ -321,7 +343,7 @@ function InstalledFonts() {
role="list"
className="font-library__fonts-list"
>
{ baseThemeFonts.map( ( font ) => (
{ pageThemeFonts.map( ( font ) => (
<li
key={ font.slug }
className="font-library__fonts-list-item"
Expand Down Expand Up @@ -349,7 +371,7 @@ function InstalledFonts() {
{ /* eslint-enable jsx-a11y/no-redundant-roles */ }
</VStack>
) }
{ baseCustomFonts.length > 0 && (
{ pageCustomFonts.length > 0 && (
<VStack>
<h2 className="font-library__fonts-title">
{
Expand All @@ -366,7 +388,7 @@ function InstalledFonts() {
role="list"
className="font-library__fonts-list"
>
{ baseCustomFonts.map( ( font ) => (
{ pageCustomFonts.map( ( font ) => (
<li
key={ font.slug }
className="font-library__fonts-list-item"
Expand Down Expand Up @@ -418,6 +440,24 @@ function InstalledFonts() {
}
size="small"
onClick={ () => {
// Show the page of the font, so focus can return to it.
const fontIndex = [
...baseThemeFonts,
...baseCustomFonts,
].findIndex(
( font ) =>
font.slug ===
libraryFontSelected?.slug &&
font.source ===
libraryFontSelected?.source
);
if ( fontIndex !== -1 ) {
setPage(
Math.floor(
fontIndex / pageSize
) + 1
);
}
setLastSelectedFontSlug(
libraryFontSelected?.slug
);
Expand Down Expand Up @@ -505,6 +545,13 @@ function InstalledFonts() {
</Progress.Track>
</Progress.Root>
) }
{ ! libraryFontSelected && totalPages > 1 && (
<Pagination
page={ page }
totalPages={ totalPages }
onChange={ setPage }
/>
) }
{ shouldDisplayDeleteButton && (
<Button
__next40pxDefaultSize
Expand Down
107 changes: 107 additions & 0 deletions packages/global-styles-ui/src/font-library/pagination.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { createInterpolateElement } from '@wordpress/element';
import { Button } from '@wordpress/components';
import { Select, Stack } from '@wordpress/ui';
import { sprintf, __, _x, isRTL } from '@wordpress/i18n';
import { next, previous } from '@wordpress/icons';

const MIN_WINDOW_HEIGHT = 500;

export function getFontsPerPage() {
// NOTE: The height of the font library modal unavailable to use for rendering font family items is roughly 417px
// The height of each font family item is 61px.
const windowHeight = Math.max( window.innerHeight, MIN_WINDOW_HEIGHT );
return Math.floor( ( windowHeight - 417 ) / 61 );
}

function Pagination( {
page,
totalPages,
onChange,
}: {
page: number;
totalPages: number;
onChange: ( page: number ) => void;
} ) {
return (
<Stack direction="row" align="center" justify="end" gap="xl">
<Stack
direction="row"
align="center"
gap="xs"
className="font-library__page-selection"
>
{ createInterpolateElement(
sprintf(
// translators: 1: Current page number, 2: Total number of pages.
_x( '<div>Page</div>%1$s<div>of %2$d</div>', 'paging' ),
'<CurrentPage />',
totalPages
),
{
div: <div aria-hidden />,
// @ts-expect-error — Tag injected via sprintf argument, not visible in format string.
CurrentPage: (
<Select.Root
value={ page.toString() }
onValueChange={ ( newPage ) => {
if ( newPage !== null ) {
onChange( parseInt( newPage ) );
}
} }
>
<Select.Trigger
className="font-library__page-selection-trigger"
size="small"
variant="minimal"
aria-label={ __( 'Current page' ) }
/>
<Select.Popup width="content">
{ [ ...Array( totalPages ) ].map(
( e, i ) => {
const value = ( i + 1 ).toString();
return (
<Select.Item
key={ value }
value={ value }
size="small"
>
<Select.ItemLabel>
{ value }
</Select.ItemLabel>
</Select.Item>
);
}
) }
</Select.Popup>
</Select.Root>
),
}
) }
</Stack>
<Stack direction="row" align="center" gap="xs">
<Button
onClick={ () => onChange( page - 1 ) }
disabled={ page === 1 }
accessibleWhenDisabled
label={ __( 'Previous page' ) }
icon={ isRTL() ? next : previous }
showTooltip
size="compact"
tooltipPosition="top"
/>
<Button
onClick={ () => onChange( page + 1 ) }
disabled={ page === totalPages }
accessibleWhenDisabled
label={ __( 'Next page' ) }
icon={ isRTL() ? previous : next }
showTooltip
size="compact"
tooltipPosition="top"
/>
</Stack>
</Stack>
);
}

export default Pagination;
Loading