motion-container.tsx 971 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { m, type MotionProps } from 'framer-motion'
  2. import { varContainer } from './variants/container'
  3. interface Props extends MotionProps {
  4. className?: string
  5. }
  6. /**
  7. * Motion 通用容器
  8. *
  9. * variants: [变体可以用于使用单个动画道具为组件的整个子树设置动画](https://www.framer.com/motion/animation/#variants)
  10. *
  11. * Variants 是一组预定义的对象
  12. * const variants = {
  13. * visible: { opacity: 1 },
  14. * hidden: { opacity: 0 },
  15. * }
  16. *
  17. * 需要指定 inital 和 animate 属性名
  18. * <motion.div
  19. * initial="hidden"
  20. * animate="visible"
  21. * variants={variants}
  22. * />
  23. */
  24. export default function MotionContainer({ children, className }: Props) {
  25. return (
  26. <m.div
  27. // 这里指定 initial、animate和exit的属性名后,子组件就不需要再重复指定
  28. initial='initial'
  29. animate='animate'
  30. exit='exit'
  31. variants={varContainer()}
  32. className={className}>
  33. {children}
  34. </m.div>
  35. )
  36. }