Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

feat(core): add keyboard navigation to Pagination dots (#3681)

Baraa Hamodi032e639f7ca7
diff --git a/.changeset/pagination-keyboard-nav.md b/.changeset/pagination-keyboard-nav.md
new file mode 100644
index 0000000000..0d92a5cdb9
--- /dev/null
+++ b/.changeset/pagination-keyboard-nav.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[feat] Pagination: the `dots` variant is now keyboard navigable via the shared useListFocus primitive. With a dot focused, Left/Right arrows move between dots and Home/End jump to the first/last, the active page follows focus (roving tabindex), and Up/Down are left to the browser so vertical scrolling is unaffected. (#3681)
+@bhamodi
diff --git a/apps/docsite/src/app/(site)/_landing/hero/HeroThemeReel.tsx b/apps/docsite/src/app/(site)/_landing/hero/HeroThemeReel.tsx
index 38ef1586a6..92383e32ae 100644
--- a/apps/docsite/src/app/(site)/_landing/hero/HeroThemeReel.tsx
+++ b/apps/docsite/src/app/(site)/_landing/hero/HeroThemeReel.tsx
@@ -12,6 +12,9 @@
* so the wordmark, cards, and dots — placed in different parts of the DOM by
* page.tsx — re-skin together. Auto-advance pauses on hover/focus and when the
* tab is hidden, and respects prefers-reduced-motion.
+ *
+ * Manual control: touch swipe (mobile) and the Pagination dots, which own
+ * keyboard navigation (arrow keys, Home/End) via the useListFocus primitive.
*/
import {
diff --git a/packages/core/src/Pagination/Pagination.test.tsx b/packages/core/src/Pagination/Pagination.test.tsx
index 3c8e8f6c50..3b263782aa 100644
--- a/packages/core/src/Pagination/Pagination.test.tsx
+++ b/packages/core/src/Pagination/Pagination.test.tsx
@@ -345,6 +345,149 @@ describe('Pagination', () => {
const activeDot = screen.getByRole('button', {name: 'Go to page 3'});
expect(activeDot).toHaveAttribute('aria-current', 'page');
});
+
+ // -------------------------------------------------------------------------
+ // Keyboard navigation (roving tabindex + arrow keys via useListFocus).
+ // Selection follows focus: arrow/Home/End move focus and select that page.
+ // -------------------------------------------------------------------------
+
+ it('uses roving tabindex — active dot has tabIndex 0, others -1', () => {
+ render(
+ <Pagination
+ page={2}
+ onChange={() => {}}
+ totalPages={4}
+ variant="dots"
+ />,
+ );
+ expect(
+ screen.getByRole('button', {name: 'Go to page 2'}),
+ ).toHaveAttribute('tabindex', '0');
+ expect(
+ screen.getByRole('button', {name: 'Go to page 1'}),
+ ).toHaveAttribute('tabindex', '-1');
+ expect(
+ screen.getByRole('button', {name: 'Go to page 3'}),
+ ).toHaveAttribute('tabindex', '-1');
+ });
+
+ it('ArrowRight moves focus to the next dot and selects it', async () => {
+ const user = userEvent.setup();
+ const onChange = vi.fn();
+ render(
+ <Pagination
+ page={2}
+ onChange={onChange}
+ totalPages={4}
+ variant="dots"
+ />,
+ );
+ screen.getByRole('button', {name: 'Go to page 2'}).focus();
+ await user.keyboard('{ArrowRight}');
+ expect(onChange).toHaveBeenCalledWith(3);
+ expect(screen.getByRole('button', {name: 'Go to page 3'})).toHaveFocus();
+ });
+
+ it('ArrowLeft moves focus to the previous dot and selects it', async () => {
+ const user = userEvent.setup();
+ const onChange = vi.fn();
+ render(
+ <Pagination
+ page={3}
+ onChange={onChange}
+ totalPages={4}
+ variant="dots"
+ />,
+ );
+ screen.getByRole('button', {name: 'Go to page 3'}).focus();
+ await user.keyboard('{ArrowLeft}');
+ expect(onChange).toHaveBeenCalledWith(2);
+ expect(screen.getByRole('button', {name: 'Go to page 2'})).toHaveFocus();
+ });
+
+ it('Home selects the first page, End the last', async () => {
+ const user = userEvent.setup();
+ const onChange = vi.fn();
+ const {rerender} = render(
+ <Pagination
+ page={3}
+ onChange={onChange}
+ totalPages={5}
+ variant="dots"
+ />,
+ );
+ screen.getByRole('button', {name: 'Go to page 3'}).focus();
+ await user.keyboard('{Home}');
+ expect(onChange).toHaveBeenCalledWith(1);
+ expect(screen.getByRole('button', {name: 'Go to page 1'})).toHaveFocus();
+
+ onChange.mockClear();
+ rerender(
+ <Pagination
+ page={3}
+ onChange={onChange}
+ totalPages={5}
+ variant="dots"
+ />,
+ );
+ screen.getByRole('button', {name: 'Go to page 3'}).focus();
+ await user.keyboard('{End}');
+ expect(onChange).toHaveBeenCalledWith(5);
+ expect(screen.getByRole('button', {name: 'Go to page 5'})).toHaveFocus();
+ });
+
+ it('wraps from the last dot to the first with ArrowRight', async () => {
+ const user = userEvent.setup();
+ const onChange = vi.fn();
+ render(
+ <Pagination
+ page={4}
+ onChange={onChange}
+ totalPages={4}
+ variant="dots"
+ />,
+ );
+ screen.getByRole('button', {name: 'Go to page 4'}).focus();
+ await user.keyboard('{ArrowRight}');
+ expect(onChange).toHaveBeenCalledWith(1);
+ expect(screen.getByRole('button', {name: 'Go to page 1'})).toHaveFocus();
+ });
+
+ it('wraps from the first dot to the last with ArrowLeft', async () => {
+ const user = userEvent.setup();
+ const onChange = vi.fn();
+ render(
+ <Pagination
+ page={1}
+ onChange={onChange}
+ totalPages={4}
+ variant="dots"
+ />,
+ );
+ screen.getByRole('button', {name: 'Go to page 1'}).focus();
+ await user.keyboard('{ArrowLeft}');
+ expect(onChange).toHaveBeenCalledWith(4);
+ expect(screen.getByRole('button', {name: 'Go to page 4'})).toHaveFocus();
+ });
+
+ it('does not navigate with arrow keys when disabled', async () => {
+ const user = userEvent.setup();
+ const onChange = vi.fn();
+ render(
+ <Pagination
+ page={2}
+ onChange={onChange}
+ totalPages={4}
+ variant="dots"
+ isDisabled
+ />,
+ );
+ const dot = screen.getByRole('button', {name: 'Go to page 2'});
+ expect(dot).toBeDisabled();
+ dot.focus();
+ await user.keyboard('{ArrowRight}');
+ expect(onChange).not.toHaveBeenCalled();
+ });
});
// ---------------------------------------------------------------------------
diff --git a/packages/core/src/Pagination/Pagination.tsx b/packages/core/src/Pagination/Pagination.tsx
index 3daa77f88d..57505565f1 100644
--- a/packages/core/src/Pagination/Pagination.tsx
+++ b/packages/core/src/Pagination/Pagination.tsx
@@ -35,6 +35,7 @@ import {Icon} from '../Icon';
import {Selector} from '../Selector';
import {Text} from '../Text';
import {useAnnounce} from '../hooks/useAnnounce';
+import {useListFocus} from '../hooks/useListFocus';
import {mergeProps} from '../utils';
import type {BaseProps} from '../BaseProps';
import {themeProps} from '../utils/themeProps';
@@ -370,6 +371,21 @@ export function Pagination({
// in-flight target instead of stalling on the last committed page.
const [optimisticPage, setOptimisticPage] = useOptimistic(page);
+ // Roving-tabindex + arrow/Home/End keyboard nav for the dots variant, owned
+ // by the shared useListFocus primitive (mirrors SegmentedControl). It stamps a
+ // single tab stop across the dots and moves focus horizontally; selection
+ // follows focus via handleDotsFocus so arrow keys move the active page.
+ const {
+ listRef: dotsListRef,
+ handleKeyDown: handleDotsKeyDown,
+ handleFocus: handleDotsRovingFocus,
+ } = useListFocus<HTMLDivElement>({
+ itemSelector: 'button',
+ hasRovingTabIndex: true,
+ wrap: true,
+ orientation: 'horizontal',
+ });
+
// Compute pagination state
const computedTotalPages =
totalPagesProp ??
@@ -410,6 +426,28 @@ export function Pagination({
});
};
+ // Selection-follows-focus for the dots (APG radiogroup pattern): useListFocus
+ // only *moves* focus, so when focus lands on a dot -- via arrow/Home/End, or a
+ // click that focuses it -- we select that dot's page. handleDotsRovingFocus
+ // keeps the roving tab stop in sync. The current page is skipped so tabbing
+ // into the group is a no-op.
+ const handleDotsFocus = (e: React.FocusEvent) => {
+ handleDotsRovingFocus(e);
+ if (isDisabled) {
+ return;
+ }
+ const focused = (e.target as HTMLElement | null)?.closest<HTMLElement>(
+ 'button[data-page]',
+ );
+ if (!focused) {
+ return;
+ }
+ const nextPage = Number(focused.dataset.page);
+ if (Number.isFinite(nextPage) && nextPage !== optimisticPage) {
+ handlePageChange(nextPage);
+ }
+ };
+
const handlePrevious = () => {
if (hasPrevious) {
handlePageChange(optimisticPage - 1);
@@ -518,33 +556,47 @@ export function Pagination({
if (computedTotalPages == null) {
return null;
}
+
return (
<div
+ ref={dotsListRef}
{...stylex.props(styles.dotsContainer)}
role="group"
- aria-label="Page indicators">
- {Array.from({length: computedTotalPages}, (_, i) => (
- <button
- key={i + 1}
- type="button"
- aria-label={`Go to page ${i + 1}`}
- aria-current={i + 1 === optimisticPage ? 'page' : undefined}
- onClick={() => handlePageChange(i + 1)}
- disabled={isDisabled}
- {...mergeProps(
- themeProps('pagination-dot', {
- active: i + 1 === optimisticPage ? 'active' : null,
- size,
- }),
- stylex.props(
- styles.dot,
- isSm && styles.dotSm,
- i + 1 === optimisticPage && styles.dotActive,
- isDisabled && styles.dotDisabled,
- ),
- )}
- />
- ))}
+ aria-label="Page indicators"
+ onKeyDown={handleDotsKeyDown}
+ onFocus={handleDotsFocus}>
+ {Array.from({length: computedTotalPages}, (_, i) => {
+ const isActive = i + 1 === optimisticPage;
+ return (
+ <button
+ key={i + 1}
+ type="button"
+ data-page={i + 1}
+ aria-label={`Go to page ${i + 1}`}
+ aria-current={isActive ? 'page' : undefined}
+ // The active dot is the single roving tab stop; useListFocus
+ // maintains it as focus and the active page move.
+ tabIndex={isActive ? 0 : -1}
+ // Selection is driven by focus (handleDotsFocus); clicking only
+ // needs to focus the dot, which some browsers (Safari) skip for
+ // buttons, so focus it explicitly.
+ onClick={e => e.currentTarget.focus()}
+ disabled={isDisabled}
+ {...mergeProps(
+ themeProps('pagination-dot', {
+ active: isActive ? 'active' : null,
+ size,
+ }),
+ stylex.props(
+ styles.dot,
+ isSm && styles.dotSm,
+ isActive && styles.dotActive,
+ isDisabled && styles.dotDisabled,
+ ),
+ )}
+ />
+ );
+ })}
</div>
);
}