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

Package detail

react-smooth-collapse

StreakYC70.6kMIT2.1.2TypeScript support: included

React component for animating showing or hiding an element.

react, react-component, animation, hide, collapse

readme

react-smooth-collapse

Circle CI npm version

This component lets you animate the height of an element to reveal or hide its contents. The animation automatically adjusts to the natural height of the contents.

Example

An example can be tried here:

https://streakyc.github.io/react-smooth-collapse/example/

You can find its code in the example directory. The example may be compiled by running:

yarn
yarn example-build
# or use this to auto-rebuild on changes:
yarn example-watch

Q: Why would I use this when I could set a transition rule for height on an element, and then change the height from "auto" to "0"?

A: You can't animate from "auto". This component has the height set to "auto" while the element is expanded, and when the element is set to collapse, the element's height is set to equal its current height, and then set to "0" so that it animates shrinking correctly.

Q: Couldn't I animate shrinking by setting a transition rule for max-height, setting max-height to a very large value when the element is expanded, and then set max-height to "0" when the element is collapsed?

A: That won't animate with the given duration and won't fully respect your timing function. For example, if you have an element that currently has a height of 100px, a max-height of 10000px, and a transition rule of "max-height 1s linear", then it will take 0.99 seconds before the element appears to start shrinking, and then it will fully shrink in 0.01 seconds. If you use a timing function like "ease" instead of "linear", then the easing will only be apparent while the element finishes shrinking to 0 or begins expanding from 0.

SmoothCollapse

This module exports the SmoothCollapse React component. The children of the component should be the contents you want to show or hide. The component also takes the following props:

  • expanded must be a boolean controlling whether to show the children.
  • onChangeEnd may be a function which will be called whenever a show or hide animation is completed.
  • collapsedHeight is the CSS height that the contents should have when collapsed. Defaults to "0".
  • heightTransition may be a string and is used for customizing the animation. This value is prefixed with "height " and is set as the CSS transition property of the SmoothCollapse element. This property defaults to ".25s ease".
  • allowOverflowWhenOpen is an optional boolean that when true causes the overflow:hidden CSS rule to be removed while the element is open. This behavior is off by default because the CSS rule must be present while animating, and contents that rely on the rule not being present while open may be jarringly effected when the rule is added. You may want this prop turned on if the children contains a dropdown element which is meant to visually escape its container.
    • eagerRender will ensure that all children are always rendered, even if they have never been expanded. This property defaults to false.

Additional props such as className will be passed on to the outer element. Care should be taken if any rules added by the class name conflict with SmoothCollapse's own CSS properties.

If the SmoothCollapse component starts out with expanded set to false, eagerRender is set to false, and collapsedHeight is 0, then the children are not rendered until the first time the component is expanded. After the component has been expanded once, the children stay rendered so that they don't lose their state when they're hidden.

Types

Both TypeScript and Flow type definitions for this module are included! The type definitions won't require any configuration to use.

changelog

2.1.2 (2022-08-28)

  • Added React v18 to peerDependencies. This library wasn't affected by any React changes and works without warnings even in Strict Mode.

2.1.1 (2022-01-22)

  • Added React v17 to peerDependencies.

2.1.0 (2019-07-30)

  • Pass any unrecognized props on to the outer div element #23

2.0.1 (2019-03-12)

  • Removed dependency on Kefir (10KB minified and gzipped).

2.0.0 (2018-09-24)

Breaking Changes

  • Now requires React ^16.3.0.
  • The module now exports the SmoothCollapse component as the default export. If you use SmoothCollapse by using import SmoothCollapse from 'react-smooth-collapse'; with Babel or TypeScript, then you don't need to change anything, but if you use require('react-smooth-collapse'), then you'll need to change your code to use import or to use require('react-smooth-collapse').default.

Improvements

  • No longer uses deprecated React methods.
  • Added TypeScript type definitions.

1.6.0 (2018-09-24)

  • Added eagerRender prop #19

1.5.0 (2018-02-19)

  • Added allowOverflowWhenOpen prop #10

1.4.0 (2018-02-19)

  • Added className prop #9

1.3.2 (2017-10-04)

  • Minor optimization to internal ref handling.
  • Changed the package's peerDependencies to mark compatibility with React v16.

1.3.0 (2017-08-23)

  • Added support for Flow 0.53.

1.2.0 (2017-04-25)

  • Breaking change: React ^0.14.9 or ^15.3.0 is now required. (The new version of React is backwards compatible as its change was only a minor version bump, so I didn't consider this a change worthy of a major version bump in this package, but arguably this was a semver violation on my part.)
  • Use the new React prop-types package.

1.1.1 (2017-03-28)

  • Fixed rare issue that could cause onChangeEnd to not be fired when it should have been.

1.1.0 (2016-08-17)

  • Added collapsedHeight prop #3

1.0.3 (2016-04-07)

  • Changed the package's peerDependencies to mark compatibility with React v15.

1.0.2 (2016-03-10)

  • Fixed issue causing the height animation to be jumpy when the inner element has a margin.

1.0.1 (2016-03-10)

  • Apply display:none to the inner element when SmoothCollapse is collapsed. Prevents the hidden elements from becoming focused.

1.0.0 (2016-02-26)

Initial stable release.