Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix times #1859

Merged
merged 2 commits into from Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -31,8 +31,6 @@ export function keyframes({
}: AnimationOptions): Animation<number | string> {
keyframeValues = [...keyframeValues]

const origin = keyframes[0]

/**
* Easing functions can be externally defined as strings. Here we convert them
* into actual functions.
Expand All @@ -45,15 +43,18 @@ export function keyframes({
* This is the Iterator-spec return value. We ensure it's mutable rather than using a generator
* to reduce GC during animation.
*/
const state: AnimationState<typeof origin> = { done: false, value: origin }
const state: AnimationState<typeof origin> = {
done: false,
value: keyframeValues[0],
}

/**
* Create a times array based on the provided 0-1 offsets
*/
const absoluteTimes = convertOffsetToTimes(
// Only use the provided offsets if they're the correct length
// TODO Maybe we should warn here if there's a length mismatch
times && times.length === keyframes.length
times && times.length === keyframeValues.length
? times
: defaultOffset(keyframeValues),
duration
Expand Down
Expand Up @@ -114,4 +114,27 @@ describe("keyframes transition", () => {

expect(xResult).toBe(50)
})

test("times works as expected", async () => {
const values = await new Promise<number[]>((resolve) => {
const output: number[] = []

const Component = () => (
<motion.div
animate={{ x: [50, 100, 200, 300] }}
transition={{ duration: 0.1, times: [0, 0, 1, 1] }}
onUpdate={(latest) =>
output.push(Math.round(latest.x as number))
}
onAnimationComplete={() => resolve(output)}
/>
)

const { rerender } = render(<Component />)
rerender(<Component />)
})

expect(values[0] >= 100).toBe(true)
expect(values[values.length - 2] <= 200).toBe(true)
})
})