Skip to content

Commit

Permalink
fix: useMeasure to include safer orientation handling (#3375)
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisSmolek authored Oct 9, 2024
1 parent 37fdbec commit ddd973b
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions packages/fiber/src/web/use-measure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,39 @@ export function useMeasure(
state.current.resizeObserver.disconnect()
state.current.resizeObserver = null
}

if (state.current.orientationHandler) {
screen.orientation.removeEventListener('orientationchange', state.current.orientationHandler)
if ('orientation' in screen && 'removeEventListener' in screen.orientation) {
screen.orientation.removeEventListener('change', state.current.orientationHandler)
} else if ('onorientationchange' in window) {
window.removeEventListener('orientationchange', state.current.orientationHandler)
}
}
}

// add scroll-listeners / observers
function addListeners() {
if (!state.current.element) return
state.current.resizeObserver = new ResizeObserver(scrollChange)
state.current.resizeObserver!.observe(state.current.element)
state.current.resizeObserver = new ResizeObserver(resizeChange)
state.current.resizeObserver?.observe(state.current.element)
if (scroll && state.current.scrollContainers) {
state.current.scrollContainers.forEach((scrollContainer) =>
scrollContainer.addEventListener('scroll', scrollChange, { capture: true, passive: true }),
)
}

// Handle orientation changes
state.current.orientationHandler = () => {
scrollChange()
}

// Use screen.orientation if available
if ('orientation' in screen && 'addEventListener' in screen.orientation) {
screen.orientation.addEventListener('change', state.current.orientationHandler)
} else if ('onorientationchange' in window) {
// Fallback to orientationchange event
window.addEventListener('orientationchange', state.current.orientationHandler)
}
}

// the ref we expose to the user
Expand Down

0 comments on commit ddd973b

Please sign in to comment.