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(webv2): clip droppable rects to scroll containers so hidden rows cannot catch drags

joshistoastc8984c437838
diff --git a/invokeai/frontend/webv2/src/workbench/shell/WorkbenchShell.tsx b/invokeai/frontend/webv2/src/workbench/shell/WorkbenchShell.tsx
index 0cbc933597..06bbe47332 100644
--- a/invokeai/frontend/webv2/src/workbench/shell/WorkbenchShell.tsx
+++ b/invokeai/frontend/webv2/src/workbench/shell/WorkbenchShell.tsx
@@ -20,6 +20,7 @@ import {
getRegionDropState,
isWidgetDndData,
isWidgetInstanceDragData,
+ measureDroppableVisibleRect,
resolveWidgetDragEnd,
type ActiveWidgetDrag,
widgetCollisionDetection,
@@ -58,6 +59,12 @@ const DND_MODIFIERS = [restrictToWindowEdges];
*/
const DND_AUTO_SCROLL = { acceleration: 3, threshold: { x: 0.08, y: 0.08 } };
+/**
+ * Droppables are measured clipped to their scroll containers so targets
+ * scrolled out of view cannot catch drags (see measureDroppableVisibleRect).
+ */
+const DND_MEASURING = { droppable: { measure: measureDroppableVisibleRect } };
+
export const WorkbenchShell = () => {
const { notifications, widgets } = useWorkbenchCommands();
const { t } = useTranslation();
@@ -226,6 +233,7 @@ export const WorkbenchShell = () => {
<DndContext
autoScroll={DND_AUTO_SCROLL}
collisionDetection={widgetCollisionDetection}
+ measuring={DND_MEASURING}
modifiers={DND_MODIFIERS}
sensors={sensors}
onDragCancel={handleDragCancel}
diff --git a/invokeai/frontend/webv2/src/workbench/widgetDnd.browser.test.ts b/invokeai/frontend/webv2/src/workbench/widgetDnd.browser.test.ts
new file mode 100644
index 0000000000..1c4b4d0aae
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgetDnd.browser.test.ts
@@ -0,0 +1,97 @@
+import { afterEach, describe, expect, it } from 'vitest';
+
+import { measureDroppableVisibleRect } from './widgetDnd';
+
+let host: HTMLDivElement | null = null;
+
+const mount = (html: string): HTMLDivElement => {
+ host = document.createElement('div');
+ host.style.cssText = 'left:0;position:fixed;top:0;';
+ host.innerHTML = html;
+ document.body.append(host);
+
+ return host;
+};
+
+afterEach(() => {
+ host?.remove();
+ host = null;
+});
+
+describe('measureDroppableVisibleRect', () => {
+ it('returns the raw client rect when nothing clips the droppable', () => {
+ const root = mount('<div id="target" style="height:40px;width:120px;"></div>');
+ const target = root.querySelector<HTMLElement>('#target')!;
+
+ expect(measureDroppableVisibleRect(target)).toMatchObject({ height: 40, width: 120 });
+ });
+
+ it('collapses a droppable scrolled out of its container to zero height', () => {
+ // Mirrors a gallery board row below the fold: its client rect would
+ // otherwise extend past the list over whatever is rendered below.
+ const root = mount(`
+ <div id="scroller" style="height:100px;overflow-y:auto;width:200px;">
+ <div style="height:120px;"></div>
+ <div id="target" style="height:40px;"></div>
+ </div>
+ `);
+ const target = root.querySelector<HTMLElement>('#target')!;
+ const scroller = root.querySelector<HTMLElement>('#scroller')!;
+ const rect = measureDroppableVisibleRect(target);
+ const clip = scroller.getBoundingClientRect();
+
+ expect(rect.height).toBe(0);
+ expect(rect.top).toBe(clip.bottom);
+ expect(rect.bottom).toBe(clip.bottom);
+ // Width axis is not clipped by an overflow-y container.
+ expect(rect.width).toBe(scroller.clientWidth);
+ });
+
+ it('keeps only the visible slice of a partially scrolled droppable', () => {
+ const root = mount(`
+ <div id="scroller" style="height:100px;overflow-y:auto;width:200px;">
+ <div style="height:80px;"></div>
+ <div id="target" style="height:40px;"></div>
+ </div>
+ `);
+ const target = root.querySelector<HTMLElement>('#target')!;
+ const scroller = root.querySelector<HTMLElement>('#scroller')!;
+ const rect = measureDroppableVisibleRect(target);
+ const clip = scroller.getBoundingClientRect();
+
+ expect(rect.height).toBe(20);
+ expect(rect.bottom).toBe(clip.bottom);
+ });
+
+ it('clips against every overflow ancestor, not just the nearest', () => {
+ // The stacked gallery wraps the board panel in an overflow:hidden box
+ // that is itself shorter than the scroll area it contains.
+ const root = mount(`
+ <div style="height:60px;overflow:hidden;width:200px;">
+ <div id="scroller" style="height:100px;overflow-y:auto;">
+ <div style="height:70px;"></div>
+ <div id="target" style="height:40px;"></div>
+ </div>
+ </div>
+ `);
+ const target = root.querySelector<HTMLElement>('#target')!;
+ const rect = measureDroppableVisibleRect(target);
+
+ expect(rect.height).toBe(0);
+ });
+
+ it('tracks the container scrolling the droppable into view', () => {
+ const root = mount(`
+ <div id="scroller" style="height:100px;overflow-y:auto;width:200px;">
+ <div style="height:120px;"></div>
+ <div id="target" style="height:40px;"></div>
+ </div>
+ `);
+ const target = root.querySelector<HTMLElement>('#target')!;
+ const scroller = root.querySelector<HTMLElement>('#scroller')!;
+
+ scroller.scrollTop = 60;
+
+ expect(measureDroppableVisibleRect(target).height).toBe(40);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/widgetDnd.ts b/invokeai/frontend/webv2/src/workbench/widgetDnd.ts
index 53c0076911..420f7d495c 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetDnd.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetDnd.ts
@@ -1,9 +1,51 @@
import type { WidgetRegion, WidgetRegionState } from '@workbench/layoutContracts';
import type { WidgetIconComponent, WidgetInstanceId, WidgetTypeId } from '@workbench/widgetContracts';
-import { closestCenter, pointerWithin, type CollisionDetection } from '@dnd-kit/core';
+import { closestCenter, getClientRect, pointerWithin, type CollisionDetection, type ClientRect } from '@dnd-kit/core';
import { arrayMove } from '@dnd-kit/sortable';
+const clipsOverflow = (value: string): boolean => value !== 'visible';
+
+/**
+ * Measures a droppable clipped by its overflow ancestors.
+ *
+ * dnd-kit's default measure is the raw client rect, which for a droppable
+ * scrolled out of a scroll container extends invisibly across whatever is
+ * rendered below — a gallery board row below the fold overlays the entire
+ * image grid. `pointerWithin` then reports drags as "over" that hidden row,
+ * and the auto-scroller races its scroll container to the bottom. Clipping
+ * the rect to what is actually visible removes the phantom targets: a fully
+ * clipped droppable collapses to a zero-size rect no pointer can be within.
+ */
+export const measureDroppableVisibleRect = (element: HTMLElement): ClientRect => {
+ const rect = { ...getClientRect(element) };
+ let ancestor = element.parentElement;
+
+ while (ancestor && ancestor !== document.body) {
+ const style = getComputedStyle(ancestor);
+
+ if (clipsOverflow(style.overflowX) || clipsOverflow(style.overflowY)) {
+ const clip = ancestor.getBoundingClientRect();
+
+ if (clipsOverflow(style.overflowY)) {
+ rect.top = Math.min(Math.max(rect.top, clip.top), clip.bottom);
+ rect.bottom = Math.min(Math.max(rect.bottom, clip.top), clip.bottom);
+ }
+ if (clipsOverflow(style.overflowX)) {
+ rect.left = Math.min(Math.max(rect.left, clip.left), clip.right);
+ rect.right = Math.min(Math.max(rect.right, clip.left), clip.right);
+ }
+ }
+
+ ancestor = ancestor.parentElement;
+ }
+
+ rect.width = Math.max(0, rect.right - rect.left);
+ rect.height = Math.max(0, rect.bottom - rect.top);
+
+ return rect;
+};
+
export interface WidgetDndProject {
widgetInstances: Record<WidgetInstanceId, { typeId: WidgetTypeId; title?: string }>;
widgetRegions: Record<WidgetRegion, Pick<WidgetRegionState, 'activeInstanceId' | 'instanceIds'>>;