File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22export 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}
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments