Skip to content

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

TypeScript

fix(FileInput): silence the busy Spinner and announce the busy start once (#7954)

Cindy Zhang88ec018ca128
diff --git a/.changeset/fileinput-busy-announces-once.md b/.changeset/fileinput-busy-announces-once.md
new file mode 100644
index 0000000000..b4300a9d93
--- /dev/null
+++ b/.changeset/fileinput-busy-announces-once.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `FileInput` announces its busy start ("Loading") once, politely, through the shared announcement channel, and the Spinner it shows while busy is hidden from assistive technology (`spec:AST-086` FR6, FR8). Before, that Spinner mounted as its own `role="status"` region already named "Loading", which screen readers often skip. A file pick keeps its single "file selected" announcement, because it is one user action, even when it starts a busy period (`changeAction`, or `isLoading` set from `onChange`). Other busy starts, such as `isLoading` on its own or a clear under `changeAction`, announce "Loading". Inside an open modal dialog the announcement goes to a status region in the dialog. Tests that found the busy Spinner with `getByRole('status', {name: 'Loading'})` should check the field's `aria-busy` instead.
+
+@cixzhang
diff --git a/packages/core/src/FileInput/FileInput.tsx b/packages/core/src/FileInput/FileInput.tsx
index abfa243934..390a559cbb 100644
--- a/packages/core/src/FileInput/FileInput.tsx
+++ b/packages/core/src/FileInput/FileInput.tsx
@@ -47,6 +47,7 @@ import {Icon} from '../Icon';
import {Spinner} from '../Spinner';
import {VisuallyHidden} from '../VisuallyHidden';
import {useAnnounce} from '../hooks/useAnnounce';
+import {useBusyAnnouncement} from '../hooks/useBusyAnnouncement';
import {useInputStatusIcon} from '../hooks/useInputStatusIcon';
import {useClickableContainer} from '../hooks/useClickableContainer';
import {useTooltip} from '../Tooltip';
@@ -490,6 +491,20 @@ export function FileInput({
const [, startTransition] = useTransition();
const [optimisticValue, setOptimisticValue] = useOptimistic(value);
const isBusy = isLoading || optimisticValue !== value;
+ // A pick is one user action, already announced as "file selected"; a busy
+ // period it starts (changeAction, or a caller setting isLoading from
+ // onChange) adds no second message (spec:AST-086 FR6). The mark lasts until
+ // the field is idle again. Other busy starts announce; the Spinner stays
+ // decorative (FR8).
+ const [isPickAnnounced, setIsPickAnnounced] = useState(false);
+ if (isPickAnnounced && !isBusy) {
+ setIsPickAnnounced(false);
+ }
+ useBusyAnnouncement(
+ isBusy && !isPickAnnounced,
+ t('@astryx.spinner.loading'),
+ containerRef,
+ );
// Announce successful file selection to screen readers via a persistent
// live region (forms-17). Validation errors are announced (assertively) by
@@ -580,6 +595,7 @@ export function FileInput({
// status mounts, so only announce the attach here (do not
// double-announce errors).
if (errors.length === 0) {
+ setIsPickAnnounced(true);
announce(
valid.length === 1
? t('@astryx.fileInput.fileSelected', {fileName: valid[0].name})
@@ -741,7 +757,7 @@ export function FileInput({
const renderDropzoneContent = () => {
if (isBusy) {
- return <Spinner size="md" />;
+ return <Spinner size="md" aria-hidden="true" />;
}
if (hasFiles) {
return (
@@ -772,7 +788,7 @@ export function FileInput({
<span {...stylex.props(styles.fileNameText)}>
{fileNames ?? displayPlaceholder}
</span>
- <Spinner size="sm" />
+ <Spinner size="sm" aria-hidden="true" />
</>
);
}
diff --git a/packages/core/src/__tests__/BusyOwnerAnnouncement.test.tsx b/packages/core/src/__tests__/BusyOwnerAnnouncement.test.tsx
index 89e592a43b..3d76540da9 100644
--- a/packages/core/src/__tests__/BusyOwnerAnnouncement.test.tsx
+++ b/packages/core/src/__tests__/BusyOwnerAnnouncement.test.tsx
@@ -8,7 +8,13 @@
* owning control announces each busy start once through the shared channel
*/
-import {StrictMode, useEffect, useRef, type ReactElement} from 'react';
+import {
+ StrictMode,
+ useEffect,
+ useRef,
+ useState,
+ type ReactElement,
+} from 'react';
import {
afterAll,
afterEach,
@@ -30,6 +36,7 @@ import {DateRangeInput} from '../DateRangeInput';
import {DateTimeInput} from '../DateTimeInput';
import {TimeInput} from '../TimeInput';
import {CheckboxInput} from '../CheckboxInput';
+import {FileInput} from '../FileInput';
import {Thumbnail} from '../Thumbnail';
import {
DropdownMenu,
@@ -156,6 +163,21 @@ const OWNERS: ReadonlyArray<
<Thumbnail src="/local.jpg" alt="Upload" isLoading={isLoading} />
),
],
+ ...(['dropzone', 'input'] as const).map(
+ mode =>
+ [
+ `FileInput (${mode})`,
+ (isLoading: boolean) => (
+ <FileInput
+ label="Attachment"
+ mode={mode}
+ value={null}
+ onChange={() => {}}
+ isLoading={isLoading}
+ />
+ ),
+ ] as const,
+ ),
[
'DateRangeInput',
isLoading => (
@@ -574,3 +596,117 @@ describe('ChatMessageList top loader (spec:AST-086 FR4, FR8)', () => {
expect(busyAnnouncements()).toEqual([['Loading', 'polite']]);
});
});
+
+describe('FileInput busy start from a pick or a clear (spec:AST-086 FR6, FR8)', () => {
+ const file = new File(['x'], 'report.pdf', {type: 'application/pdf'});
+
+ it('keeps a pick under a pending changeAction to its one "selected" message', async () => {
+ const {container} = render(
+ strict(
+ <FileInput
+ label="Attachment"
+ value={null}
+ onChange={() => {}}
+ changeAction={async () => new Promise<void>(() => {})}
+ />,
+ ),
+ );
+ act(() => {
+ fireEvent.change(
+ container.querySelector('input[type="file"]') as HTMLInputElement,
+ {target: {files: [file]}},
+ );
+ });
+ await nextFrame();
+ expect(container.querySelector('.astryx-spinner')).toHaveAttribute(
+ 'aria-hidden',
+ 'true',
+ );
+ expect(announce).toHaveBeenCalledWith('1 file selected: report.pdf');
+ expect(busyAnnouncements()).toEqual([]);
+ });
+
+ it('keeps a pick whose onChange starts an upload (isLoading) to its one "selected" message', async () => {
+ function Uploader() {
+ const [value, setValue] = useState<File | null>(null);
+ const [isLoading, setIsLoading] = useState(false);
+ return (
+ <FileInput
+ label="Attachment"
+ value={value}
+ isLoading={isLoading}
+ onChange={next => {
+ setValue(next as File | null);
+ setIsLoading(true);
+ }}
+ />
+ );
+ }
+ const {container} = render(strict(<Uploader />));
+ act(() => {
+ fireEvent.change(
+ container.querySelector('input[type="file"]') as HTMLInputElement,
+ {target: {files: [file]}},
+ );
+ });
+ await nextFrame();
+ await nextFrame();
+ expect(announce).toHaveBeenCalledWith('1 file selected: report.pdf');
+ expect(busyAnnouncements()).toEqual([]);
+ });
+
+ it('announces nothing more when an earlier pick settles while a later one is still pending', async () => {
+ const settles: (() => void)[] = [];
+ const {container} = render(
+ strict(
+ <FileInput
+ label="Attachment"
+ value={null}
+ onChange={() => {}}
+ changeAction={async () =>
+ new Promise<void>(resolve => {
+ settles.push(resolve);
+ })
+ }
+ />,
+ ),
+ );
+ const input = container.querySelector(
+ 'input[type="file"]',
+ ) as HTMLInputElement;
+ act(() => {
+ fireEvent.change(input, {target: {files: [file]}});
+ });
+ await nextFrame();
+ act(() => {
+ fireEvent.change(input, {
+ target: {files: [new File(['y'], 'notes.txt', {type: 'text/plain'})]},
+ });
+ });
+ await nextFrame();
+ await act(async () => {
+ settles[0]();
+ });
+ await nextFrame();
+ await nextFrame();
+ expect(busyAnnouncements()).toEqual([]);
+ });
+
+ it('announces the busy start of a clear under a pending changeAction', async () => {
+ render(
+ strict(
+ <FileInput
+ label="Attachment"
+ value={file}
+ onChange={() => {}}
+ changeAction={async () => new Promise<void>(() => {})}
+ />,
+ ),
+ );
+ act(() => {
+ fireEvent.click(screen.getByRole('button', {name: /Clear/}));
+ });
+ await nextFrame();
+ expect(busyAnnouncements()).toEqual([['Loading', 'polite']]);
+ });
+});