Skip to content

Instantly share code, notes, and snippets.

@danharper
Last active May 16, 2016 20:31
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save danharper/2d6fa54c2155f54327b3 to your computer and use it in GitHub Desktop.
Save danharper/2d6fa54c2155f54327b3 to your computer and use it in GitHub Desktop.
BorderedInput, with Material design style focus animation. Preview: https://i.imgur.com/Fek7rXF.gif
// note there may be a better way to abuse flexbox than this :)
var React = require('react-native')
var { View, TextInput } = React
var BorderedInput = React.createClass({
getInitialState() {
return { i: 0 }
},
getDefaultProps() {
return {
mutedColor: '#dddddd',
focusedColor: 'blue',
}
},
render() {
var a = 10 / Math.pow(2, this.state.i), b = .01;
return (
<View style={this.props.style}>
<TextInput
{...this.props}
onFocus={this.onFocus}
onBlur={this.onBlur}
style={[ this.props.inputStyle, { flex: 1 }]}
/>
<View style={{ flexDirection: 'row', height: 1 }}>
<View style={{ flex: a, backgroundColor: this.props.mutedColor }}></View>
<View style={{ flex: b, backgroundColor: this.props.focusedColor }}></View>
<View style={{ flex: a, backgroundColor: this.props.mutedColor }}></View>
</View>
</View>
)
},
onFocus() {
this.animateIn()
this.props.onFocus && this.props.onFocus()
},
onBlur() {
this.animateOut()
this.props.onBlur && this.props.onBlur()
},
animateIn() {
requestAnimationFrame(() => {
this.setState({ i: this.state.i + 1 })
if (this.state.i < 20) this.animateIn()
})
},
animateOut() {
requestAnimationFrame(() => {
this.setState({ i: this.state.i - 1 })
if (this.state.i > 0) this.animateOut()
})
},
})
module.exports = BorderedInput
render() {
<BorderedInput
value="hi@dans.email"
style={{ height: 40 }}
inputStyle={{ fontSize: 15 }}
mutedColor="#cccccc"
focusedColor="#4970f2"
/>
}
@varmab
Copy link

varmab commented Apr 21, 2015

I am unable to get this work. Can you provide your sample project?

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