An open source design system that's fully customizable and agent ready
TypeScript
fix(Button): mount the loading live region empty when a Button first renders loading (#7870)
Cindy Zhang0fd4f9eb43be
diff --git a/.changeset/button-loading-region-mounts-empty.md b/.changeset/button-loading-region-mounts-empty.mdnew file mode 100644index 0000000000..f382b2d26e--- /dev/null+++ b/.changeset/button-loading-region-mounts-empty.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] A `Button` first rendered with `isLoading` (for example after a remount) now mounts its visually hidden loading live region empty and writes "Loading" into it on the next frame, so screen readers announce the busy start instead of skipping a live region born holding its text (`spec:AST-086` FR3). A Button that becomes loading after it mounts is unchanged, and a Button mounted idle does no extra render.++@cixzhangdiff --git a/packages/core/src/Button/Button.test.tsx b/packages/core/src/Button/Button.test.tsxindex b6907a0b04..964d185ac4 100644--- a/packages/core/src/Button/Button.test.tsx+++ b/packages/core/src/Button/Button.test.tsx@@ -17,6 +17,7 @@ import {fireEvent,createEvent,act,+ waitFor,} from '@testing-library/react';import userEvent from '@testing-library/user-event';import {Button} from './Button';@@ -643,7 +644,17 @@ describe('Button', () => {expect(liveRegion).toHaveTextContent('Loading');});- it('localizes the loading announcement through the i18n catalog', () => {+ it('mounts the loading live region empty when first rendered loading, then fills it (spec:AST-086 FR3)', async () => {+ render(<Button label="Submit" isLoading />);+ const button = screen.getByRole('button');+ // The Spinner also has role="status", so grab the live region explicitly.+ const regions = button.querySelectorAll('[role="status"]');+ const liveRegion = regions[regions.length - 1];+ expect(liveRegion).toHaveTextContent('');+ await waitFor(() => expect(liveRegion).toHaveTextContent('Loading'));+ });++ it('localizes the loading announcement through the i18n catalog', async () => {render(<InternationalizationProviderlocale="fr"@@ -655,7 +666,7 @@ describe('Button', () => {// The Spinner also has role="status", so grab the live region explicitly.const regions = button.querySelectorAll('[role="status"]');const liveRegion = regions[regions.length - 1];- expect(liveRegion).toHaveTextContent('Chargement');+ await waitFor(() => expect(liveRegion).toHaveTextContent('Chargement'));});describe('in a narrow row', () => {diff --git a/packages/core/src/Button/Button.tsx b/packages/core/src/Button/Button.tsxindex 6405518871..7b7ab1ab2f 100644--- a/packages/core/src/Button/Button.tsx+++ b/packages/core/src/Button/Button.tsx@@ -18,7 +18,13 @@* Last synced props: label, variant, size, isDisabled, isLoading, isInterruptible, clickAction, icon, isIconOnly, width, children, tooltip, endContent, href, as, target, rel*/-import {useRef, useTransition, type ReactNode} from 'react';+import {+ useEffect,+ useRef,+ useState,+ useTransition,+ type ReactNode,+} from 'react';import type {BaseProps} from '../BaseProps';import type {Elevation, SizeValue} from '../utils/types';import * as stylex from '@stylexjs/stylex';@@ -587,6 +593,21 @@ export function Button({// re-click can land and interrupt the in-flight action with a fresh one.const actionInFlightRef = useRef(false);const isLoadingState = isLoading || isPending;+ // The loading live region must exist empty before it receives "Loading":+ // many screen readers skip a live region that mounts already holding its+ // text, which is what a Button first rendered loading did (spec:AST-086+ // FR3). Only that case holds the text back, until the frame after the+ // region mounts; a Button mounted idle never re-renders for this.+ const [isLiveRegionReady, setIsLiveRegionReady] = useState(+ () => !isLoadingState,+ );+ useEffect(() => {+ if (isLiveRegionReady) {+ return;+ }+ const frame = requestAnimationFrame(() => setIsLiveRegionReady(true));+ return () => cancelAnimationFrame(frame);+ }, [isLiveRegionReady]);// Delay the spinner reveal for action-driven loading (clickAction's own// transition) so a fast action that settles within the delay does not flash// a spinner. Interruptible loading is delayed too, so rapid re-clicks settle@@ -767,7 +788,7 @@ export function Button({</span>{/* Live region for loading state announcements */}<VisuallyHidden role="status" aria-live="polite">- {isLoadingState ? t('@astryx.button.loading') : ''}+ {isLoadingState && isLiveRegionReady ? t('@astryx.button.loading') : ''}</VisuallyHidden></>);