-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
93 lines (86 loc) · 1.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import React, { PureComponent } from 'react';
import { View } from 'react-native';
export default class LinearGradient extends PureComponent {
static defaultProps = {
start: {
x: 0.5,
y: 0,
},
end: {
x: 0.5,
y: 1,
},
locations: [],
colors: [],
useAngle: false,
angle: 0,
};
state = {
width: 1,
height: 1,
};
measure = (event) => {
// Zero-size indicates element disappearance.
// Do not repaint background gradient image to prevent jumps when it reappears.
if (event.nativeEvent.layout.width === 0 && event.nativeEvent.layout.height === 0) {
return;
}
this.setState({
width: event.nativeEvent.layout.width,
height: event.nativeEvent.layout.height,
});
if (this.props.onLayout) {
this.props.onLayout(event);
}
}
getAngle = () => {
if (this.props.useAngle) {
return this.props.angle + 'deg';
}
// Math.atan2 handles Infinity
const angle =
Math.atan2(
this.state.width * (this.props.end.y - this.props.start.y),
this.state.height * (this.props.end.x - this.props.start.x)
) +
Math.PI / 2;
return angle + 'rad';
};
getColors = () =>
this.props.colors
.map((color, index) => {
const location = this.props.locations[index];
let locationStyle = '';
if (location) {
locationStyle = ' ' + location * 100 + '%';
}
return color + locationStyle;
})
.join(',');
render() {
const {
start,
end,
colors,
locations,
useAngle,
angleCenter,
angle,
style,
children,
...otherProps
} = this.props;
return (
<View
{...otherProps}
style={[
style,
{ backgroundImage: `linear-gradient(${this.getAngle()},${this.getColors()})` },
]}
onLayout={this.measure}
>
{children}
</View>
);
}
}