Last active
December 11, 2017 14:08
-
-
Save agungsb/1e7f0761125f723de9fec4b78314bf4d to your computer and use it in GitHub Desktop.
Gist demo for https://medium.com/p/ae37dcf6ddd3/edit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const divisor = 1000; | |
const pi = 1/divisor; | |
class Faded extends React.PureComponent { | |
constructor(props) { | |
super(props); | |
let i; | |
let collection = []; | |
let pixelsStyle = { | |
width: '100%', | |
position: 'absolute', | |
height: props.height, | |
flexDirection: 'column' | |
}; | |
if (props.direction === 'up') { | |
pixelsStyle = { | |
...pixelsStyle, bottom: 0 | |
} | |
collection.push(0); | |
i = pi; | |
while (i < 1) { | |
collection.push(i); | |
i += pi; | |
} | |
collection.push(1); | |
} else { | |
pixelsStyle = { | |
...pixelsStyle, top: 0 | |
} | |
collection.push(1); | |
i = 1.00; | |
while (i > 0) { | |
collection.push(i); | |
i -= pi; | |
} | |
collection.push(0); | |
} | |
let r = 0, g = 0, b = 0; | |
if (hexToRgb(props.color)) { | |
r = hexToRgb(props.color).r; | |
g = hexToRgb(props.color).g; | |
b = hexToRgb(props.color).b; | |
} | |
this.state = { | |
collection, | |
pixelsStyle, | |
r, g, b | |
}; | |
} | |
render() { | |
const { children, height } = this.props; | |
const { collection, pixelsStyle, r, g, b } = this.state; | |
return ( | |
<View style={{ flexDirection: 'column' }}> | |
<View style={pixelsStyle}> | |
{collection.map((o, key) => | |
<View key={key} style={{ height: (height / divisor), backgroundColor: `rgba(${r}, ${g}, ${b}, ${o})` }} /> | |
)} | |
</View> | |
{children} | |
</View> | |
); | |
} | |
} | |
Faded.propTypes = { | |
children: PropTypes.object, | |
color: PropTypes.string, | |
direction: PropTypes.string | |
} | |
Faded.defaultProps = { | |
color: '#000000', | |
direction: 'up' | |
} | |
export default Faded |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment