Skip to content

Commit

Permalink
Merge pull request #2083 from framer/fix/svg-scale-attr
Browse files Browse the repository at this point in the history
Adding support for `attrScale`
  • Loading branch information
mergetron[bot] authored Apr 14, 2023
2 parents c447773 + b2cd14f commit a60b9e9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe("SVG useProps", () => {
{
attrX: 1,
attrY: motionValue(5),
attrScale: 3,
cx: 2,
style: {
x: 3,
Expand All @@ -18,6 +19,7 @@ describe("SVG useProps", () => {
{
attrX: 6,
attrY: 10,
attrScale: 4,
cx: 7,
x: 8,
scale: 9,
Expand All @@ -30,6 +32,7 @@ describe("SVG useProps", () => {
expect(result.current).toStrictEqual({
x: 6,
y: 10,
scale: 4,
cx: 7,
style: {},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ describe("SVG scrapeMotionValuesFromProps", () => {
const x = motionValue(0)
const attrX = motionValue(0)
const attrY = motionValue(0)
const scale = motionValue(0)
const attrScale = motionValue(0)

expect(
scrapeMotionValuesFromProps(
{
x: attrX,
attrY,
scale: attrScale,
prev: 0,
style: { x },
style: { x, scale },
} as any,
{
prev: motionValue(1),
Expand All @@ -22,7 +25,9 @@ describe("SVG scrapeMotionValuesFromProps", () => {
).toEqual({
attrX,
attrY,
attrScale,
x,
scale,
prev: 0,
})
})
Expand Down
4 changes: 3 additions & 1 deletion packages/framer-motion/src/render/svg/utils/build-attrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function buildSVGAttrs(
{
attrX,
attrY,
attrScale,
originX,
originY,
pathLength,
Expand Down Expand Up @@ -63,9 +64,10 @@ export function buildSVGAttrs(
)
}

// Treat x/y not as shortcuts but as actual attributes
// Render attrX/attrY/attrScale as attributes
if (attrX !== undefined) attrs.x = attrX
if (attrY !== undefined) attrs.y = attrY
if (attrScale !== undefined) attrs.scale = attrScale

// Build SVG path if one has been defined
if (pathLength !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MotionProps } from "../../../motion/types"
import { isMotionValue } from "../../../value/utils/is-motion-value"
import { scrapeMotionValuesFromProps as scrapeHTMLMotionValuesFromProps } from "../../html/utils/scrape-motion-values"
import { transformPropOrder } from "../../html/utils/transform"

export function scrapeMotionValuesFromProps(
props: MotionProps,
Expand All @@ -11,7 +12,10 @@ export function scrapeMotionValuesFromProps(
for (const key in props) {
if (isMotionValue(props[key]) || isMotionValue(prevProps[key])) {
const targetKey =
key === "x" || key === "y" ? "attr" + key.toUpperCase() : key
transformPropOrder.indexOf(key) !== -1
? "attr" + key.charAt(0).toUpperCase() + key.substring(1)
: key

newValues[targetKey] = props[key]
}
}
Expand Down

0 comments on commit a60b9e9

Please sign in to comment.