Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-animate-height

Stanko1.6mMIT3.2.3TypeScript support: included

Lightweight React component for animating height using CSS transitions.

react, react-component, slide, slide up, slide down, animate height

readme

React Animate Height

npm version npm downloads

No dependencies React component for animating height using CSS transitions. Slide an element up and down or animate it to any specific height. Content's opacity can be optionally animated as well (check animateOpacity prop bellow).

CSS classes are applied in specific animation states, check animationStateClasses prop.

Changelog

Version 3

This is version 3.x branch, rewritten to hooks, which means you'll need React version 16.8 or newer.

For version 2.x, check v2 branch

Breaking changes:

  • Callback names changed (to avoid clashing with the native ones):
    • onAnimationStart -> onHeightAnimationStart
    • onAnimationEnd -> onHeightAnimationEnd

Demo

Live demo: muffinman.io/react-animate-height

Because multiple people asked how to animate list items, I created this simple example for that as well.

To build the examples locally, run:

npm install
npm start

Then open http://127.0.0.1:8000/ in your browser of choice browser.

Or play with this sandbox.

Quick start

Get it from npm

$ npm install --save react-animate-height

Import and use it in your React app.

import React, { useState } from 'react';
import AnimateHeight from 'react-animate-height';

const Example = () => {
  const [height, setHeight] = useState(0);

  return (
    <div>
      <button
        aria-expanded={height !== 0}
        aria-controls="example-panel"
        onClick={() => setHeight(height === 0 ? 'auto' : 0)}
      >
        {height === 0 ? 'Open' : 'Close'}
      </button>

      <AnimateHeight
        id="example-panel"
        duration={500}
        height={height} // see props documentation below
      >
        <h1>Your content goes here</h1>
        <p>Put as many React or HTML components here.</p>
      </AnimateHeight>
    </div>
  );
};

Props

  • height: numeric or percentage value (ie. '50%') or 'auto', required

    When changed, element height will be animated to that height.
    To slide up use 0, for slide down use 'auto'

  • duration: integer, default: 500

    Duration of the animation in milliseconds

  • delay: integer, default: 0

    Animation delay in milliseconds

  • easing: string, default: 'ease'

    CSS easing function to be applied to the animation

  • id: string

    HTML id attribute.

  • className: string

    CSS class to be applied to the element

    Please note that you shouldn't apply properties that are messing with the layout (like display, height...), as these might break height calculations

  • ref: React.MutableRefObject<HTMLDivElement | null>

    Reference to the main div element.

      const wrapperDiv = useRef<HTMLDivElement | null>(null);
    
      <AnimateHeight ref={wrapperDiv}>
  • contentRef: React.MutableRefObject<HTMLDivElement | null>

    Reference to the content div element.

      const contentDiv = useRef<HTMLDivElement | null>(null);
    
      <AnimateHeight contentRef={contentDiv}>
  • style: object

    CSS style object, it will be merged with inline styles of the component

    Please note that you shouldn't apply properties that are messing with the layout (display, height are omitted from the type already), as these might break height calculations

  • contentClassName: string

    CSS class to be applied to content wrapper element

    Please note that you shouldn't apply properties that are messing with the layout (like display, height...), as these might break height calculations

  • animationStateClasses: object

    Object containing CSS class names for animation states, default:

    {
      animating:                  'rah-animating',
      animatingUp:                'rah-animating--up',
      animatingDown:              'rah-animating--down',
      static:                     'rah-static',
      animatingToHeightZero:      'rah-animating--to-height-zero',
      animatingToHeightAuto:      'rah-animating--to-height-auto',
      animatingToHeightSpecific:  'rah-animating--to-height-specific',
      staticHeightZero:           'rah-static--height-zero',
      staticHeightAuto:           'rah-static--height-auto',
      staticHeightSpecific:       'rah-static--height-specific',
    }

    Please note that this one will be merged with the default object and cached when component is created, so changing it afterwards will have no effect.

  • onHeightAnimationStart: function

    Callback which will be called when animation starts.

    This first argument passed to this callback is an object containing newHeight, the pixel value of the height at which the animation will end.

  • onHeightAnimationEnd: function

    Callback which will be called when animation ends.

    This first argument passed to this callback is an object containing newHeight, the pixel value of the height at which the animation ended.

  • applyInlineTransitions: boolean, default: true

    If this flag is set to false only CSS classes will be applied to the element and inline transition styles will not be present.

  • animateOpacity: boolean, default: false

    If set to true content will fade-in (and fade-out) while height is animated.

  • aria-hidden: boolean

    By default, library will set aria-hidden to true when height is zero. If you wish to override it, you can pass the prop yourself.

  • disableDisplayNone: boolean, default: false

    By default, library will set display: none when height is zero. This prop allows you to disable that behavior and handle it yourself. It is useful when using auto height, check this issue for more info. Please be careful not to break accessibility when using this prop.

Additional props will be passed to the wrapper div, to make adding attrs like aria-* easier.

Accessibility

Library will hide the content using display: hidden when height props is 0. It will also apply aria-hidden="true" in the same case, but you can override it by passing aria-hidden prop yourself.

When using a button to toggle height, make sure you add aria-expanded and aria-controls to make everything accessible. Here's an example:

<button
  aria-expanded={ height !== 0 }
  aria-controls="example-panel" // it has to match the id passed to AnimateHeight
  onClick={ toggleHeight } // your click handler that toggles height
  // ... all other props
