The error you're encountering stems from how styled-components handles keyframe interpolations in version 4 and above. In earlier versions (like v3), it was possible to interpolate keyframes directly within untagged strings. However, in v4+, styled-components requires you to use the css helper when interpolating keyframes to ensure they're correctly injected and managed.
Here's a step-by-step guide to resolving the issue:
1. Import the css Helper
First, ensure that you import the css helper from styled-components. This helper allows you to construct styles that can include interpolations like keyframes.
import styled, { keyframes, css } from 'styled-components';
2. Update the StyledSkillsList Component
In your StyledSkillsList styled component, you're conditionally applying animations based on the prefersReducedMotion and direction props. To adhere to styled-components v4+ requirements, wrap the interpolated styles using the css helper.
Before Fix:
const StyledSkillsList = styled.ul`
/* ... other styles ... */
${({ prefersReducedMotion, direction }) =>
!prefersReducedMotion &&
`
animation: ${
direction === 'left-to-right' ? scrollLeftToRight : scrollRightToLeft
} 10s linear infinite;
`}
/* ... other styles ... */
`;
After Fix:
const StyledSkillsList = styled.ul`
display: flex;
white-space: nowrap;
overflow: hidden;
list-style: none;
padding: 0;
margin: 0;
/* Apply animation if not reduced motion */
${({ prefersReducedMotion, direction }) =>
!prefersReducedMotion &&
css`
animation: ${
direction === 'left-to-right' ? scrollLeftToRight : scrollRightToLeft
} 10s linear infinite;
`}
li {
display: inline-block;
margin-right: 2rem;
font-family: var(--font-mono);
font-size: var(--fz-xs);
position: relative;
&:before {
content: '▹';
position: absolute;
left: -1.5rem;
color: var(--green);
font-size: var(--fz-sm);
line-height: 12px;
}
}
`;
3. Complete Updated Code
Here's the complete revised version of your component with the necessary fixes:
import React, { useEffect, useRef } from 'react';
import { StaticImage } from 'gatsby-plugin-image';
import styled, { keyframes, css } from 'styled-components'; // Import `css`
import { srConfig } from '@config';
import sr from '@utils/sr';
import { usePrefersReducedMotion } from '@hooks';
// Define keyframe animations for scrolling left to right and right to left
const scrollLeftToRight = keyframes`
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(100%);
}
`;
const scrollRightToLeft = keyframes`
0% {
transform: translateX(100%);
}
100% {
transform: translateX(-100%);
}
`;
const StyledAboutSection = styled.section`
max-width: 900px;
.inner {
display: grid;
grid-template-columns: 3fr 2fr;
grid-gap: 50px;
@media (max-width: 768px) {
display: block;
}
}
`;
const StyledText = styled.div`
padding: 1rem;
div {
margin-bottom: 2rem;
}
h5 {
margin-bottom: 0.5rem;
font-size: 1.2rem;
color: var(--green);
}
`;
// Updated styled component for the scrolling skills list using `css`
const StyledSkillsList = styled.ul`
display: flex;
white-space: nowrap;
overflow: hidden;
list-style: none;
padding: 0;
margin: 0;
/* Apply animation if not reduced motion */
${({ prefersReducedMotion, direction }) =>
!prefersReducedMotion &&
css` /* Use `css` helper here */
animation: ${
direction === 'left-to-right' ? scrollLeftToRight : scrollRightToLeft
} 10s linear infinite;
`}
li {
display: inline-block;
margin-right: 2rem;
font-family: var(--font-mono);
font-size: var(--fz-xs);
position: relative;
&:before {
content: '▹';
position: absolute;
left: -1.5rem;
color: var(--green);
font-size: var(--fz-sm);
line-height: 12px;
}
}
`;
const StyledPic = styled.div`
position: relative;
max-width: 300px;
@media (max-width: 768px) {
margin: 50px auto 0;
width: 70%;
}
.wrapper {
${({ theme }) => theme.mixins.boxShadow};
display: block;
position: relative;
width: 100%;
border-radius: var(--border-radius);
background-color: var(--green);
&:hover,
&:focus {
outline: 0;
transform: translate(-4px, -4px);
&:after {
transform: translate(8px, 8px);
}
.img {
filter: none;
mix-blend-mode: normal;
}
}
.img {
position: relative;
border-radius: var(--border-radius);
mix-blend-mode: multiply;
filter: grayscale(100%) contrast(1);
transition: var(--transition);
}
&:before,
&:after {
content: '';
display: block;
position: absolute;
width: 100%;
height: 100%;
border-radius: var(--border-radius);
transition: var(--transition);
}
&:before {
top: 0;
left: 0;
background-color: var(--navy);
mix-blend-mode: screen;
}
&:after {
border: 2px solid var(--green);
top: 14px;
left: 14px;
z-index: -1;
}
}
`;
const About = () => {
const revealContainer = useRef(null);
const prefersReducedMotion = usePrefersReducedMotion();
useEffect(() => {
if (prefersReducedMotion) {
return;
}
sr.reveal(revealContainer.current, srConfig());
}, [prefersReducedMotion]);
const skills = {
Languages: ['C/C++', 'Java', 'Python', 'JavaScript', 'TypeScript', 'Solidity', 'Rust', 'Move'],
Databases: ['SQLite', 'PostgreSQL', 'MongoDB', 'Firebase'],
DevOps: ['Docker', 'Jenkins'],
Others: ['Computer Networks', 'Operating Systems', 'System Design', 'Microservices', 'Cloud Computing'],
Frameworks: ['Node.js', 'React.js', 'Express.js', 'Django', 'Flask'],
Tools: ['Git', 'Linux', 'Docker', 'GCP', 'AWS', 'Cisco Packet Tracer']
};
return (
<StyledAboutSection id="about" ref={revealContainer}>
<h2 className="numbered-heading">About Me</h2>
<div className="inner">
<StyledText>
<div>
<p>
I started my journey by participating in hackathons, events, and buildathons. I have also undertaken freelancing and internships as a Full Stack Developer.
</p>
<p>Here are a few technologies I’ve worked on:</p>
<br />
</div>
{Object.keys(skills).map((category, index) => (
<div key={index}>
<h5>{category}</h5>
<StyledSkillsList
prefersReducedMotion={prefersReducedMotion}
direction={index % 2 === 0 ? 'left-to-right' : 'right-to-left'}
>
{skills[category].map((skill, i) => (
<li key={i}>{skill}</li>
))}
</StyledSkillsList>
</div>
))}
</StyledText>
{/* If you have an image or additional content, include it here */}
{/* <StyledPic>
<div className="wrapper">
<StaticImage
className="img"
src="../../images/profile-pic.png"
alt="Profile Picture"
quality={95}
formats={['AUTO', 'WEBP', 'AVIF']}
/>
</div>
</StyledPic> */}
</div>
</StyledAboutSection>
);
};
export default About;
3. Explanation of Changes
-
Importing css: By importing the css helper from styled-components, you can construct interpolations that handle more complex styling scenarios, such as injecting keyframes.
import styled, { keyframes, css } from 'styled-components';
-
Using css in Interpolations: Within the StyledSkillsList component, wrap the conditional styles using the css helper. This ensures that the keyframe animations are properly processed and injected by styled-components.
${({ prefersReducedMotion, direction }) =>
!prefersReducedMotion &&
css` /* Using `css` helper */
animation: ${
direction === 'left-to-right' ? scrollLeftToRight : scrollRightToLeft
} 10s linear infinite;
`}
4. Additional Recommendations
-
Dependencies: Ensure that your project is using styled-components version 4 or higher, as the changes pertain to behavior introduced in these later versions.
You can check your package.json:
{
// ... other dependencies
"styled-components": "^5.0.0",
// ... other dependencies
}
If it's below v4, consider updating it:
npm install styled-components@latest
# or
yarn add styled-components@latest
-
Review Other Styles: While the error specifically points to the interpolation of keyframes, it's a good practice to review other styled components in your project to ensure they’re compliant with styled-components v4+ syntax and best practices.
5. Conclusion
By wrapping your interpolated keyframe animations with the css helper, you adhere to the updated styled-components requirements, ensuring that animations are correctly processed and injected. This change should resolve the runtime error you’re experiencing.
If you encounter any further issues or have additional questions, feel free to ask!