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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(ui): focus the concept picker after removing the last concept
Josh Corbettd2abf5de86cf
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/shared/ConceptRow.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/shared/ConceptRow.browser.test.tsxindex 2a7d027005..939bf68489 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/shared/ConceptRow.browser.test.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/shared/ConceptRow.browser.test.tsx@@ -3,7 +3,7 @@ import type { GenerateLora } from '@features/generation/core/types';import { ChakraProvider } from '@chakra-ui/react';import { flushWorkbenchDrafts } from '@platform/react/draftRegistry';import { system } from '@theme/system';-import { act, Profiler } from 'react';+import { act, Profiler, useCallback, useState } from 'react';import { createRoot, type Root } from 'react-dom/client';import { afterEach, describe, expect, it, vi } from 'vitest';import { userEvent } from 'vitest/browser';@@ -302,4 +302,48 @@ describe('ConceptRow', () => {expect(onRemove).toHaveBeenCalledWith('lora-1');await expect.poll(() => document.activeElement).toBe(second);});++ it('hands focus to the picker the last row was excluded from once that row is removed', async () => {+ // A picker whose only candidate is applied is disabled until the removal renders, and the control before the+ // whole section must not take focus in its place.+ const Section = () => {+ const [loras, setLoras] = useState([makeLora()]);+ const removeAll = useCallback(() => setLoras([]), []);++ return (+ <ChakraProvider value={system}>+ <button type="button">Before the section</button>+ <div>+ <button disabled={loras.length > 0} type="button">+ Add concept+ </button>+ {loras.length > 0 ? (+ <ConceptList label="Concepts" projectId="project-1">+ {loras.map((lora) => (+ <ConceptRow+ key={lora.model.key}+ models={MODELS}+ lora={lora}+ onRemove={removeAll}+ onUpdate={vi.fn()}+ />+ ))}+ </ConceptList>+ ) : null}+ <button type="button">After the list</button>+ </div>+ </ChakraProvider>+ );+ };+ host = document.createElement('div');+ document.body.append(host);+ root = createRoot(host);+ await act(() => root?.render(<Section />));+ const remove = host.querySelector<HTMLElement>('[aria-label="widgets.generate.removeConceptNamed"]');++ remove!.focus();+ await act(() => userEvent.click(remove!));++ await expect.poll(() => document.activeElement?.textContent).toBe('Add concept');+ });});diff --git a/invokeai/frontend/webv2/src/features/generation/ui/shared/ConceptRow.tsx b/invokeai/frontend/webv2/src/features/generation/ui/shared/ConceptRow.tsxindex ef67a5ec51..baed69256d 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/shared/ConceptRow.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/shared/ConceptRow.tsx@@ -60,6 +60,50 @@ const findNeighbourRow = (from: Element | null): HTMLElement | null => {return null;};+interface ListSlot {+ container: Element;+ next: Element | null;+}++/** Where the list sat at each ancestor level, so focus can be placed after the last row unmounts it. */+const captureListSlots = (from: Element | null): ListSlot[] => {+ const slots: ListSlot[] = [];++ for (let node = from?.closest('[role="list"]'); node?.parentElement; node = node.parentElement) {+ slots.push({ container: node.parentElement, next: node.nextElementSibling });+ }++ return slots;+};++const TABBABLE = 'a[href], button, input:not([type="hidden"]), select, textarea, [tabindex], [contenteditable="true"]';++/**+ * The control before the removed list, read after the removal renders: the concept picker, which may only now be+ * enabled once the removed concept is no longer excluded from it.+ */+const findControlBeforeList = (slots: ListSlot[]): HTMLElement | null => {+ for (const { container, next } of slots) {+ if (!container.isConnected) {+ continue;+ }++ const preceding = [...container.querySelectorAll<HTMLElement>(TABBABLE)].filter(+ (element) =>+ element.tabIndex >= 0 &&+ !element.matches(':disabled') &&+ element.getClientRects().length > 0 &&+ (!next?.isConnected || Boolean(next.compareDocumentPosition(element) & Node.DOCUMENT_POSITION_PRECEDING))+ );++ if (preceding.length > 0) {+ return preceding.at(-1) ?? null;+ }+ }++ return null;+};+const ConceptProjectContext = createContext<string | undefined>(undefined);/** A short concept list shares its project lifetime with the rows' drafts and menus. */@@ -134,11 +178,10 @@ const ConceptRowContent = ({const removeFrom = useCallback((origin: Element | null) => {const neighbour = findNeighbourRow(origin);+ const slots = neighbour ? [] : captureListSlots(origin);onRemove(key);- if (neighbour) {- requestAnimationFrame(() => neighbour.focus());- }+ requestAnimationFrame(() => (neighbour ?? findControlBeforeList(slots))?.focus());},[key, onRemove]);