Skip to content

Instantly share code, notes, and snippets.

@leon3s
Created November 10, 2017 14:45
Show Gist options
  • Save leon3s/a53ef0965e7c24a739d7adbb4946d97a to your computer and use it in GitHub Desktop.
Save leon3s/a53ef0965e7c24a739d7adbb4946d97a to your computer and use it in GitHub Desktop.
LinearGradient expo compatibility with react-native-web
import React from 'react';
import PropTypes from 'prop-types';
import { View } from 'react-native';
const propTypes = {
colors: PropTypes.arrayOf(PropTypes.string),
start: PropTypes.arrayOf(PropTypes.number),
end: PropTypes.arrayOf(PropTypes.number),
style: View.propTypes.style,
children: PropTypes.oneOfType([
PropTypes.node,
PropTypes.arrayOf(PropTypes.node),
]),
};
const defaultProps = {
colors: ['#C8B287', '#B4985F'],
start: [0, 0],
end: [0, 0],
style: null,
children: null,
};
const LinearGradient = (props) => {
const {
start, end, colors, style, children, ...otherProps
} = props;
const vec = [end[0] - start[0], -(end[1] - start[1])];
const angleRad = Math.atan(vec[1] / vec[0]);
const angleDeg = Math.round((angleRad * 180) / Math.PI) + 180;
const realLocations = colors.map((color, i) => (1 / (colors.length - 1)) * i);
const colorStrings = colors.map((color, i) => `${color} ${Math.round(realLocations[i] * 100)}%`).join(', ');
return (
<View
{...otherProps}
style={[{
backgroundImage: `linear-gradient(${angleDeg}deg, ${colorStrings})`,
},
style,
]}
>
{children}
</View>
);
};
LinearGradient.propTypes = propTypes;
LinearGradient.defaultProps = defaultProps;
export default LinearGradient;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment