Skip to content

Commit

Permalink
fix: use layout effect to prevent tearing on Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jan 21, 2021
1 parent ceb3671 commit 6b62559
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { rubberbandIfOutOfBounds, useDrag } from 'react-use-gesture'
import {
useAriaHider,
useFocusTrap,
useLayoutEffect,
useReady,
useReducedMotion,
useScrollLock,
Expand Down Expand Up @@ -143,7 +144,7 @@ export const BottomSheet = React.forwardRef<
const findSnapRef = useRef(findSnap)
const defaultSnapRef = useRef(0)
// Sync the refs with current state, giving the spring full control over when to respond to changes
useEffect(() => {
useLayoutEffect(() => {
maxHeightRef.current = maxHeight
maxSnapRef.current = maxSnap
minSnapRef.current = minSnap
Expand Down Expand Up @@ -409,15 +410,15 @@ export const BottomSheet = React.forwardRef<
send('CLOSE')
}
}, [_open, send, ready])
useEffect(() => {
useLayoutEffect(() => {
// Adjust the height whenever the snap points are changed due to resize events
if (maxHeight || maxSnap || minSnap) {
send('RESIZE')
}
}, [maxHeight, maxSnap, minSnap, send])
useEffect(
() => () => {
// Ensure effects are cleaned up on unmount, in case they're not cleaend up otherwise
// Ensure effects are cleaned up on unmount, in case they're not cleaned up otherwise
scrollLockRef.current.deactivate()
focusTrapRef.current.deactivate()
ariaHiderRef.current.deactivate()
Expand Down
18 changes: 11 additions & 7 deletions src/hooks/useSnapPoints.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { defaultSnapProps, ResizeSource, snapPoints } from '../types'
import { processSnapPoints, roundAndCheckForNaN } from '../utils'
import { useReady } from './useReady'
import { ResizeObserverOptions } from '@juggle/resize-observer/lib/ResizeObserverOptions'
import { useLayoutEffect } from './useLayoutEffect'

export function useSnapPoints({
contentRef,
Expand Down Expand Up @@ -187,13 +188,16 @@ function useElementSizeObserver(

useDebugValue(`${label}: ${size}`)

const handleResize = useCallback((entries: ResizeObserverEntry[]) => {
// we only observe one element, so accessing the first entry here is fine
setSize(entries[0].borderBoxSize[0].blockSize)
resizeSourceRef.current = 'element'
}, [])
const handleResize = useCallback(
(entries: ResizeObserverEntry[]) => {
// we only observe one element, so accessing the first entry here is fine
setSize(entries[0].borderBoxSize[0].blockSize)
resizeSourceRef.current = 'element'
},
[resizeSourceRef]
)

useEffect(() => {
useLayoutEffect(() => {
if (!ref.current || !enabled) {
return
}
Expand Down Expand Up @@ -232,7 +236,7 @@ function useMaxHeight(
}
}, [ready, setReady])

useEffect(() => {
useLayoutEffect(() => {
// Bail if the max height is a controlled prop
if (controlledMaxHeight) {
setMaxHeight(roundAndCheckForNaN(controlledMaxHeight))
Expand Down

0 comments on commit 6b62559

Please sign in to comment.