-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Fix wrong popover position #57920
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
Fix wrong popover position #57920
Changes from all commits
9811f2e
fd41d62
8302ac1
544b47f
868fdef
67eee8b
a90224f
4d9339b
61ed05e
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 |
---|---|---|
@@ -1,10 +1,8 @@ | ||
import React, {useMemo, useRef, useState} from 'react'; | ||
import React, {useCallback, useMemo, useRef} from 'react'; | ||
import {View} from 'react-native'; | ||
import type {LayoutChangeEvent} from 'react-native'; | ||
import * as Expensicons from '@components/Icon/Expensicons'; | ||
import ThreeDotsMenu from '@components/ThreeDotsMenu'; | ||
import type ThreeDotsMenuProps from '@components/ThreeDotsMenu/types'; | ||
import type {LayoutChangeEventWithTarget} from '@components/ThreeDotsMenu/types'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import useResponsiveLayout from '@hooks/useResponsiveLayout'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
|
@@ -22,7 +20,6 @@ function TaxExemptActions() { | |
const {shouldUseNarrowLayout} = useResponsiveLayout(); | ||
const styles = useThemeStyles(); | ||
const {translate} = useLocalize(); | ||
const [threeDotsMenuPosition, setThreeDotsMenuPosition] = useState<AnchorPosition>({horizontal: 0, vertical: 0}); | ||
const threeDotsMenuContainerRef = useRef<View>(null); | ||
|
||
const overflowMenu: ThreeDotsMenuProps['menuItems'] = useMemo( | ||
|
@@ -40,26 +37,28 @@ function TaxExemptActions() { | |
[translate], | ||
); | ||
|
||
const calculateAndSetThreeDotsMenuPosition = useCallback(() => { | ||
if (shouldUseNarrowLayout) { | ||
return Promise.resolve({horizontal: 0, vertical: 0}); | ||
} | ||
return new Promise<AnchorPosition>((resolve) => { | ||
threeDotsMenuContainerRef.current?.measureInWindow((x, y, width, height) => { | ||
resolve({ | ||
horizontal: x + width, | ||
vertical: y + height, | ||
}); | ||
}); | ||
}); | ||
}, [shouldUseNarrowLayout]); | ||
|
||
return ( | ||
<View | ||
ref={threeDotsMenuContainerRef} | ||
style={[styles.mtn2, styles.pAbsolute, styles.rn3]} | ||
onLayout={(e: LayoutChangeEvent) => { | ||
if (shouldUseNarrowLayout) { | ||
return; | ||
} | ||
const target = e.target || (e as LayoutChangeEventWithTarget).nativeEvent.target; | ||
target?.measureInWindow((x, y, width) => { | ||
setThreeDotsMenuPosition({ | ||
horizontal: x + width, | ||
vertical: y, | ||
}); | ||
}); | ||
}} | ||
> | ||
<ThreeDotsMenu | ||
getAnchorPosition={calculateAndSetThreeDotsMenuPosition} | ||
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. Why are we calculating it from the outside View but not from inside the 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 notice that not all 3 dot has the same calculation. saved search 3 dot for example doesn't add height to the vertical position.
|
||
menuItems={overflowMenu} | ||
anchorPosition={threeDotsMenuPosition} | ||
anchorAlignment={anchorAlignment} | ||
shouldOverlay | ||
/> | ||
|
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.
Why are we passing both
position
andanchorPosition
, when we already passing the anchorPosition callback?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.
We should pass
{horizontal: 0, vertical: 0}
as default value toposition
state.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.
If a component passes
anchorPosition
and notgetAnchorPosition
, then the position will be undefined and fallback toanchorPosition
props.