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(promptfields): keep wildcard autocomplete open while scrolling (#9999)

Matt Van Horn365b22d3c94a
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PositivePromptField.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PositivePromptField.browser.test.tsx
index af5dcfc989..a10c71fcac 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PositivePromptField.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PositivePromptField.browser.test.tsx
@@ -1,6 +1,7 @@
/* oxlint-disable react-perf/jsx-no-new-array-as-prop, react-perf/jsx-no-new-function-as-prop */
import type { PromptTemplateSnapshot } from '@features/generation/core/promptTemplates';
import type { GenerateModelConfig } from '@features/generation/core/types';
+import type { WildcardRecord } from '@features/generation/data/wildcards';
import type { DynamicPromptsFieldConfig } from '@features/generation/ui/promptFields/DynamicPromptsPanel';
import { ChakraProvider } from '@chakra-ui/react';
@@ -10,7 +11,7 @@ import { adjustFocusedPromptAttention } from '@features/generation/ui/promptFiel
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { system } from '@theme/system';
import i18next from 'i18next';
-import { act, useSyncExternalStore } from 'react';
+import { act, useState, useSyncExternalStore } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { I18nextProvider, initReactI18next } from 'react-i18next';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -63,6 +64,8 @@ vi.mock('@features/generation/ui/GenerationUiContext', async (importOriginal) =>
}),
}));
+const wildcardRecords: WildcardRecord[] = [];
+
vi.mock('@features/generation/data/promptUtilities', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
parseDynamicPrompts,
@@ -75,7 +78,10 @@ vi.mock('@features/generation/data/wildcards', async (importOriginal) => ({
deleteWildcard: vi.fn(),
invalidateWildcardDependents: vi.fn(),
updateWildcard: vi.fn(),
- wildcardsQueryOptions: () => ({ queryFn: () => Promise.resolve([]), queryKey: ['generation', 'wildcards'] }),
+ wildcardsQueryOptions: () => ({
+ queryFn: () => Promise.resolve(wildcardRecords),
+ queryKey: ['generation', 'wildcards'],
+ }),
}));
let host: HTMLDivElement | null = null;
@@ -87,6 +93,7 @@ afterEach(async () => {
host?.remove();
host = null;
root = null;
+ wildcardRecords.length = 0;
});
it('keeps positive prompt actions enabled when stale view mode has no active template', async () => {
@@ -389,3 +396,62 @@ it('shows a JSON prompt under a template whole, as it generates, and never expan
expect(dynamicButton?.textContent).not.toMatch(/\d/);
expect(parseDynamicPrompts).not.toHaveBeenCalled();
});
+
+it('keeps overflowing wildcard suggestions open when the list scrolls', async () => {
+ wildcardRecords.push(
+ ...Array.from({ length: 24 }, (_, index) => ({ id: `wildcard-${index}`, name: `w${index}`, values: ['red'] }))
+ );
+ const Prompt = () => {
+ const [value, setValue] = useState('');
+
+ return (
+ <PositivePromptField
+ heightPx={96}
+ loras={[]}
+ projectId="project-1"
+ selectedModel={undefined}
+ showSyntaxHighlighting={false}
+ value={value}
+ onChange={setValue}
+ onResizeEnd={vi.fn()}
+ onUsePrompt={vi.fn()}
+ />
+ );
+ };
+
+ host = document.createElement('div');
+ document.body.append(host);
+ root = createRoot(host);
+ await act(() => {
+ root?.render(
+ <I18nextProvider i18n={i18n}>
+ <QueryClientProvider client={new QueryClient()}>
+ <ChakraProvider value={system}>
+ <DndContext>
+ <Prompt />
+ </DndContext>
+ </ChakraProvider>
+ </QueryClientProvider>
+ </I18nextProvider>
+ );
+ });
+ await act(async () => {
+ await userEvent.type(host!.querySelector('textarea')!, '__');
+ });
+ await expect.poll(() => document.querySelector('[role="listbox"]')).not.toBeNull();
+
+ const list = document.querySelector<HTMLElement>('[role="listbox"]')!;
+
+ expect(list.scrollHeight).toBeGreaterThan(list.clientHeight);
+ await act(async () => {
+ const scrolled = new Promise<void>((resolve) => {
+ list.addEventListener('scroll', () => resolve(), { once: true });
+ });
+
+ list.scrollTop = list.scrollHeight;
+ await scrolled;
+ });
+
+ expect(document.querySelector('[role="listbox"]')).toBe(list);
+ expect(list.scrollTop).toBeGreaterThan(0);
+});
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerAutocomplete.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerAutocomplete.tsx
index a636e8c269..dc393f4ba3 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerAutocomplete.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerAutocomplete.tsx
@@ -7,6 +7,7 @@ import type { MouseEvent } from 'react';
import { Box, Portal, Stack, Text } from '@chakra-ui/react';
import { groupPromptTriggerOptions } from '@features/generation/ui/promptFields/promptTriggerOptions';
+import { PROMPT_TRIGGER_AUTOCOMPLETE_SCROLL_PROPS } from '@features/generation/ui/promptFields/useDismissOnViewportChange';
import { MiddleTruncate } from '@platform/ui/MiddleTruncate';
import { useCallback, useLayoutEffect, useMemo, useRef } from 'react';
import { useTranslation } from 'react-i18next';
@@ -70,6 +71,7 @@ export const PromptTriggerAutocomplete = ({
return (
<Portal>
<Box
+ {...PROMPT_TRIGGER_AUTOCOMPLETE_SCROLL_PROPS}
aria-label={t('widgets.generate.completionSuggestions')}
bg="bg.muted"
borderColor="border.emphasized"
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/useDismissOnViewportChange.ts b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/useDismissOnViewportChange.ts
index 7b4c97ee1b..9da8cd2700 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/useDismissOnViewportChange.ts
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/useDismissOnViewportChange.ts
@@ -1,6 +1,16 @@
import { useMountEffect } from '@platform/react/useMountEffect';
-/** Dismiss stale page-space anchors on scrolling, excluding the popover's own scroll. */
+/**
+ * Marks the prompt-trigger autocomplete list, the scroll container for its options.
+ * Scrolling that list is the suggestion moving, not the anchored field leaving the viewport.
+ */
+const PROMPT_TRIGGER_AUTOCOMPLETE_ATTR = 'data-prompt-trigger-autocomplete';
+export const PROMPT_TRIGGER_AUTOCOMPLETE_SCROLL_PROPS = { [PROMPT_TRIGGER_AUTOCOMPLETE_ATTR]: '' } as const;
+
+/** Popover content and the autocomplete list. Descendants count: the event target may be an option. */
+const EXEMPT_SCROLL_SELECTOR = `[data-scope="popover"][data-part="content"], [${PROMPT_TRIGGER_AUTOCOMPLETE_ATTR}]`;
+
+/** Dismiss stale page-space anchors on scroll or resize, excluding scrolls inside an anchored surface. */
export const useDismissOnViewportChange = (enabled: boolean, dismiss: () => void): void => {
useMountEffect(() => {
if (!enabled) {
@@ -8,7 +18,8 @@ export const useDismissOnViewportChange = (enabled: boolean, dismiss: () => void
}
const handleViewportChange = (event: Event) => {
- if (event.target instanceof Element && event.target.closest('[data-scope="popover"][data-part="content"]')) {
+ // Resize targets the window, so it still dismisses. Only element scrolls can be internal.
+ if (event.target instanceof Element && event.target.closest(EXEMPT_SCROLL_SELECTOR)) {
return;
}