An open source design system that's fully customizable and agent ready
TypeScript
fix(typeahead): close dropdown when focus leaves the input (#3340)
Cindy Zhang1673ce116c99
diff --git a/.changeset/typeahead-close-on-focus-out.md b/.changeset/typeahead-close-on-focus-out.mdnew file mode 100644index 0000000000..cfc430a9fc--- /dev/null+++ b/.changeset/typeahead-close-on-focus-out.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Typeahead/Tokenizer: close the results dropdown when focus leaves the input (e.g. tabbing away), matching the existing outside-click and Escape dismissal. Previously the menu could stay open after the trigger lost focus.++@cixzhangdiff --git a/packages/core/src/Typeahead/BaseTypeahead.tsx b/packages/core/src/Typeahead/BaseTypeahead.tsxindex eb356b5573..df13014e4b 100644--- a/packages/core/src/Typeahead/BaseTypeahead.tsx+++ b/packages/core/src/Typeahead/BaseTypeahead.tsx@@ -541,6 +541,32 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({showLayer,]);+ // Handle blur — close the dropdown when focus leaves the input for an+ // element that is neither inside the field wrapper (anchor) nor inside the+ // dropdown popover. The native popover="auto" light-dismiss only fires on+ // outside pointer clicks and Escape; it does not close when focus moves away+ // via the keyboard (Tab) or programmatically, which would otherwise leave an+ // orphaned open menu. Clicking a result moves focus onto the option (it is+ // tabIndex={-1}, so it lives inside the popover) and selection re-focuses the+ // input, so this only closes on a genuine focus-out of the whole field.+ const handleBlur = useCallback(+ (e: React.FocusEvent<HTMLInputElement>) => {+ if (!popover.isOpen) {+ return;+ }+ const next = e.relatedTarget as Node | null;+ if (next) {+ const anchorEl = anchorRef?.current ?? fallbackAnchorRef.current;+ const popoverEl = document.getElementById(popover.id);+ if (anchorEl?.contains(next) || popoverEl?.contains(next)) {+ return;+ }+ }+ popover.hide();+ },+ [popover, anchorRef],+ );+// Keyboard navigationconst handleKeyDown = useCallback((e: React.KeyboardEvent<HTMLInputElement>) => {@@ -672,6 +698,7 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({);}}onFocus={handleFocus}+ onBlur={handleBlur}onKeyDown={handleKeyDown}placeholder={placeholder}disabled={isDisabled}diff --git a/packages/core/src/Typeahead/Typeahead.test.tsx b/packages/core/src/Typeahead/Typeahead.test.tsxindex 21427ef4ba..810d973e46 100644--- a/packages/core/src/Typeahead/Typeahead.test.tsx+++ b/packages/core/src/Typeahead/Typeahead.test.tsx@@ -180,6 +180,90 @@ describe('BaseTypeahead', () => {});});+describe('BaseTypeahead focus-out', () => {+ it('closes the dropdown when focus leaves the input', async () => {+ render(+ <>+ <BaseTypeahead+ searchSource={fruitSource}+ value={null}+ onChange={() => {}}+ debounceMs={0}+ />+ <button type="button">Outside</button>+ </>,+ );+ const input = screen.getByRole('combobox');+ fireEvent.change(input, {target: {value: 'App'}});++ await waitFor(() => {+ expect(input).toHaveAttribute('aria-expanded', 'true');+ });++ // Focus moves to an element outside the field/dropdown → menu closes.+ const outside = screen.getByRole('button', {name: 'Outside'});+ fireEvent.blur(input, {relatedTarget: outside});++ await waitFor(() => {+ expect(input).toHaveAttribute('aria-expanded', 'false');+ });+ });++ it('keeps the dropdown open when focus moves into the anchor wrapper', async () => {+ const anchor = document.createElement('div');+ document.body.appendChild(anchor);+ const anchorRef = {current: anchor};+ render(+ <BaseTypeahead+ searchSource={fruitSource}+ value={null}+ onChange={() => {}}+ anchorRef={anchorRef}+ debounceMs={0}+ />,+ );+ const input = screen.getByRole('combobox');+ // The input lives inside the wrapper we hand to anchorRef.+ anchor.appendChild(input.closest('div') ?? input);+ fireEvent.change(input, {target: {value: 'App'}});++ await waitFor(() => {+ expect(input).toHaveAttribute('aria-expanded', 'true');+ });++ // A sibling control inside the field (e.g. a clear button) receives focus.+ const sibling = document.createElement('button');+ anchor.appendChild(sibling);+ fireEvent.blur(input, {relatedTarget: sibling});++ // Menu stays open because focus is still within the field.+ expect(input).toHaveAttribute('aria-expanded', 'true');+ document.body.removeChild(anchor);+ });++ it('does not close when a dropdown option receives focus', async () => {+ render(+ <BaseTypeahead+ searchSource={fruitSource}+ value={null}+ onChange={() => {}}+ debounceMs={0}+ />,+ );+ const input = screen.getByRole('combobox');+ fireEvent.change(input, {target: {value: 'App'}});++ await waitFor(() => {+ expect(input).toHaveAttribute('aria-expanded', 'true');+ });++ const option = screen.getByRole('option', {hidden: true});+ fireEvent.blur(input, {relatedTarget: option});++ expect(input).toHaveAttribute('aria-expanded', 'true');+ });+});+describe('Typeahead', () => {it('renders with label', () => {render(