Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop-app): r2-h1 resolve nested submenu bounds from the in-drawer trigger

Diego Zacariasef5aabfc2067
diff --git a/desktop-app/ui/src/components/agents/AgentTitleSelector.tsx b/desktop-app/ui/src/components/agents/AgentTitleSelector.tsx
index c717c3057..c0716b8d0 100644
--- a/desktop-app/ui/src/components/agents/AgentTitleSelector.tsx
+++ b/desktop-app/ui/src/components/agents/AgentTitleSelector.tsx
@@ -82,6 +82,12 @@ export function AgentTitleSelector({
flyoutRef: submenuRef,
open: open && openAgent !== null,
placement: 'right-of',
+ // The submenu anchor (a row's dots button) lives inside the menu, which is
+ // portaled to document.body — so it can't reach `.chat-drawer` via
+ // `closest`. Resolve the drawer bounds from the trigger, which stays in the
+ // drawer tree; otherwise the submenu clamps to the viewport and can paint
+ // over the native embed to the drawer's left.
+ boundsAnchorRef: triggerRef,
})
// All three refs: the menu and its sub-menu are portaled, so a mousedown
diff --git a/desktop-app/ui/src/hooks/__tests__/useFlyoutPosition.test.ts b/desktop-app/ui/src/hooks/__tests__/useFlyoutPosition.test.ts
new file mode 100644
index 000000000..402d0a72a
--- /dev/null
+++ b/desktop-app/ui/src/hooks/__tests__/useFlyoutPosition.test.ts
@@ -0,0 +1,89 @@
+// @vitest-environment jsdom
+import type { RefObject } from 'react'
+import { afterEach, describe, expect, it } from 'vitest'
+import { cleanup, renderHook } from '@testing-library/react'
+import { useFlyoutPosition } from '../useFlyoutPosition'
+
+// jsdom performs no layout, so every getBoundingClientRect is 0. Stub it
+// per-element to give the hook the geometry it reads.
+function stubRect(
+ el: HTMLElement,
+ rect: { left: number; top: number; width: number; height: number }
+): void {
+ el.getBoundingClientRect = () =>
+ ({
+ left: rect.left,
+ top: rect.top,
+ right: rect.left + rect.width,
+ bottom: rect.top + rect.height,
+ width: rect.width,
+ height: rect.height,
+ x: rect.left,
+ y: rect.top,
+ toJSON: () => ({}),
+ }) as DOMRect
+}
+
+function ref<T extends HTMLElement>(el: T): RefObject<T | null> {
+ return { current: el }
+}
+
+afterEach(() => {
+ cleanup()
+ document.body.innerHTML = ''
+})
+
+describe('useFlyoutPosition — portaled anchor bounds wiring', () => {
+ it('resolves bounds from boundsAnchorRef when the position anchor is portaled outside the drawer', () => {
+ // Real widths: jsdom reports 0, and a zero-width bounds rect makes the hook
+ // fall back to the viewport, which would hide the very bug under test. Give
+ // the drawer a real rect docked to the right of a 1200px viewport.
+ window.innerWidth = 1200
+ window.innerHeight = 1000
+
+ // The confining ancestor and its in-tree trigger (the boundsAnchor).
+ const drawer = document.createElement('div')
+ drawer.className = 'chat-drawer'
+ stubRect(drawer, { left: 400, top: 0, width: 300, height: 800 })
+ const trigger = document.createElement('button')
+ drawer.appendChild(trigger)
+ document.body.appendChild(drawer)
+
+ // The position anchor (row-dots button) is portaled to body, OUTSIDE the
+ // drawer — so anchor.closest('.chat-drawer') is null.
+ const anchor = document.createElement('button')
+ stubRect(anchor, { left: 650, top: 100, width: 20, height: 20 })
+ document.body.appendChild(anchor)
+
+ const flyout = document.createElement('span')
+ stubRect(flyout, { left: 0, top: 0, width: 210, height: 300 })
+ document.body.appendChild(flyout)
+
+ // Stable ref identities across renders: the hook keys its effect/callback
+ // off ref identity, so recreating them each render would loop forever.
+ const anchorRef = ref(anchor)
+ const flyoutRef = ref(flyout)
+ const boundsAnchorRef = ref(trigger)
+
+ const { result } = renderHook(() =>
+ useFlyoutPosition({
+ anchorRef,
+ flyoutRef,
+ boundsAnchorRef,
+ boundsSelector: '.chat-drawer',
+ open: true,
+ placement: 'right-of',
+ })
+ )
+
+ // Observable result: the submenu is confined to the DRAWER, not the
+ // viewport. Anchor hugs the drawer's right edge, so 'right-of' would
+ // overflow area.right (692) and flips to the anchor's left:
+ // candidateLeft = anchor.left - width - gap = 650 - 210 - 6 = 434
+ // clamp(434, [408, 482]) = 434
+ // Without boundsAnchorRef the anchor can't reach the drawer, bounds become
+ // the viewport, no flip fires and left = anchor.right + gap = 676. Pinning
+ // 434 is what fails against the parent (which lacks boundsAnchorRef).
+ expect(result.current).toEqual({ left: 434, top: 100 })
+ })
+})
diff --git a/desktop-app/ui/src/hooks/useFlyoutPosition.ts b/desktop-app/ui/src/hooks/useFlyoutPosition.ts
index 8232a7ee9..0ff7a1a09 100644
--- a/desktop-app/ui/src/hooks/useFlyoutPosition.ts
+++ b/desktop-app/ui/src/hooks/useFlyoutPosition.ts
@@ -14,6 +14,14 @@ type UseFlyoutPositionArgs = {
// not which axis the ancestor bounds. Defaults to the right-docked chat
// drawer, whose ancestors clip overflow.
boundsSelector?: string
+ // Element to resolve the confining ancestor (`boundsSelector`) from, when the
+ // position anchor is itself portaled OUT of that ancestor. `closest()` walks
+ // the DOM tree, so a portaled anchor (e.g. a submenu button rendered inside a
+ // menu that portals to document.body) can no longer reach `.chat-drawer` and
+ // would fall back to the full viewport. This ref must live INSIDE the
+ // confining ancestor (e.g. the still-in-tree trigger). Defaults to
+ // `anchorRef`, so omitting it preserves today's behavior exactly.
+ boundsAnchorRef?: RefObject<HTMLElement | null>
gap?: number
inset?: number
}
@@ -32,6 +40,7 @@ export function useFlyoutPosition({
open,
placement,
boundsSelector = '.chat-drawer',
+ boundsAnchorRef,
gap,
inset,
}: UseFlyoutPositionArgs): FlyoutPosition | null {
@@ -44,7 +53,11 @@ export function useFlyoutPosition({
const anchorRect = anchor.getBoundingClientRect()
const flyoutRect = flyout.getBoundingClientRect()
- const boundsEl = anchor.closest<HTMLElement>(boundsSelector)
+ // Resolve the confining ancestor from `boundsAnchorRef` when supplied — the
+ // position anchor may be portaled out of that ancestor and unable to reach
+ // it via `closest`.
+ const boundsAnchor = boundsAnchorRef?.current ?? anchor
+ const boundsEl = boundsAnchor.closest<HTMLElement>(boundsSelector)
const boundsRect = boundsEl?.getBoundingClientRect()
const viewportWidth = window.innerWidth
const viewportHeight = window.innerHeight
@@ -57,7 +70,7 @@ export function useFlyoutPosition({
: { left: 0, right: viewportWidth, top: 0, bottom: viewportHeight }
setPosition(computeFlyoutPosition({ anchorRect, flyoutRect, bounds, placement, gap, inset }))
- }, [anchorRef, boundsSelector, flyoutRef, gap, inset, placement])
+ }, [anchorRef, boundsAnchorRef, boundsSelector, flyoutRef, gap, inset, placement])
useLayoutEffect(() => {
if (!open) {
@@ -70,7 +83,10 @@ export function useFlyoutPosition({
window.addEventListener('scroll', update, true)
const resizeObserver = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(update)
const anchor = anchorRef.current
- const boundsEl = anchor?.closest<HTMLElement>(boundsSelector)
+ // Observe the same confining ancestor `update()` resolves, so dragging the
+ // drawer's resize handle re-positions the flyout even when the position
+ // anchor is portaled out of the drawer.
+ const boundsEl = (boundsAnchorRef?.current ?? anchor)?.closest<HTMLElement>(boundsSelector)
if (anchor) resizeObserver?.observe(anchor)
if (flyoutRef.current) resizeObserver?.observe(flyoutRef.current)
if (boundsEl) resizeObserver?.observe(boundsEl)
@@ -80,7 +96,7 @@ export function useFlyoutPosition({
window.removeEventListener('scroll', update, true)
resizeObserver?.disconnect()
}
- }, [anchorRef, boundsSelector, flyoutRef, open, update])
+ }, [anchorRef, boundsAnchorRef, boundsSelector, flyoutRef, open, update])
return position
}