`useOverlayPosition` throws when `getTargetRect` returns a `DOMRect` for a `position: fixed` target and the overlay's containing block is not the viewport
Provide a general summary of the issue here
getTargetRect is typed to return a DOMRect and documented as defaulting to target.getBoundingClientRect(). Passing exactly that crashes when the target is position: fixed and the overlay's containing block isn't the viewport.
react-aria 3.52.1, tested headless with Playwright in Chromium 153, Firefox 152 and WebKit 26.5. calculatePosition is unchanged on main.
Expected Behavior?
The overlay is positioned against the returned rect, as it is without getTargetRect (left: 20px; top: 41px in the repro).
Current Behavior
TypeError: Cannot set property top of #<DOMRectReadOnly> which has only a getterFirefox: setting getter-only property "top". WebKit: Attempted to assign to readonly property.
It's thrown from getPosition inside a layout effect, so the whole tree unmounts.
Possible Solution
For a fixed node, getPosition uses overrideRect directly (:787) and then runs offset.top -= … on it (:798), which throws on a DOMRect. A plain object wouldn't throw, but the caller's rect would be mutated, here and again in calculatePosition. Copying {top, left, width, height} out of it, as getOffset already does, should fix both.
Context
I use getTargetRect to position a dropdown against a rect built from two elements. With a position: fixed trigger and the dropdown portalled under a transformed ancestor, the render crashes. For now I copy the rect into a plain object before returning it.
️ Steps to Reproduce
Runnable version: CodeSandbox. It opens on the bug, with a link that switches to the control.
import { useRef, useState } from 'react'
import { createPortal } from 'react-dom'
import { useOverlayPosition } from 'react-aria'
export function Repro() {
const targetRef = useRef<HTMLButtonElement>(null)
const overlayRef = useRef<HTMLDivElement>(null)
const [container, setContainer] = useState<HTMLDivElement | null>(null)
const { overlayProps } = useOverlayPosition({
targetRef,
overlayRef,
placement: 'bottom start',
isOpen: container !== null,
// The documented default, written out.
getTargetRect: target => target.getBoundingClientRect(),
})
return (
<>
<button ref={targetRef} style={{ position: 'fixed', top: 20, left: 20 }}>trigger</button>
{/* A transformed portal container: the overlay's containing block is not the viewport. */}
<div ref={setContainer} style={{ position: 'absolute', top: 0, left: 0, transform: 'translateZ(0)' }} />
{container && createPortal(<div ref={overlayRef} {...overlayProps}>overlay</div>, container)}
</>
)
}Render it: the error is thrown on mount. Remove the getTargetRect line and it renders normally.
Version
react-aria 3.52.1
What browsers are you seeing the problem on?
Chrome, Firefox, Other
If other, please specify.
WebKit 26.5 (Playwright's build, the engine behind Safari). Not tested in Safari itself.
What operating system are you using?
macOS
Your Company/Team
No response
Tracking Issue
No response
Source: adobe/react-spectrum