Skip to content

Commit

Permalink
fix: fix dropdown render in SSR (#1202)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurTriis1 authored Mar 31, 2022
1 parent 186871f commit 60f65b0
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/ui/src/molecules/Dropdown/hooks/useDropdownPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ type DropdownPosition = Pick<React.CSSProperties, 'position' | 'top' | 'left'>
export const useDropdownPosition = (): DropdownPosition => {
const { dropdownButtonRef } = useDropdown()

// Necessary to use this component in SSR
const isBrowser = typeof window !== 'undefined'

const buttonRect = dropdownButtonRef?.current?.getBoundingClientRect()
const topLevel: number = buttonRect?.top ?? 0
const topOffset: number = buttonRect?.height ?? 0
const topLevel = buttonRect?.top ?? 0
const topOffset = buttonRect?.height ?? 0
const leftLevel = buttonRect?.left ?? 0

// The scroll properties fix the position of DropdownMenu when the scroll is activated.
const topPosition = topLevel + topOffset + document.documentElement.scrollTop
const leftLevel = buttonRect?.left ?? 0
const leftPosition = leftLevel + document.documentElement.scrollLeft
const scrollTop = isBrowser ? document?.documentElement?.scrollTop : 0
const scrollLeft = isBrowser ? document?.documentElement?.scrollLeft : 0

const topPosition = topLevel + topOffset + scrollTop

const leftPosition = leftLevel + scrollLeft

return {
position: 'absolute',
Expand Down

0 comments on commit 60f65b0

Please sign in to comment.