Skip to content
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ ref
.DS_Store
node_modules
dist
notes.md
notes.md
*.tgz
42 changes: 42 additions & 0 deletions packages/react/src/hooks/__tests__/useSearchHotkey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ function appendTo(parent: HTMLElement, tag: string): HTMLElement {
return el
}

/** Viewport renders an <svg>, NodeRenderer a <g> — canvas clicks are SVG. */
function appendSvgTo(parent: Element, tag: string): SVGElement {
const el = document.createElementNS('http://www.w3.org/2000/svg', tag)
parent.appendChild(el)
return el
}

function click(target: EventTarget): void {
target.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true }))
}
Expand Down Expand Up @@ -211,4 +218,39 @@ describe('useSearchHotkey', () => {
expect(first).not.toHaveBeenCalled()
expect(second).toHaveBeenCalledTimes(1)
})

it('toggles after a node is clicked', () => {
const onToggle = vi.fn()
mount(onToggle)

const svg = appendSvgTo(container, 'svg')
click(appendSvgTo(svg, 'g'))
const prevented = pressCmdF(document.body)

expect(onToggle).toHaveBeenCalledTimes(1)
expect(prevented).toBe(true)
})

it('toggles after the empty canvas surface is clicked', () => {
const onToggle = vi.fn()
mount(onToggle)

click(appendSvgTo(container, 'svg'))
const prevented = pressCmdF(document.body)

expect(onToggle).toHaveBeenCalledTimes(1)
expect(prevented).toBe(true)
})

it('still hands Cmd+F back after a canvas click then a host-app click', () => {
const onToggle = vi.fn()
mount(onToggle)

click(appendSvgTo(container, 'svg'))
click(appendTo(chatPanel, 'div'))
const prevented = pressCmdF(document.body)

expect(onToggle).not.toHaveBeenCalled()
expect(prevented).toBe(false)
})
})
5 changes: 4 additions & 1 deletion packages/react/src/hooks/useSearchHotkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export function useSearchHotkey(
useEffect(() => {
const onPointerDown = (e: PointerEvent) => {
const container = containerRef.current
const el = e.target instanceof HTMLElement ? e.target : null
// `Element`, not `HTMLElement`: the canvas surface is an <svg>, so a
// click on a node, an edge or the background targets an SVGElement and
// would otherwise read as a click *outside* the canvas.
const el = e.target instanceof Element ? e.target : null
canvasActiveRef.current = !!container && !!el && container.contains(el)
}

Expand Down
Loading