Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions website/src/components/ChatDropOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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,
Expand Down
29 changes: 28 additions & 1 deletion website/src/components/ui/context-menu.tsx
Original file line number Diff line number Diff line change
@@ -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<React.ComponentProps<typeof ContextMenuPrimitive.Root>, '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 <ContextMenuPrimitive.Root open={open} onOpenChange={handleOpenChange} modal={modal} {...rest} />
}
const ContextMenuTrigger = ContextMenuPrimitive.Trigger
const ContextMenuGroup = ContextMenuPrimitive.Group
const ContextMenuPortal = ContextMenuPrimitive.Portal
Expand Down
29 changes: 28 additions & 1 deletion website/src/components/ui/dropdown-menu.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof DropdownMenuPrimitive.Root>

/**
* 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 <DropdownMenuPrimitive.Root open={open} onOpenChange={handleOpenChange} modal={modal} {...rest} />
}
const DropdownMenuTrigger = DropdownMenuPrimitive.Trigger
const DropdownMenuGroup = DropdownMenuPrimitive.Group
const DropdownMenuPortal = DropdownMenuPrimitive.Portal
Expand Down
44 changes: 44 additions & 0 deletions website/src/hooks/useCloseOnFileDrag.ts
Original file line number Diff line number Diff line change
@@ -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 `<html>` 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])
}
16 changes: 16 additions & 0 deletions website/src/lib/fileDrag.ts
Original file line number Diff line number Diff line change
@@ -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
}
142 changes: 142 additions & 0 deletions website/src/test/DropdownMenu.fileDragClose.test.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<DropdownMenu defaultOpen onOpenChange={onOpenChange} modal={modal}>
<DropdownMenuTrigger data-testid="trigger">more</DropdownMenuTrigger>
<DropdownMenuContent data-testid="content">
<DropdownMenuItem>Rename</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)
}

function Controlled({ onOpenChange }: { onOpenChange: (open: boolean) => void }) {
const [open, setOpen] = useState(true)
return (
<DropdownMenu open={open} onOpenChange={(next) => { setOpen(next); onOpenChange(next) }}>
<DropdownMenuTrigger data-testid="trigger">more</DropdownMenuTrigger>
<DropdownMenuContent data-testid="content">
<DropdownMenuItem>Rename</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)
}

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(<Uncontrolled onOpenChange={onOpenChange} />)
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(<Controlled onOpenChange={onOpenChange} />)
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(<Uncontrolled onOpenChange={onOpenChange} />)

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(<Uncontrolled onOpenChange={onOpenChange} modal={false} />)

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(<Uncontrolled onOpenChange={onOpenChange} />)

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 (
<ContextMenu onOpenChange={onOpenChange}>
<ContextMenuTrigger data-testid="row">row</ContextMenuTrigger>
<ContextMenuContent data-testid="content">
<ContextMenuItem>Close session</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
)
}

it('dismisses a right-click menu and reports it through onOpenChange', async () => {
const onOpenChange = vi.fn()
render(<Harness onOpenChange={onOpenChange} />)
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(<Harness onOpenChange={onOpenChange} />)
fireEvent.contextMenu(screen.getByTestId('row'))
onOpenChange.mockClear()

windowDragEnter(['text/plain'])

expect(screen.getByTestId('content')).toBeInTheDocument()
expect(onOpenChange).not.toHaveBeenCalled()
})
})
Loading