Skip to content

Commit ceec1e7

Browse files
authored
feat(use-pagination): hide prev/next button instead of disabling (#4123)
1 parent b766733 commit ceec1e7

5 files changed

Lines changed: 24 additions & 10 deletions

File tree

.changeset/neat-houses-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@digdir/designsystemet-css": patch
3+
---
4+
5+
**pagination**: If direct child of `li` has `aria-hidden="true"` it sets `visibility: hidden;`

.changeset/six-grapes-jump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@digdir/designsystemet-react": patch
3+
---
4+
5+
**usePagination**: Hide prev/next buttons with `aria-hidden="true"` and `visibility: hidden;` instead of disabling

packages/css/src/pagination.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
display: flex;
1616
}
1717

18+
& > li > [aria-hidden='true'] {
19+
visibility: hidden;
20+
}
21+
1822
& > li:first-child > ::before,
1923
& > li:last-child > ::before {
2024
content: '';

packages/react/src/utilities/hooks/use-pagination/use-pagination.test.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,29 @@ describe('usePagination', () => {
3737
}
3838
});
3939

40-
it('should prevet previous when at start', () => {
40+
it('should prevent previous when at start', () => {
4141
const { result } = renderHook(() =>
4242
usePagination({ totalPages: 10, currentPage: 1 }),
4343
);
4444

4545
expect(result.current.hasPrev).toBe(false);
4646
expect(result.current.hasNext).toBe(true);
47-
expect(result.current.prevButtonProps['aria-disabled']).toBe(true);
48-
expect(result.current.nextButtonProps['aria-disabled']).toBe(false);
47+
expect(result.current.prevButtonProps['aria-hidden']).toBe(true);
48+
expect(result.current.nextButtonProps['aria-hidden']).toBe(false);
4949
});
5050

51-
it('should prevet next when at end', () => {
51+
it('should prevent next when at end', () => {
5252
const { result } = renderHook(() =>
5353
usePagination({ totalPages: 10, currentPage: 10 }),
5454
);
5555

5656
expect(result.current.hasPrev).toBe(true);
5757
expect(result.current.hasNext).toBe(false);
58-
expect(result.current.prevButtonProps['aria-disabled']).toBe(false);
59-
expect(result.current.nextButtonProps['aria-disabled']).toBe(true);
58+
expect(result.current.prevButtonProps['aria-hidden']).toBe(false);
59+
expect(result.current.nextButtonProps['aria-hidden']).toBe(true);
6060
});
6161

62-
it('should trigger onChange when clickinging button', async () => {
62+
it('should trigger onChange when clicking button', async () => {
6363
const mockOnChange = vi.fn();
6464
const event = { preventDefault: () => {} } as MouseEvent<HTMLButtonElement>;
6565
const { result } = renderHook(() =>
@@ -71,7 +71,7 @@ describe('usePagination', () => {
7171
expect(mockOnChange).toHaveBeenCalledWith(event, 2);
7272
});
7373

74-
it('should not trigger onChange when clickinging previous button and in start', async () => {
74+
it('should not trigger onChange when clicking previous button and in start', async () => {
7575
const mockOnChange = vi.fn();
7676
const event = { preventDefault: () => {} } as MouseEvent<HTMLButtonElement>;
7777
const { result } = renderHook(() =>

packages/react/src/utilities/hooks/use-pagination/use-pagination.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ export const usePagination = ({
114114
),
115115
/** Properties to spread on Pagination.Button used for previous naviagation */
116116
prevButtonProps: {
117-
'aria-disabled': !hasPrev, // Using aria-disabled to support all HTML elements because of potential asChild
117+
'aria-hidden': !hasPrev, // Using aria-hidden to support all HTML elements because of potential asChild
118118
onClick: handleClick(currentPage - 1),
119119
variant: 'tertiary',
120120
} as PaginationButtonProps,
121121
/** Properties to spread on Pagination.Button used for next naviagation */
122122
nextButtonProps: {
123-
'aria-disabled': !hasNext, // Using aria-disabled to support all HTML elements because of potential asChild
123+
'aria-hidden': !hasNext, // Using aria-hidden to support all HTML elements because of potential asChild
124124
onClick: handleClick(currentPage + 1),
125125
variant: 'tertiary',
126126
} as PaginationButtonProps,

0 commit comments

Comments
 (0)