Created
March 2, 2021 22:07
-
-
Save ItsJonQ/31c470eb64cbce3762db19dd7841206e to your computer and use it in GitHub Desktop.
React Spring: Height auto collapse/expand animations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useReducedMotion } from '@wp-g2/styles'; | |
import React from 'react'; | |
import { animated, useSpring } from 'react-spring'; | |
function useCollapsibleHeightAnimation({ duration = 120, isVisible = false }) { | |
const contentRef = React.useRef(); | |
const previouslyVisible = React.useRef(isVisible); | |
const [reducedMotion] = useReducedMotion(); | |
const [animatedHeight, set] = useSpring(() => ({ | |
height: isVisible ? 'auto' : 0, | |
opacity: isVisible ? 1 : 0, | |
config: { duration }, | |
})); | |
React.useEffect(() => { | |
if (isVisible === previouslyVisible.current) return; | |
previouslyVisible.current = isVisible; | |
if (isVisible) { | |
contentRef.current.style.display = 'block'; | |
contentRef.current.style.height = 'auto'; | |
const height = contentRef.current.getBoundingClientRect().height; | |
contentRef.current.style.height = 0; | |
set({ | |
height, | |
immediate: reducedMotion, | |
opacity: 1, | |
}); | |
} else { | |
const height = contentRef.current.getBoundingClientRect().height; | |
contentRef.current.style.height = height; | |
set({ | |
height: 0, | |
immediate: reducedMotion, | |
opacity: 0, | |
}); | |
} | |
set({ | |
onRest: () => { | |
if (isVisible) { | |
contentRef.current.style.height = 'auto'; | |
contentRef.current.style.opacity = 1; | |
} else { | |
contentRef.current.style.display = 'none'; | |
contentRef.current.style.opacity = 0; | |
} | |
}, | |
}); | |
}, [isVisible, reducedMotion, set]); | |
return [contentRef, animatedHeight]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment