TabPaneTransition.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* eslint-disable react/destructuring-assignment */
  2. import React, { FC, ReactNode } from 'react';
  3. import { Transition } from '@douyinfe/semi-animation-react';
  4. import { toInteger } from 'lodash';
  5. import { TabPaneTransitionProps } from './interface';
  6. const TabPaneTransition: FC<TabPaneTransitionProps> = (props = {}) => {
  7. const direction = props.direction ? 1 : -1;
  8. const { mode } = props;
  9. let { motion } = props;
  10. const ratio = 60;
  11. if (typeof motion === 'function') {
  12. motion = motion(props);
  13. } else if (!motion || typeof motion !== 'object') {
  14. motion = {};
  15. }
  16. if (mode === 'vertical') {
  17. return (
  18. <Transition
  19. {...props}
  20. config={{ tension: 612, friction: 32 }}
  21. from={{ translateY: direction * ratio, opacity: 0 }}
  22. enter={{ translateY: 0, opacity: { val: 1, duration: 200 } } as any}
  23. leave={{ translateY: -1 * direction * ratio, opacity: { val: 0, duration: 200 } } as any}
  24. {...motion}
  25. >
  26. {typeof props.children === 'function' ?
  27. ({ translateY, opacity }: { translateY: number;opacity: number }): ReactNode => {
  28. // delete translateX in 0 in case of zIndex problems.
  29. const finalState = toInteger(translateY) === 0 ? { opacity } : {
  30. transform: `translateY(${toInteger(translateY)}px)`,
  31. opacity,
  32. };
  33. return props.children(finalState);
  34. } :
  35. props.children}
  36. </Transition>
  37. );
  38. }
  39. return (
  40. <Transition
  41. {...props}
  42. config={{ tension: 612, friction: 32 }}
  43. from={{ translateX: direction * ratio, opacity: 0 }}
  44. enter={{ translateX: 0, opacity: { val: 1, duration: 200 } } as any}
  45. leave={{ translateX: -1 * direction * ratio, opacity: { val: 0, duration: 200 } } as any}
  46. {...motion}
  47. >
  48. {typeof props.children === 'function' ?
  49. ({ translateX, opacity }: { translateX: number;opacity: number }): ReactNode => {
  50. // delete translateX in 0 in case of zIndex problems.
  51. const finalState = toInteger(translateX) === 0 ? { opacity } : {
  52. transform: `translateX(${toInteger(translateX)}px)`,
  53. opacity,
  54. };
  55. return props.children(finalState);
  56. } :
  57. props.children}
  58. </Transition>
  59. );
  60. };
  61. export default TabPaneTransition;