-
-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Menu] Fix custom anchor positioning #609
Changes from 5 commits
a3f7055
ad03599
cd69068
2fb1421
566218b
db20d89
4c9aa1b
e2362e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
import * as Menu from '@base_ui/react/Menu'; | ||
|
||
export default function Page() { | ||
const [anchorEl, setAnchor] = React.useState<HTMLDivElement | null>(null); | ||
const handleRef = React.useCallback((element: HTMLDivElement | null) => { | ||
setAnchor(element); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<h1>Element passed to anchor</h1> | ||
<Menu.Root animated={false}> | ||
<Menu.Trigger>Trigger</Menu.Trigger> | ||
<Menu.Positioner side="bottom" alignment="start" arrowPadding={0} anchor={anchorEl}> | ||
<Menu.Popup> | ||
<Menu.Item style={{ background: 'lightgray', padding: '5px' }}>One</Menu.Item> | ||
<Menu.Item style={{ background: 'lightgray', padding: '5px' }}>Two</Menu.Item> | ||
</Menu.Popup> | ||
</Menu.Positioner> | ||
</Menu.Root> | ||
<div | ||
data-testid="anchor" | ||
style={{ margin: '100px', background: 'yellowgreen', height: '50px', width: '200px' }} | ||
ref={handleRef} | ||
> | ||
Anchor | ||
</div> | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
import * as Menu from '@base_ui/react/Menu'; | ||
|
||
export default function Page() { | ||
const anchor = React.useRef<HTMLDivElement>(null); | ||
|
||
return ( | ||
<div> | ||
<h1>Ref passed to anchor</h1> | ||
<Menu.Root animated={false}> | ||
<Menu.Trigger>Trigger</Menu.Trigger> | ||
<Menu.Positioner side="bottom" alignment="start" arrowPadding={0} anchor={anchor}> | ||
<Menu.Popup> | ||
<Menu.Item style={{ background: 'lightgray', padding: '5px' }}>One</Menu.Item> | ||
<Menu.Item style={{ background: 'lightgray', padding: '5px' }}>Two</Menu.Item> | ||
</Menu.Popup> | ||
</Menu.Positioner> | ||
</Menu.Root> | ||
<div | ||
data-testid="anchor" | ||
style={{ margin: '100px', background: 'yellowgreen', height: '50px', width: '200px' }} | ||
ref={anchor} | ||
> | ||
Anchor | ||
</div> | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,9 +19,8 @@ import { | |||||
type FloatingContext, | ||||||
} from '@floating-ui/react'; | ||||||
import { getSide, getAlignment } from '@floating-ui/utils'; | ||||||
import { isElement } from '@floating-ui/utils/dom'; | ||||||
import { useEnhancedEffect } from './useEnhancedEffect'; | ||||||
import { useLatestRef } from './useLatestRef'; | ||||||
import { useForcedRerendering } from './useForcedRerendering'; | ||||||
|
||||||
export type Side = 'top' | 'bottom' | 'left' | 'right'; | ||||||
export type Alignment = 'start' | 'center' | 'end'; | ||||||
|
@@ -205,20 +204,29 @@ export function useAnchorPositioning( | |||||
nodeId, | ||||||
}); | ||||||
|
||||||
// We can assume that element anchors are stable across renders, and thus can be reactive. | ||||||
const reactiveAnchorDep = anchor == null || isElement(anchor); | ||||||
const anchorRef = useLatestRef(anchor); | ||||||
const rerender = useForcedRerendering(); | ||||||
|
||||||
const registeredPositionReference = React.useRef<Element | VirtualElement | null>(null); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
useEnhancedEffect(() => { | ||||||
function isRef(param: unknown): param is React.MutableRefObject<any> { | ||||||
return {}.hasOwnProperty.call(param, 'current'); | ||||||
const resolvedAnchor = typeof anchor === 'function' ? anchor() : anchor; | ||||||
|
||||||
if (resolvedAnchor) { | ||||||
const unwrappedElement = isRef(resolvedAnchor) ? resolvedAnchor.current : resolvedAnchor; | ||||||
refs.setPositionReference(unwrappedElement); | ||||||
registeredPositionReference.current = unwrappedElement; | ||||||
} | ||||||
const resolvedAnchor = | ||||||
typeof anchorRef.current === 'function' ? anchorRef.current() : anchorRef.current; | ||||||
if (resolvedAnchor && !isElement(resolvedAnchor)) { | ||||||
refs.setPositionReference(isRef(resolvedAnchor) ? resolvedAnchor.current : resolvedAnchor); | ||||||
}, [refs, anchor]); | ||||||
|
||||||
React.useEffect(() => { | ||||||
// Refs from parent components are set after useLayoutEffect runs and are available in useEffect. | ||||||
// Therefore, if the anchor is a ref, we need to update the position reference in useEffect. | ||||||
const resolvedAnchor = typeof anchor === 'function' ? anchor() : anchor; | ||||||
if (isRef(resolvedAnchor) && resolvedAnchor.current !== registeredPositionReference.current) { | ||||||
refs.setPositionReference(resolvedAnchor.current); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't noticed it flickering. With the menu initially open, even with 20x CPU slowdown, the popup appears only near the anchor. |
||||||
registeredPositionReference.current = resolvedAnchor.current; | ||||||
} | ||||||
}, [refs, anchorRef, reactiveAnchorDep]); | ||||||
}, [refs, anchor, rerender]); | ||||||
|
||||||
React.useEffect(() => { | ||||||
if (keepMounted && mounted && elements.domReference && elements.floating) { | ||||||
|
@@ -275,3 +283,7 @@ export function useAnchorPositioning( | |||||
], | ||||||
); | ||||||
} | ||||||
|
||||||
function isRef(param: unknown): param is React.RefObject<any> { | ||||||
return param != null && {}.hasOwnProperty.call(param, 'current'); | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to be actually used? It's just in the dep array
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, it's a leftover from a previous attempt.