Skip to content

Commit

Permalink
Fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgperry committed Feb 13, 2024
1 parent 7a97bd1 commit 789e502
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 15 deletions.
9 changes: 5 additions & 4 deletions dev/benchmarks/mix-unit-value-framer-motion.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@
<script src="../../packages/framer-motion/dist/dom.js"></script>
<script src="../../node_modules/gsap/dist/gsap.min.js"></script>
<script>
const { interpolate } = window.Motion
const { mix } = window.Motion

/**
* Create an interpolate function that mixes unit values.
*/
const px = interpolate([0, 1], ["0px", "100px"])
const px = mix("var(--test-1) 10px", "var(--test-9) 60px")

const numRuns = 1000000
const numRuns = 10
let startTime = performance.now()
for (let i = 0; i < numRuns; i++) {
px(i / numRuns)
console.log(i / numRuns)
console.log(px(i / numRuns))
}
console.log(`First run: ${performance.now() - startTime}ms`)
</script>
Expand Down
9 changes: 6 additions & 3 deletions dev/benchmarks/mix-unit-value-greensock.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@
/**
* Create an interpolate function that mixes unit values.
*/
const px = gsap.utils.interpolate("0px", "100px")
const px = gsap.utils.interpolate(
"var(--test-1, 1) 10px",
"var(--test-9, 3) 60px"
)

const numRuns = 1000000
const numRuns = 10
let startTime = performance.now()
for (let i = 0; i < numRuns; i++) {
px(i / numRuns)
console.log(px(i / numRuns))
}
console.log(`First run: ${performance.now() - startTime}ms`)
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ const checkStringStartsWith =
typeof key === "string" && key.startsWith(token)

export const isCSSVariableName = checkStringStartsWith<CSSVariableName>("--")
export const isCSSVariableToken =
checkStringStartsWith<CSSVariableToken>("var(--")

const startsAsVariableToken = checkStringStartsWith<CSSVariableToken>("var(--")
export const isCSSVariableToken = (key?: string): key is CSSVariableToken =>
startsAsVariableToken(key) && singleCssVariableRegex.test(key)

export const cssVariableRegex =
/var\s*\(\s*--[\w-]+(\s*,\s*(?:(?:[^)(]|\((?:[^)(]+|\([^)(]*\))*\))*)+)?\s*\)/g
const singleCssVariableRegex =
/var\s*\(\s*--[\w-]+(\s*,\s*(?:(?:[^)(]|\((?:[^)(]+|\([^)(]*\))*\))*)+)?\s*\)$/i
9 changes: 9 additions & 0 deletions packages/framer-motion/src/utils/mix/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ describe("mix", () => {

expect(mixer(0.5)).toEqual([[1, 2], { c: 0.5, d: "1.5px" }])
})

test("mixes complex values", () => {
expect(mix("var(--test) 0px", "var(--test) 20px")(0.5)).toBe(
"var(--test) 10px"
)
expect(mix("var(--test-1) 10px", "var(--test-9) 60px")(0.5)).toBe(
"var(--test-9) 35px"
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ test("mixComplex errors", () => {
)
})

test("mixComplex mixes var() and unit types", () => {
expect(mixComplex("var(--test) 0px", "var(--test) 20px")(0.5)).toBe(
"var(--test) 10px"
)
expect(mixComplex("var(--test-1) 10px", "var(--test-9) 60px")(0.5)).toBe(
"var(--test-9) 35px"
)
})

test("mixComplex can interpolate out-of-order values", () => {
expect(mixComplex("#fff 0px 0px", "20px 0px #000")(0.5)).toBe(
"10px 0px rgba(180, 180, 180, 1)"
Expand Down
12 changes: 6 additions & 6 deletions packages/framer-motion/src/utils/mix/complex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
analyseComplexValue,
complex,
} from "../../value/types/complex"
import { isCSSVariableToken } from "../../render/dom/utils/is-css-variable"

type MixableArray = Array<number | RGBA | HSLA | string>
type MixableObject = {
Expand All @@ -28,12 +29,11 @@ export function getMixer<T>(a: T) {
if (typeof a === "number") {
return mixNumber
} else if (typeof a === "string") {
if (a.startsWith("var(")) {
return mixImmediate
} else if (color.test(a)) {
return mixColor
}
return mixComplex
return isCSSVariableToken(a)
? mixImmediate
: color.test(a)
? mixColor
: mixComplex
} else if (Array.isArray(a)) {
return mixArray
} else if (typeof a === "object") {
Expand Down

0 comments on commit 789e502

Please sign in to comment.