Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add orientation handling #3365

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
scrollContainers: HTMLOrSVGElement[] | null
resizeObserver: ResizeObserver | null
lastBounds: RectReadOnly
orientationHandler: null | (() => void)
}

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

// 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 @@
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 @@
scrollContainer.addEventListener('scroll', scrollChange, { capture: true, passive: true }),
)
}
state.current.orientationHandler = () => {
scrollChange()
}
}

// the ref we expose to the user
Expand All @@ -155,10 +168,10 @@
useEffect(() => {
removeListeners()
addListeners()
}, [scroll, scrollChange, resizeChange])

Check warning on line 171 in packages/fiber/src/web/use-measure.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test

React Hook useEffect has missing dependencies: 'addListeners' and 'removeListeners'. Either include them or remove the dependency array

// remove all listeners when the components unmounts
useEffect(() => removeListeners, [])

Check warning on line 174 in packages/fiber/src/web/use-measure.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test

React Hook useEffect has a missing dependency: 'removeListeners'. Either include it or remove the dependency array
return [ref, bounds, forceRefresh]
}

Expand Down