Skip to content

Commit

Permalink
Merge pull request #2703 from framer/fix/color-string
Browse files Browse the repository at this point in the history
fix(isColorString): cannot convert undefined or null to object
  • Loading branch information
mergetron[bot] authored Jul 4, 2024
2 parents ed6a830 + 6f597e8 commit 977f2f0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ describe("hex()", () => {
expect(hex.test("#F000AA00")).toEqual(true)
expect(hex.test("#f00 0px")).toEqual(false)
expect(hex.test(red)).toEqual(false)
expect(hex.test(null)).toEqual(false)
expect(hex.test(undefined)).toEqual(false)
})

it("should split a hex value into the correct params", () => {
Expand All @@ -210,6 +212,8 @@ describe("rgba()", () => {
expect(rgba.test("rgba(255, 0, 0, 0.5) 0px")).toEqual(false)
expect(rgba.test({ red: 255 })).toEqual(true)
expect(rgba.test({ hue: 255 })).toEqual(false)
expect(rgba.test(null)).toEqual(false)
expect(rgba.test(undefined)).toEqual(false)
})

it("should split an rgba value into the correct params", () => {
Expand Down Expand Up @@ -248,6 +252,8 @@ describe("hsla()", () => {
expect(hsla.test("hsla(170 50% 45%/1)")).toEqual(true)
expect(hsla.test("hsla(177 37.4978% 76.66804% / 1)")).toEqual(true)
expect(hsla.test("hsla(170, 50%, 45%, 1) 0px")).toEqual(false)
expect(hsla.test(null)).toEqual(false)
expect(hsla.test(undefined)).toEqual(false)
})

it("should split an hsl value into the correct params", () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/framer-motion/src/value/types/color/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Color, HSLA, RGBA } from "../types"
import { floatRegex, isString, singleColorRegex } from "../utils"
import { floatRegex, isNullish, isString, singleColorRegex } from "../utils"

/**
* Returns true if the provided string is a color, ie rgba(0,0,0,0) or #000,
Expand All @@ -8,7 +8,9 @@ import { floatRegex, isString, singleColorRegex } from "../utils"
export const isColorString = (type: string, testProp?: string) => (v: any) => {
return Boolean(
(isString(v) && singleColorRegex.test(v) && v.startsWith(type)) ||
(testProp && Object.prototype.hasOwnProperty.call(v, testProp))
(testProp &&
!isNullish(v) &&
Object.prototype.hasOwnProperty.call(v, testProp))
)
}

Expand Down
4 changes: 4 additions & 0 deletions packages/framer-motion/src/value/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ export const singleColorRegex =
export function isString(v: any): v is string {
return typeof v === "string"
}

export function isNullish(v: any): v is null | undefined {
return v == null
}

0 comments on commit 977f2f0

Please sign in to comment.