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(launchpad): host one shared project actions menu instead of racing per-card menus
Josh Corbett6c8c709788c8
diff --git a/invokeai/frontend/webv2/src/workbench/launchpad/Launchpad.tsx b/invokeai/frontend/webv2/src/workbench/launchpad/Launchpad.tsxindex ef9a10afc2..2cbc9be42d 100644--- a/invokeai/frontend/webv2/src/workbench/launchpad/Launchpad.tsx+++ b/invokeai/frontend/webv2/src/workbench/launchpad/Launchpad.tsx@@ -13,6 +13,7 @@ import { LaunchpadNav, type LaunchpadNavGroupId } from './LaunchpadNav';import { LaunchpadTopBar } from './LaunchpadTopBar';import { HomePage } from './pages/HomePage';import { ProjectsPage } from './pages/ProjectsPage';+import { ProjectActionsMenuProvider } from './projects/ProjectActionsMenuHost';/*** The landing surface at `/`: a full-height shell with a slim section rail and@@ -131,11 +132,13 @@ export const Launchpad = () => {);return (- <Flex bg="bg" color="fg" direction="column" h="100dvh" overflow="hidden">- <LaunchpadTopBar />- <LaunchpadSections sections={filtered} />- <LaunchpadCommandPalette />- </Flex>+ <ProjectActionsMenuProvider>+ <Flex bg="bg" color="fg" direction="column" h="100dvh" overflow="hidden">+ <LaunchpadTopBar />+ <LaunchpadSections sections={filtered} />+ <LaunchpadCommandPalette />+ </Flex>+ </ProjectActionsMenuProvider>);};diff --git a/invokeai/frontend/webv2/src/workbench/launchpad/home/ResumeCard.tsx b/invokeai/frontend/webv2/src/workbench/launchpad/home/ResumeCard.tsxindex a22ed1df91..e211140562 100644--- a/invokeai/frontend/webv2/src/workbench/launchpad/home/ResumeCard.tsx+++ b/invokeai/frontend/webv2/src/workbench/launchpad/home/ResumeCard.tsx@@ -1,18 +1,20 @@import type { ProjectSummary } from '@workbench/projects/library';import type { MouseEvent } from 'react';-import { Box, Flex, HStack, Menu, Stack, Text } from '@chakra-ui/react';+import { Box, Flex, HStack, Stack, Text } from '@chakra-ui/react';import { Button, IconButton } from '@platform/ui/Button';import { MiddleTruncate } from '@platform/ui/MiddleTruncate';import { Link } from '@tanstack/react-router';import { formatRelativeTime } from '@workbench/launchpad/formatRelativeTime';-import { ProjectActionsMenu } from '@workbench/launchpad/projects/ProjectActionsMenu';+import {+ useProjectActionsMenu,+ useProjectActionsMenuTrigger,+} from '@workbench/launchpad/projects/ProjectActionsMenuHost';import { ProjectCompatibilityBadge } from '@workbench/launchpad/projects/ProjectCompatibilityBadge';import { ProjectCover } from '@workbench/launchpad/projects/ProjectCover';-import { useProjectCardActions } from '@workbench/launchpad/projects/useProjectCardActions';import { isProjectSummaryCompatible } from '@workbench/projects/library';import { ArrowRightIcon, EllipsisVerticalIcon } from 'lucide-react';-import { useCallback, useMemo, useState } from 'react';+import { useCallback, useMemo } from 'react';import { useTranslation } from 'react-i18next';/**@@ -40,26 +42,16 @@ export const ResumeCard = ({onTogglePin: (projectId: string) => void;}) => {const { t } = useTranslation();- const actions = useProjectCardActions(summary);- const [isActionsOpen, setIsActionsOpen] = useState(false);- const [contextMenuTarget, setContextMenuTarget] = useState<{ x: number; y: number } | null>(null);+ const menu = useProjectActionsMenu();const search = useMemo(() => ({ project: summary.id }), [summary.id]);const isCompatible = isProjectSummaryCompatible(summary);- const handleContextMenu = useCallback((event: MouseEvent<HTMLDivElement>) => {- event.preventDefault();- setContextMenuTarget({ x: event.clientX, y: event.clientY });- setIsActionsOpen(true);- }, []);- const handleOpenChange = useCallback((event: { open: boolean }) => {- setIsActionsOpen(event.open);-- if (!event.open) {- setContextMenuTarget(null);- }- }, []);- const clearContextMenuTarget = useCallback(() => setContextMenuTarget(null), []);- const handleTogglePin = useCallback(() => onTogglePin(summary.id), [onTogglePin, summary.id]);+ const menuTarget = useMemo(() => ({ isPinned, onTogglePin, summary }), [isPinned, onTogglePin, summary]);+ const handleContextMenu = useCallback(+ (event: MouseEvent<HTMLDivElement>) => menu.openAtPointer(event, menuTarget),+ [menu, menuTarget]+ );+ const menuTrigger = useProjectActionsMenuTrigger(menuTarget);return (<Flex@@ -121,29 +113,18 @@ export const ResumeCard = ({<ArrowRightIcon /></Button>)}- <ProjectActionsMenu- actions={actions}- contextMenuTarget={contextMenuTarget}- isOpen={isActionsOpen}- isCompatible={isCompatible}- isPinned={isPinned}- projectId={summary.id}- projectName={summary.name}- onOpenChange={handleOpenChange}- onTogglePin={handleTogglePin}+ <IconButton+ aria-expanded={menuTrigger.isExpanded}+ aria-haspopup="menu"+ aria-label={t('common.actions')}+ color="fg.muted"+ size="xs"+ variant="ghost"+ onClick={menuTrigger.onClick}+ onPointerDown={menuTrigger.onPointerDown}>- <Menu.Trigger asChild>- <IconButton- aria-label={t('common.actions')}- color="fg.muted"- size="xs"- variant="ghost"- onClick={clearContextMenuTarget}- >- <EllipsisVerticalIcon />- </IconButton>- </Menu.Trigger>- </ProjectActionsMenu>+ <EllipsisVerticalIcon />+ </IconButton></HStack></Flex></Flex>diff --git a/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectActionsMenu.tsx b/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectActionsMenu.tsxindex 8ea0268ef7..1c16619ecc 100644--- a/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectActionsMenu.tsx+++ b/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectActionsMenu.tsx@@ -1,118 +1,15 @@-import type { ReactNode } from 'react';--import { Icon, Menu, Portal } from '@chakra-ui/react';-import { ConfirmDialog } from '@platform/ui/ConfirmDialog';+import { Icon, Menu } from '@chakra-ui/react';import { MenuContent } from '@platform/ui/Menu';-import { RenameDialog } from '@platform/ui/RenameDialog';import { Link } from '@tanstack/react-router';import { ArrowRightIcon, CopyIcon, FileDownIcon, PencilIcon, PinIcon, PinOffIcon, Trash2Icon } from 'lucide-react';-import { useCallback, useMemo, useState } from 'react';import { useTranslation } from 'react-i18next';-import type { ProjectCardActions } from './useProjectCardActions';-/**- * The per-project action menu, plus the rename and delete dialogs it opens.- * The grid card and the list row both mount this, so right-click and the- * overflow button offer the same things in the same order everywhere.+ * The per-project action menu's content. Right-click and the overflow button+ * offer the same things in the same order everywhere; the single mounted+ * instance and its dialogs live in `ProjectActionsMenuHost`.*/--export interface ProjectActionsMenuProps {- actions: ProjectCardActions;- isCompatible: boolean;- isPinned: boolean;- /** Anchor point for a right-click; `null` anchors to the trigger instead. */- contextMenuTarget: { x: number; y: number } | null;- isOpen: boolean;- projectId: string;- projectName: string;- children: ReactNode;- onOpenChange: (details: { open: boolean }) => void;- onTogglePin: () => void;-}--const MENU_POSITION_BOTTOM_END = { placement: 'bottom-end' } as const;--export const ProjectActionsMenu = ({- actions,- children,- contextMenuTarget,- isOpen,- isCompatible,- isPinned,- onOpenChange,- onTogglePin,- projectId,- projectName,-}: ProjectActionsMenuProps) => {- const { t } = useTranslation();- const [isRenameOpen, setIsRenameOpen] = useState(false);- const [isDeleteOpen, setIsDeleteOpen] = useState(false);- const projectSearch = useMemo(() => ({ project: projectId }), [projectId]);- const positioning = useMemo(- () =>- contextMenuTarget- ? {- getAnchorRect: () => ({ height: 1, width: 1, x: contextMenuTarget.x, y: contextMenuTarget.y }),- placement: 'bottom-start' as const,- }- : MENU_POSITION_BOTTOM_END,- [contextMenuTarget]- );-- const openRenameDialog = useCallback(() => setIsRenameOpen(true), []);- const closeRenameDialog = useCallback(() => setIsRenameOpen(false), []);- const openDeleteDialog = useCallback(() => setIsDeleteOpen(true), []);- const closeDeleteDialog = useCallback(() => setIsDeleteOpen(false), []);- const handleDuplicate = useCallback(() => void actions.duplicate(), [actions]);- const handleExport = useCallback(() => void actions.export(), [actions]);-- return (- <>- <Menu.Root open={isOpen} positioning={positioning} onOpenChange={onOpenChange}>- {children}- <Portal>- <Menu.Positioner>- <MenuBody- isPinned={isPinned}- isCompatible={isCompatible}- projectSearch={projectSearch}- onDelete={openDeleteDialog}- onDuplicate={handleDuplicate}- onExport={handleExport}- onRename={openRenameDialog}- onTogglePin={onTogglePin}- />- </Menu.Positioner>- </Portal>- </Menu.Root>-- <RenameDialog- initialName={projectName}- isOpen={isRenameOpen}- label={t('projects.renameProjectNameLabel')}- submitLabel={t('common.rename')}- title={t('projects.renameProject')}- onClose={closeRenameDialog}- onSubmit={actions.rename}- />-- <ConfirmDialog- // No "it is open, so it may come back" caveat any more: an open project is deleted through- // the editor's own sync handle, so the deletion is final either way. What is worth saying- // instead is what else goes — the project's board — and what does not.- body={`${t('projects.deleteProjectCardBody', { name: projectName })} ${t('projects.deleteProjectBoardNote')}`}- confirmLabel={t('projects.deleteProject')}- isOpen={isDeleteOpen}- title={t('projects.deleteProjectQuestion')}- onClose={closeDeleteDialog}- onConfirm={actions.delete}- />- </>- );-};--const MenuBody = ({+export const ProjectActionsMenuBody = ({isCompatible,isPinned,onDelete,diff --git a/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectActionsMenuHost.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectActionsMenuHost.browser.test.tsxnew file mode 100644index 0000000000..0bc86716ba--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectActionsMenuHost.browser.test.tsx@@ -0,0 +1,164 @@+import type { ProjectSummary } from '@workbench/projects/library';+import type { MouseEvent } from 'react';++import { ChakraProvider } from '@chakra-ui/react';+import { IconButton } from '@platform/ui/Button';+import { system } from '@theme/system';+import { act, useCallback, useMemo } from 'react';+import { createRoot, type Root } from 'react-dom/client';+import { afterEach, describe, expect, it, vi } from 'vitest';++vi.mock('./useProjectCardActions', () => ({+ useProjectCardActions: () => ({+ delete: () => Promise.resolve(),+ duplicate: () => {},+ export: () => {},+ rename: () => Promise.resolve(),+ }),+}));++vi.mock('react-i18next', () => ({+ useTranslation: () => ({ t: (key: string) => key }),+}));++vi.mock('@tanstack/react-router', () => ({+ Link: ({ children, ...props }: { children?: unknown } & Record<string, unknown>) => (+ <a href="/app" {...props}>+ {children as never}+ </a>+ ),+}));++const { ProjectActionsMenuProvider, useProjectActionsMenu, useProjectActionsMenuTrigger } =+ await import('./ProjectActionsMenuHost');++(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;++const makeSummary = (id: string): ProjectSummary =>+ ({ coverUrl: undefined, id, name: `Project ${id}`, schemaVersion: 1, updatedAt: 0 }) as unknown as ProjectSummary;++const NOOP_TOGGLE_PIN = () => {};++/** The card shape the launchpad project cards share: right-click anchor plus a dots trigger. */+const Card = ({ id }: { id: string }) => {+ const menu = useProjectActionsMenu();+ const summary = useMemo(() => makeSummary(id), [id]);+ const menuTarget = useMemo(() => ({ isPinned: false, onTogglePin: NOOP_TOGGLE_PIN, summary }), [summary]);+ const handleContextMenu = useCallback(+ (event: MouseEvent<HTMLDivElement>) => menu.openAtPointer(event, menuTarget),+ [menu, menuTarget]+ );+ const menuTrigger = useProjectActionsMenuTrigger(menuTarget);++ return (+ <div data-card={id} style={{ height: 120, position: 'relative', width: 200 }} onContextMenu={handleContextMenu}>+ <IconButton+ aria-expanded={menuTrigger.isExpanded}+ aria-haspopup="menu"+ aria-label={`actions ${id}`}+ size="2xs"+ variant="ghost"+ onClick={menuTrigger.onClick}+ onPointerDown={menuTrigger.onPointerDown}+ >+ …+ </IconButton>+ </div>+ );+};++let host: HTMLDivElement | null = null;+let root: Root | null = null;++afterEach(async () => {+ await act(() => root?.unmount());+ host?.remove();+ host = null;+ root = null;+});++const rightClick = async (element: Element) => {+ await act(async () => {+ const rect = element.getBoundingClientRect();+ const init = {+ bubbles: true,+ button: 2,+ cancelable: true,+ clientX: rect.left + 20,+ clientY: rect.top + 20,+ };+ element.dispatchEvent(new PointerEvent('pointerdown', init));+ element.dispatchEvent(new MouseEvent('contextmenu', init));+ element.dispatchEvent(new PointerEvent('pointerup', init));+ await new Promise<void>((resolve) => {+ globalThis.setTimeout(resolve, 250);+ });+ });+};++const leftClick = async (element: Element) => {+ await act(async () => {+ const init = { bubbles: true, button: 0, cancelable: true };+ element.dispatchEvent(new PointerEvent('pointerdown', init));+ element.dispatchEvent(new PointerEvent('pointerup', init));+ element.dispatchEvent(new MouseEvent('click', init));+ await new Promise<void>((resolve) => {+ globalThis.setTimeout(resolve, 250);+ });+ });+};++const openMenus = () => document.querySelectorAll('[role="menu"][data-state="open"]');+const dotsButton = (id: string) => document.querySelector(`button[aria-label="actions ${id}"]`)!;++describe('ProjectActionsMenuHost', () => {+ it('moves the one menu across cards instead of racing sibling layers', async () => {+ // Per-card menus died here: zag's dismissable stack treats a layer mounted+ // above another as nested, so the second card's menu was dismissed along+ // with the first card's still-closing one.+ host = document.createElement('div');+ document.body.append(host);+ root = createRoot(host);++ await act(async () => {+ root?.render(+ <ChakraProvider value={system}>+ <ProjectActionsMenuProvider>+ <Card id="one" />+ <Card id="two" />+ </ProjectActionsMenuProvider>+ </ChakraProvider>+ );+ await new Promise<void>((resolve) => {+ globalThis.setTimeout(resolve, 50);+ });+ });++ const cardOne = host.querySelector('[data-card="one"]')!;+ const cardTwo = host.querySelector('[data-card="two"]')!;++ await rightClick(cardOne);+ expect(openMenus()).toHaveLength(1);+ expect(dotsButton('one').getAttribute('aria-expanded')).toBe('true');++ await rightClick(cardTwo);+ expect(openMenus()).toHaveLength(1);+ expect(dotsButton('one').getAttribute('aria-expanded')).toBe('false');+ expect(dotsButton('two').getAttribute('aria-expanded')).toBe('true');++ // Re-right-clicking the open card's own body keeps its menu open too.+ await rightClick(cardTwo);+ expect(openMenus()).toHaveLength(1);+ expect(dotsButton('two').getAttribute('aria-expanded')).toBe('true');++ // A dots click opens; a second dots click toggles closed (the pointerdown+ // already dismissed the menu — the click must not reopen it).+ await leftClick(dotsButton('one'));+ expect(openMenus()).toHaveLength(1);+ expect(dotsButton('one').getAttribute('aria-expanded')).toBe('true');++ await leftClick(dotsButton('one'));+ expect(openMenus()).toHaveLength(0);+ expect(dotsButton('one').getAttribute('aria-expanded')).toBe('false');+ });+});diff --git a/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectActionsMenuHost.tsx b/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectActionsMenuHost.tsxnew file mode 100644index 0000000000..96befdeeb8--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectActionsMenuHost.tsx@@ -0,0 +1,253 @@+import type { ProjectSummary } from '@workbench/projects/library';+import type { MouseEvent, ReactNode } from 'react';++import { Menu, Portal } from '@chakra-ui/react';+import { ConfirmDialog } from '@platform/ui/ConfirmDialog';+import { RenameDialog } from '@platform/ui/RenameDialog';+import { isProjectSummaryCompatible } from '@workbench/projects/library';+import { createContext, useCallback, useContext, useMemo, useRef, useState } from 'react';+import { useTranslation } from 'react-i18next';++import type { ProjectCardActions } from './useProjectCardActions';++import { ProjectActionsMenuBody } from './ProjectActionsMenu';+import { useProjectCardActions } from './useProjectCardActions';++/**+ * One menu instance for every project card on the page.+ *+ * Per-card menus raced: zag's dismissable stack treats any layer mounted+ * above another as nested, so right-clicking card B while card A's menu was+ * still tearing down dismissed B's menu along with A's. A single host never+ * has two layers — switching targets remounts the one menu, and React runs+ * the old instance's cleanup before the new one's effects in the same commit.+ *+ * The rename/delete dialogs live beside the menu, not inside it, because+ * choosing a menu item closes the menu (and with it anything it rendered).+ */++interface ProjectMenuTarget {+ isPinned: boolean;+ summary: ProjectSummary;+ onTogglePin: (projectId: string) => void;+}++type MenuAnchor =+ | { kind: 'pointer'; x: number; y: number }+ | { kind: 'trigger'; rect: { height: number; width: number; x: number; y: number } };++interface MenuRequest extends ProjectMenuTarget {+ anchor: MenuAnchor;+ /** Where focus goes when the menu closes: without a registered zag trigger,+ * the machine's own focus restore has nothing to return to. */+ returnFocus: HTMLElement | null;+ /** Distinguishes successive opens so the hosted menu remounts even when the+ * anchor repeats — zag may have internally closed the previous machine. */+ ticket: number;+}++interface DialogRequest {+ actions: ProjectCardActions;+ kind: 'delete' | 'rename';+ name: string;+}++interface ProjectActionsMenuControl {+ /** The project whose menu is showing; drives the dots triggers' `aria-expanded`. */+ activeProjectId: string | null;+ openAtPointer: (event: MouseEvent, target: ProjectMenuTarget) => void;+ openFromTrigger: (element: HTMLElement, target: ProjectMenuTarget) => void;+}++const ProjectActionsMenuContext = createContext<ProjectActionsMenuControl | null>(null);++export const useProjectActionsMenu = (): ProjectActionsMenuControl => {+ const control = useContext(ProjectActionsMenuContext);++ if (!control) {+ throw new Error('useProjectActionsMenu must be used within a ProjectActionsMenuProvider');+ }++ return control;+};++export const ProjectActionsMenuProvider = ({ children }: { children: ReactNode }) => {+ const { t } = useTranslation();+ const [menuRequest, setMenuRequest] = useState<MenuRequest | null>(null);+ const [dialogRequest, setDialogRequest] = useState<DialogRequest | null>(null);+ const ticketRef = useRef(0);++ const closeMenu = useCallback(() => setMenuRequest(null), []);+ const closeDialog = useCallback(() => setDialogRequest(null), []);+ const openAtPointer = useCallback((event: MouseEvent, target: ProjectMenuTarget) => {+ event.preventDefault();+ ticketRef.current += 1;+ setMenuRequest({+ ...target,+ anchor: { kind: 'pointer', x: event.clientX, y: event.clientY },+ returnFocus: (event.currentTarget as HTMLElement).querySelector<HTMLElement>('a, button, [tabindex]'),+ ticket: ticketRef.current,+ });+ }, []);+ const openFromTrigger = useCallback((element: HTMLElement, target: ProjectMenuTarget) => {+ const { height, width, x, y } = element.getBoundingClientRect();++ ticketRef.current += 1;+ setMenuRequest({+ ...target,+ anchor: { kind: 'trigger', rect: { height, width, x, y } },+ returnFocus: element,+ ticket: ticketRef.current,+ });+ }, []);++ const control = useMemo(+ () => ({ activeProjectId: menuRequest?.summary.id ?? null, openAtPointer, openFromTrigger }),+ [menuRequest?.summary.id, openAtPointer, openFromTrigger]+ );++ const menuKey = menuRequest ? `${menuRequest.summary.id}:${menuRequest.ticket}` : '';++ return (+ <ProjectActionsMenuContext.Provider value={control}>+ {children}+ {menuRequest ? (+ <HostedProjectActionsMenu+ key={menuKey}+ request={menuRequest}+ onClose={closeMenu}+ onRequestDialog={setDialogRequest}+ />+ ) : null}++ <RenameDialog+ initialName={dialogRequest?.kind === 'rename' ? dialogRequest.name : ''}+ isOpen={dialogRequest?.kind === 'rename'}+ label={t('projects.renameProjectNameLabel')}+ submitLabel={t('common.rename')}+ title={t('projects.renameProject')}+ onClose={closeDialog}+ onSubmit={dialogRequest?.actions.rename ?? NOOP_SUBMIT}+ />++ <ConfirmDialog+ // No "it is open, so it may come back" caveat: an open project is deleted through the+ // editor's own sync handle, so the deletion is final either way. What is worth saying+ // instead is what else goes — the project's board — and what does not.+ body={`${t('projects.deleteProjectCardBody', { name: dialogRequest?.name ?? '' })} ${t('projects.deleteProjectBoardNote')}`}+ confirmLabel={t('projects.deleteProject')}+ isOpen={dialogRequest?.kind === 'delete'}+ title={t('projects.deleteProjectQuestion')}+ onClose={closeDialog}+ onConfirm={dialogRequest?.actions.delete ?? NOOP_SUBMIT}+ />+ </ProjectActionsMenuContext.Provider>+ );+};++const NOOP_SUBMIT = () => Promise.resolve();++/**+ * Handlers for a card's dots button. The button is not a registered zag+ * trigger, so a pointerdown on it while its own menu is open dismisses the+ * menu as an outside interaction — and the click that follows would reopen+ * it. The pointerdown handler runs while the pre-dismiss state is still+ * rendered, so it can mark the click as a toggle-close instead.+ */+export const useProjectActionsMenuTrigger = (target: ProjectMenuTarget) => {+ const menu = useProjectActionsMenu();+ const suppressNextOpenRef = useRef(false);+ const isExpanded = menu.activeProjectId === target.summary.id;++ const onPointerDown = useCallback(() => {+ if (menu.activeProjectId === target.summary.id) {+ suppressNextOpenRef.current = true;+ }+ }, [menu.activeProjectId, target.summary.id]);+ const onClick = useCallback(+ (event: MouseEvent<HTMLButtonElement>) => {+ if (suppressNextOpenRef.current) {+ suppressNextOpenRef.current = false;++ return;+ }++ menu.openFromTrigger(event.currentTarget, target);+ },+ [menu, target]+ );++ return { isExpanded, onClick, onPointerDown };+};++const HostedProjectActionsMenu = ({+ request,+ onClose,+ onRequestDialog,+}: {+ request: MenuRequest;+ onClose: () => void;+ onRequestDialog: (dialog: DialogRequest) => void;+}) => {+ const actions = useProjectCardActions(request.summary);+ const isCompatible = isProjectSummaryCompatible(request.summary);+ const projectSearch = useMemo(() => ({ project: request.summary.id }), [request.summary.id]);+ const positioning = useMemo(() => {+ const anchor = request.anchor;++ return anchor.kind === 'pointer'+ ? {+ getAnchorRect: () => ({ height: 1, width: 1, x: anchor.x, y: anchor.y }),+ placement: 'bottom-start' as const,+ }+ : {+ getAnchorRect: () => anchor.rect,+ placement: 'bottom-end' as const,+ };+ }, [request.anchor]);++ const handleOpenChange = useCallback(+ (event: { open: boolean }) => {+ if (!event.open) {+ // Focus would otherwise drop to the body (Escape, item select). When+ // an outside click moved it already, leave the browser's target alone.+ if (document.activeElement?.closest('[data-scope="menu"]')) {+ request.returnFocus?.focus();+ }++ onClose();+ }+ },+ [onClose, request.returnFocus]+ );+ const handleRename = useCallback(+ () => onRequestDialog({ actions, kind: 'rename', name: request.summary.name }),+ [actions, onRequestDialog, request.summary.name]+ );+ const handleDelete = useCallback(+ () => onRequestDialog({ actions, kind: 'delete', name: request.summary.name }),+ [actions, onRequestDialog, request.summary.name]+ );+ const handleDuplicate = useCallback(() => void actions.duplicate(), [actions]);+ const handleExport = useCallback(() => void actions.export(), [actions]);+ const handleTogglePin = useCallback(() => request.onTogglePin(request.summary.id), [request]);++ return (+ <Menu.Root open positioning={positioning} onOpenChange={handleOpenChange}>+ <Portal>+ <Menu.Positioner>+ <ProjectActionsMenuBody+ isPinned={request.isPinned}+ isCompatible={isCompatible}+ projectSearch={projectSearch}+ onDelete={handleDelete}+ onDuplicate={handleDuplicate}+ onExport={handleExport}+ onRename={handleRename}+ onTogglePin={handleTogglePin}+ />+ </Menu.Positioner>+ </Portal>+ </Menu.Root>+ );+};diff --git a/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectCard.tsx b/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectCard.tsxindex 812d6d4f77..a74e464604 100644--- a/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectCard.tsx+++ b/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectCard.tsx@@ -1,25 +1,25 @@import type { ProjectSummary } from '@workbench/projects/library';import type { MouseEvent } from 'react';-import { Box, Flex, Icon, Menu, Stack, Text } from '@chakra-ui/react';+import { Box, Flex, Icon, Stack, Text } from '@chakra-ui/react';import { IconButton } from '@platform/ui/Button';import { MiddleTruncate } from '@platform/ui/MiddleTruncate';import { Link } from '@tanstack/react-router';import { formatRelativeTime } from '@workbench/launchpad/formatRelativeTime';import { isProjectSummaryCompatible } from '@workbench/projects/library';import { EllipsisVerticalIcon, PinIcon } from 'lucide-react';-import { useCallback, useMemo, useState } from 'react';+import { useCallback, useMemo } from 'react';import { useTranslation } from 'react-i18next';-import { ProjectActionsMenu } from './ProjectActionsMenu';+import { useProjectActionsMenu, useProjectActionsMenuTrigger } from './ProjectActionsMenuHost';import { ProjectCompatibilityBadge } from './ProjectCompatibilityBadge';import { ProjectCover } from './ProjectCover';-import { useProjectCardActions } from './useProjectCardActions';/*** One saved project in the library grid. The whole card is a deep link into* the editor (`/app?project=…` — hovering preloads the editor chunk); the- * corner menu and right-click carry the library actions.+ * corner menu and right-click carry the library actions, served by the page's+ * shared `ProjectActionsMenuHost`.*/const CARD_HOVER = { bg: 'bg.muted', borderColor: 'border.emphasized' } as const;@@ -38,25 +38,16 @@ export const ProjectCard = ({onTogglePin: (projectId: string) => void;}) => {const { t } = useTranslation();- const actions = useProjectCardActions(summary);- const [isActionsOpen, setIsActionsOpen] = useState(false);- const [contextMenuTarget, setContextMenuTarget] = useState<{ x: number; y: number } | null>(null);+ const menu = useProjectActionsMenu();const isCompatible = isProjectSummaryCompatible(summary);const projectSearch = useMemo(() => ({ project: summary.id }), [summary.id]);- const handleContextMenu = useCallback((event: MouseEvent<HTMLDivElement>) => {- event.preventDefault();- setContextMenuTarget({ x: event.clientX, y: event.clientY });- setIsActionsOpen(true);- }, []);- const handleOpenChange = useCallback((event: { open: boolean }) => {- setIsActionsOpen(event.open);-- if (!event.open) {- setContextMenuTarget(null);- }- }, []);- const clearContextMenuTarget = useCallback(() => setContextMenuTarget(null), []);+ const menuTarget = useMemo(() => ({ isPinned, onTogglePin, summary }), [isPinned, onTogglePin, summary]);+ const handleContextMenu = useCallback(+ (event: MouseEvent<HTMLDivElement>) => menu.openAtPointer(event, menuTarget),+ [menu, menuTarget]+ );+ const menuTrigger = useProjectActionsMenuTrigger(menuTarget);const handleTogglePin = useCallback(() => onTogglePin(summary.id), [onTogglePin, summary.id]);return (@@ -120,29 +111,18 @@ export const ProjectCard = ({</Box><Box bottom="2" pointerEvents="auto" position="absolute" right="2" zIndex="1">- <ProjectActionsMenu- actions={actions}- contextMenuTarget={contextMenuTarget}- isOpen={isActionsOpen}- isCompatible={isCompatible}- isPinned={isPinned}- projectId={summary.id}- projectName={summary.name}- onOpenChange={handleOpenChange}- onTogglePin={handleTogglePin}+ <IconButton+ aria-expanded={menuTrigger.isExpanded}+ aria-haspopup="menu"+ aria-label={t('common.actions')}+ color="fg.muted"+ size="2xs"+ variant="ghost"+ onClick={menuTrigger.onClick}+ onPointerDown={menuTrigger.onPointerDown}>- <Menu.Trigger asChild>- <IconButton- aria-label={t('common.actions')}- color="fg.muted"- size="2xs"- variant="ghost"- onClick={clearContextMenuTarget}- >- <EllipsisVerticalIcon />- </IconButton>- </Menu.Trigger>- </ProjectActionsMenu>+ <EllipsisVerticalIcon />+ </IconButton></Box></Box>);diff --git a/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectRow.tsx b/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectRow.tsxindex 5e2c09918c..8d69ccccb0 100644--- a/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectRow.tsx+++ b/invokeai/frontend/webv2/src/workbench/launchpad/projects/ProjectRow.tsx@@ -1,7 +1,7 @@import type { ProjectSummary } from '@workbench/projects/library';import type { MouseEvent } from 'react';-import { Box, Flex, Icon, Menu, Text } from '@chakra-ui/react';+import { Box, Flex, Icon, Text } from '@chakra-ui/react';import { IconButton } from '@platform/ui/Button';import { MiddleTruncate } from '@platform/ui/MiddleTruncate';import { Row } from '@platform/ui/Row';@@ -9,18 +9,18 @@ import { Link } from '@tanstack/react-router';import { formatRelativeTime } from '@workbench/launchpad/formatRelativeTime';import { isProjectSummaryCompatible } from '@workbench/projects/library';import { EllipsisVerticalIcon, PinIcon } from 'lucide-react';-import { useCallback, useMemo, useState } from 'react';+import { useCallback, useMemo } from 'react';import { useTranslation } from 'react-i18next';-import { ProjectActionsMenu } from './ProjectActionsMenu';+import { useProjectActionsMenu, useProjectActionsMenuTrigger } from './ProjectActionsMenuHost';import { ProjectCompatibilityBadge } from './ProjectCompatibilityBadge';import { ProjectCover } from './ProjectCover';-import { useProjectCardActions } from './useProjectCardActions';/*** One saved project in the list view — the density a large library wants.- * Carries the same link target and the same action menu as the grid card;- * only the shape differs.+ * Carries the same link target and the same action menu as the grid card+ * (served by the page's shared `ProjectActionsMenuHost`); only the shape+ * differs.*/const LINK_STYLE = { cursor: 'default', inset: 0, position: 'absolute' } as const;@@ -37,25 +37,16 @@ export const ProjectRow = ({onTogglePin: (projectId: string) => void;}) => {const { t } = useTranslation();- const actions = useProjectCardActions(summary);- const [isActionsOpen, setIsActionsOpen] = useState(false);- const [contextMenuTarget, setContextMenuTarget] = useState<{ x: number; y: number } | null>(null);+ const menu = useProjectActionsMenu();const isCompatible = isProjectSummaryCompatible(summary);const projectSearch = useMemo(() => ({ project: summary.id }), [summary.id]);- const handleContextMenu = useCallback((event: MouseEvent<HTMLDivElement>) => {- event.preventDefault();- setContextMenuTarget({ x: event.clientX, y: event.clientY });- setIsActionsOpen(true);- }, []);- const handleOpenChange = useCallback((event: { open: boolean }) => {- setIsActionsOpen(event.open);-- if (!event.open) {- setContextMenuTarget(null);- }- }, []);- const clearContextMenuTarget = useCallback(() => setContextMenuTarget(null), []);+ const menuTarget = useMemo(() => ({ isPinned, onTogglePin, summary }), [isPinned, onTogglePin, summary]);+ const handleContextMenu = useCallback(+ (event: MouseEvent<HTMLDivElement>) => menu.openAtPointer(event, menuTarget),+ [menu, menuTarget]+ );+ const menuTrigger = useProjectActionsMenuTrigger(menuTarget);const handleTogglePin = useCallback(() => onTogglePin(summary.id), [onTogglePin, summary.id]);return (@@ -102,29 +93,18 @@ export const ProjectRow = ({><Icon as={PinIcon} boxSize="3" fill={isPinned ? 'currentColor' : 'none'} /></IconButton>- <ProjectActionsMenu- actions={actions}- contextMenuTarget={contextMenuTarget}- isOpen={isActionsOpen}- isCompatible={isCompatible}- isPinned={isPinned}- projectId={summary.id}- projectName={summary.name}- onOpenChange={handleOpenChange}- onTogglePin={handleTogglePin}+ <IconButton+ aria-expanded={menuTrigger.isExpanded}+ aria-haspopup="menu"+ aria-label={t('common.actions')}+ color="fg.muted"+ size="2xs"+ variant="ghost"+ onClick={menuTrigger.onClick}+ onPointerDown={menuTrigger.onPointerDown}>- <Menu.Trigger asChild>- <IconButton- aria-label={t('common.actions')}- color="fg.muted"- size="2xs"- variant="ghost"- onClick={clearContextMenuTarget}- >- <EllipsisVerticalIcon />- </IconButton>- </Menu.Trigger>- </ProjectActionsMenu>+ <EllipsisVerticalIcon />+ </IconButton></Flex></Row>);