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 2 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
74 changes: 74 additions & 0 deletions src/projection/animation/__tests__/mix-values.test.ts
@@ -0,0 +1,74 @@
import { mixValues } from "../mix-values"

describe("mixValues", () => {
test("mixes borderRadius numbers", () => {
const output = {}

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

expect(output).toEqual({ borderTopLeftRadius: 15 })
})

test("mixes borderRadius px", () => {
const output = {}

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

expect(output).toEqual({ borderTopLeftRadius: 15 })
})

test("mixes borderRadius percentage", () => {
const output = {}

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

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

test("mixes borderRadius percentage with 0", () => {
const output = {}

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

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

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

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

const borders = ["TopLeft", "TopRight", "BottomLeft", "BottomRight"]
const numBorders = borders.length

const asNumber = (value: string | number) =>
typeof value === "string" ? parseFloat(value) : value

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

/**
* Currently we're only crossfading between numerical border radius.
* It would be possible to crossfade between percentages for a little
* extra bundle size.
*/
if (
typeof followRadius === "number" &&
typeof leadRadius === "number"
) {
const radius = Math.max(mix(followRadius, leadRadius, progress), 0)
target[borderLabel] = radius
target[borderLabel] = Math.max(
mix(asNumber(followRadius), asNumber(leadRadius), progress),
0
)

if (percent.test(leadRadius) || percent.test(followRadius)) {
target[borderLabel] += "%"
nvh marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
1 change: 1 addition & 0 deletions src/projection/styles/scale-border-radius.ts
Expand Up @@ -17,6 +17,7 @@ export function pixelsToPercent(pixels: number, axis: Axis): number {
export const correctBorderRadius: ScaleCorrectorDefinition = {
correct: (latest, node) => {
if (!node.target) return latest

Copy link
Contributor

Choose a reason for hiding this comment

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

This seems unnecessary

/**
* If latest is a string, if it's a percentage we can return immediately as it's
* going to be stretched appropriately. Otherwise, if it's a pixel, convert it to a number.
Expand Down