[BUG] SVG animation fails when no initial value set
See original GitHub issueDescribe the bug When I don’t define default/initial values as attributes, the component fails with the error:
Uncaught TypeError: Cannot read property 'red' of null
To Reproduce As an example, this component/animation fails. This should be a looping spinner.
const animation = {
strokeDasharray: ['1px, 200px', '100px, 200px', '100px, 200px'],
strokeDashoffset: [0, -15, -125],
transition: { duration: 1.4, ease: 'linear' },
};
const Spinner: FC = (): ReactElement => {
const controls = useAnimation();
const handleAnimationComplete = useCallback(() => {
controls.start(animation);
}, [controls]);
useEffect(() => {
controls.start(animation);
}, [controls]);
return (
<svg viewBox='22 22 44 44'>
<Circle
animate={controls}
onAnimationComplete={handleAnimationComplete}
cx='44'
cy='44'
r='20.2'
fill='none'
strokeWidth='3.6'
/>
</svg>
);
};
Expected behavior Modifing the circle to the below works, but this is the only way to resolve the issue. Setting the styles in CSS (with styled-components as an example) does not help.
<Circle
animate={controls}
onAnimationComplete={handleAnimationComplete}
strokeDasharray='80px, 200px'
strokeDashoffset='0'
cx='44'
cy='44'
r='20.2'
fill='none'
strokeWidth='3.6'
/>
Desktop (please complete the following information):
- OS: MacOS 10.14.5 (18F132)
- Browser Version: Chrome 75.0.3770.100 (Official Build) (64-bit)
Additional context
I had the same issue when trying to add a rotate
animation to an SVG element. I could not find any workaround for this one, so any help would be greatly appreciated.
const Spinner: FC= (): ReactElement => {
return (
<motion.svg viewBox='22 22 44 44' animate={{ rotate: 100 }}>
<Circle />
</motion.svg>
);
};
Other than this, I’m loving Framer Motion - amazing work!
Issue Analytics
- State:
- Created 4 years ago
- Comments:14 (1 by maintainers)
Top Results From Across the Web
begin - SVG: Scalable Vector Graphics - MDN Web Docs
The element's animation start time is defined relative to the begin or active end of another animation. A valid syncbase-value consists of an...
Read more >SVG Animation throws a 'Cannot read property 'style' of null ...
My issue is the building of an SVG arch that expands upon ... the application throws an error: Cannot read property 'style' of...
Read more >A Guide to SVG Animations (SMIL) - CSS-Tricks
The animation elements were initially defined in the SMIL ... If the value for attributeType is not explicitly set or is set to...
Read more >Animation – SVG 1.1 (Second Edition) - W3C
SMIL Animation defines error processing behavior where the document continues to ... The default value is 'auto'. ... There is no default value...
Read more >Most Common GSAP Mistakes - Learning Center - GreenSock
Using fromTo() when from() or to() would work; Not setting ALL transforms with GSAP; Not using xPercent and yPercent; Recreating animations over ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Currently working on this bug but if anyone runs into similar, you can always explicitly set an initial value via the
initial
prop.Thanks for this!