Skip to content

Instantly share code, notes, and snippets.

@ItsJonQ
Created March 2, 2021 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ItsJonQ/31c470eb64cbce3762db19dd7841206e to your computer and use it in GitHub Desktop.
Save ItsJonQ/31c470eb64cbce3762db19dd7841206e to your computer and use it in GitHub Desktop.
React Spring: Height auto collapse/expand animations
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