Skip to content

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

TypeScript

fix(core): PowerSearch does not announce its initial resultCount under StrictMode (#8650)

Xinzi Zhoucf17cf2d63ec
diff --git a/.changeset/powersearch-result-count-strictmode.md b/.changeset/powersearch-result-count-strictmode.md
new file mode 100644
index 0000000000..dbdc94490c
--- /dev/null
+++ b/.changeset/powersearch-result-count-strictmode.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] PowerSearch no longer announces its initial `resultCount` on mount under React StrictMode. The result-count live-region effect guarded its first run with a mounted flag that StrictMode's effect replay reset, so the count present on mount was spoken unprompted; the guard now compares against the last announced text, so only a change after mount is announced in every render mode.
+
+@imdreamrunner
diff --git a/packages/core/src/PowerSearch/PowerSearch.test.tsx b/packages/core/src/PowerSearch/PowerSearch.test.tsx
index fd1f491dc6..113c03e88f 100644
--- a/packages/core/src/PowerSearch/PowerSearch.test.tsx
+++ b/packages/core/src/PowerSearch/PowerSearch.test.tsx
@@ -9,7 +9,7 @@
* SYNC: When PowerSearch.tsx changes, update tests to match
*/
-import {useState} from 'react';
+import {StrictMode, useState} from 'react';
import {describe, it, expect, vi, beforeAll, afterAll, afterEach} from 'vitest';
import {
render,
@@ -476,6 +476,37 @@ describe('PowerSearch', () => {
});
});
+ it('does not announce the result count present on mount under StrictMode, only a later change', async () => {
+ // StrictMode replays the mount effects: a mounted-flag guard is reset by
+ // the replay and speaks the initial count unprompted; comparing against
+ // the last announced text does not.
+ const {rerender} = render(
+ <StrictMode>
+ <PowerSearch
+ config={config}
+ filters={[]}
+ onChange={() => {}}
+ resultCount={12}
+ />
+ </StrictMode>,
+ );
+ await new Promise(resolve => setTimeout(resolve, 20));
+ expect(politeRegion()?.textContent ?? '').toBe('');
+ rerender(
+ <StrictMode>
+ <PowerSearch
+ config={config}
+ filters={[]}
+ onChange={() => {}}
+ resultCount={1}
+ />
+ </StrictMode>,
+ );
+ await waitFor(() => {
+ expect(politeRegion()).toHaveTextContent('1 result');
+ });
+ });
+
it('announces "1 result" (singular) for a single match', async () => {
const {rerender} = render(
<PowerSearch
diff --git a/packages/core/src/PowerSearch/PowerSearch.tsx b/packages/core/src/PowerSearch/PowerSearch.tsx
index f91501832e..50f5154aac 100644
--- a/packages/core/src/PowerSearch/PowerSearch.tsx
+++ b/packages/core/src/PowerSearch/PowerSearch.tsx
@@ -1057,15 +1057,17 @@ export function PowerSearch({
// Announce result-count changes to screen readers through a polite live
// region, mirroring the way Typeahead announces its dropdown result count.
// The count is otherwise only shown visually and stays silent to assistive
- // tech. Skip the first run so the count already present on mount isn't
- // announced unprompted — only user-driven changes are spoken.
+ // tech. Only a change after mount is spoken: the guard compares against the
+ // last announced text rather than a mounted flag, so StrictMode's effect
+ // replay (which resets a flag and would speak the count present on mount
+ // unprompted) announces nothing until the count changes.
const announce = useAnnounce();
- const hasMountedRef = useRef(false);
+ const announcedResultCountRef = useRef(resultCountText);
useEffect(() => {
- if (!hasMountedRef.current) {
- hasMountedRef.current = true;
+ if (resultCountText === announcedResultCountRef.current) {
return;
}
+ announcedResultCountRef.current = resultCountText;
if (resultCountText != null) {
announce(resultCountText);
}