Skip to content

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

TypeScript

refactor(useTriggerMenu): announce Searching… through the shared busy path (#9154)

Cindy Zhang9963c0e16b1a
diff --git a/.changeset/trigger-menu-busy-announcement.md b/.changeset/trigger-menu-busy-announcement.md
new file mode 100644
index 0000000000..35a6472b7d
--- /dev/null
+++ b/.changeset/trigger-menu-busy-announcement.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] ChatComposerInput's trigger menu announces its "Searching…" row through Core's shared busy announcement, like other busy controls: once per search, not repeated while typing continues, and not at all when results arrive first. Inside a native modal dialog an app renders without Astryx, it is now heard inside that dialog.
+
+@HelloOjasMutreja
diff --git a/packages/core/src/Chat/ChatComposerInput.test.tsx b/packages/core/src/Chat/ChatComposerInput.test.tsx
index 2694d2d8f1..6699d614a9 100644
--- a/packages/core/src/Chat/ChatComposerInput.test.tsx
+++ b/packages/core/src/Chat/ChatComposerInput.test.tsx
@@ -15,7 +15,10 @@ import {createStaticSource} from '../Typeahead/createStaticSource';
import type {SearchableItem, SearchSource} from '../Typeahead/types';
const announce = vi.hoisted(() => vi.fn());
-vi.mock('../hooks/useAnnounce', () => ({useAnnounce: () => announce}));
+vi.mock('../hooks/useAnnounce', async importOriginal => ({
+ ...(await importOriginal<typeof import('../hooks/useAnnounce')>()),
+ useAnnounce: () => announce,
+}));
// =============================================================================
// Helpers
@@ -1988,7 +1991,7 @@ describe('ChatComposerInput trigger loading announcement (spec:AST-086)', () =>
/**
* Lets a settled search apply, as the browser would before the next frame,
- * then lets the loading announcement's two frames run.
+ * then lets the busy announcement's frame run.
*/
async function nextFrame() {
await act(async () => {});
@@ -2067,7 +2070,7 @@ describe('ChatComposerInput trigger loading announcement (spec:AST-086)', () =>
]);
});
- it('announces nothing for a synchronous source, whose results are already listed (FR7)', async () => {
+ it('announces nothing for a synchronous source, whose results are already listed (FR8)', async () => {
render(
<ChatComposerInput triggers={[createMentionTrigger()]} debounceMs={0} />,
);
@@ -2090,7 +2093,7 @@ describe('ChatComposerInput trigger loading announcement (spec:AST-086)', () =>
expect(announce).not.toHaveBeenCalled();
});
- it('announces nothing for a search that settles before the row shows (FR7)', async () => {
+ it('a search whose outcome arrives before its busy message is out announces nothing (FR8)', async () => {
const settled: SearchSource = {
bootstrap: async () => [],
search: async () => [{id: 'cindy', label: 'Cindy Zhang'}],
@@ -2105,9 +2108,20 @@ describe('ChatComposerInput trigger loading announcement (spec:AST-086)', () =>
editable.focus();
typeAtEnd(editable, '@c');
- await nextFrame();
+ // Let the search settle and the busy frames run inside one act, so the
+ // settled state is not committed (cancelling the busy frame) before the
+ // frame runs: only applyResults' settle call keeps this silent.
+ await act(async () => {
+ await new Promise(resolve =>
+ requestAnimationFrame(() => requestAnimationFrame(resolve)),
+ );
+ });
typeAtEnd(editable, '@ci');
- await nextFrame();
+ await act(async () => {
+ await new Promise(resolve =>
+ requestAnimationFrame(() => requestAnimationFrame(resolve)),
+ );
+ });
expect(screen.queryByText('Searching…')).toBeNull();
expect(announce).not.toHaveBeenCalled();
diff --git a/packages/core/src/Chat/useTriggerMenu.tsx b/packages/core/src/Chat/useTriggerMenu.tsx
index 68ae119d32..0d0fee3af2 100644
--- a/packages/core/src/Chat/useTriggerMenu.tsx
+++ b/packages/core/src/Chat/useTriggerMenu.tsx
@@ -4,7 +4,7 @@
/**
* @file useTriggerMenu.tsx
- * @input Uses React, usePopover, SearchSource
+ * @input Uses React, usePopover, SearchSource, useBusyAnnouncement
* @output Exports useTriggerMenu hook for trigger-based menus in contentEditable
* @position Internal hook; consumed by ChatComposerInput
*
@@ -32,7 +32,7 @@ import {
} from 'react';
import * as stylex from '@stylexjs/stylex';
import {usePopover} from '../Popover/usePopover';
-import {useAnnounce} from '../hooks/useAnnounce';
+import {useBusyAnnouncement} from '../hooks/useBusyAnnouncement';
import {useHighlightedOptionScroll} from '../hooks/useHighlightedOptionScroll';
import {
colorVars,
@@ -355,36 +355,22 @@ export function useTriggerMenu(
});
// The loading row carries no live role: it mounts already holding its text
- // and is removed when results arrive, so it would be skipped. The shared
- // channel announces each loading episode once instead (spec:AST-086 FR2,
- // FR6), once the row has been painted: the second animation frame runs
- // after the first paint, and a search that settles before then never shows
- // the row, so it announces nothing (FR7). The flag clears when the loading
- // row goes away, so the next search is a new occasion.
- const announce = useAnnounce();
- const hasAnnouncedLoadingRef = useRef(false);
+ // and is removed when results arrive, so it would be skipped. "Searching…"
+ // is the menu's busy state, so the shared busy path announces each loading
+ // episode once (spec:AST-086 FR6, FR8): rerenders and further keystrokes
+ // while loading do not repeat it, results arriving re-arm it, and a search
+ // that settles before the announcement goes out announces nothing.
// Mirrors renderMenu: no row exists without an active trigger or while hidden.
const isLoadingRowShown =
state.isLoading && !state.isHidden && state.activeTrigger !== null;
// One catalog string serves the row and the announcement (FR7).
const loadingText =
state.activeTrigger?.loadingText ?? t('@astryx.chatTriggerMenu.searching');
- useEffect(() => {
- if (!isLoadingRowShown) {
- hasAnnouncedLoadingRef.current = false;
- return;
- }
- if (hasAnnouncedLoadingRef.current) {
- return;
- }
- let frame = requestAnimationFrame(() => {
- frame = requestAnimationFrame(() => {
- hasAnnouncedLoadingRef.current = true;
- announce(loadingText, 'polite');
- });
- });
- return () => cancelAnimationFrame(frame);
- }, [announce, isLoadingRowShown, loadingText]);
+ const settleBusyAnnouncement = useBusyAnnouncement(
+ isLoadingRowShown,
+ loadingText,
+ editableRef,
+ );
const triggerStartRef = useRef<number>(-1);
const searchTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
@@ -570,6 +556,9 @@ export function useTriggerMenu(
};
const applyResults = (items: SearchableItem[]) => {
+ // Results that arrive before the busy start is heard withdraw it, so a
+ // search that settles on its first frame announces nothing (FR8).
+ settleBusyAnnouncement();
const isHidden = isUnmatchedMultiWordQuery(trigger, items, false);
setState(prev => ({
...prev,
@@ -596,7 +585,7 @@ export function useTriggerMenu(
trigger.searchSource.cancel?.();
const result = trigger.searchSource.search(query);
// A synchronous source already has its results, so it never shows
- // or announces the loading row (spec:AST-086 FR7).
+ // or announces the loading row.
if (!Array.isArray(result)) {
setState(prev => ({...prev, isLoading: true}));
}
@@ -638,6 +627,7 @@ export function useTriggerMenu(
setAnchor,
hidePopover,
showPopover,
+ settleBusyAnnouncement,
],
);