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): stabilize tooltip-wrapped menu triggers
joshistoastb294cab71fb0
diff --git a/invokeai/frontend/webv2/src/workbench/components/ui/Tooltip.tsx b/invokeai/frontend/webv2/src/workbench/components/ui/Tooltip.tsxindex 6ba90897b7..fedac7f4e0 100644--- a/invokeai/frontend/webv2/src/workbench/components/ui/Tooltip.tsx+++ b/invokeai/frontend/webv2/src/workbench/components/ui/Tooltip.tsx@@ -1,15 +1,81 @@-import type { ReactNode, Ref, RefObject } from 'react';-import { Tooltip as ChakraTooltip, Portal } from '@chakra-ui/react';+import {+ cloneElement,+ isValidElement,+ type ComponentProps,+ type ReactElement,+ type ReactNode,+ type Ref,+ type RefObject,+} from 'react';++type TooltipTriggerProps = Omit<ComponentProps<typeof ChakraTooltip.Trigger>, 'children'>;++const ROOT_PROP_NAMES = new Set([+ 'closeDelay',+ 'closeOnClick',+ 'closeOnEscape',+ 'closeOnPointerDown',+ 'closeOnScroll',+ 'defaultOpen',+ 'disabled',+ 'ids',+ 'immediate',+ 'interactive',+ 'lazyMount',+ 'onOpenChange',+ 'open',+ 'openDelay',+ 'positioning',+ 'present',+ 'unmountOnExit',+]);++const splitTooltipProps = (props: Record<string, unknown>) => {+ const rootProps: Record<string, unknown> = {};+ const triggerProps: Record<string, unknown> = {};++ for (const [key, value] of Object.entries(props)) {+ if (ROOT_PROP_NAMES.has(key)) {+ rootProps[key] = value;+ } else {+ triggerProps[key] = value;+ }+ }++ return { rootProps, triggerProps };+};++const setRef = <T,>(ref: Ref<T> | undefined, value: T | null): void => {+ if (typeof ref === 'function') {+ ref(value);+ return;+ }++ if (ref) {+ ref.current = value;+ }+};++const composeRefs =+ <T,>(...refs: (Ref<T> | undefined)[]): Ref<T> =>+ (value) => {+ for (const ref of refs) {+ setRef(ref, value);+ }+ };++type TooltipChildProps = Record<string, unknown> & { ref?: Ref<HTMLButtonElement> };export interface TooltipProps extends ChakraTooltip.RootProps {showArrow?: boolean;portalled?: boolean;portalRef?: RefObject<HTMLElement | null>;content: ReactNode;+ contentRef?: Ref<HTMLDivElement>;contentProps?: ChakraTooltip.ContentProps;- disabled?: boolean;- ref?: Ref<HTMLDivElement>;+ ref?: Ref<HTMLButtonElement>;+ triggerProps?: TooltipTriggerProps;}/**@@ -18,18 +84,41 @@ export interface TooltipProps extends ChakraTooltip.RootProps {* structure and the `content` convenience API.*/export const Tooltip = (props: TooltipProps) => {- const { showArrow, children, disabled, portalled = true, content, contentProps, portalRef, ref, ...rest } = props;+ const {+ showArrow = true,+ children,+ disabled,+ portalled = true,+ content,+ contentProps,+ contentRef,+ portalRef,+ ref,+ triggerProps,+ ...rest+ } = props;+ const { rootProps, triggerProps: triggerPassthroughProps } = splitTooltipProps(rest);+ const { ref: explicitTriggerRef, ...explicitTriggerProps } = triggerProps ?? {};+ const mergedTriggerProps = { ...triggerPassthroughProps, ...explicitTriggerProps };+ const hasTriggerPassthrough = ref || explicitTriggerRef || Object.keys(mergedTriggerProps).length > 0;+ const triggerChild =+ hasTriggerPassthrough && isValidElement<TooltipChildProps>(children)+ ? cloneElement(children as ReactElement<TooltipChildProps>, {+ ...mergedTriggerProps,+ ref: composeRefs(children.props.ref, explicitTriggerRef, ref),+ })+ : children;if (disabled) {- return children;+ return triggerChild;}return (- <ChakraTooltip.Root {...rest}>- <ChakraTooltip.Trigger asChild>{children}</ChakraTooltip.Trigger>+ <ChakraTooltip.Root {...(rootProps as ChakraTooltip.RootProps)}>+ <ChakraTooltip.Trigger asChild>{triggerChild}</ChakraTooltip.Trigger><Portal disabled={!portalled} container={portalRef}><ChakraTooltip.Positioner>- <ChakraTooltip.Content ref={ref} {...contentProps}>+ <ChakraTooltip.Content ref={contentRef} {...contentProps}>{showArrow && (<ChakraTooltip.Arrow><ChakraTooltip.ArrowTip />diff --git a/invokeai/frontend/webv2/src/workbench/widgets/workflow/WorkflowWidgetChrome.tsx b/invokeai/frontend/webv2/src/workbench/widgets/workflow/WorkflowWidgetChrome.tsxindex 0cf68f5cf0..cdd732ad78 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/workflow/WorkflowWidgetChrome.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/workflow/WorkflowWidgetChrome.tsx@@ -2,7 +2,7 @@ import type { WidgetLabelProps, WidgetViewProps } from '@workbench/types';import type { InvocationTemplate, XYPosition } from '@workbench/workflows/types';import { HStack, Icon, Input, Menu, Portal, Stack, Text } from '@chakra-ui/react';-import { IconButton, ConfirmDialog } from '@workbench/components/ui';+import { IconButton, ConfirmDialog, Tooltip } from '@workbench/components/ui';import { useNotify } from '@workbench/useNotify';import { useActiveProjectSelector, useWorkbenchDispatch } from '@workbench/WorkbenchContext';import { CONNECTOR_INPUT_HANDLE, CONNECTOR_OUTPUT_HANDLE } from '@workbench/workflows/connectors';@@ -146,6 +146,7 @@ export const WorkflowHeaderActions = ({ region }: WidgetViewProps) => {// Both workflow surfaces can be mounted at once; exactly one (the first to// mount) hosts the shared dialogs and the import file input.const hostId = useId();+ const historyTriggerId = useId();const isDialogHost = dialogHostId === hostId;const lastImportRequestRef = useRef(importRequestCount);@@ -321,40 +322,43 @@ export const WorkflowHeaderActions = ({ region }: WidgetViewProps) => {return (<HStack gap="0.5">{region === 'center' ? (- <IconButton- aria-label="Add node"- color="fg.muted"- size="2xs"- title="Add node"- variant="ghost"- onClick={() => setAddNodeOpen(true)}- >- <Icon as={PlusIcon} boxSize="3.5" />- </IconButton>- ) : null}- <IconButton- aria-label="Workflow library"- color="fg.muted"- size="2xs"- title="Workflow library"- variant="ghost"- onClick={() => setWorkflowLibraryOpen(true)}- >- <Icon as={LibraryIcon} boxSize="3.5" />- </IconButton>- <Menu.Root positioning={{ placement: 'bottom-end' }}>- <Menu.Trigger asChild>+ <Tooltip content="Add node"><IconButton- aria-label="Graph history snapshots"+ aria-label="Add node"color="fg.muted"- disabled={restorableHistory.length === 0}size="2xs"- title="Graph history snapshots"variant="ghost"+ onClick={() => setAddNodeOpen(true)}>- <Icon as={HistoryIcon} boxSize="3.5" />+ <Icon as={PlusIcon} boxSize="3.5" /></IconButton>- </Menu.Trigger>+ </Tooltip>+ ) : null}+ <Tooltip content="Workflow library">+ <IconButton+ aria-label="Workflow library"+ color="fg.muted"+ size="2xs"+ variant="ghost"+ onClick={() => setWorkflowLibraryOpen(true)}+ >+ <Icon as={LibraryIcon} boxSize="3.5" />+ </IconButton>+ </Tooltip>+ <Menu.Root ids={{ trigger: historyTriggerId }} positioning={{ placement: 'bottom-end' }}>+ <Tooltip content="Graph history snapshots" ids={{ trigger: historyTriggerId }}>+ <Menu.Trigger asChild>+ <IconButton+ aria-label="Graph history snapshots"+ color="fg.muted"+ disabled={restorableHistory.length === 0}+ size="2xs"+ variant="ghost"+ >+ <Icon as={HistoryIcon} boxSize="3.5" />+ </IconButton>+ </Menu.Trigger>+ </Tooltip><Portal><Menu.Positioner><Menu.Content maxH="18rem" minW="16rem" overflowY="auto">@@ -383,6 +387,7 @@ export const WorkflowHeaderActions = ({ region }: WidgetViewProps) => {</Menu.Positioner></Portal></Menu.Root>+{isDialogHost ? (<><input