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(projects): keep export dialog open when its menu closes (#10208)

Josh Corbett0ec8f9b9b8be
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/library/LibraryCopyChoiceDialog.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/library/LibraryCopyChoiceDialog.tsx
index 36f45f0cd0..cab4b5f2f7 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/library/LibraryCopyChoiceDialog.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/library/LibraryCopyChoiceDialog.tsx
@@ -16,8 +16,6 @@ import { useOpenLibraryWorkflow, type OpenLibraryWorkflow } from './useOpenLibra
/**
* Asks what opening a template the project already holds should do; one host serves the library and the palette.
- * The root stays mounted and only its `open` changes: a dialog that mounts open while the menu that asked for it is
- * still closing gets dismissed with that menu's layer.
*/
export const LibraryCopyChoiceHost = () => {
const { project } = useWorkflowUi();
diff --git a/invokeai/frontend/webv2/src/platform/ui/Dialog.tsx b/invokeai/frontend/webv2/src/platform/ui/Dialog.tsx
index 114b86f6bc..344b7c2c19 100644
--- a/invokeai/frontend/webv2/src/platform/ui/Dialog.tsx
+++ b/invokeai/frontend/webv2/src/platform/ui/Dialog.tsx
@@ -14,8 +14,22 @@ const ModalPresenceRegistration = () => {
// the exit animation, but stops being present the moment it closes, and is present again if it reopens meanwhile.
const ModalPresence = () => (useDialogContext().open ? <ModalPresenceRegistration /> : null);
-const DialogRoot = ({ children, ...props }: ChakraDialog.RootProps) => (
- <ChakraDialog.Root {...props}>
+type LayerDismissEvent = Parameters<NonNullable<ChakraDialog.RootProps['onRequestDismiss']>>[0];
+
+/**
+ * The layer stack dismisses every layer registered after one being torn down, as if nested in it. A menu item can
+ * open a dialog that registers before the closing menu's layer is removed (an already-loaded dialog, a controlled
+ * menu), so that removal would close the dialog the menu just opened. A dialog is never a menu's child; only layers
+ * that really nest it may dismiss it.
+ */
+const keepOpenThroughMenuTeardown = (event: LayerDismissEvent) => {
+ if (event.detail.targetLayer?.dataset.scope === 'menu') {
+ event.preventDefault();
+ }
+};
+
+const DialogRoot = ({ children, ...props }: Dialog.RootProps) => (
+ <ChakraDialog.Root {...props} onRequestDismiss={keepOpenThroughMenuTeardown}>
{props.modal === false ? null : <ModalPresence />}
{children}
</ChakraDialog.Root>
@@ -48,5 +62,5 @@ export const Dialog = {
export declare namespace Dialog {
export type OpenChangeDetails = ChakraDialog.OpenChangeDetails;
- export type RootProps = ChakraDialog.RootProps;
+ export type RootProps = Omit<ChakraDialog.RootProps, 'onRequestDismiss'>;
}
diff --git a/invokeai/frontend/webv2/src/workbench/projects/components/ProjectFileOptionsProvider.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/projects/components/ProjectFileOptionsProvider.browser.test.tsx
index ac3e26165e..6fd75eeda8 100644
--- a/invokeai/frontend/webv2/src/workbench/projects/components/ProjectFileOptionsProvider.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/projects/components/ProjectFileOptionsProvider.browser.test.tsx
@@ -1,6 +1,6 @@
import type * as projectsApi from '@workbench/projects/api';
-import { ChakraProvider } from '@chakra-ui/react';
+import { ChakraProvider, Menu, Portal } from '@chakra-ui/react';
/* oxlint-disable react-perf/jsx-no-new-function-as-prop */
import { settleAnimations } from '@platform/browser/settleAnimations.testing';
import { accountLifecycle, captureAccountScope } from '@platform/state/accountLifecycle';
@@ -9,7 +9,7 @@ import { isModalPresent } from '@platform/ui/modalPresence';
import { system } from '@theme/system';
import { getWorkbenchPreferences, patchWorkbenchPreferences } from '@workbench/settings/store';
import { createInstance } from 'i18next';
-import { act } from 'react';
+import { act, useState } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { I18nextProvider, initReactI18next } from 'react-i18next';
import { afterEach, expect, it, vi } from 'vitest';
@@ -49,7 +49,33 @@ const Harness = () => {
);
};
-const render = async () => {
+// Like the project menus, the menu is controlled: it closes in the same render that mounts the dialog.
+const MenuHarness = () => {
+ const { requestExportOptions } = useProjectFileOptions();
+ const [isOpen, setIsOpen] = useState(false);
+ return (
+ <Menu.Root
+ open={isOpen}
+ onOpenChange={(details) => setIsOpen(details.open)}
+ onSelect={() => {
+ void requestExportOptions('Typography', captureAccountScope()).then((result) => results.push(result));
+ }}
+ >
+ <Menu.Trigger asChild>
+ <button type="button">Project actions</button>
+ </Menu.Trigger>
+ <Portal>
+ <Menu.Positioner>
+ <Menu.Content>
+ <Menu.Item value="export">Export…</Menu.Item>
+ </Menu.Content>
+ </Menu.Positioner>
+ </Portal>
+ </Menu.Root>
+ );
+};
+
+const render = async (harness = <Harness />) => {
await page.viewport(1280, 900);
results = [];
accountLifecycle.activate('fonts-test');
@@ -60,9 +86,7 @@ const render = async () => {
root!.render(
<ChakraProvider value={system}>
<I18nextProvider i18n={i18n}>
- <ProjectFileOptionsProvider>
- <Harness />
- </ProjectFileOptionsProvider>
+ <ProjectFileOptionsProvider>{harness}</ProjectFileOptionsProvider>
</I18nextProvider>
</ChakraProvider>
)
@@ -120,6 +144,21 @@ it('includes my workspace unless I turn it off, and remembers that choice for th
}
});
+it('exports again from a menu without the closing menu dismissing the dialog', async () => {
+ await render(<MenuHarness />);
+ // The first request loads the dialog module; later ones mount it at once, while the menu is still leaving.
+ for (const attempt of [1, 2, 3]) {
+ await act(() => page.getByRole('button', { name: 'Project actions', exact: true }).click());
+ await act(() => page.getByRole('menuitem', { name: 'Export…', exact: true }).click());
+ await settleAnimations();
+ await expect.element(page.getByRole('dialog')).toBeVisible();
+ expect(results).toHaveLength(attempt - 1);
+ await act(() => page.getByRole('button', { name: 'Export project', exact: true }).click());
+ await expect.element(page.getByRole('dialog')).not.toBeInTheDocument();
+ }
+ expect(results).toEqual(Array.from({ length: 3 }, () => ({ includeFonts: false, includeWorkspace: true })));
+});
+
it('cancels a pending choice when its account goes away', async () => {
await render();
await act(() => page.getByRole('button', { name: 'Export', exact: true }).click());
diff --git a/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.browser.test.tsx
index 92f6ab98e7..95f1e447a5 100644
--- a/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.browser.test.tsx
@@ -188,7 +188,6 @@ describe('LayoutPresetStrip', () => {
expect(store.getSnapshot().activeProject.layout.presetId).toBe('edit');
});
- // The menu's layer must be gone before an admin dialog mounts, or zag dismisses the dialog as nested above it.
it.each(['save-as', 'edit'] as const)(
'animates the %s dialog out instead of unmounting it on close',
async (kind) => {
diff --git a/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.tsx b/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.tsx
index 549fc66658..4dace36a8a 100644
--- a/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.tsx
+++ b/invokeai/frontend/webv2/src/workbench/shell/topbar/LayoutPresetStrip.tsx
@@ -537,10 +537,7 @@ interface PresetMenuTarget {
ticket: number;
}
-/**
- * Mounted only while open: closing unmounts it in the same commit that opens an admin dialog, so zag never sees the
- * dialog's layer nested above a menu that is still exiting (which dismisses the dialog).
- */
+/** Mounted only while open. */
const PresetMenu = ({
isActive,
isUnsaved,
@@ -567,8 +564,6 @@ const PresetMenu = ({
const apply = useCallback(() => onApply(preset), [onApply, preset]);
const revert = useCallback(() => layout.revertPreset(preset.id), [layout, preset.id]);
const save = useCallback(() => layout.savePreset(preset.id), [layout, preset.id]);
- // Unmount the menu in the same commit the dialog mounts; zag's own close lands a commit later and its layer
- // teardown would dismiss the dialog as nested above it.
const edit = useCallback(() => {
onClose();
onEdit(preset);
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerContextMenu.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerContextMenu.tsx
index a395d2691a..5f6d8f9af3 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerContextMenu.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerContextMenu.tsx
@@ -1104,8 +1104,6 @@ const LayerMenu = ({
onExitComplete={onDialogExitComplete}
onSubmit={submitRename}
/>
- {/* Mounted before it opens, like the rename dialog: a dialog that mounts open while the menu is closing is
- dismissed as nested above the menu's departing layer. */}
<RunLayerWorkflowDialog
key={runWorkflowGeneration}
availability={workflowAvailability}