A container that animates its height to fit its content whenever it changes.
Installation
Usage
import { AutoHeight } from '@animate-ui/react/primitives/effects/auto-height';
export function Details({ expanded }: { expanded: boolean }) {
return (
<AutoHeight deps={[expanded]}>
{expanded ? <LongContent /> : <ShortContent />}
</AutoHeight>
);
}The content is measured with a ResizeObserver, so the height also follows changes deps doesn't know about (fonts loading, images, a resized window). deps re-measures right away when your state changes.
Animation
- The height springs to the content's height (stiffness 300, damping 30, no bounce), clipping the content while it moves.
- With
box-sizing: border-box(Tailwind's default), the container's padding and border are included in the measured height. <AnimateUIProvider tempo>paces the default spring.- Under reduced motion, the height changes at once.
API Reference
AutoHeight
| Prop | Type | Default |
|---|---|---|
asChild? | boolean | false |
deps? | React.DependencyList | [] |
transition? | Transition | { type: 'spring', stiffness: 300, damping: 30, bounce: 0, restDelta: 0.01 } |
animate? | TargetAndTransition | LegacyAnimationControls | - |
children | React.ReactNode | - |
...props? | Omit<HTMLMotionProps<'div'>, 'animate'> | - |