Skip to content

Commit

Permalink
fix: add orientation handling (#3365)
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisSmolek authored Oct 1, 2024
1 parent 3eff722 commit e5f96f9
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/fiber/src/web/use-measure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type State = {
scrollContainers: HTMLOrSVGElement[] | null
resizeObserver: ResizeObserver | null
lastBounds: RectReadOnly
orientationHandler: null | (() => void)
}

export type Options = {
Expand Down Expand Up @@ -67,7 +68,13 @@ export function useMeasure(
}

// keep all state in a ref
const state = useRef<State>({ element: null, scrollContainers: null, resizeObserver: null, lastBounds: bounds })
const state = useRef<State>({
element: null,
scrollContainers: null,
resizeObserver: null,
lastBounds: bounds,
orientationHandler: null,
})

// set actual debounce values early, so effects know if they should react accordingly
const scrollDebounce = debounce ? (typeof debounce === 'number' ? debounce : debounce.scroll) : null
Expand Down Expand Up @@ -124,6 +131,9 @@ export function useMeasure(
state.current.resizeObserver.disconnect()
state.current.resizeObserver = null
}
if (state.current.orientationHandler) {
screen.orientation.removeEventListener('orientationchange', state.current.orientationHandler)
}
}

// add scroll-listeners / observers
Expand All @@ -136,6 +146,9 @@ export function useMeasure(
scrollContainer.addEventListener('scroll', scrollChange, { capture: true, passive: true }),
)
}
state.current.orientationHandler = () => {
scrollChange()
}
}

// the ref we expose to the user
Expand Down

0 comments on commit e5f96f9

Please sign in to comment.