Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(webv2): use the scroll-area scrollbar in the combobox list

Josh Corbett523a3b2418d1
diff --git a/invokeai/frontend/webv2/src/platform/ui/Combobox.browser.test.tsx b/invokeai/frontend/webv2/src/platform/ui/Combobox.browser.test.tsx
index 2064e5ee91..6392278b02 100644
--- a/invokeai/frontend/webv2/src/platform/ui/Combobox.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/platform/ui/Combobox.browser.test.tsx
@@ -191,6 +191,58 @@ describe('Combobox', () => {
expect(document.querySelectorAll('[role="option"]')).toHaveLength(options.length);
});
+ it('scrolls long lists in the themed viewport, following the keyboard highlight and reporting the end', async () => {
+ const longOptions = Array.from({ length: 40 }, (_, index) => ({
+ label: `Scheduler ${index + 1}`,
+ value: `scheduler_${index + 1}`,
+ }));
+ const onListScrollToBottom = vi.fn();
+ host = document.createElement('div');
+ document.body.append(host);
+ root = createRoot(host);
+ await interact(() => {
+ root?.render(
+ <I18nextProvider i18n={i18n}>
+ <ChakraProvider value={system}>
+ <Combobox
+ aria-label="Scheduler"
+ options={longOptions}
+ value="scheduler_1"
+ onListScrollToBottom={onListScrollToBottom}
+ onValueChange={() => undefined}
+ />
+ </ChakraProvider>
+ </I18nextProvider>
+ );
+ });
+ const input = host.querySelector<HTMLInputElement>('input[role="combobox"]')!;
+
+ await interact(() => input.click());
+ const content = document.querySelector<HTMLElement>('[data-scope="combobox"][data-part="content"]')!;
+ const viewport = content.querySelector<HTMLElement>('[data-scope="scroll-area"][data-part="viewport"]')!;
+ expect(viewport.scrollHeight).toBeGreaterThan(viewport.clientHeight);
+ expect(content.scrollHeight).toBe(content.clientHeight);
+
+ for (let step = 0; step < 20; step += 1) {
+ await interact(() => {
+ input.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true, key: 'ArrowDown' }));
+ });
+ }
+ const highlighted = content.querySelector<HTMLElement>('[role="option"][data-highlighted]')!;
+ const highlightedRect = highlighted.getBoundingClientRect();
+ const viewportRect = viewport.getBoundingClientRect();
+ expect(highlighted.textContent).toContain('Scheduler 21');
+ expect(highlightedRect.top).toBeGreaterThanOrEqual(viewportRect.top);
+ expect(highlightedRect.bottom).toBeLessThanOrEqual(viewportRect.bottom);
+ expect(onListScrollToBottom).not.toHaveBeenCalled();
+
+ await interact(() => {
+ viewport.scrollTop = viewport.scrollHeight;
+ viewport.dispatchEvent(new Event('scroll'));
+ });
+ expect(onListScrollToBottom).toHaveBeenCalled();
+ });
+
it('honors the disabled state', async () => {
const { input } = await renderCombobox(true);
diff --git a/invokeai/frontend/webv2/src/platform/ui/Combobox.tsx b/invokeai/frontend/webv2/src/platform/ui/Combobox.tsx
index 36188d8534..108ae02537 100644
--- a/invokeai/frontend/webv2/src/platform/ui/Combobox.tsx
+++ b/invokeai/frontend/webv2/src/platform/ui/Combobox.tsx
@@ -10,12 +10,13 @@ import { CheckIcon, ChevronDownIcon } from 'lucide-react';
import { useCallback, useMemo, useRef, useState, type UIEvent } from 'react';
import { useTranslation } from 'react-i18next';
+import { Scrollable } from './Scrollable';
+
const COMBOBOX_POSITIONING = { placement: 'bottom-start', sameWidth: true } as const;
-const LIST_SCROLL_CSS = {
- scrollbarColor: 'var(--chakra-colors-border-emphasized) transparent',
- scrollbarWidth: 'thin',
-} as const;
+// Zag scrolls its content element by default; the Scrollable viewport is the scroll container here.
+const scrollHighlightedIntoView = ({ getElement }: { getElement: () => HTMLElement | null }) =>
+ getElement()?.scrollIntoView({ block: 'nearest' });
export interface ComboboxOption extends CollectionItem {
disabled?: boolean;
@@ -125,13 +126,19 @@ export const Combobox = ({
const handleListScroll = useCallback(
(event: UIEvent<HTMLDivElement>) => {
const list = event.currentTarget;
+ // The scroll area dispatches no-op scrolls while the popup is still unmeasured; only real overflow counts.
+ const overflows = list.scrollHeight > list.clientHeight;
- if (list.scrollHeight - list.scrollTop - list.clientHeight <= 24) {
+ if (overflows && list.scrollHeight - list.scrollTop - list.clientHeight <= 24) {
onListScrollToBottom?.();
}
},
[onListScrollToBottom]
);
+ const listViewportProps = useMemo(
+ () => (onListScrollToBottom ? { onScroll: handleListScroll } : undefined),
+ [handleListScroll, onListScrollToBottom]
+ );
const handleValueChange = useCallback(
(details: ComboboxValueChangeDetails<ComboboxOption>) => {
const nextValue = details.value[0];
@@ -152,7 +159,10 @@ export const Combobox = ({
);
return (
+ // Mount items only while open to avoid per-combobox scroll observers at rest.
<ChakraCombobox.Root
+ lazyMount
+ unmountOnExit
{...rootProps}
allowCustomValue={false}
closeOnSelect
@@ -163,6 +173,7 @@ export const Combobox = ({
open={isOpen}
openOnClick
positioning={COMBOBOX_POSITIONING}
+ scrollToIndexFn={scrollHighlightedIntoView}
selectionBehavior="replace"
value={selectedValues}
onInputValueChange={handleInputValueChange}
@@ -185,25 +196,21 @@ export const Combobox = ({
<Portal>
<ChakraCombobox.Positioner>
<ChakraCombobox.Content>
- {/* Keep this as Ark's scroll container so keyboard highlight remains visible. */}
- <ChakraCombobox.List
- css={LIST_SCROLL_CSS}
- maxH="16rem"
- overflowY="auto"
- onScroll={onListScrollToBottom ? handleListScroll : undefined}
- >
- {collection.items.map((item) => (
- <ChakraCombobox.Item key={item.value} item={item}>
- <ChakraCombobox.ItemText>{item.label}</ChakraCombobox.ItemText>
- <ChakraCombobox.ItemIndicator>
- <CheckIcon />
- </ChakraCombobox.ItemIndicator>
- </ChakraCombobox.Item>
- ))}
- <ChakraCombobox.Empty color="fg.muted" fontSize="xs" px="3" py="2">
- {noResultsText ?? t('common.noSchedulersFound')}
- </ChakraCombobox.Empty>
- </ChakraCombobox.List>
+ <Scrollable maxH="16rem" viewportProps={listViewportProps}>
+ <ChakraCombobox.List>
+ {collection.items.map((item) => (
+ <ChakraCombobox.Item key={item.value} item={item}>
+ <ChakraCombobox.ItemText>{item.label}</ChakraCombobox.ItemText>
+ <ChakraCombobox.ItemIndicator>
+ <CheckIcon />
+ </ChakraCombobox.ItemIndicator>
+ </ChakraCombobox.Item>
+ ))}
+ <ChakraCombobox.Empty color="fg.muted" fontSize="xs" px="3" py="2">
+ {noResultsText ?? t('common.noSchedulersFound')}
+ </ChakraCombobox.Empty>
+ </ChakraCombobox.List>
+ </Scrollable>
</ChakraCombobox.Content>
</ChakraCombobox.Positioner>
</Portal>