An open source design system that's fully customizable and agent ready
TypeScript
fix(command-palette): keep empty state mounted during pending search (#3516)
Cindy Zhangf8644420227f
diff --git a/.changeset/command-palette-empty-flash.md b/.changeset/command-palette-empty-flash.mdnew file mode 100644index 0000000000..3fd24deeff--- /dev/null+++ b/.changeset/command-palette-empty-flash.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] CommandPalette: the empty state ("No results") no longer flashes when typing further characters into an already-empty search. The empty state stays mounted for the full duration of the pending search instead of briefly unmounting and re-appearing.+@cixzhangdiff --git a/packages/core/src/CommandPalette/CommandPalette.test.tsx b/packages/core/src/CommandPalette/CommandPalette.test.tsxindex 3f9709f3c8..530be637fe 100644--- a/packages/core/src/CommandPalette/CommandPalette.test.tsx+++ b/packages/core/src/CommandPalette/CommandPalette.test.tsx@@ -8,9 +8,10 @@*/import {describe, it, expect, vi, beforeEach} from 'vitest';-import {render, screen, waitFor} from '@testing-library/react';+import {render, screen, waitFor, fireEvent} from '@testing-library/react';import {CommandPalette} from './CommandPalette';import {createStaticSource} from '@astryxdesign/core/Typeahead';+import type {SearchSource, SearchableItem} from '@astryxdesign/core/Typeahead';const simpleSource = createStaticSource([{id: 'home', label: 'Home'},@@ -223,4 +224,49 @@ describe('CommandPalette', () => {dialog.dispatchEvent(escapeEvent);expect(handleOpenChange).toHaveBeenCalledWith(false);});++ it('keeps the empty state mounted while a no-result search is pending', async () => {+ // A source whose searches resolve only when we release them, so we can+ // observe the render output while a search transition is in flight.+ const resolvers: ((items: SearchableItem[]) => void)[] = [];+ const source: SearchSource = {+ bootstrap: () => [],+ async search(): Promise<SearchableItem[]> {+ return new Promise<SearchableItem[]>(resolve => {+ resolvers.push(resolve);+ });+ },+ };++ render(+ <CommandPalette+ isOpen={true}+ onOpenChange={() => {}}+ searchSource={source}+ emptyBootstrapText="Type to search"+ emptySearchText="No results"+ />,+ );++ const input = screen.getByRole('combobox');++ // First search: commits an empty result for query "z".+ fireEvent.change(input, {target: {value: 'z'}});+ await waitFor(() => expect(resolvers).toHaveLength(1));+ resolvers[0]([]);+ await waitFor(() =>+ expect(screen.getByText('No results')).toBeInTheDocument(),+ );++ // Second keystroke while already empty: the empty state must remain in the+ // DOM for the whole pending window — no unmount/remount flash.+ fireEvent.change(input, {target: {value: 'zz'}});+ expect(screen.getByText('No results')).toBeInTheDocument();+ await waitFor(() => expect(resolvers).toHaveLength(2));+ expect(screen.getByText('No results')).toBeInTheDocument();+ resolvers[1]([]);+ await waitFor(() =>+ expect(screen.getByText('No results')).toBeInTheDocument(),+ );+ });});diff --git a/packages/core/src/CommandPalette/CommandPalette.tsx b/packages/core/src/CommandPalette/CommandPalette.tsxindex a6b02e9c46..3a8498ad9d 100644--- a/packages/core/src/CommandPalette/CommandPalette.tsx+++ b/packages/core/src/CommandPalette/CommandPalette.tsx@@ -24,12 +24,7 @@ import {type ReactNode,} from 'react';import {Dialog} from '../Dialog';-import {- Layout,- LayoutHeader,- LayoutContent,- LayoutFooter,-} from '../Layout';+import {Layout, LayoutHeader, LayoutContent, LayoutFooter} from '../Layout';import type {SearchSource, SearchableItem} from '../Typeahead';import {useCombobox} from '../Selector';import type {SelectorOptionData} from '../Selector';@@ -133,9 +128,7 @@ function getGroup(item: SearchableItem): string | undefined {* When groups are present, items are ordered by group (preserving insertion order),* with ungrouped items at the end — matching the DefaultRenderer layout.*/-function buildSelectableItems(- items: SearchableItem[],-): SelectorOptionData[] {+function buildSelectableItems(items: SearchableItem[]): SelectorOptionData[] {const hasGroups = items.some(item => getGroup(item) != null);if (!hasGroups) {@@ -258,9 +251,7 @@ function ItemRenderer<T extends SearchableItem>({* />* ```*/-export function CommandPalette<- T extends SearchableItem = SearchableItem,->({+export function CommandPalette<T extends SearchableItem = SearchableItem>({ref,isOpen,isInline,@@ -488,13 +479,12 @@ export function CommandPalette<],);- // Empty state uses committed search (= what current results correspond to),- // not optimisticSearch (= what the user typed).- // While the transition is pending, search stays at '' so showEmptyBootstrap- // remains true even after the user has started typing.+ // `search` is the committed query the on-screen results correspond to (it+ // still holds the previous query while a transition is pending). Keeping both+ // flags ungated by `isPending` makes them exhaustive over the empty case, so+ // the empty state is never unmounted and re-added mid-search (which flashed).const showEmptyBootstrap = search === '' && optimisticResults.length === 0;- const showEmptySearch =- !isPending && search !== '' && optimisticResults.length === 0;+ const showEmptySearch = search !== '' && optimisticResults.length === 0;let listContent: ReactNode;if (showEmptyBootstrap) {@@ -502,9 +492,7 @@ export function CommandPalette<<CommandPaletteEmpty>{emptyBootstrapText}</CommandPaletteEmpty>);} else if (showEmptySearch) {- listContent = (- <CommandPaletteEmpty>{emptySearchText}</CommandPaletteEmpty>- );+ listContent = <CommandPaletteEmpty>{emptySearchText}</CommandPaletteEmpty>;} else {listContent = (<ItemRenderer