Any way to animate the height of a div with height="auto" when the content of that div changes size? #1884
Replies: 6 comments 7 replies
-
I know this is an older thread, but I had the same challenge. Ended up doing what you suggested: Measuring the height manually whenever it changes. This is the abstracted component I came up with: import classNames from 'classnames'
import { motion } from 'framer-motion'
import React, { useEffect, useRef, useState } from 'react'
interface AnimateChangeInHeightProps {
children: React.ReactNode
className?: string
}
export const AnimateChangeInHeight: React.FC<AnimateChangeInHeightProps> = ({ children, className }) => {
const containerRef = useRef<HTMLDivElement | null>(null)
const [height, setHeight] = useState<number | 'auto'>('auto')
useEffect(() => {
if (containerRef.current) {
const resizeObserver = new ResizeObserver((entries) => {
// We only have one entry, so we can use entries[0].
const observedHeight = entries[0].contentRect.height
setHeight(observedHeight)
})
resizeObserver.observe(containerRef.current)
return () => {
// Cleanup the observer when the component is unmounted
resizeObserver.disconnect()
}
}
}, [])
return (
<motion.div className={classNames(className, 'overflow-hidden')} style={{ height }} animate={{ height }} transition={{ duration: 0.1 }}>
<div ref={containerRef}>{children}</div>
</motion.div>
)
} You can then use it like this: <AnimateChangeInHeight>
<div>
// some content that changes in height
</div>
</AnimateChangeInHeight> |
Beta Was this translation helpful? Give feedback.
-
I think this won't work when the height of children becomes smaller right? Because the parent has to follow reduction in height so it won't animate |
Beta Was this translation helpful? Give feedback.
-
For future reference, this solution is much simpler to implement - just a few lines of CSS: |
Beta Was this translation helpful? Give feedback.
-
If you want to animate the height of the parent div when children divs are added to it, then instead of animating the parent, animate the height of the children divs. Codesandbox: https://codesandbox.io/p/devbox/expandable-div-animation-zckhwj |
Beta Was this translation helpful? Give feedback.
-
Here's another code example if anyone is interested and uses radix. You can supplement
|
Beta Was this translation helpful? Give feedback.
-
HELP PLEASE: I tried the solution with ref pasted for height. It works well with height, but gets the width gets stuck based on the first child. Please fork it and not edit it directly |
Beta Was this translation helpful? Give feedback.
-
Hey everyone!
I'm having a bit of an issue with Framer Motion (using it in React) that I can't seem to solve elegantly.
I have a motion.div that has some content in it. That div has a height="auto" so it wraps the div content. When that content change, I animate that change. So far so good. The problem is, if the content changes height, the height of the animated div doesn't animate to the new value and instead jumps directly to it.
This makes sense from my understanding, since the height is set to "auto", there's no animation happening to go from height="auto" to height="auto". I could animate the height by specifying the values in px or rem or whatever, but the problem is: I don't know that height since the height is dependent on the content.
Here's a simple example of what I mean : https://stackblitz.com/edit/react-pxnjuj?file=src/App.js
Notice how the buttons below the div jumps instead of animating when the content is changed.
I'm assuming I could make it work by measuring the height of the current content and using it as a starting value before changing the content, but that seems a bit cumbersome to me. Is there no easier way to do this? Maybe something like
where useCurrentValue() would automatically use the currently measured value?
Beta Was this translation helpful? Give feedback.
All reactions