Skip to content

Commit 7bb6a58

Browse files
authored
fix: add-offset-to-dropdown-if-out-of-screen (#5753)
1 parent 453004b commit 7bb6a58

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

frontend/common/utils/calculateListPosition.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22
export function calculateListPosition(
33
btnEl: HTMLElement,
44
listEl: HTMLElement,
5+
forceInView = false,
56
): { top: number; left: number } {
67
const listPosition = listEl.getBoundingClientRect()
78
const btnPosition = btnEl.getBoundingClientRect()
89
const pageTop = window.visualViewport?.pageTop ?? 0
10+
const isOverflowing =
11+
listPosition.height + btnPosition.top > window.innerHeight
12+
13+
const defaultTop = pageTop + btnPosition.bottom
14+
const defaultLeft = btnPosition.right - listPosition.width
15+
16+
const bottomOverflowOffset = listPosition.height + btnPosition.height + 5
17+
918
return {
10-
left: btnPosition.right - listPosition.width,
11-
top: pageTop + btnPosition.bottom,
19+
left: defaultLeft,
20+
top: defaultTop - (forceInView && isOverflowing ? bottomOverflowOffset : 0),
1221
}
1322
}

frontend/web/components/FeatureAction.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ export const FeatureAction: FC<FeatureActionProps> = ({
4444
tags,
4545
}) => {
4646
const [isOpen, setIsOpen] = useState<boolean>(false)
47-
47+
const [top, setTop] = useState<number>(0)
48+
const [left, setLeft] = useState<number>(0)
4849
const btnRef = useRef<HTMLDivElement>(null)
4950
const listRef = useRef<HTMLDivElement>(null)
5051

@@ -76,9 +77,15 @@ export const FeatureAction: FC<FeatureActionProps> = ({
7677

7778
useLayoutEffect(() => {
7879
if (!isOpen || !listRef.current || !btnRef.current) return
79-
const { left, top } = calculateListPosition(btnRef.current, listRef.current)
80+
const { left, top } = calculateListPosition(
81+
btnRef.current,
82+
listRef.current,
83+
true,
84+
)
8085
listRef.current.style.top = `${top}px`
8186
listRef.current.style.left = `${left}px`
87+
setTop(top)
88+
setLeft(left)
8289
}, [isOpen])
8390

8491
const isProtected = !!protectedTags?.length

0 commit comments

Comments
 (0)