1
- import type { DOMKeyframesDefinition } from 'framer-motion'
1
+ import type { DOMKeyframesDefinition , ResolvedValues } from 'framer-motion'
2
2
import { isCssVar , isNumber } from './utils'
3
3
import { buildTransformTemplate , isTransform , transformAlias , transformDefinitions } from './transform'
4
4
import { isMotionValue } from '@/utils'
5
5
import type { MotionStyle } from '@/types'
6
+ import { px } from '@/value/types/numbers/units'
6
7
7
8
type MotionStyleKey = Exclude <
8
9
keyof CSSStyleDeclaration ,
@@ -83,15 +84,12 @@ const SVG_STYLE_TO_ATTRIBUTES = {
83
84
cy : true ,
84
85
r : true ,
85
86
d : true ,
86
- x : true ,
87
- y : true ,
88
87
x1 : true ,
89
88
y1 : true ,
90
89
x2 : true ,
91
90
y2 : true ,
92
91
points : true ,
93
92
pathLength : true ,
94
- transform : true ,
95
93
viewBox : true ,
96
94
width : true ,
97
95
height : true ,
@@ -116,22 +114,36 @@ const SVG_STYLE_TO_ATTRIBUTES = {
116
114
vectorEffect : true ,
117
115
} as const
118
116
117
+ function buildSVGPath (
118
+ attrs : ResolvedValues ,
119
+ length : number ,
120
+ spacing = 1 ,
121
+ offset = 0 ,
122
+ ) {
123
+ attrs . pathLength = 1
124
+ // Build the dash offset
125
+ attrs [ 'stroke-dashoffset' ] = px . transform ( - offset )
126
+
127
+ // Build the dash array
128
+ const pathLength = px . transform ! ( length )
129
+ const pathSpacing = px . transform ! ( spacing )
130
+ attrs [ 'stroke-dasharray' ] = `${ pathLength } ${ pathSpacing } `
131
+ }
119
132
export function convertSvgStyleToAttributes ( keyframes ?: MotionStyle | DOMKeyframesDefinition ) {
120
133
const attributes : Record < string , any > = { }
121
134
const styleProps : Record < string , any > = { }
122
-
123
135
for ( const key in keyframes as any ) {
124
136
if ( key in SVG_STYLE_TO_ATTRIBUTES ) {
125
- const attrKey = SVG_STYLE_TO_ATTRIBUTES [ key as keyof typeof SVG_STYLE_TO_ATTRIBUTES ]
126
- const attrName = typeof attrKey === 'string' ? attrKey : key
127
137
const value = keyframes [ key ]
128
- attributes [ attrName ] = isMotionValue ( value ) ? value . get ( ) : value
138
+ attributes [ key ] = isMotionValue ( value ) ? value . get ( ) : value
129
139
}
130
140
else {
131
141
styleProps [ key ] = keyframes [ key ]
132
142
}
133
143
}
134
-
144
+ if ( attributes . pathLength ) {
145
+ buildSVGPath ( attributes , attributes . pathLength , attributes . pathSpacing , attributes . pathOffset )
146
+ }
135
147
return {
136
148
attributes,
137
149
style : styleProps ,
0 commit comments