From 69a4ed08850a582d1ae8f3ce7cb14e6f2c38ff31 Mon Sep 17 00:00:00 2001 From: Pearce Kieser <5055971+Pearcekieser@users.noreply.github.com> Date: Sun, 27 Sep 2026 08:47:37 +0000 Subject: [PATCH] fix(ui): close open dropdown and context menus when a file drag enters the window --- website/src/components/ChatDropOverlay.tsx | 7 +- website/src/components/ui/context-menu.tsx | 29 +++- website/src/components/ui/dropdown-menu.tsx | 29 +++- website/src/hooks/useCloseOnFileDrag.ts | 44 ++++++ website/src/lib/fileDrag.ts | 16 ++ .../test/DropdownMenu.fileDragClose.test.tsx | 142 ++++++++++++++++++ 6 files changed, 259 insertions(+), 8 deletions(-) create mode 100644 website/src/hooks/useCloseOnFileDrag.ts create mode 100644 website/src/lib/fileDrag.ts create mode 100644 website/src/test/DropdownMenu.fileDragClose.test.tsx diff --git a/website/src/components/ChatDropOverlay.tsx b/website/src/components/ChatDropOverlay.tsx index 97b63d3ca3c5..4e9b9de7bc4d 100644 --- a/website/src/components/ChatDropOverlay.tsx +++ b/website/src/components/ChatDropOverlay.tsx @@ -4,6 +4,7 @@ import { AnimatePresence, motion, useReducedMotion } from 'framer-motion' import kiroFileChomper from '../assets/kiro-file-chomper.png' import { i18nT } from '../i18n/t' +import { carriesFiles } from '../lib/fileDrag' type DropHandler = (event: ReactDragEvent) => void type DropTargetProps = { @@ -16,12 +17,6 @@ type DropTargetProps = { const FILE_COPY_DROP_EFFECT: DataTransfer['dropEffect'] = 'copy' const BOB_CYCLE_SECONDS = 2.8 -function carriesFiles(dataTransfer: DataTransfer): boolean { - return dataTransfer.types?.includes('Files') - || Array.from(dataTransfer.items).some((item) => item.kind === 'file') - || dataTransfer.files.length > 0 -} - /** * Owns one file drag across an entire chat pane. A depth counter absorbs the * enter/leave pairs emitted while the pointer crosses nested chat controls, diff --git a/website/src/components/ui/context-menu.tsx b/website/src/components/ui/context-menu.tsx index 716417986be9..00900a2a6130 100644 --- a/website/src/components/ui/context-menu.tsx +++ b/website/src/components/ui/context-menu.tsx @@ -1,10 +1,37 @@ import * as React from 'react' import * as ContextMenuPrimitive from '@radix-ui/react-context-menu' import { cn } from '../../lib/utils' +import { useCloseOnFileDrag } from '../../hooks/useCloseOnFileDrag' import { useIsTouchDevice } from '../../hooks/useIsTouchDevice' import { PhoneSubContentDiv, PhoneSubTriggerDiv, usePhoneSubState } from './phoneSubmenu' -const ContextMenu = ContextMenuPrimitive.Root +/** + * A context menu opens on a right-click, so no caller drives it: `open` is + * deliberately not accepted (a future controlled caller gets a type error + * here instead of a silently ignored prop). Radix's Root does take `open`, + * which is what lets this wrapper close it. + */ +type ContextMenuProps = Omit, 'open'> + +/** + * Radix `ContextMenu.Root`, plus the same rule as `DropdownMenu`: an open + * modal menu closes when a file drag from outside the page enters the window, + * so the chat composer's drop zone can receive the drop. See + * `useCloseOnFileDrag` for the mechanism. + */ +function ContextMenu({ onOpenChange, modal = true, ...rest }: ContextMenuProps) { + const [open, setOpen] = React.useState(false) + + const handleOpenChange = React.useCallback((next: boolean) => { + setOpen(next) + onOpenChange?.(next) + }, [onOpenChange]) + const close = React.useCallback(() => handleOpenChange(false), [handleOpenChange]) + + useCloseOnFileDrag(open && modal, close) + + return +} const ContextMenuTrigger = ContextMenuPrimitive.Trigger const ContextMenuGroup = ContextMenuPrimitive.Group const ContextMenuPortal = ContextMenuPrimitive.Portal diff --git a/website/src/components/ui/dropdown-menu.tsx b/website/src/components/ui/dropdown-menu.tsx index ec78bec7d1b7..4c1fa6aa9f6a 100644 --- a/website/src/components/ui/dropdown-menu.tsx +++ b/website/src/components/ui/dropdown-menu.tsx @@ -1,10 +1,37 @@ import * as React from 'react' import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu' import { cn } from '../../lib/utils' +import { useCloseOnFileDrag } from '../../hooks/useCloseOnFileDrag' import { useIsTouchDevice } from '../../hooks/useIsTouchDevice' import { PhoneSubContentDiv, PhoneSubTriggerDiv, usePhoneSubState } from './phoneSubmenu' -const DropdownMenu = DropdownMenuPrimitive.Root +type DropdownMenuProps = React.ComponentProps + +/** + * Radix `DropdownMenu.Root`, plus one rule: an open modal menu closes the + * moment a file drag from outside the page enters the window, so the chat + * composer's drop zone can receive the drop. The mechanism is documented on + * `useCloseOnFileDrag`; `ContextMenu` applies the same rule. + * + * Controlled (`open`) and uncontrolled (`defaultOpen`) usage both work: the + * close goes through the same path as a click-outside, so `onOpenChange(false)` + * fires for callers that track the state themselves. + */ +function DropdownMenu({ open: openProp, defaultOpen, onOpenChange, modal = true, ...rest }: DropdownMenuProps) { + const isControlled = openProp !== undefined + const [uncontrolledOpen, setUncontrolledOpen] = React.useState(defaultOpen ?? false) + const open = isControlled ? openProp : uncontrolledOpen + + const handleOpenChange = React.useCallback((next: boolean) => { + if (!isControlled) setUncontrolledOpen(next) + onOpenChange?.(next) + }, [isControlled, onOpenChange]) + const close = React.useCallback(() => handleOpenChange(false), [handleOpenChange]) + + useCloseOnFileDrag(open && modal, close) + + return +} const DropdownMenuTrigger = DropdownMenuPrimitive.Trigger const DropdownMenuGroup = DropdownMenuPrimitive.Group const DropdownMenuPortal = DropdownMenuPrimitive.Portal diff --git a/website/src/hooks/useCloseOnFileDrag.ts b/website/src/hooks/useCloseOnFileDrag.ts new file mode 100644 index 000000000000..602ff663280e --- /dev/null +++ b/website/src/hooks/useCloseOnFileDrag.ts @@ -0,0 +1,44 @@ +import { useEffect, useRef } from 'react' + +import { carriesFiles } from '../lib/fileDrag' + +/** + * Close a modal Radix menu the moment a file drag from outside the page + * enters the window. + * + * Why: a modal Radix menu (DropdownMenu, ContextMenu) sets + * `pointer-events: none` on `document.body` while open, and HTML5 drag + * events honour that property. Nothing under the menu -- the chat composer's + * drop zone included -- can receive `dragover`/`drop`, so a file dragged in + * from the OS while a menu is open is silently refused. The menu never + * dismisses on its own either: Radix closes on an outside `pointerdown`, and + * a native file drag produces none. The `dragenter` still reaches `window` + * (it fires on the `` element, which `body`'s pointer-events cannot + * hide; Radix's own click-outside dismissal relies on the same fact), so + * listen there and close the menu; the drop zone then picks up the following + * `dragover`. + * + * @param active true while the menu is open AND modal. A non-modal menu + * never blocks the drop zone, so pass false for it. + * @param close the menu's own close path (its `onOpenChange(false)`), so + * controlled callers see the change like any click-outside. + */ +export function useCloseOnFileDrag(active: boolean, close: () => void): void { + // Callers often pass an inline `onOpenChange`, which gives `close` a new + // identity per render; read it through a ref so the listener is armed once + // per open, not once per render. The ref is written in an effect, not + // during render, so a discarded concurrent render cannot leave it stale. + const closeRef = useRef(close) + useEffect(() => { + closeRef.current = close + }, [close]) + useEffect(() => { + if (!active) return + const onFileDragEnter = (event: DragEvent) => { + if (carriesFiles(event.dataTransfer)) closeRef.current() + } + // Capture phase: fire before any handler that might stop propagation. + window.addEventListener('dragenter', onFileDragEnter, true) + return () => window.removeEventListener('dragenter', onFileDragEnter, true) + }, [active]) +} diff --git a/website/src/lib/fileDrag.ts b/website/src/lib/fileDrag.ts new file mode 100644 index 000000000000..015079a5ea28 --- /dev/null +++ b/website/src/lib/fileDrag.ts @@ -0,0 +1,16 @@ +/** + * Does this drag carry files from outside the page (OS file manager, another + * app), as opposed to the app's own in-page drag payloads (session rows, + * folder moves, board cards)? + * + * During `dragenter` / `dragover` the browser hides the file list for + * security, so `files` is empty until the drop; `types` includes `'Files'` + * and `items[].kind === 'file'` are the signals available mid-drag. All + * three are checked so a drop (where `files` is populated) matches too. + */ +export function carriesFiles(dataTransfer: DataTransfer | null | undefined): boolean { + if (!dataTransfer) return false + return dataTransfer.types?.includes('Files') + || Array.from(dataTransfer.items ?? []).some((item) => item.kind === 'file') + || (dataTransfer.files?.length ?? 0) > 0 +} diff --git a/website/src/test/DropdownMenu.fileDragClose.test.tsx b/website/src/test/DropdownMenu.fileDragClose.test.tsx new file mode 100644 index 000000000000..002ee25692cf --- /dev/null +++ b/website/src/test/DropdownMenu.fileDragClose.test.tsx @@ -0,0 +1,142 @@ +// Regression: dropping a file into the chat while a sidebar "..." menu was open +// did nothing. A modal Radix DropdownMenu puts `pointer-events: none` on +// `document.body` while open, drag events honour it, and a native file drag +// never produces the outside `pointerdown` that would dismiss the menu. The +// shared `DropdownMenu` wrapper now closes an open modal menu when a file drag +// enters the window, so the composer's drop zone receives the following +// `dragover`/`drop`. +// +// happy-dom has no `DragEvent`, so the drag is a plain `Event` with a +// `dataTransfer` attached the same way the browser exposes it. +import { fireEvent, render, screen, waitFor } from '@testing-library/react' +import { useState } from 'react' +import { describe, expect, it, vi } from 'vitest' + +import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from '../components/ui/context-menu' +import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '../components/ui/dropdown-menu' + +function windowDragEnter(types: string[]): void { + const event = new Event('dragenter', { bubbles: true, cancelable: true }) + Object.defineProperty(event, 'dataTransfer', { + value: { types, items: [], files: [], dropEffect: 'none' }, + }) + window.dispatchEvent(event) +} + +function Uncontrolled({ onOpenChange, modal }: { onOpenChange?: (open: boolean) => void; modal?: boolean }) { + return ( + + more + + Rename + + + ) +} + +function Controlled({ onOpenChange }: { onOpenChange: (open: boolean) => void }) { + const [open, setOpen] = useState(true) + return ( + { setOpen(next); onOpenChange(next) }}> + more + + Rename + + + ) +} + +describe('DropdownMenu closes when a file drag enters the window', () => { + it('dismisses an open uncontrolled menu and reports it through onOpenChange', async () => { + const onOpenChange = vi.fn() + render() + expect(screen.getByTestId('content')).toBeInTheDocument() + + windowDragEnter(['Files']) + + await waitFor(() => expect(screen.queryByTestId('content')).not.toBeInTheDocument()) + expect(onOpenChange).toHaveBeenCalledWith(false) + }) + + it('dismisses an open controlled menu through onOpenChange', async () => { + const onOpenChange = vi.fn() + render() + expect(screen.getByTestId('content')).toBeInTheDocument() + + windowDragEnter(['Files']) + + await waitFor(() => expect(screen.queryByTestId('content')).not.toBeInTheDocument()) + expect(onOpenChange).toHaveBeenCalledWith(false) + }) + + it("ignores the app's own in-page drags (session rows, folder moves)", () => { + const onOpenChange = vi.fn() + render() + + windowDragEnter(['text/plain']) + windowDragEnter(['application/x-kiro-session']) + + expect(screen.getByTestId('content')).toBeInTheDocument() + expect(onOpenChange).not.toHaveBeenCalled() + }) + + it('leaves a non-modal menu alone: it never blocks the drop zone', () => { + const onOpenChange = vi.fn() + render() + + windowDragEnter(['Files']) + + expect(screen.getByTestId('content')).toBeInTheDocument() + expect(onOpenChange).not.toHaveBeenCalled() + }) + + it('stops listening once the menu is closed by other means', async () => { + const onOpenChange = vi.fn() + render() + + fireEvent.keyDown(screen.getByTestId('content'), { key: 'Escape' }) + await waitFor(() => expect(screen.queryByTestId('content')).not.toBeInTheDocument()) + onOpenChange.mockClear() + + windowDragEnter(['Files']) + expect(onOpenChange).not.toHaveBeenCalled() + }) +}) + +describe('ContextMenu closes when a file drag enters the window', () => { + function Harness({ onOpenChange }: { onOpenChange: (open: boolean) => void }) { + return ( + + row + + Close session + + + ) + } + + it('dismisses a right-click menu and reports it through onOpenChange', async () => { + const onOpenChange = vi.fn() + render() + fireEvent.contextMenu(screen.getByTestId('row')) + expect(screen.getByTestId('content')).toBeInTheDocument() + expect(onOpenChange).toHaveBeenCalledWith(true) + + windowDragEnter(['Files']) + + await waitFor(() => expect(screen.queryByTestId('content')).not.toBeInTheDocument()) + expect(onOpenChange).toHaveBeenLastCalledWith(false) + }) + + it("ignores the app's own in-page drags", () => { + const onOpenChange = vi.fn() + render() + fireEvent.contextMenu(screen.getByTestId('row')) + onOpenChange.mockClear() + + windowDragEnter(['text/plain']) + + expect(screen.getByTestId('content')).toBeInTheDocument() + expect(onOpenChange).not.toHaveBeenCalled() + }) +})