Fix: Failed to execute 'animate' on 'Element': Non numeric offset provided #1991
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello 👋 I couldn't find the related issue, and while it's an on-line fix, I have decided to open PR directly 🙂
During development, I encountered an error being thrown from
framer-motion
libraryI have to dig a little deeper into the library source code to trace the bug and found it originaes from the native Browser
Element.animate
call on this line: https://github.com/framer/motion/blob/f06854066a1de33561578ce2c4fd785e78fdf3be/packages/framer-motion/src/animation/waapi/index.ts#L17Direct reproduction was hard at first as after analyzing what type of arguments I get to this function from my app, no combination was causing the error to happen, but customers were facing it sometimes. Fortunately, the report came from Sentry, meaning I had OS and browser versions available.
After spawning Android 8 and Chrome Web View 60 on the simulator I tried to re-run this minimal reproduction step of calling
element.animate
directly, providing some example sets of values from my app.And then, I caused exactly that error, by providing
offset: undefined
explicitly as a value.The same values does not cause an error on currently fresh browsers versions
Looking quickly at the WAAPI docs, I have found:
https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Keyframe_Formats
https://www.w3.org/TR/web-animations-1/#keyframes-section
This means according to spec, the value should never be explicitly set to
undefined
, which is currently happening if you don't provide value for argumenttimes
ofanimateStyle
, which is being directly passed as value foroffset
key in keyframes definition: https://github.com/framer/motion/blob/f06854066a1de33561578ce2c4fd785e78fdf3be/packages/framer-motion/src/animation/waapi/index.ts#L18It looks like all modern browsers filter out such value treating it the same way as it would never be provided, but older browsers might still interpret the spec strictly and don't provide any additional handling throwing error to throw.
Because of some issues with
lib.dom.ts
definitions, passingnull
instead of defaultundefined
was a little problematic, I am proposing to provideoffset
conditionally only iftimes
is truthy meaning fromnumber[] | undefined
union we are left with justnumber[]
.Here is the gif showcasing the error happening in Android simulator 👇