>
  Toggle
</button>

<AnimateHeight id="example-panel">
  Content
</AnimateHeight>

Reduced motion

Component checks for prefers-reduced-motion on start and disables animations if it is set to true. Please note that component doesn't listen for potential changes of prefers-reduced-motion option.

Animate height on content change

It is not built in, but you can use AnimateHeight and ResizeObserver to automatically animate height on content change. I created a small example for you here:

Gotchas

Collapsing margins

While it is animating, component has overflow: hidden. When the animation is finished and height is set to "auto", overflow: hidden is removed. At that moment, any margins you have on the content inside AnimateHeight will collapse, causing content to "jump". To avoid this, just use padding inside the AnimateHeight instead of margins.

Bounded flexboxes

If AnimateHeight is a flex child and it's parent has a fixed height, animation won't work. To fix this, you just need to add the following CSS rule to the AnimateHeight instance.

flex-shrink: 0;

You can do it by passing a className or directly in the style prop

<AnimateHeight style={{flexShrink: 0}}>

Check the issue #89 for the example and more details.

License

MIT

changelog

Changelog

v3.2.3

10.11.2023.

Added

  • Added prop to disable display: none when height is set to zero #16.

v3.2.1, v3.2.2

11.07.2022.

Fixed

  • Added "type": "module" to the package.json #143 and hopefully fixed all import/require shenanigans.

v3.2.0

10.07.2022.

Added

  • Added "ref" and "contentRef" props.
  • Added <AutoHeight> example.

Fixed

  • Removed two package.json files to resolve issue with Webpack Module Federation #140

v3.1.2

05.07.2022.

Fixed

  • Added version to two package.json files to resolve issue with Webpack Module Federation #140

v3.1.1

21.02.2022.

Fixed

  • Replaced __rest shim to avoid rollup warnings #136

v3.1.0

15.11.2022.

Changed

  • Dropped classnames dependency, thanks Davey #135

v3.0.5

15.11.2022.

Changed

  • TypeScript definitions are now exported.

v3.0.4

19.06.2022.

Fixed

  • Fixed #129 by swapping ESM module target from esnext to ES2015 (aka es6).

v3.0.0-v3.0.3

31.05.2022.

Changed

  • Rewritten to hooks
  • Switched to TypeScript
  • Callback names changed
    • onAnimationStart -> onHeightAnimationStart
    • onAnimationEnd -> onHeightAnimationEnd
  • Switched back to a short timeout instead of requestAnimationFrame for starting the animation

v2.1.1, v2.1.2

12.04.2022.

Added

  • Added check for prefers-reduced-motion and disabled animations if it is set to true - #124

v2.1.0

12.04.2022.

Fixed

  • Fixed component not working with React 18 and StrictMode - #123

v2.0.23

01.08.2020.

Added

  • Added "id" and "aria-hidden" props.
  • Added section about accessibility in the readme.

v2.0.22

15.07.2020.

Added

  • Added "engines" to package.json - #91

v2.0.21

18.04.2020.

Updated

  • Updated the readme with the specific flex box usecase - #89

v2.0.18, v2.0.19 and v2.0.20

23.12.2019. and 17.01.2020.

Fixed

  • Fixed TS types - #88

v2.0.17

09.11.2019.

Fixed

  • Fixed TS types to include HTMLAttributes<HTMLDivElement> #86

v2.0.16

08.10.2019.

Fixed

  • Request animation frame is now cleared on unmount

v2.0.14-v2.0.15

18.06.2019.

Fixed

  • Reverted back changes, which led to a broken build.

v2.0.10-v2.0.13

17.06.2019.

Fixed

  • Package now works as a ESM module #73.

v2.0.9

16.05.2019.

Fixed

  • Improved prop types for height prop.

v2.0.8

16.03.2019.

Fixed

  • Fixed type definitions #68.

v2.0.7

12.11.2018.

Added

  • Added param to onAnimationStart and onAnimationEnd callbacks, it is a object containing newHeight value in pixels.

v2.0.6

03.10.2018.

Changed

  • Removed @types/react from optionalDependencies #62.

v2.0.5

26.09.2018.

Fixed

  • Fixed #61 - omitted onAnimationStart and onAnimationEnd from being passed to the DOM element directly.

v2.0.4

15.08.2018.

Refactored

  • Added isPercentage helper function.

v2.0.3

12.07.2018.

Fixed

  • Fixed small bug introduced with 2.0.0 - In componentDidMount, this.state was used instead of prevState
  • Content is not being hidden if set height is auto

v2.0.2

11.07.2018.

Fixed

  • Fixed type script definitions file

v2.0.0 and v2.0.1

20.05.2018.

Changed

  • Replaced componentWillReceiveProps by componentDidUpdate to address changes introduces with React v16.3

v1.0.4

20.05.2018.

Changed

  • Enabled react v16.3+ in peer dependencies until react-animate-height v2 is out

v1.0.3

20.05.2018.

Changed

  • Moved helpers outside of component to make component lighter
  • Updated few dependencies

v1.0.2

21.04.2018.

Added

  • Added delay prop, kudos to @quagliero #51
  • Changelog

v1.0.1

30.03.2018.

Fixed

  • animateOpacity prop was passed directly to the div element, omitted it

v1.0.0

30.03.2018.

Added

  • animateOpacity prop which fades content in or out depending on animation direction

Changed

  • Removed unused vendor prefixes for translate

For changes prior version 1.0.0 please check the commit list.