Skip to content

Commit 4b19e59

Browse files
committed
fix createRef
1 parent c9a1bd6 commit 4b19e59

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed

examples/demos/dashoffset/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react'
2+
import { Spring, animated } from 'react-spring'
3+
import { TimingAnimation } from '../../../src/addons'
4+
import { GradientPinkRed as Gradient } from '@vx/gradient'
5+
6+
export default class App extends React.Component {
7+
state = { offset: 0 }
8+
path = React.createRef()
9+
componentDidMount() {
10+
this.setState({ offset: this.path.current.getTotalLength() })
11+
}
12+
render() {
13+
const { offset } = this.state
14+
return (
15+
<svg width="500" viewBox="0 0 23 23">
16+
<Gradient id="gradient" />
17+
<g fill="transparent" stroke="url(#gradient)" strokeWidth="0.2">
18+
<Spring
19+
native
20+
reset
21+
from={{ dash: offset }}
22+
to={{ dash: 0 }}
23+
impl={TimingAnimation}
24+
config={{ duration: 5000 }}>
25+
{props => (
26+
<animated.path
27+
ref={this.path}
28+
strokeDasharray={offset}
29+
strokeDashoffset={props.dash}
30+
d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z"
31+
/>
32+
)}
33+
</Spring>
34+
</g>
35+
</svg>
36+
)
37+
}
38+
}

examples/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const components = [
2424
'script',
2525
'auto',
2626
'router',
27-
'scroll',
27+
//'scroll',
28+
//'dashoffset'
2829
].reduce(
2930
(acc, path) => ({
3031
...acc,
@@ -37,7 +38,7 @@ const components = [
3738
)
3839

3940
const DEBUG = false
40-
const DebugComponent = components['timing']
41+
const DebugComponent = components['dashoffset']
4142

4243
ReactDOM.render(
4344
DEBUG ? (

src/animated/createAnimatedComponent.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,25 @@ export default function createAnimatedComponent(Component) {
6060
this.attachProps(nextProps)
6161
}
6262

63+
setRef = node => {
64+
this.node = node
65+
const forwardRef = this.props.forwardRef
66+
if (forwardRef) {
67+
// If it's a function, assume it's a ref callback
68+
if (typeof forwardRef === 'function') forwardRef(node)
69+
// If it's an object and has a 'current' property, assume it's a ref object
70+
else if (typeof forwardRef === 'object') forwardRef.current = node
71+
}
72+
}
73+
6374
render() {
6475
const forwardRef = this.props.forwardRef
6576
const {
6677
scrollTop,
6778
scrollLeft,
6879
...animatedProps
6980
} = this._propsAnimated.__getValue()
70-
return (
71-
<Component
72-
{...animatedProps}
73-
ref={node => {
74-
this.node = node
75-
if (forwardRef) forwardRef(node)
76-
}}
77-
/>
78-
)
81+
return <Component {...animatedProps} ref={this.setRef} />
7982
}
8083
}
8184
return React.forwardRef((props, ref) => (

src/targets/web/globals.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ Globals.injectApplyAnimatedValues(
101101

102102
// Set attributes ...
103103
for (let name in attributes) {
104-
if (instance.getAttribute(name))
105-
instance.setAttribute(name, attributes[name])
104+
let dashCase = name.replace(/([A-Z])/g, $1 => '-' + $1.toLowerCase())
105+
if (typeof instance.getAttribute(dashCase) !== 'undefined')
106+
instance.setAttribute(dashCase, attributes[name])
106107
}
107108
} else return false
108109
},

0 commit comments

Comments
 (0)