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(webv2): restore keyboard access to the projects layout toggle
joshistoast8e53c59a1deb
diff --git a/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectsToolbar.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectsToolbar.browser.test.tsxnew file mode 100644index 0000000000..b11a4d05c7--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectsToolbar.browser.test.tsx@@ -0,0 +1,158 @@+import { ChakraProvider } from '@chakra-ui/react';+import { system } from '@theme/system';+import { act } from 'react';+import { createRoot, type Root } from 'react-dom/client';+import { afterEach, describe, expect, it, vi } from 'vitest';++import { ProjectsToolbar } from './ProjectsToolbar';++/**+ * The layout control, which is a segmented radio group wearing two icons.+ *+ * Both assertions here exist because of a shape that looks right and is not.+ * `Tooltip.Trigger` is `asChild` and merges its own `data-state` onto whatever+ * it clones, so wrapping the *item* silently overwrites `data-state="checked"`+ * — the selected segment styles as unselected and the indicator measures 0×0.+ * Moving the tooltip onto the icon fixes that but puts it on an `<svg>`, which+ * cannot take focus, so the label goes hover-only. Both need to hold at once.+ */++let host: HTMLDivElement | null = null;+let root: Root | null = null;+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;++afterEach(async () => {+ await act(() => root?.unmount());+ host?.remove();+ host = null;+ root = null;+});++const render = async (overrides: { view?: 'grid' | 'list'; onViewChange?: (view: 'grid' | 'list') => void } = {}) => {+ host = document.createElement('div');+ host.style.width = '720px';+ document.body.append(host);+ root = createRoot(host);++ await act(() => {+ root?.render(+ <ChakraProvider value={system}>+ <ProjectsToolbar+ searchTerm=""+ sort="edited"+ view={overrides.view ?? 'grid'}+ onSearchTermChange={vi.fn()}+ onSortChange={vi.fn()}+ onViewChange={overrides.onViewChange ?? vi.fn()}+ />+ </ChakraProvider>+ );+ });++ return host;+};++const settle = async () => {+ await act(async () => {+ await new Promise((resolve) => {+ setTimeout(resolve, 60);+ });+ });+};++/** The tooltip fades in and out, so its visibility is polled rather than sampled. */+const waitUntil = async (predicate: () => boolean, description: string) => {+ for (let attempt = 0; attempt < 40; attempt += 1) {+ if (predicate()) {+ return;+ }++ await settle();+ }++ throw new Error(`never became true: ${description}`);+};++/**+ * By position, not by label: these tests run without an i18next instance, so+ * `t()` returns its key. What matters here is the wiring, and the two segments+ * render in `PROJECTS_VIEW_IDS` order — grid, then list.+ */+const viewRadios = (container: HTMLElement): HTMLInputElement[] => {+ const radios = [...container.querySelectorAll<HTMLInputElement>('input[type="radio"]')];++ expect(radios).toHaveLength(2);++ return radios;+};++const tooltipContent = (): HTMLElement | null =>+ document.querySelector<HTMLElement>('[data-scope="tooltip"][data-part="content"]');++/**+ * Asked as "can someone see it", not as a `data-state` reading: the node is+ * lazily mounted and then kept, and its state attribute is not what decides+ * whether the label is on screen.+ */+const isTooltipVisible = (): boolean => tooltipContent()?.checkVisibility() ?? false;++describe('ProjectsToolbar layout control', () => {+ it('puts the accessible name on the control that actually takes focus', async () => {+ const [grid, list] = viewRadios(await render());++ expect(grid!.getAttribute('aria-label')).toBeTruthy();+ expect(list!.getAttribute('aria-label')).toBeTruthy();+ expect(grid!.getAttribute('aria-label')).not.toBe(list!.getAttribute('aria-label'));+ });++ it('keeps the selected segment marked as checked', async () => {+ const [grid, list] = viewRadios(await render({ view: 'list' }));++ expect(list!.closest('label')?.dataset.state).toBe('checked');+ expect(grid!.closest('label')?.dataset.state).toBe('unchecked');+ });++ /**+ * The indicator is positioned from the checked item's measured box, so a+ * `data-state` clobbered by a tooltip trigger collapses it to nothing.+ */+ it('gives the selection indicator a measurable box', async () => {+ const container = await render({ view: 'grid' });++ await settle();++ const indicator = container.querySelector<HTMLElement>('[data-scope="segment-group"][data-part="indicator"]');++ expect(indicator).not.toBeNull();+ expect(indicator!.getBoundingClientRect().width).toBeGreaterThan(0);+ });++ it('shows the label on keyboard focus, not only on pointer hover', async () => {+ const container = await render();+ const [, list] = viewRadios(container);++ expect(isTooltipVisible()).toBe(false);++ await act(() => {+ list!.focus();+ });+ await waitUntil(isTooltipVisible, 'the tooltip appeared on focus');++ expect(tooltipContent()?.textContent).toContain(list!.getAttribute('aria-label'));+ });++ it('hides the label again when focus leaves', async () => {+ const container = await render();+ const [, list] = viewRadios(container);++ await act(() => {+ list!.focus();+ });+ await waitUntil(isTooltipVisible, 'the tooltip appeared on focus');++ await act(() => {+ list!.blur();+ });+ await waitUntil(() => !isTooltipVisible(), 'the tooltip went away on blur');+ });+});diff --git a/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectsToolbar.tsx b/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectsToolbar.tsxindex 5527567365..4161c0267e 100644--- a/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectsToolbar.tsx+++ b/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectsToolbar.tsx@@ -3,7 +3,7 @@ import { Button, IconButton } from '@platform/ui/Button';import { MenuContent } from '@platform/ui/Menu';import { Tooltip } from '@platform/ui/Tooltip';import { ArrowUpDownIcon, CheckIcon, LayoutGridIcon, ListIcon, SearchIcon, XIcon } from 'lucide-react';-import { useCallback } from 'react';+import { useCallback, useState } from 'react';import { useTranslation } from 'react-i18next';/* eslint-disable react-perf/jsx-no-jsx-as-prop */@@ -45,6 +45,40 @@ const VIEW_ICON: Record<ProjectsViewId, typeof LayoutGridIcon> = {list: ListIcon,};+/**+ * One position of the layout control.+ *+ * The tooltip wraps the icon rather than the item, because `Tooltip.Trigger` is+ * `asChild` and merges its own `data-state` onto whatever it clones — on the+ * item that overwrites `data-state="checked"`, so the selected segment styles+ * as unselected and the indicator measures 0×0.+ *+ * An icon is not focusable, though, so hover alone would leave the label+ * unreachable by keyboard — the tooltip used to sit on an `IconButton`, which+ * was. The hidden radio *is* the focusable control here, so it drives the+ * tooltip open alongside pointer hover. Screen readers were never affected:+ * the radio carries the accessible name either way.+ */+const ViewSegment = ({ id }: { id: ProjectsViewId }) => {+ const { t } = useTranslation();+ const [isTooltipOpen, setIsTooltipOpen] = useState(false);+ const handleTooltipOpenChange = useCallback((details: { open: boolean }) => setIsTooltipOpen(details.open), []);+ const showTooltip = useCallback(() => setIsTooltipOpen(true), []);+ const hideTooltip = useCallback(() => setIsTooltipOpen(false), []);+ const label = t(VIEW_LABEL_KEY[id]);++ return (+ <SegmentGroup.Item px="2" value={id}>+ <SegmentGroup.ItemHiddenInput aria-label={label} onBlur={hideTooltip} onFocus={showTooltip} />+ <SegmentGroup.ItemText display="flex">+ <Tooltip content={label} open={isTooltipOpen} onOpenChange={handleTooltipOpenChange}>+ <Icon as={VIEW_ICON[id]} boxSize="3.5" />+ </Tooltip>+ </SegmentGroup.ItemText>+ </SegmentGroup.Item>+ );+};+export const ProjectsToolbar = ({searchTerm,sort,@@ -138,21 +172,7 @@ export const ProjectsToolbar = ({<SegmentGroup.Root aria-label={t('projects.viewLabel')} size="xs" value={view} onValueChange={handleViewChange}><SegmentGroup.Indicator />{PROJECTS_VIEW_IDS.map((id) => (- <SegmentGroup.Item key={id} px="2" value={id}>- {/* The hidden radio is the control, so it carries the name — the- visible label is an icon with no text of its own. */}- <SegmentGroup.ItemHiddenInput aria-label={t(VIEW_LABEL_KEY[id])} />- <SegmentGroup.ItemText display="flex">- {/* The tooltip wraps the icon, never the item: `Tooltip.Trigger`- is `asChild` and merges its own `data-state` onto whatever it- clones, which on the item overwrites `data-state="checked"` —- the selected segment then styles as unselected and the- indicator measures 0×0. */}- <Tooltip content={t(VIEW_LABEL_KEY[id])}>- <Icon as={VIEW_ICON[id]} boxSize="3.5" />- </Tooltip>- </SegmentGroup.ItemText>- </SegmentGroup.Item>+ <ViewSegment key={id} id={id} />))}</SegmentGroup.Root></Flex>