An open source design system that's fully customizable and agent ready
TypeScript
fix(Switch): announce busy once per busy start (#7872)
Cindy Zhang2d0692c7fc0f
diff --git a/.changeset/switch-busy-one-announcement.md b/.changeset/switch-busy-one-announcement.mdnew file mode 100644index 0000000000..5b15e0e938--- /dev/null+++ b/.changeset/switch-busy-one-announcement.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Switch: a busy switch announces "Loading" once per busy start. Under React StrictMode the announcement used to be made twice.++@cixzhangdiff --git a/packages/core/src/Switch/Switch.test.tsx b/packages/core/src/Switch/Switch.test.tsxindex 1357e369b3..bd0db9af3f 100644--- a/packages/core/src/Switch/Switch.test.tsx+++ b/packages/core/src/Switch/Switch.test.tsx@@ -22,6 +22,7 @@* SYNC: When Switch.tsx changes, update tests to match new behavior*/+import {StrictMode} from 'react';import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';import {render, screen, fireEvent, waitFor} from '@testing-library/react';import userEvent from '@testing-library/user-event';@@ -417,6 +418,61 @@ describe('Switch', () => {});});+ it('announces one busy start once under StrictMode, from the switch alone', async () => {+ // Count every message the shared polite region is given. Each announce+ // writes its text in a later frame, so two announces add it twice.+ const written: string[] = [];+ const observer = new MutationObserver(records => {+ for (const record of records) {+ const target = record.target as Element;+ if (target.getAttribute?.('data-astryx-live-region') !== 'polite') {+ continue;+ }+ for (const node of record.addedNodes) {+ if (node.textContent) {written.push(node.textContent);}+ }+ }+ });+ observer.observe(document.body, {childList: true, subtree: true});++ const {container} = render(+ <StrictMode>+ <Switch+ label="Sync photos"+ value={false}+ onChange={() => {}}+ isLoading+ />+ </StrictMode>,+ );+ await waitFor(() => {+ expect(written).toContain('Loading');+ });+ await new Promise(resolve => setTimeout(resolve, 50));+ observer.disconnect();++ expect(written.filter(text => text === 'Loading')).toHaveLength(1);+ // The thumb's spinner is a silent indicator: no live role reaches+ // assistive technology from inside the switch.+ for (const status of container.querySelectorAll('[role="status"]')) {+ expect(status.closest('[aria-hidden="true"]')).not.toBeNull();+ }+ expect(container.querySelector('[aria-live]')).toBeNull();+ });++ it('announces again when the switch becomes busy again after settling', async () => {+ const polite = () =>+ document.querySelector('[data-astryx-live-region="polite"]');+ const props = {label: 'Sync photos', value: false, onChange: () => {}};+ const {rerender} = render(<Switch {...props} isLoading />);+ await waitFor(() => expect(polite()).toHaveTextContent('Loading'));++ rerender(<Switch {...props} />);+ polite()!.textContent = '';+ rerender(<Switch {...props} isLoading />);+ await waitFor(() => expect(polite()).toHaveTextContent('Loading'));+ });+it('localizes the loading announcement through the i18n catalog', async () => {render(<InternationalizationProviderdiff --git a/packages/core/src/Switch/Switch.tsx b/packages/core/src/Switch/Switch.tsxindex 186e01062a..022504b8d4 100644--- a/packages/core/src/Switch/Switch.tsx+++ b/packages/core/src/Switch/Switch.tsx@@ -21,6 +21,7 @@ import {useOptimistic,useTransition,useEffect,+ useRef,type ChangeEvent,type FocusEvent,type ReactNode,@@ -587,10 +588,20 @@ export function Switch({const isBusy = isLoading || optimisticValue !== value;const announce = useAnnounce();+ // One announcement per busy start (spec:AST-086 FR6). The ref outlives+ // StrictMode's effect replay and rerenders, so only a new busy start (busy+ // again after settling) speaks again.+ const announcedBusyRef = useRef(false);useEffect(() => {- if (isBusy) {- announce(t('@astryx.switch.loading'));+ if (!isBusy) {+ announcedBusyRef.current = false;+ return;}+ if (announcedBusyRef.current) {+ return;+ }+ announcedBusyRef.current = true;+ announce(t('@astryx.switch.loading'));}, [announce, isBusy, t]);const isOn = optimisticValue === true;