All components

MotionHeight

Height that follows its content: collapse, expand and content changes.

WebReact NativeContent and size

Try it

Preparing the example…

Shortest usage

Details.jsx
<button onClick={() => setOpen(!open)}>Details</button>
<MotionHeight open={open}>
  <Answer />
</MotionHeight>

What it handles for you

  • Animates to the content's real height, measured for you.
  • Follows the content when it grows or shrinks while open.
  • Starts from the height on screen, even in the middle of a change.
  • Resizing the page snaps instead of lagging behind the new width.
  • Closed content stays mounted but hidden and inert, so its state is kept.

Use it for

  • Answers in a FAQ.
  • A group of settings that opens.
  • A list whose total height should animate (wrap MotionList).

Props

PropTypeDefaultDescription
openbooleantruefalse collapses to zero height; the content stays mounted but hidden and inert.
childrenReactNodeThe content.
aselement'div'The element to render.
transitionTransitionspringA timing curve { duration, easing } or a spring { tempo: 'spring', spring }. Springs keep their momentum when interrupted.
onMotionComplete(open) => voidRuns with open once the height has settled.

Other props go to the rendered element, so className, style and event handlers work as usual.

Web and React Native

On the web the height plays on the Web Animations API and the content keeps its normal layout.

On React Native the view clips its content and animates height on the JS driver, because height is layout.

Used in these examples

Search

Type to search the documentation.