Skip to content

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

TypeScript

fix(combobox): scroll highlighted option into view (comboboxes-5) (#3377)

Cindy Zhang5a5fdfbddf01
diff --git a/.changeset/combobox-scroll-into-view.md b/.changeset/combobox-scroll-into-view.md
new file mode 100644
index 0000000000..cf94be9f9b
--- /dev/null
+++ b/.changeset/combobox-scroll-into-view.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Selector/MultiSelector/Typeahead: the highlighted option is now scrolled into view during keyboard navigation, so arrow keys no longer move the highlight off-screen in long lists (matches CommandPalette) (#3343).
+@cixzhang
diff --git a/packages/core/src/MultiSelector/MultiSelector.test.tsx b/packages/core/src/MultiSelector/MultiSelector.test.tsx
index bc7c25d0b3..71b016ce30 100644
--- a/packages/core/src/MultiSelector/MultiSelector.test.tsx
+++ b/packages/core/src/MultiSelector/MultiSelector.test.tsx
@@ -736,5 +736,39 @@ describe('MultiSelector', () => {
const clear = screen.getByRole('button', {name: 'Clear all Fruit'});
expect(clear).not.toHaveAttribute('tabIndex', '-1');
});
+
+ it('scrolls the highlighted option into view during arrow navigation', async () => {
+ const scrollIntoView = vi.fn();
+ Object.defineProperty(HTMLElement.prototype, 'scrollIntoView', {
+ configurable: true,
+ value: scrollIntoView,
+ });
+ try {
+ const user = userEvent.setup();
+ const longOptions = Array.from(
+ {length: 20},
+ (_, i) => `Option ${i + 1}`,
+ );
+ render(
+ <MultiSelector
+ label="Fruit"
+ options={longOptions}
+ value={[]}
+ onChange={() => {}}
+ />,
+ );
+
+ const trigger = screen.getByRole('combobox');
+ await user.click(trigger);
+ scrollIntoView.mockClear();
+ await user.keyboard('{ArrowDown}');
+ await user.keyboard('{ArrowDown}');
+
+ expect(scrollIntoView).toHaveBeenCalledWith({block: 'nearest'});
+ } finally {
+ delete (HTMLElement.prototype as unknown as {scrollIntoView?: unknown})
+ .scrollIntoView;
+ }
+ });
});
});
diff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsx
index ae698c9f51..0929862acf 100644
--- a/packages/core/src/MultiSelector/MultiSelector.tsx
+++ b/packages/core/src/MultiSelector/MultiSelector.tsx
@@ -846,6 +846,19 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
listboxId,
});
+ // Keep the highlighted option visible during keyboard navigation. The
+ // listbox is a fixed-height scroll container, so without this the virtual
+ // cursor walks off-screen once navigation passes the visible window. Mirrors
+ // CommandPaletteItem's scrollIntoView({block: 'nearest'}) behavior.
+ useEffect(() => {
+ if (!popover.isOpen || highlightedIndex < 0) {
+ return;
+ }
+ document
+ .getElementById(getItemId(highlightedIndex))
+ ?.scrollIntoView?.({block: 'nearest'});
+ }, [popover.isOpen, highlightedIndex, getItemId]);
+
// Build trigger display content
const selectedLabels = useMemo(() => {
return optimisticValue.map(v => {
diff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsx
index 9aff593e7b..936d586c52 100644
--- a/packages/core/src/Selector/Selector.test.tsx
+++ b/packages/core/src/Selector/Selector.test.tsx
@@ -552,5 +552,32 @@ describe('Selector', () => {
const clear = screen.getByRole('button', {name: 'Clear Fruit'});
expect(clear).not.toHaveAttribute('tabIndex', '-1');
});
+
+ it('scrolls the highlighted option into view during arrow navigation', async () => {
+ const scrollIntoView = vi.fn();
+ Object.defineProperty(HTMLElement.prototype, 'scrollIntoView', {
+ configurable: true,
+ value: scrollIntoView,
+ });
+ try {
+ const user = userEvent.setup();
+ const longOptions = Array.from(
+ {length: 20},
+ (_, i) => `Option ${i + 1}`,
+ );
+ render(<Selector label="Fruit" options={longOptions} />);
+
+ await user.tab();
+ await user.keyboard('{Enter}'); // open
+ scrollIntoView.mockClear();
+ await user.keyboard('{ArrowDown}'); // move highlight
+ await user.keyboard('{ArrowDown}');
+
+ expect(scrollIntoView).toHaveBeenCalledWith({block: 'nearest'});
+ } finally {
+ delete (HTMLElement.prototype as unknown as {scrollIntoView?: unknown})
+ .scrollIntoView;
+ }
+ });
});
});
diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsx
index 3d3583286e..43c61963c9 100644
--- a/packages/core/src/Selector/Selector.tsx
+++ b/packages/core/src/Selector/Selector.tsx
@@ -688,6 +688,19 @@ export function Selector<T extends SelectorOptionType>(
listboxId,
});
+ // Keep the highlighted option visible during keyboard navigation. The
+ // listbox is a fixed-height scroll container, so without this the virtual
+ // cursor walks off-screen once navigation passes the visible window. Mirrors
+ // CommandPaletteItem's scrollIntoView({block: 'nearest'}) behavior.
+ useEffect(() => {
+ if (!popover.isOpen || highlightedIndex < 0) {
+ return;
+ }
+ document
+ .getElementById(getItemId(highlightedIndex))
+ ?.scrollIntoView?.({block: 'nearest'});
+ }, [popover.isOpen, highlightedIndex, getItemId]);
+
// Handle clear button click
const handleClear = useCallback(
(e: React.MouseEvent) => {
diff --git a/packages/core/src/Typeahead/BaseTypeahead.tsx b/packages/core/src/Typeahead/BaseTypeahead.tsx
index 5cfd120a4e..e32c40c9ad 100644
--- a/packages/core/src/Typeahead/BaseTypeahead.tsx
+++ b/packages/core/src/Typeahead/BaseTypeahead.tsx
@@ -48,9 +48,10 @@ import {themeProps} from '../utils/themeProps';
// Types
// =============================================================================
-export interface BaseTypeaheadProps<
- T extends SearchableItem,
-> extends Omit<BaseProps<HTMLElement>, 'onChange'> {
+export interface BaseTypeaheadProps<T extends SearchableItem> extends Omit<
+ BaseProps<HTMLElement>,
+ 'onChange'
+> {
ref?: React.Ref<HTMLInputElement>;
/**
* Search source providing items.
@@ -280,9 +281,7 @@ const itemSizeStyles = stylex.create({
* />
* ```
*/
-export const BaseTypeahead = function BaseTypeahead<
- T extends SearchableItem,
->({
+export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({
searchSource,
value,
onChange,
@@ -615,6 +614,19 @@ export const BaseTypeahead = function BaseTypeahead<
[listboxId],
);
+ // Keep the highlighted option visible during keyboard navigation. The
+ // listbox is a fixed-height scroll container, so without this the virtual
+ // cursor walks off-screen once navigation passes the visible window. Mirrors
+ // CommandPaletteItem's scrollIntoView({block: 'nearest'}) behavior.
+ useEffect(() => {
+ if (!popover.isOpen || highlightedIndex < 0) {
+ return;
+ }
+ document
+ .getElementById(getItemId(highlightedIndex))
+ ?.scrollIntoView?.({block: 'nearest'});
+ }, [popover.isOpen, highlightedIndex, getItemId]);
+
const selectedKey =
value == null ? null : getKey(value.id, () => results.indexOf(value));
diff --git a/packages/core/src/Typeahead/Typeahead.test.tsx b/packages/core/src/Typeahead/Typeahead.test.tsx
index 3ec22a866e..21427ef4ba 100644
--- a/packages/core/src/Typeahead/Typeahead.test.tsx
+++ b/packages/core/src/Typeahead/Typeahead.test.tsx
@@ -573,4 +573,39 @@ describe('BaseTypeahead paste behavior', () => {
expect(screen.getByText('No results found')).toBeInTheDocument();
});
});
+
+ it('scrolls the highlighted option into view during arrow navigation', async () => {
+ const scrollIntoView = vi.fn();
+ Object.defineProperty(HTMLElement.prototype, 'scrollIntoView', {
+ configurable: true,
+ value: scrollIntoView,
+ });
+ try {
+ 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 multiple fruits, opens listbox
+ await waitFor(() => {
+ expect(screen.getByRole('listbox', {hidden: true})).toBeInTheDocument();
+ });
+
+ scrollIntoView.mockClear();
+ await user.keyboard('{ArrowDown}');
+ await user.keyboard('{ArrowDown}');
+
+ expect(scrollIntoView).toHaveBeenCalledWith({block: 'nearest'});
+ } finally {
+ delete (HTMLElement.prototype as unknown as {scrollIntoView?: unknown})
+ .scrollIntoView;
+ }
+ });
});