Skip to content

Commit

Permalink
fix(app): Fix dropdown menu open direction issue (#15091)
Browse files Browse the repository at this point in the history
* fix(app): Fix dropdown menu open direction issue
  • Loading branch information
koji committed May 8, 2024
1 parent 49cdabc commit 1be30f7
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion app/src/atoms/MenuList/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,47 @@ export function DropdownMenu(props: DropdownMenuProps): JSX.Element {
} = props
const [targetProps, tooltipProps] = useHoverTooltip()
const [showDropdownMenu, setShowDropdownMenu] = React.useState<boolean>(false)

const [dropdownPosition, setDropdownPosition] = React.useState<
'top' | 'bottom'
>('bottom')

React.useEffect(() => {
const handlePositionCalculation = (): void => {
const dropdownRect = dropDownMenuWrapperRef.current?.getBoundingClientRect()
if (dropdownRect != null) {
const parentElement = dropDownMenuWrapperRef?.current?.parentElement
const grandParentElement = parentElement?.parentElement?.parentElement
let availableHeight = window.innerHeight
let scrollOffset = 0

if (grandParentElement != null) {
const grandParentRect = grandParentElement.getBoundingClientRect()
availableHeight = grandParentRect.bottom - grandParentRect.top
scrollOffset = grandParentRect.top
} else if (parentElement != null) {
const parentRect = parentElement.getBoundingClientRect()
availableHeight = parentRect.bottom - parentRect.top
scrollOffset = parentRect.top
}

const dropdownBottom =
dropdownRect.bottom + (filterOptions.length + 1) * 34 - scrollOffset

setDropdownPosition(dropdownBottom > availableHeight ? 'top' : 'bottom')
}
}

window.addEventListener('resize', handlePositionCalculation)
window.addEventListener('scroll', handlePositionCalculation)
handlePositionCalculation()

return () => {
window.removeEventListener('resize', handlePositionCalculation)
window.removeEventListener('scroll', handlePositionCalculation)
}
}, [filterOptions.length, window.innerHeight])

const toggleSetShowDropdownMenu = (): void => {
setShowDropdownMenu(!showDropdownMenu)
}
Expand Down Expand Up @@ -170,7 +211,8 @@ export function DropdownMenu(props: DropdownMenuProps): JSX.Element {
backgroundColor={COLORS.white}
flexDirection={DIRECTION_COLUMN}
width={width}
top="2.5rem"
top={dropdownPosition === 'bottom' ? '2.5rem' : undefined}
bottom={dropdownPosition === 'top' ? '2.5rem' : undefined}
>
{filterOptions.map((option, index) => (
<MenuItem
Expand Down

0 comments on commit 1be30f7

Please sign in to comment.