refactor: extract isInteractiveDesktopDragTarget and add debug logging

This commit is contained in:
Mohamed Boudra
2026-03-21 15:44:49 +07:00
parent d1314a4d5d
commit 1d42542514
6 changed files with 152 additions and 7 deletions
@@ -0,0 +1,40 @@
import { describe, expect, it } from 'vitest'
import { isInteractiveDesktopDragTarget } from './desktop-window'
function createTarget(input: { matchesSelector: (selector: string) => boolean }): EventTarget {
return {
closest: (selector: string) => (input.matchesSelector(selector) ? ({} as Element) : null),
} as unknown as EventTarget
}
describe('isInteractiveDesktopDragTarget', () => {
it('treats focusable pressables as interactive drag exemptions', () => {
const target = createTarget({
matchesSelector: (selector) => selector.includes('[tabindex]'),
})
expect(isInteractiveDesktopDragTarget(target)).toBe(true)
})
it('treats semantic button targets as interactive drag exemptions', () => {
const target = createTarget({
matchesSelector: (selector) => selector.includes("[role='button']"),
})
expect(isInteractiveDesktopDragTarget(target)).toBe(true)
})
it('returns false for non-interactive targets', () => {
const target = createTarget({
matchesSelector: () => false,
})
expect(isInteractiveDesktopDragTarget(target)).toBe(false)
})
it('returns false when the target does not support closest()', () => {
expect(isInteractiveDesktopDragTarget(null)).toBe(false)
expect(isInteractiveDesktopDragTarget({} as EventTarget)).toBe(false)
})
})
+11 -4
View File
@@ -31,7 +31,7 @@ const INTERACTIVE_SELECTOR =
'button, a, input, textarea, select, ' +
"[role='button'], [role='link'], [role='textbox'], [role='combobox'], " +
"[role='tab'], [role='switch'], [role='checkbox'], [role='slider'], " +
"[role='menuitem'], [contenteditable='true']"
"[role='menuitem'], [tabindex], [contenteditable='true']"
const DOUBLE_CLICK_MS = 300
@@ -40,6 +40,15 @@ type DesktopDragViewProps = Pick<
'onPointerDown' | 'onPointerMove' | 'onPointerUp' | 'onPointerCancel'
>
export function isInteractiveDesktopDragTarget(target: unknown): boolean {
const candidate = target as unknown as { closest?: (selector: string) => Element | null } | null
if (!candidate || typeof candidate.closest !== 'function') {
return false
}
return Boolean(candidate.closest(INTERACTIVE_SELECTOR))
}
export function useDesktopDragHandlers(): DesktopDragViewProps {
const isDragging = useRef(false)
const lastPointerDownAt = useRef(0)
@@ -74,9 +83,7 @@ export function useDesktopDragHandlers(): DesktopDragViewProps {
onPointerDown: (e: RNPointerEvent) => {
if (e.nativeEvent.button !== 0) return
// On web, e.target is a DOM Element (typed as HostInstance in RN)
const target = e.target as unknown as Element
if (target.closest?.(INTERACTIVE_SELECTOR)) return
if (isInteractiveDesktopDragTarget(e.target)) return
e.preventDefault()