FadeIn.tsx 1010 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import React, { FC, CSSProperties } from 'react';
  2. import Transition, { ExitHandler } from 'react-transition-group/Transition';
  3. interface Props {
  4. duration: number;
  5. children: JSX.Element;
  6. in: boolean;
  7. unmountOnExit?: boolean;
  8. onExited?: ExitHandler;
  9. }
  10. export const FadeIn: FC<Props> = props => {
  11. const defaultStyle: CSSProperties = {
  12. transition: `opacity ${props.duration}ms linear`,
  13. opacity: 0,
  14. };
  15. const transitionStyles: { [str: string]: CSSProperties } = {
  16. exited: { opacity: 0, display: 'none' },
  17. entering: { opacity: 0 },
  18. entered: { opacity: 1 },
  19. exiting: { opacity: 0 },
  20. };
  21. return (
  22. <Transition
  23. in={props.in}
  24. timeout={props.duration}
  25. unmountOnExit={props.unmountOnExit || false}
  26. onExited={props.onExited}
  27. >
  28. {state => (
  29. <div
  30. style={{
  31. ...defaultStyle,
  32. ...transitionStyles[state],
  33. }}
  34. >
  35. {props.children}
  36. </div>
  37. )}
  38. </Transition>
  39. );
  40. };