Skip to content

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

TypeScript

fix(CommandPalette): announce each busy start from inside its dialog (#7956)

Cindy Zhangeccc6eee02f2
diff --git a/.changeset/commandpalette-busy-in-dialog.md b/.changeset/commandpalette-busy-in-dialog.md
new file mode 100644
index 0000000000..7e4cd1763a
--- /dev/null
+++ b/.changeset/commandpalette-busy-in-dialog.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `CommandPalette` announces each busy start ("Loading"), including its load on opening or clearing the query, once from a status region inside its dialog, and its search Spinner is hidden from assistive technology (`spec:AST-086` FR3, FR8). Before, the Spinner mounted as its own `role="status"` region already named "Loading", and the palette's own "Loading" went to the page-level announcement channel, which an open native modal hides from assistive technology. Result-count announcements are unchanged. Tests that found the Spinner with `getByRole('status')` should query the Spinner element instead.
+
+@cixzhang
diff --git a/packages/core/src/CommandPalette/CommandPalette.test.tsx b/packages/core/src/CommandPalette/CommandPalette.test.tsx
index 1019430d8f..a706a2dc2b 100644
--- a/packages/core/src/CommandPalette/CommandPalette.test.tsx
+++ b/packages/core/src/CommandPalette/CommandPalette.test.tsx
@@ -729,7 +729,7 @@ describe('CommandPalette', () => {
});
});
- it('does not announce on initial open (bootstrap)', async () => {
+ it('announces no result count on initial open (bootstrap)', async () => {
render(
<CommandPalette
isOpen={true}
@@ -742,7 +742,9 @@ describe('CommandPalette', () => {
await act(async () => {
await new Promise(resolve => setTimeout(resolve, 50));
});
- expect(politeRegion()?.textContent ?? '').toBe('');
+ // The bootstrap's busy start may say "Loading" (spec:AST-086 FR3, FR8);
+ // its results are never counted aloud.
+ expect(['', 'Loading']).toContain(politeRegion()?.textContent ?? '');
});
});
});
diff --git a/packages/core/src/CommandPalette/CommandPalette.tsx b/packages/core/src/CommandPalette/CommandPalette.tsx
index c586a76146..117bffa0dc 100644
--- a/packages/core/src/CommandPalette/CommandPalette.tsx
+++ b/packages/core/src/CommandPalette/CommandPalette.tsx
@@ -318,11 +318,11 @@ export function CommandPalette<T extends SearchableItem = SearchableItem>({
const searchVersionRef = useRef(0);
const emptyStateRef = useRef<HTMLDivElement>(null);
- // Announce search status to screen readers through the shared polite live
+ // Announce search results to screen readers through the shared polite live
// region (comboboxes-7 announce path, mirroring Selector / BaseTypeahead).
- // The busy spinner and the empty states are otherwise purely visual, so a
- // screen-reader user typing a query would hear nothing when loading starts
- // or results disappear.
+ // The empty states are otherwise purely visual, so a screen-reader user
+ // typing a query would hear nothing when results disappear. The busy start
+ // is announced by CommandPaletteInput.
const announce = useAnnounce();
const value = controlledValue ?? internalValue;
@@ -407,13 +407,8 @@ export function CommandPalette<T extends SearchableItem = SearchableItem>({
startTransition(async () => {
const isBootstrap = query === '';
- // Loading started for a user query: tell screen-reader users the
- // spinner appeared. The polite region coalesces rapid updates, so for
- // fast sources the result-count announcement below simply replaces
- // this instead of stacking one "Loading" per keystroke.
- if (!isBootstrap) {
- announce(t('@astryx.commandPalette.loading'));
- }
+ // The busy start ("Loading") is announced by CommandPaletteInput,
+ // from a region inside the palette's dialog (spec:AST-086 FR8, FR3).
// Client-filter previous results for instant narrowing while fetch is in flight
if (!isBootstrap && searchResults.length > 0) {
diff --git a/packages/core/src/CommandPalette/CommandPaletteInput.test.tsx b/packages/core/src/CommandPalette/CommandPaletteInput.test.tsx
index 9a4d90a1b3..21dc2613c7 100644
--- a/packages/core/src/CommandPalette/CommandPaletteInput.test.tsx
+++ b/packages/core/src/CommandPalette/CommandPaletteInput.test.tsx
@@ -377,7 +377,9 @@ describe('CommandPaletteInput popup context', () => {
</CommandPaletteContext>,
);
- const spinner = screen.getByRole('status');
+ // The Spinner is decorative (spec:AST-086 FR8), so find it by its class.
+ const spinner = document.querySelector('.astryx-spinner') as HTMLElement;
+ expect(spinner).toHaveAttribute('aria-hidden', 'true');
const trailingContent = screen.getByTestId('trailing-content');
expect(trailingContent.parentElement?.firstElementChild).toContainElement(
spinner,
diff --git a/packages/core/src/CommandPalette/CommandPaletteInput.tsx b/packages/core/src/CommandPalette/CommandPaletteInput.tsx
index 6910c110d8..4011324c09 100644
--- a/packages/core/src/CommandPalette/CommandPaletteInput.tsx
+++ b/packages/core/src/CommandPalette/CommandPaletteInput.tsx
@@ -36,6 +36,7 @@ import {
import {useTranslator} from '../i18n';
import {useMergedRefs} from '../hooks/useMergedRefs';
+import {useBusyAnnouncement} from '../hooks/useBusyAnnouncement';
const styles = stylex.create({
wrapper: {
display: 'flex',
@@ -190,6 +191,14 @@ export function CommandPaletteInput({
const hasListbox = ctx ? ctx.selectableItems.length > 0 : true;
const dialogContext = useDialogContext();
const inputRef = useRef<HTMLInputElement>(null);
+ // The search Spinner is decorative; the input announces each busy start,
+ // including the load on opening or clearing the query. Inside the palette's
+ // native modal it does so from a region in the dialog (spec:AST-086 FR8, FR3).
+ useBusyAnnouncement(
+ ctx?.isBusy === true,
+ t('@astryx.commandPalette.loading'),
+ inputRef,
+ );
const [isKeyboardFocus, setIsKeyboardFocus] = useState(false);
// Text-entry controls match :focus-visible after pointer focus in Chromium.
@@ -335,7 +344,7 @@ export function CommandPaletteInput({
<span {...stylex.props(styles.end)}>
{ctx?.isBusy && (
<span {...stylex.props(styles.icon, styles.spinner)}>
- <Spinner size="sm" />
+ <Spinner size="sm" aria-hidden="true" />
</span>
)}
{hasEndContent && endContent}
diff --git a/packages/core/src/__tests__/BusyOwnerAnnouncement.test.tsx b/packages/core/src/__tests__/BusyOwnerAnnouncement.test.tsx
index 3d76540da9..d0b34d5a6d 100644
--- a/packages/core/src/__tests__/BusyOwnerAnnouncement.test.tsx
+++ b/packages/core/src/__tests__/BusyOwnerAnnouncement.test.tsx
@@ -37,6 +37,7 @@ import {DateTimeInput} from '../DateTimeInput';
import {TimeInput} from '../TimeInput';
import {CheckboxInput} from '../CheckboxInput';
import {FileInput} from '../FileInput';
+import {CommandPalette} from '../CommandPalette';
import {Thumbnail} from '../Thumbnail';
import {
DropdownMenu,
@@ -710,3 +711,82 @@ describe('FileInput busy start from a pick or a clear (spec:AST-086 FR6, FR8)',
expect(busyAnnouncements()).toEqual([['Loading', 'polite']]);
});
});
+
+describe('CommandPalette busy start inside its modal (spec:AST-086 FR8, FR3)', () => {
+ beforeEach(() => {
+ HTMLDialogElement.prototype.showModal = vi.fn(function (
+ this: HTMLDialogElement,
+ ) {
+ this.setAttribute('open', '');
+ });
+ HTMLDialogElement.prototype.close = vi.fn(function (
+ this: HTMLDialogElement,
+ ) {
+ this.removeAttribute('open');
+ });
+ const matches = Element.prototype.matches;
+ vi.spyOn(Element.prototype, 'matches').mockImplementation(function (
+ this: Element,
+ selector: string,
+ ) {
+ return selector === ':modal'
+ ? this instanceof HTMLDialogElement && this.open
+ : matches.call(this, selector);
+ });
+ });
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ function regionOf(): Element | null {
+ return (
+ document
+ .querySelector('dialog[open]')
+ ?.querySelector('[data-astryx-modal-busy-region]') ?? null
+ );
+ }
+
+ it('announces the load on opening from a region inside its dialog, with a silent Spinner', async () => {
+ render(
+ strict(
+ <CommandPalette
+ isOpen
+ onOpenChange={() => {}}
+ searchSource={{
+ bootstrap: async () => new Promise<SearchableItem[]>(() => {}),
+ search: async () => new Promise<SearchableItem[]>(() => {}),
+ }}
+ />,
+ ),
+ );
+ await nextFrame();
+ await nextFrame();
+ const dialog = document.querySelector('dialog[open]') as HTMLElement;
+ expect(dialog.querySelector('.astryx-spinner')).toHaveAttribute(
+ 'aria-hidden',
+ 'true',
+ );
+ expect(regionOf()).toHaveTextContent('Loading');
+ expect(busyAnnouncements()).toEqual([]);
+ });
+
+ it('announces a search start once from inside its dialog, not on the hidden page channel', async () => {
+ render(
+ strict(
+ <CommandPalette
+ isOpen
+ onOpenChange={() => {}}
+ searchSource={pendingSource()}
+ />,
+ ),
+ );
+ await nextFrame();
+ act(() => {
+ fireEvent.change(screen.getByRole('combobox'), {target: {value: 'se'}});
+ });
+ await nextFrame();
+ await nextFrame();
+ expect(regionOf()).toHaveTextContent('Loading');
+ expect(busyAnnouncements()).toEqual([]);
+ });
+});