SlideDown.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import React from 'react';
  2. import Transition from 'react-transition-group/Transition';
  3. interface Style {
  4. transition?: string;
  5. overflow?: string;
  6. }
  7. // When animating using max-height we need to use a static value.
  8. // If this is not enough, pass in <SlideDown maxHeight="....
  9. const defaultMaxHeight = '200px';
  10. const defaultDuration = 200;
  11. export const defaultStyle: Style = {
  12. transition: `max-height ${defaultDuration}ms ease-in-out`,
  13. overflow: 'hidden',
  14. };
  15. export default ({ children, in: inProp, maxHeight = defaultMaxHeight, style = defaultStyle }) => {
  16. // There are 4 main states a Transition can be in:
  17. // ENTERING, ENTERED, EXITING, EXITED
  18. // https://reactcommunity.org/react-transition-group/
  19. const transitionStyles = {
  20. exited: { maxHeight: 0 },
  21. entering: { maxHeight: maxHeight },
  22. entered: { maxHeight: maxHeight, overflow: 'visible' },
  23. exiting: { maxHeight: 0 },
  24. };
  25. return (
  26. <Transition in={inProp} timeout={defaultDuration}>
  27. {state => (
  28. <div
  29. style={{
  30. ...style,
  31. ...transitionStyles[state],
  32. }}
  33. >
  34. {children}
  35. </div>
  36. )}
  37. </Transition>
  38. );
  39. };