SubNavTransition.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { Transition } from '@douyinfe/semi-animation-react';
  2. import PropTypes from 'prop-types';
  3. import React, { useState } from 'react';
  4. import { Motion } from '@douyinfe/semi-foundation/utils/type';
  5. const ease = 'cubicBezier(.25,.1,.25,1)';
  6. const formatStyle = function formatStyle({ maxHeight, opacity }: { maxHeight: number; opacity: number }) {
  7. return {
  8. maxHeight,
  9. opacity,
  10. };
  11. };
  12. export interface SubNavTransitionProps {
  13. children?: React.ReactNode;
  14. isCollapsed?: boolean;
  15. maxHeight?: number;
  16. motion?: Motion;
  17. }
  18. function SubNavTransition(props: SubNavTransitionProps = {}) {
  19. const { children, isCollapsed, maxHeight = 999 } = props;
  20. // eslint-disable-next-line no-unused-vars
  21. const [immediate, setImmediate] = useState(false);
  22. // useEffect(() => {
  23. // setImmediate(isCollapsed);
  24. // }, [isCollapsed]);
  25. return (
  26. <Transition
  27. from={{ maxHeight: 0, opacity: 0 }}
  28. enter={{
  29. maxHeight: { val: maxHeight, easing: 'easeInQuad', duration: 250 },
  30. opacity: { val: 1, duration: 200, easing: 'cubic-bezier(0.5, -0.1, 1, 0.4)' },
  31. }}
  32. leave={{
  33. maxHeight: { val: 0, easing: ease, duration: 250 },
  34. opacity: {
  35. val: 0,
  36. duration: isCollapsed ? 1 : 200, // Need to be fast and transparent when put away, otherwise there will be jumping
  37. easing: 'cubic-bezier(0.5, -0.1, 1, 0.4)',
  38. },
  39. }}
  40. immediate={immediate}
  41. >
  42. {typeof children === 'function' ? (transitionStyle: { maxHeight: number; opacity: number }) => children(formatStyle(transitionStyle)) : children}
  43. </Transition>
  44. );
  45. }
  46. SubNavTransition.propTypes = {
  47. children: PropTypes.any,
  48. isCollapsed: PropTypes.bool,
  49. };
  50. export default SubNavTransition;