An open source design system that's fully customizable and agent ready
TypeScript
fix(core): announce Calendar month changes to screen readers (#3724)
Baraa Hamodi926e5e3a1222
diff --git a/.changeset/calendar-month-announce.md b/.changeset/calendar-month-announce.mdnew file mode 100644index 0000000000..6697e50286--- /dev/null+++ b/.changeset/calendar-month-announce.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Calendar: month navigation now announces the newly visible month (e.g. "March 2026") to screen readers via a polite live region. Previously the grid changed silently. (#3724)+@bhamodidiff --git a/packages/core/src/Calendar/Calendar.test.tsx b/packages/core/src/Calendar/Calendar.test.tsxindex 9ffba3fd07..8e149bbdda 100644--- a/packages/core/src/Calendar/Calendar.test.tsx+++ b/packages/core/src/Calendar/Calendar.test.tsx@@ -9,14 +9,23 @@* SYNC: When Calendar.tsx changes, update tests accordingly*/-import {describe, it, expect, vi} from 'vitest';-import {act, render, screen, within} from '@testing-library/react';+import {describe, it, expect, vi, afterEach} from 'vitest';+import {act, render, screen, within, waitFor} from '@testing-library/react';import userEvent from '@testing-library/user-event';import {getButton} from '../__tests__/fastRoleQueries';import * as stylex from '@stylexjs/stylex';import {Calendar} from './Calendar';import type {CalendarHandle} from './Calendar';import {calendarStyles} from './styles';+import {__resetLiveRegionsForTest} from '../hooks/useAnnounce';++afterEach(() => {+ __resetLiveRegionsForTest();+});++function politeRegion(): HTMLElement | null {+ return document.querySelector('[data-astryx-live-region="polite"]');+}/*** Helper to find a day button by its day number.@@ -545,6 +554,98 @@ describe('Calendar', () => {expect(getButton('Next month')).toBeInTheDocument();});+ // ─── Month Change Announcements ──────────────────────────────++ describe('month change announcements', () => {+ it('does not announce on initial render', () => {+ render(<Calendar focusDate="2026-01-01" />);+ // The live region is only created lazily on first announce; mounting the+ // calendar must not speak the initial month.+ expect(politeRegion()).toBeNull();+ });++ it('announces the new month politely when clicking next', async () => {+ const user = userEvent.setup();+ render(<Calendar focusDate="2026-01-01" />);++ await user.click(screen.getByRole('button', {name: 'Next month'}));++ await waitFor(() => {+ expect(politeRegion()).toHaveTextContent('February 2026');+ });+ });++ it('announces the new month politely when clicking previous', async () => {+ const user = userEvent.setup();+ render(<Calendar focusDate="2026-02-01" />);++ await user.click(screen.getByRole('button', {name: 'Previous month'}));++ await waitFor(() => {+ expect(politeRegion()).toHaveTextContent('January 2026');+ });+ });++ it('announces the next month when paging the grid with PageDown', async () => {+ const user = userEvent.setup();+ render(<Calendar focusDate="2026-01-01" />);++ // PageDown from a focused day pages the visible grid to the next month.+ getDayButton(15).focus();+ await user.keyboard('{PageDown}');++ await waitFor(() => {+ expect(politeRegion()).toHaveTextContent('February 2026');+ });+ });++ it('announces the newly visible month when navigated via the handle', async () => {+ let handle: CalendarHandle | null = null;+ render(+ <Calendar+ focusDate="2026-01-01"+ handleRef={h => {+ handle = h;+ }}+ />,+ );++ act(() => {+ handle?.navigateTo('2026-03-01');+ });++ await waitFor(() => {+ expect(politeRegion()).toHaveTextContent('March 2026');+ });+ });++ it('announces both months in a two-month view', async () => {+ const user = userEvent.setup();+ render(<Calendar numberOfMonths={2} focusDate="2026-01-01" />);++ await user.click(screen.getByRole('button', {name: 'Next month'}));++ await waitFor(() => {+ expect(politeRegion()).toHaveTextContent('February 2026 – March 2026');+ });+ });++ it('does not announce when selecting a date leaves the visible month unchanged', async () => {+ const user = userEvent.setup();+ render(<Calendar focusDate="2026-01-01" />);++ // Selecting an in-month day does not move the grid, so nothing should be+ // announced (the live region stays uncreated).+ await user.click(getDayButton(15));++ // Allow the announce rAF a chance to run before asserting silence.+ await act(async () => {+ await new Promise(resolve => requestAnimationFrame(resolve));+ });+ expect(politeRegion()).toBeNull();+ });+ });+// ─── Bug Regression Tests ───────────────────────────────────it('day buttons have data-date attribute with ISO string', () => {diff --git a/packages/core/src/Calendar/Calendar.tsx b/packages/core/src/Calendar/Calendar.tsxindex 54b9479ada..f503802984 100644--- a/packages/core/src/Calendar/Calendar.tsx+++ b/packages/core/src/Calendar/Calendar.tsx@@ -23,12 +23,13 @@ import {useCallback,useEffect,useImperativeHandle,+ useRef,} from 'react';import type {BaseProps} from '../BaseProps';import * as stylex from '@stylexjs/stylex';import {Button} from '../Button';import {Icon} from '../Icon';-import {useGridFocus} from '../hooks';+import {useAnnounce, useGridFocus} from '../hooks';import {useCalendarDays,useCalendarConstraints,@@ -311,6 +312,25 @@ export function Calendar({ref, ...props}: CalendarProps) {.join(' – ');}, [visibleMonths, numberOfMonths]);+ // Announce the newly visible month to screen readers whenever it changes.+ // The visible month label (`<span>`) carries no live semantics, so paging the+ // grid — via the header prev/next buttons, keyboard grid paging (arrow keys+ // across a month boundary, PageUp/PageDown), the `navigateTo` handle, or a+ // controlled `focusDate` change — otherwise updates the grid silently. Keying+ // off `monthYearLabel` reuses the existing single-/multi-month formatting and+ // only fires when the visible month actually changes (so selecting a date,+ // which does not move the grid, stays silent). The first-render guard avoids+ // announcing the initial month on mount.+ const announce = useAnnounce();+ const isInitialRenderRef = useRef(true);+ useEffect(() => {+ if (isInitialRenderRef.current) {+ isInitialRenderRef.current = false;+ return;+ }+ announce(monthYearLabel);+ }, [monthYearLabel, announce]);+// Determine if prev/next navigation is possible based on min/maxconst canNavigatePrevious = useMemo(() => {if (!min) {diff --git a/packages/core/src/DateInput/DateInput.test.tsx b/packages/core/src/DateInput/DateInput.test.tsxindex e4fdb53ee1..dc89738f0e 100644--- a/packages/core/src/DateInput/DateInput.test.tsx+++ b/packages/core/src/DateInput/DateInput.test.tsx@@ -10,7 +10,13 @@*/import {describe, it, expect, vi, beforeEach} from 'vitest';-import {render, screen, fireEvent, waitFor} from '@testing-library/react';+import {+ render,+ screen,+ fireEvent,+ waitFor,+ within,+} from '@testing-library/react';import userEvent from '@testing-library/user-event';import {getButton, queryButton} from '../__tests__/fastRoleQueries';import {DateInput} from './DateInput';@@ -133,21 +139,27 @@ describe('DateInput', () => {});it('announces an alert message when typed input is invalid', () => {- render(<DateInput label="Date" onChange={() => {}} />);+ // Scope to the component's own container: the embedded Calendar uses the+ // shared `useAnnounce` hook, whose global polite/assertive live-region pair+ // (both mounted on document.body by any announce) would otherwise make a+ // document-wide `getByRole('alert')` ambiguous.+ const {container} = render(<DateInput label="Date" onChange={() => {}} />);const input = screen.getByRole('combobox');fireEvent.change(input, {target: {value: '13/45/2024'}});- expect(screen.getByRole('alert')).toHaveTextContent('Invalid date');+ expect(within(container).getByRole('alert')).toHaveTextContent(+ 'Invalid date',+ );});it('does not announce an alert message when input is valid', () => {- render(<DateInput label="Date" onChange={() => {}} />);+ const {container} = render(<DateInput label="Date" onChange={() => {}} />);const input = screen.getByRole('combobox');fireEvent.change(input, {target: {value: '03/15/2026'}});- expect(screen.getByRole('alert')).toHaveTextContent('');+ expect(within(container).getByRole('alert')).toHaveTextContent('');expect(screen.queryByText('Invalid date')).not.toBeInTheDocument();});