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

fix(Pagination): use empty li for ellipsis #2942

Merged
merged 17 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 14 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
5 changes: 5 additions & 0 deletions .changeset/bright-eyes-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@digdir/designsystemet-css": patch
---

Button: add `height: fit-content`
6 changes: 6 additions & 0 deletions .changeset/cold-seals-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@digdir/designsystemet-css": patch
"@digdir/designsystemet-react": patch
---

Pagination: Use empty `li` for ellipsis
9 changes: 8 additions & 1 deletion apps/_components/src/Showcase/Showcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,14 @@ export function Showcase({ className, ...props }: ShowcaseProps) {
</Pagination.Item>
{pagination.pages.map(({ itemKey, buttonProps, page }) => (
<Pagination.Item key={itemKey}>
<Pagination.Button {...buttonProps}>{page}</Pagination.Button>
{typeof page === 'number' && (
<Pagination.Button
{...buttonProps}
aria-label={`Side ${page}`}
>
{page}
</Pagination.Button>
)}
</Pagination.Item>
))}
<Pagination.Item>
Expand Down
1 change: 1 addition & 0 deletions packages/css/src/button.css
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
font-family: inherit;
font-weight: var(--ds-font-weight-medium);
gap: var(--dsc-button-gap);
height: fit-content; /* If placed in flex container */
justify-content: center;
line-height: var(--ds-line-height-sm);
min-height: var(--dsc-button-size);
Expand Down
22 changes: 9 additions & 13 deletions packages/css/src/pagination.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@

& > :is(ol, ul) {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: var(--dsc-pagination-gap);
list-style: none;
margin: 0;
padding: 0;

& > li:first-child > ::before,
& > li:last-child > ::after {
& > li:last-child > ::before {
content: '';
background: currentcolor;
height: var(--dsc-pagination-chevron-size);
mask: center / contain no-repeat var(--dsc-pagination-icon-url);
width: var(--dsc-pagination-chevron-size);
order: 1;

@media (forced-colors: active) {
background: LinkText;
Expand All @@ -27,21 +29,15 @@

& > li:first-child > ::before {
rotate: 180deg;
order: 0;
}

/* Style as non-interactive ellipsis when empty */
& > li > :empty {
color: inherit;
padding: 0; /* Make ellipsis element square */
pointer-events: none;

&::before {
content: '\2026'; /* ellipsis */
}
& > li:empty::before {
content: '\2026'; /* ellipsis */
display: block;
min-width: var(--ds-size-12);
text-align: center;
}
}

&[data-compact] {
--dsc-pagination-gap: var(--ds-size-0);
}
}
8 changes: 5 additions & 3 deletions packages/react/src/components/Pagination/Pagination.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ const { pages, prevButtonProps, nextButtonProps, hasNext, hasPrevious } = usePag
</Pagination.Item>
{pages.map(({ page, itemKey, buttonProps }) => (
<Pagination.Item key={itemKey}>
<Pagination.Button {...buttonProps} aria-label={`Side ${page}`}>
{page}
</Pagination.Button>
{typeof page === 'number' && (
<Pagination.Button {...buttonProps} aria-label={`Side ${page}`}>
{page}
</Pagination.Button>
)}
</Pagination.Item>
))}
<Pagination.Item>
Expand Down
32 changes: 18 additions & 14 deletions packages/react/src/components/Pagination/Pagination.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useArgs } from '@storybook/preview-api';
import type { Meta, StoryFn } from '@storybook/react';

import { useState } from 'react';
import { Pagination, type UsePaginationProps, usePagination } from '.';

export default {
Expand All @@ -9,13 +10,12 @@ export default {
} as Meta;

export const Preview: StoryFn<typeof Pagination> = (args) => {
const [, updateArgs] = useArgs();
const [page, setCurrentPage] = useState(4);
const { pages, nextButtonProps, prevButtonProps } = usePagination({
currentPage: 4,
onChange: console.log,
currentPage: page,
totalPages: 10,
showPages: 7,
setCurrentPage: (currentPage) => updateArgs({ currentPage }),
setCurrentPage,
});

return (
Expand All @@ -28,9 +28,11 @@ export const Preview: StoryFn<typeof Pagination> = (args) => {
</Pagination.Item>
{pages.map(({ page, itemKey, buttonProps }) => (
<Pagination.Item key={itemKey}>
<Pagination.Button {...buttonProps} aria-label={`Side ${page}`}>
{page}
</Pagination.Button>
{typeof page === 'number' && (
<Pagination.Button {...buttonProps} aria-label={`Side ${page}`}>
{page}
</Pagination.Button>
)}
</Pagination.Item>
))}
<Pagination.Item>
Expand Down Expand Up @@ -68,13 +70,15 @@ export const WithAnchor: StoryFn<UsePaginationProps> = (args) => {
</Pagination.Item>
{pages.map(({ page, itemKey, buttonProps }) => (
<Pagination.Item key={itemKey}>
<Pagination.Button
asChild
aria-label={`Side ${page}`}
{...buttonProps}
>
<a href={`#side-${page}`}>{page}</a>
</Pagination.Button>
{page && (
<Pagination.Button
asChild
aria-label={`Side ${page}`}
{...buttonProps}
>
<a href={`#side-${page}`}>{page}</a>
</Pagination.Button>
)}
</Pagination.Item>
))}
<Pagination.Item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ const renderWithRoot = (props: PaginationProps) => {
<Pagination.Item>
<Pagination.Button>3</Pagination.Button>
</Pagination.Item>
<Pagination.Item>
<Pagination.Button />
</Pagination.Item>
<Pagination.Item />
<Pagination.Item>
<Pagination.Button>6</Pagination.Button>
</Pagination.Item>
Expand Down
14 changes: 7 additions & 7 deletions packages/react/src/components/Pagination/usePagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ export const usePagination = ({
(page, index) => ({
page: page || '',
mimarz marked this conversation as resolved.
Show resolved Hide resolved
itemKey: page ? `page-${page}` : `ellipsis-${index}`, // React key utility
buttonProps: {
'aria-current': page === currentPage ? 'page' : undefined,
'aria-hidden': !page || undefined, // Hide ellipsis from screen reader
onClick: handleClick(page),
tabIndex: page ? undefined : -1, // Hide ellipsis keyboard
variant: page === currentPage ? 'primary' : 'tertiary',
} as PaginationButtonProps,
buttonProps: (page
? {
'aria-current': page === currentPage ? 'page' : undefined,
onClick: handleClick(page),
variant: page === currentPage ? 'primary' : 'tertiary',
}
: null) as PaginationButtonProps | null,
}),
),
/** Properties to spread on Pagination.Button used for previous naviagation */
Expand Down
11 changes: 8 additions & 3 deletions packages/react/stories/testing.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,14 @@ export const Sizes: StoryFn = () => {
</Pagination.Item>
{pages.map(({ page, itemKey, buttonProps }) => (
<Pagination.Item key={itemKey}>
<Pagination.Button {...buttonProps} aria-label={`Side ${page}`}>
{page}
</Pagination.Button>
{typeof page === 'number' && (
<Pagination.Button
{...buttonProps}
aria-label={`Side ${page}`}
>
{page}
</Pagination.Button>
)}
</Pagination.Item>
))}
<Pagination.Item>
Expand Down
Loading