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(layers): keep the stack hint closed over header actions

Josh Corbett4377fb91a484
diff --git a/invokeai/frontend/webv2/src/platform/ui/hints/FeatureHint.tsx b/invokeai/frontend/webv2/src/platform/ui/hints/FeatureHint.tsx
index 16b9ec7537..108b1fa773 100644
--- a/invokeai/frontend/webv2/src/platform/ui/hints/FeatureHint.tsx
+++ b/invokeai/frontend/webv2/src/platform/ui/hints/FeatureHint.tsx
@@ -1,9 +1,22 @@
-import type { ReactElement } from 'react';
+import type { HTMLAttributes, ReactElement, ReactNode } from 'react';
-import { Heading, HoverCard, HStack, Icon, Link, Portal, Separator, Spacer, Stack, Text } from '@chakra-ui/react';
+import {
+ Heading,
+ HoverCard,
+ HStack,
+ Icon,
+ Link,
+ mergeProps,
+ Portal,
+ Separator,
+ Spacer,
+ Stack,
+ Text,
+ useHoverCardContext,
+} from '@chakra-ui/react';
import { Button } from '@platform/ui/Button';
import { ExternalLinkIcon } from 'lucide-react';
-import { useMemo } from 'react';
+import { cloneElement, createContext, useContext, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { DEFAULT_HINT_PLACEMENT, getFeatureHint, type FeatureHintId } from './hintRegistry';
@@ -58,6 +71,34 @@ const HintCard = ({ hint, onDisable }: { hint: FeatureHintId; onDisable: (() =>
);
};
+type ExclusionHandlers = Pick<HTMLAttributes<HTMLElement>, 'onPointerEnter' | 'onPointerLeave'>;
+
+const ExclusionContext = createContext<ExclusionHandlers | null>(null);
+
+/** Hands the trigger's own pointer handlers, swapped, to any exclusion inside the trigger. */
+const ExclusionProvider = ({ children }: { children: ReactNode }) => {
+ const { onPointerEnter, onPointerLeave } = useHoverCardContext().getTriggerProps();
+ const handlers = useMemo<ExclusionHandlers | null>(
+ () =>
+ onPointerEnter && onPointerLeave ? { onPointerEnter: onPointerLeave, onPointerLeave: onPointerEnter } : null,
+ [onPointerEnter, onPointerLeave]
+ );
+ return <ExclusionContext.Provider value={handlers}>{children}</ExclusionContext.Provider>;
+};
+
+/**
+ * A region inside a hint's trigger that the pointer can rest on without
+ * summoning the hint — controls that carry their own tooltips. Entering the
+ * region counts as leaving the trigger and leaving it as re-entering, so the
+ * card closes over the region and reopens over the rest of the trigger. React
+ * dispatches enter events outermost first, so a pointer landing straight on the
+ * region never opens the card. Keyboard focus on the trigger is unaffected.
+ */
+export const FeatureHintExclusion = ({ children }: { children: ReactElement<HTMLAttributes<HTMLElement>> }) => {
+ const handlers = useContext(ExclusionContext);
+ return handlers ? cloneElement(children, mergeProps(handlers, children.props)) : children;
+};
+
export interface FeatureHintProps {
hint: FeatureHintId;
/** Becomes the hover target verbatim — it gains data attributes, not a wrapper. */
@@ -89,7 +130,9 @@ export const FeatureHint = ({ children, hint }: FeatureHintProps) => {
return (
<HoverCard.Root closeDelay={CLOSE_DELAY} lazyMount openDelay={OPEN_DELAY} positioning={positioning} unmountOnExit>
- <HoverCard.Trigger asChild>{children}</HoverCard.Trigger>
+ <ExclusionProvider>
+ <HoverCard.Trigger asChild>{children}</HoverCard.Trigger>
+ </ExclusionProvider>
<Portal>
<HoverCard.Positioner>
<HoverCard.Content>
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerStackHeader.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerStackHeader.browser.test.tsx
index a24733ea05..d78246f6e7 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerStackHeader.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerStackHeader.browser.test.tsx
@@ -4,6 +4,7 @@ import { applyThemeToRoot } from '@theme/applyTheme';
import { system } from '@theme/system';
import { createEmptyCanvasDocument } from '@workbench/canvasMigration';
import { createInstance } from 'i18next';
+import { PlusIcon } from 'lucide-react';
import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { I18nextProvider, initReactI18next } from 'react-i18next';
@@ -14,7 +15,11 @@ import type { LayerRowCommands } from './layerRowCommands';
import { LayerStackHeader } from './LayerStackHeader';
-vi.mock('./useLayerStackActions', () => ({ useLayerStackActions: () => [] }));
+vi.mock('./useLayerStackActions', () => ({
+ useLayerStackActions: () => [
+ { disabled: false, icon: PlusIcon, id: 'add', label: 'New layer', run: () => undefined },
+ ],
+}));
const HEADING = 'Raster Layers';
const GIST = 'The pixel layers that compose your image and feed generation.';
@@ -54,6 +59,15 @@ let host: HTMLDivElement | null = null;
let root: Root | null = null;
(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+const wait = (ms: number): Promise<void> =>
+ act(
+ () =>
+ new Promise<void>((resolve) => {
+ globalThis.setTimeout(resolve, ms);
+ })
+ );
+const hintCard = (): Element | null => document.querySelector('[data-scope="hover-card"][data-part="content"]');
+
const settle = (action: () => void): Promise<void> =>
act(async () => {
action();
@@ -112,14 +126,34 @@ describe('LayerStackHeader hint', () => {
await userEvent.tab();
});
expect(document.activeElement).toBe(header);
- await act(
- () =>
- new Promise<void>((resolve) => {
- globalThis.setTimeout(resolve, 900);
- })
- );
- const card = document.querySelector('[data-scope="hover-card"][data-part="content"]');
+ await wait(900);
+ const card = hintCard();
expect(card?.textContent).toContain(HEADING);
expect(card?.textContent).toContain(GIST);
});
+
+ it('opens over the title but never while the pointer rests on a stack action', async () => {
+ await render();
+ const action = host!.querySelector<HTMLElement>('button[aria-label="New layer"]')!;
+ const title = [...host!.querySelectorAll<HTMLElement>('[role="treeitem"] *')].find(
+ (element) => element.textContent === 'Raster Layers (0)'
+ )!;
+
+ await act(() => userEvent.hover(action));
+ await wait(900);
+ expect(hintCard()).toBeNull();
+
+ await act(() => userEvent.hover(title));
+ await wait(900);
+ expect(hintCard()?.textContent).toContain(HEADING);
+
+ await act(() => userEvent.hover(action));
+ await wait(900);
+ expect(hintCard()).toBeNull();
+
+ // Leaving the header straight from an action must not arm a late open.
+ await act(() => userEvent.unhover(action));
+ await wait(900);
+ expect(hintCard()).toBeNull();
+ });
});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerStackHeader.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerStackHeader.tsx
index 1c17bcd123..ddcdd7c1cd 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerStackHeader.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerStackHeader.tsx
@@ -4,7 +4,7 @@ import type { KeyboardEvent, MouseEvent } from 'react';
import { Box, HStack, Icon, Text, VisuallyHidden } from '@chakra-ui/react';
import { IconButton, Tooltip } from '@platform/ui';
-import { FeatureHint } from '@platform/ui/hints/FeatureHint';
+import { FeatureHint, FeatureHintExclusion } from '@platform/ui/hints/FeatureHint';
import { ChevronDownIcon } from 'lucide-react';
import { memo, useCallback, useRef } from 'react';
import { useTranslation } from 'react-i18next';
@@ -98,7 +98,8 @@ const LayerStackHeaderComponent = ({
return (
// The informational popover triggers on the whole tree item, so it opens
// for the roving keyboard focus as well as hover; the persistent
- // described-by span announces the gist without the card.
+ // described-by span announces the gist without the card. The action
+ // buttons carry their own tooltips, so hovering them is excluded.
<FeatureHint hint={hintId}>
<Box
ref={element}
@@ -151,23 +152,25 @@ const LayerStackHeaderComponent = ({
>
{t(`widgets.layers.groups.${stack}`)} ({leafCount})
</Text>
- <HStack gap="0.5" onClick={stopPropagation} onMouseDown={keepFocus}>
- {actions.map((action) => (
- <Tooltip key={action.id} content={action.label}>
- <IconButton
- aria-label={action.label}
- color="fg.muted"
- disabled={action.disabled}
- size="2xs"
- tabIndex={-1}
- variant="ghost"
- onClick={action.run}
- >
- <Icon as={action.icon} boxSize="3.5" />
- </IconButton>
- </Tooltip>
- ))}
- </HStack>
+ <FeatureHintExclusion>
+ <HStack gap="0.5" onClick={stopPropagation} onMouseDown={keepFocus}>
+ {actions.map((action) => (
+ <Tooltip key={action.id} content={action.label}>
+ <IconButton
+ aria-label={action.label}
+ color="fg.muted"
+ disabled={action.disabled}
+ size="2xs"
+ tabIndex={-1}
+ variant="ghost"
+ onClick={action.run}
+ >
+ <Icon as={action.icon} boxSize="3.5" />
+ </IconButton>
+ </Tooltip>
+ ))}
+ </HStack>
+ </FeatureHintExclusion>
</HStack>
</Box>
</FeatureHint>