The TypeScript error you're encountering stems from a type conflict between Chakra UI's Box component and Framer Motion's motion properties. Specifically, both Chakra's Box and Framer Motion define a transition prop, but they expect different types for it. Here's a detailed explanation and a step-by-step solution to resolve this issue.
Understanding the Error
The error message:
Type '{ repeat: number; duration: number; repeatType: "reverse"; }' is not assignable to type 'ResponsiveValue<Transition<string & {}>> | undefined'.
Types of property 'repeat' are incompatible.
Type 'number' is not assignable to type '((count: number) => string) | Transition<string & {}> | undefined'.
Cause:
-
Chakra UI's transition Prop: Chakra's Box component includes a transition prop, which is used for CSS transitions. This expects a different type, typically a string representing CSS transition properties.
-
Framer Motion's transition Prop: Framer Motion uses the transition prop for animation configurations, accepting objects that define animation behavior (e.g., duration, repeat, repeatType, etc.).
When you combine Chakra UI with Framer Motion by creating a MotionBox, TypeScript gets confused because it doesn't know which transition prop you're referring to—the one from Chakra or the one from Framer Motion.
Solution
To resolve this type conflict, you need to ensure that your MotionBox component correctly merges Chakra UI's BoxProps with Framer Motion's MotionProps, while handling the overlapping transition prop appropriately.
Here's how you can do it:
-
Create a Custom MotionBox Component:
Define a MotionBox component that combines Chakra UI's Box with Framer Motion's motion. Importantly, you'll need to omit Chakra's transition prop to prevent type conflicts and then reintroduce Framer Motion's transition prop.
// components/MotionBox.tsx
import { motion, MotionProps } from 'framer-motion'
import { Box, BoxProps } from '@chakra-ui/react'
import React from 'react'
// Define the props for MotionBox by omitting Chakra's transition
type MotionBoxProps = Omit<BoxProps, 'transition'> & MotionProps
const MotionBox = motion<BoxProps>(Box) as React.FC<MotionBoxProps>
export default MotionBox
Explanation:
- Omitting
transition from BoxProps: This prevents Chakra UI's transition prop from clashing with Framer Motion's transition prop.
- Combining Props: The
MotionBoxProps type combines Chakra's BoxProps (excluding transition) with Framer Motion's MotionProps.
- Type Assertion: Using
as React.FC<MotionBoxProps> ensures that TypeScript understands the combined prop types.
-
Update Your Page Component:
With the custom MotionBox in place, you can now use it in your page without TypeScript errors.
// pages/index.tsx
import { Box, Image } from '@chakra-ui/react'
import type { NextPage } from 'next'
import { HeadComponent } from '../components/HeadComponent'
import { HomeMenu } from '../components/Home/HomeMenu'
import MotionBox from '../components/MotionBox'
const Home: NextPage = () => {
return (
<Box
minH="100vh"
alignItems="center"
justifyContent="center"
display="flex"
flexDir="column"
>
<HeadComponent
title="GuessGeoWorld - Home"
description="GuessGeoWorld Home page"
/>
<HomeMenu />
<MotionBox
animate={{ y: 20 }}
transition={{ repeat: Infinity, duration: 2, repeatType: 'reverse' }}
width={['75%', '70%', '50%', '30%']}
margin="0 auto"
>
<Image src="/images/front-bg.png" />
</MotionBox>
</Box>
)
}
export default Home
-
Alternative Approach Using forwardRef:
If you prefer a more refined approach, especially for larger projects, you can use forwardRef to create a MotionBox that properly forwards refs and merges props without conflicts.
// components/MotionBox.tsx
import { forwardRef } from '@chakra-ui/react'
import { motion, MotionProps } from 'framer-motion'
import { Box, BoxProps } from '@chakra-ui/react'
import React from 'react'
type MotionBoxProps = Omit<BoxProps, 'transition'> & MotionProps
const MotionBox = forwardRef<MotionBoxProps, 'div'>((props, ref) => {
return <motion.div ref={ref} {...props} />
})
export default MotionBox
Explanation:
- Using
forwardRef: This ensures that refs are correctly forwarded to the underlying DOM element.
- Custom Element (
motion.div): Instead of wrapping Chakra's Box, you directly use Framer Motion's motion.div, providing greater control over prop merging.
Summary
The TypeScript error arises because Chakra UI and Framer Motion both define a transition prop with different types. By creating a custom MotionBox component that omits Chakra's transition and correctly types Framer Motion's transition, you can seamlessly integrate both libraries without type conflicts.
Final Code Example
components/MotionBox.tsx:
import { motion, MotionProps } from 'framer-motion'
import { Box, BoxProps } from '@chakra-ui/react'
import React from 'react'
// Define the props for MotionBox by omitting Chakra's transition
type MotionBoxProps = Omit<BoxProps, 'transition'> & MotionProps
const MotionBox = motion<BoxProps>(Box) as React.FC<MotionBoxProps>
export default MotionBox
pages/index.tsx:
import { Box, Image } from '@chakra-ui/react'
import type { NextPage } from 'next'
import { HeadComponent } from '../components/HeadComponent'
import { HomeMenu } from '../components/Home/HomeMenu'
import MotionBox from '../components/MotionBox'
const Home: NextPage = () => {
return (
<Box
minH="100vh"
alignItems="center"
justifyContent="center"
display="flex"
flexDir="column"
>
<HeadComponent
title="GuessGeoWorld - Home"
description="GuessGeoWorld Home page"
/>
<HomeMenu />
<MotionBox
animate={{ y: 20 }}
transition={{ repeat: Infinity, duration: 2, repeatType: 'reverse' }}
width={['75%', '70%', '50%', '30%']}
margin="0 auto"
>
<Image src="/images/front-bg.png" />
</MotionBox>
</Box>
)
}
export default Home
With these adjustments, your MotionBox should work seamlessly within your Chakra UI and Framer Motion setup, eliminating the TypeScript error.