An open source design system that's fully customizable and agent ready
TypeScript
fix(Typeahead): highlight follows pointer movement, not a resting pointer (#8247)
EJ Hammond2597fa4fbbcb
diff --git a/.changeset/typeahead-highlight-follows-pointer-movement.md b/.changeset/typeahead-highlight-follows-pointer-movement.mdnew file mode 100644index 0000000000..58128b3a67--- /dev/null+++ b/.changeset/typeahead-highlight-follows-pointer-movement.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] BaseTypeahead, and the Typeahead, Tokenizer and PowerSearch menus built on it: an option now takes the highlight when the pointer moves over it, not when it merely appears under a resting pointer. A menu opened from the keyboard, or one that scrolls during arrow-key navigation, used to hand its highlight to whichever option slid under the cursor, so Enter could pick an option the user never pointed at and arrow keys appeared to jump back. The keyboard highlight now stays put until the pointer actually moves.++@ejhammonddiff --git a/packages/core/src/PowerSearch/PowerSearch.test.tsx b/packages/core/src/PowerSearch/PowerSearch.test.tsxindex 79c9416bab..fd1f491dc6 100644--- a/packages/core/src/PowerSearch/PowerSearch.test.tsx+++ b/packages/core/src/PowerSearch/PowerSearch.test.tsx@@ -881,6 +881,51 @@ describe('field menu grouping', () => {});});+describe('field menu highlight under a resting pointer', () => {+ it('keeps the first field highlighted when the menu opens under the pointer', async () => {+ const user = userEvent.setup();+ render(<PowerSearchWrapper config={config} />);+ const input = screen.getByRole('combobox', {name: 'Search'});++ // Open the field menu from the keyboard; the option that lands under a+ // resting pointer receives mouseenter but no mousemove.+ await user.tab();+ const status = await screen.findByRole('option', {+ name: 'Status',+ hidden: true,+ });+ const title = screen.getByRole('option', {name: 'Title', hidden: true});+ expect(input.getAttribute('aria-activedescendant')).toBe(title.id);++ fireEvent.mouseEnter(status);+ expect(input.getAttribute('aria-activedescendant')).toBe(title.id);++ // Enter acts on the highlighted first field, not the one under the+ // pointer.+ await user.keyboard('{Enter}');+ expect(+ await screen.findByRole('button', {name: 'Apply', hidden: true}),+ ).toBeInTheDocument();+ expect(+ screen.getByRole('combobox', {name: 'Field', hidden: true}),+ ).toHaveTextContent('Title');+ });++ it('moves the highlight when the pointer moves over a field', async () => {+ const user = userEvent.setup();+ render(<PowerSearchWrapper config={config} />);+ const input = screen.getByRole('combobox', {name: 'Search'});++ await user.tab();+ const status = await screen.findByRole('option', {+ name: 'Status',+ hidden: true,+ });+ fireEvent.mouseMove(status);+ expect(input.getAttribute('aria-activedescendant')).toBe(status.id);+ });+});+describe('field menu after the search source changes', () => {it('opens the new field menu on the first click after a config change', async () => {const user = userEvent.setup();diff --git a/packages/core/src/Typeahead/BaseTypeahead.tsx b/packages/core/src/Typeahead/BaseTypeahead.tsxindex e12e4514c6..4f4cc27b7b 100644--- a/packages/core/src/Typeahead/BaseTypeahead.tsx+++ b/packages/core/src/Typeahead/BaseTypeahead.tsx@@ -1276,7 +1276,12 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({aria-selected={isSelected}tabIndex={-1}onClick={() => handleSelect(item)}- onMouseEnter={() => highlightOnHover(index)}+ // Highlight follows pointer movement, not pointer+ // presence: a menu that opens or reflows under a resting+ // pointer keeps its keyboard highlight, so Enter acts on+ // the option the user was shown, not the one that+ // happened to appear under the cursor.+ onMouseMove={() => highlightOnHover(index)}{...stylex.props(styles.item,itemSizeStyles[size],diff --git a/packages/core/src/Typeahead/Typeahead.test.tsx b/packages/core/src/Typeahead/Typeahead.test.tsxindex afa2393dc6..b39d5afea8 100644--- a/packages/core/src/Typeahead/Typeahead.test.tsx+++ b/packages/core/src/Typeahead/Typeahead.test.tsx@@ -2029,7 +2029,7 @@ describe('BaseTypeahead paste behavior', () => {scrollIntoView.mockClear();const options = screen.getAllByRole('option', {hidden: true});- fireEvent.mouseEnter(options[1]);+ fireEvent.mouseMove(options[1]);expect(input.getAttribute('aria-activedescendant')).toBe(options[1].id);expect(scrollIntoView).not.toHaveBeenCalled();@@ -2043,6 +2043,39 @@ describe('BaseTypeahead paste behavior', () => {});});+describe('BaseTypeahead highlight under a resting pointer', () => {+ it('keeps the keyboard highlight when an option appears under the pointer, and follows pointer movement', async () => {+ // A menu that opens or reflows under a pointer the user is not moving+ // fires mouseenter on the option that lands beneath it. Taking the+ // highlight from that would make Enter pick an option the user never+ // pointed at; only real pointer movement moves the highlight.+ const user = userEvent.setup();+ render(+ <BaseTypeahead+ searchSource={fruitSource}+ value={null}+ onChange={() => {}}+ debounceMs={0}+ />,+ );++ const input = screen.getByRole('combobox');+ await user.click(input);+ await user.paste('e'); // matches Cherry, Date, Elderberry+ await waitFor(() => {+ expect(screen.getByRole('listbox', {hidden: true})).toBeInTheDocument();+ });+ const options = screen.getAllByRole('option', {hidden: true});+ expect(input.getAttribute('aria-activedescendant')).toBe(options[0].id);++ fireEvent.mouseEnter(options[2]);+ expect(input.getAttribute('aria-activedescendant')).toBe(options[0].id);++ fireEvent.mouseMove(options[2]);+ expect(input.getAttribute('aria-activedescendant')).toBe(options[2].id);+ });+});+describe('Typeahead disabledMessage', () => {// jsdom does not implement the Popover API used by the tooltip, so mock// showPopover/hidePopover to toggle a `popover-open` attribute the tests