Skip to content
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(components): fix dropdownmenu expand direction #17055

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions components/src/molecules/DropdownMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ import { Tooltip } from '../../atoms/Tooltip'
import { StyledText } from '../../atoms/StyledText'
import { LiquidIcon } from '../LiquidIcon'

/** this is the max height to display 10 items */
const MAX_HEIGHT = 316

/** this is for adjustment variable for the case that the space of the bottom and the space of the top are very close */
const HEIGHT_ADJUSTMENT = 100

export interface DropdownOption {
name: string
value: string
Expand Down Expand Up @@ -115,34 +109,34 @@ export function DropdownMenu(props: DropdownMenuProps): JSX.Element {

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 (!dropdownRect) return

const parentElement = dropDownMenuWrapperRef.current?.parentElement
const grandParentElement = parentElement?.parentElement?.parentElement

let availableHeight = window.innerHeight
let scrollOffset = 0

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

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 dropdownHeight = filterOptions.length * 34 + 10 // note (kk:2024/12/06) need to modify the value since design uses different height in desktop and pd
const dropdownBottom = dropdownRect.bottom + dropdownHeight - scrollOffset

const downSpace =
filterOptions.length + 1 > 10
? MAX_HEIGHT
: (filterOptions.length + 1) * 34
const dropdownBottom = dropdownRect.bottom + downSpace - scrollOffset
const fitsBelow = dropdownBottom <= availableHeight
const fitsAbove = dropdownRect.top - dropdownHeight >= scrollOffset

setDropdownPosition(
dropdownBottom > availableHeight &&
Math.abs(dropdownBottom - availableHeight) > HEIGHT_ADJUSTMENT
? 'top'
: 'bottom'
)
if (menuPlacement === 'auto') {
setDropdownPosition(fitsBelow ? 'bottom' : fitsAbove ? 'top' : 'bottom')
} else {
setDropdownPosition(menuPlacement)
}
}

Expand Down
Loading