Skip to content

Commit 565f4f3

Browse files
Merge pull request #2691 from framer/fix/mix-immediate-invalid-color
Updating interpolation for invalid colors
2 parents a58d3e7 + d3248d2 commit 565f4f3

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

packages/framer-motion/src/utils/mix/__tests__/mix-color.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,9 @@ test("mixColor rgba with slash (without slash spaces) to rgba without", () => {
128128
test("doesn't return NaN", () => {
129129
expect(mixLinearColor(255, 0, 2)).not.toBeNaN()
130130
})
131+
132+
test("mixColor mixes immediately with unknown color", () => {
133+
expect(mixColor("red", "rgba(0, 0, 0, 0)")(0)).toBe("red")
134+
expect(mixColor("red", "rgba(0, 0, 0, 0)")(0.5)).toBe("rgba(0, 0, 0, 0)")
135+
expect(mixColor("red", "rgba(0, 0, 0, 0)")(1)).toBe("rgba(0, 0, 0, 0)")
136+
})

packages/framer-motion/src/utils/mix/color.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { mixNumber } from "./number"
2-
import { invariant } from "../errors"
2+
import { warning } from "../errors"
33
import { hslaToRgba } from "../hsla-to-rgba"
44
import { hex } from "../../value/types/color/hex"
55
import { rgba } from "../../value/types/color/rgba"
66
import { hsla } from "../../value/types/color/hsla"
77
import { Color, HSLA, RGBA } from "../../value/types/types"
8+
import { mixImmediate } from "./immediate"
89

910
// Linear color space blending
1011
// Explained https://www.youtube.com/watch?v=LKnqECcg6Gw
@@ -22,11 +23,13 @@ const getColorType = (v: Color | string) =>
2223
function asRGBA(color: Color | string) {
2324
const type = getColorType(color)
2425

25-
invariant(
26+
warning(
2627
Boolean(type),
2728
`'${color}' is not an animatable color. Use the equivalent color code instead.`
2829
)
2930

31+
if (!Boolean(type)) return false
32+
3033
let model = type!.parse(color)
3134

3235
if (type === hsla) {
@@ -41,6 +44,10 @@ export const mixColor = (from: Color | string, to: Color | string) => {
4144
const fromRGBA = asRGBA(from)
4245
const toRGBA = asRGBA(to)
4346

47+
if (!fromRGBA || !toRGBA) {
48+
return mixImmediate(from, to)
49+
}
50+
4451
const blended = { ...fromRGBA }
4552

4653
return (v: number) => {

packages/framer-motion/src/utils/mix/complex.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@ import {
1212
} from "../../value/types/complex"
1313
import { isCSSVariableToken } from "../../render/dom/utils/is-css-variable"
1414
import { invisibleValues, mixVisibility } from "./visibility"
15+
import { mixImmediate } from "./immediate"
1516

1617
type MixableArray = Array<number | RGBA | HSLA | string>
1718
type MixableObject = {
1819
[key: string]: string | number | RGBA | HSLA
1920
}
2021

21-
function mixImmediate<T>(a: T, b: T) {
22-
return (p: number) => (p > 0 ? b : a)
23-
}
24-
2522
function mixNumber(a: number, b: number) {
2623
return (p: number) => mixNumberImmediate(a, b, p)
2724
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function mixImmediate<T>(a: T, b: T) {
2+
return (p: number) => (p > 0 ? b : a)
3+
}

0 commit comments

Comments
 (0)