Skip to content

Instantly share code, notes, and snippets.

@jdnichollsc
Last active February 22, 2020 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdnichollsc/080c75b6c6d520e3abaef0370069dcf5 to your computer and use it in GitHub Desktop.
Save jdnichollsc/080c75b6c6d520e3abaef0370069dcf5 to your computer and use it in GitHub Desktop.
Animatable Button using Higher Order Components with React Native and react-native-animatable
import React, { Component } from 'react'
import {
createAnimatableComponent,
initializeRegistryWithDefinitions
} from 'react-native-animatable'
import { Button } from 'native-base'
import { throttle } from 'lodash'
initializeRegistryWithDefinitions({
zoomInOut: {
0: {
scale: 1,
},
0.5: {
scale: 1.8,
},
0.8: {
scale: 1.4,
},
1: {
scale: 1,
}
}
})
function withPressAnimate (WrappedComponent) {
const AnimatableComponent = createAnimatableComponent(WrappedComponent)
return class extends Component {
constructor(props) {
super(props)
this.onPressAnimatedDelayed = throttle(this.onPressAnimated, 3000, { 'trailing': false })
}
onPressAnimated (event, originalEvent) {
this.ref.animate('zoomInOut', 1200)
setTimeout(() => originalEvent(event), 400)
}
render () {
const { onPress, children, ...rest } = this.props
return (
<AnimatableComponent
ref={(ref) => this.ref = ref}
onPress={(e) => this.onPressAnimatedDelayed(e, onPress)}
{...rest}
>{ children }
</AnimatableComponent>
)
}
}
}
export default withPressAnimate(Button)
@jdnichollsc
Copy link
Author

A better version of this code is here 👍 https://snack.expo.io/@jdnichollsc/animatable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment