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

Crossfade border-radius of "%" and "px" units #1416

Merged
merged 3 commits into from Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 26 additions & 0 deletions src/projection/animation/__tests__/mix-values.test.ts
Expand Up @@ -71,4 +71,30 @@ describe("mixValues", () => {

expect(output).toEqual({ borderTopLeftRadius: "10%" })
})

test("doesn't mix % with px", () => {
const output = {}

mixValues(
output,
{ borderTopLeftRadius: "10px" },
{ borderTopLeftRadius: "20%" },
0.5,
false,
false
)

expect(output).toEqual({ borderTopLeftRadius: "20%" })

mixValues(
output,
{ borderTopLeftRadius: "20%" },
{ borderTopLeftRadius: "10px" },
0.5,
false,
false
)

expect(output).toEqual({ borderTopLeftRadius: "10px" })
})
})
26 changes: 19 additions & 7 deletions src/projection/animation/mix-values.ts
@@ -1,5 +1,5 @@
import { circOut, linear, mix, progress as calcProgress } from "popmotion"
import { percent } from "style-value-types"
import { percent, px } from "style-value-types"
import { ResolvedValues } from "../../render/types"
import { EasingFunction } from "../../types"

Expand All @@ -9,6 +9,9 @@ const numBorders = borders.length
const asNumber = (value: string | number) =>
typeof value === "string" ? parseFloat(value) : value

const isPx = (value: string | number) =>
typeof value === "number" || px.test(value)

export function mixValues(
target: ResolvedValues,
follow: ResolvedValues,
Expand Down Expand Up @@ -51,13 +54,22 @@ export function mixValues(
followRadius ||= 0
leadRadius ||= 0

target[borderLabel] = Math.max(
mix(asNumber(followRadius), asNumber(leadRadius), progress),
0
)
const canMix =
followRadius === 0 ||
leadRadius === 0 ||
isPx(followRadius) === isPx(leadRadius)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused why we're not adding percent.test(leadRadius) === percent.test(followRadius) to this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the test case seems to be still passing...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is essentially testing if they're different

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if they're both false it's all good too

Copy link
Contributor

@nvh nvh Jan 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right! So in the purely hypothetical case that there would be a third string-based format, we would be fucked.
Let's add a comment about that then, because I think it's pretty confusing.


if (canMix) {
target[borderLabel] = Math.max(
mix(asNumber(followRadius), asNumber(leadRadius), progress),
0
)

if (percent.test(leadRadius) || percent.test(followRadius)) {
target[borderLabel] += "%"
if (percent.test(leadRadius) || percent.test(followRadius)) {
target[borderLabel] += "%"
}
} else {
target[borderLabel] = leadRadius
}
}

Expand Down