collapsable.js 773 B

123456789101112131415161718192021
  1. import React from 'react';
  2. import { Motion, spring } from 'react-motion';
  3. import PropTypes from 'prop-types';
  4. const Collapsable = ({ fullHeight, isVisible, children }) => (
  5. <Motion defaultStyle={{ opacity: !isVisible ? 0 : 100, height: isVisible ? fullHeight : 0 }} style={{ opacity: spring(!isVisible ? 0 : 100), height: spring(!isVisible ? 0 : fullHeight) }}>
  6. {({ opacity, height }) =>
  7. <div style={{ height: `${height}px`, overflow: 'hidden', opacity: opacity / 100, display: Math.floor(opacity) === 0 ? 'none' : 'block' }}>
  8. {children}
  9. </div>
  10. }
  11. </Motion>
  12. );
  13. Collapsable.propTypes = {
  14. fullHeight: PropTypes.number.isRequired,
  15. isVisible: PropTypes.bool.isRequired,
  16. children: PropTypes.node.isRequired
  17. };
  18. export default Collapsable;