UNPKG

1.55 kBJavaScriptView Raw
1import React, { Component } from 'react';
2import PropTypes from 'prop-types';
3
4import {
5 ViewPropTypes,
6 processColor,
7 requireNativeComponent,
8} from 'react-native';
9
10export default class LinearGradient extends Component {
11 static propTypes = {
12 start: PropTypes.oneOfType([
13 PropTypes.arrayOf(PropTypes.number),
14 PropTypes.object,
15 ]),
16 end: PropTypes.oneOfType([
17 PropTypes.arrayOf(PropTypes.number),
18 PropTypes.object,
19 ]),
20 colors: PropTypes.arrayOf(PropTypes.string).isRequired,
21 locations: PropTypes.arrayOf(PropTypes.number),
22 ...ViewPropTypes,
23 };
24
25 render() {
26 const { colors, locations, start, end, ...otherProps } = this.props;
27 if (colors && locations && colors.length !== locations.length) {
28 console.warn(
29 'LinearGradient colors and locations props should be arrays of the same length'
30 );
31 }
32
33 // support for current react-native-linear-gradient api
34 let startProp = start;
35 let endProp = end;
36 if (start && start.x !== undefined && start.y !== undefined) {
37 startProp = [start.x, start.y];
38 }
39 if (end && end.x !== undefined && end.y !== undefined) {
40 endProp = [end.x, end.y];
41 }
42
43 return (
44 <NativeLinearGradient
45 {...otherProps}
46 colors={colors.map(processColor)}
47 locations={locations ? locations.slice(0, colors.length) : null}
48 start={startProp}
49 end={endProp}
50 />
51 );
52 }
53}
54
55const NativeLinearGradient = requireNativeComponent(
56 'ExponentLinearGradient',
57 null
58);