1import * as React from 'react';
2import { LayoutRectangle, View } from 'react-native';
3import normalizeColor from 'react-native-web/src/modules/normalizeColor';
4
5import { NativeLinearGradientPoint, NativeLinearGradientProps } from './NativeLinearGradient.types';
6
7export default function NativeLinearGradient({
8  colors,
9  locations,
10  startPoint,
11  endPoint,
12  ...props
13}: NativeLinearGradientProps): React.ReactElement {
14  const [layout, setLayout] = React.useState<LayoutRectangle | null>(null);
15
16  const { width = 1, height = 1 } = layout ?? {};
17
18  const pseudoAngle = React.useMemo(() => {
19    const getControlPoints = (): NativeLinearGradientPoint[] => {
20      let correctedStartPoint: NativeLinearGradientPoint = [0, 0];
21      if (Array.isArray(startPoint)) {
22        correctedStartPoint = [
23          startPoint[0] != null ? startPoint[0] : 0.0,
24          startPoint[1] != null ? startPoint[1] : 0.0,
25        ];
26      }
27      let correctedEndPoint: NativeLinearGradientPoint = [0.0, 1.0];
28      if (Array.isArray(endPoint)) {
29        correctedEndPoint = [
30          endPoint[0] != null ? endPoint[0] : 0.0,
31          endPoint[1] != null ? endPoint[1] : 1.0,
32        ];
33      }
34      return [correctedStartPoint, correctedEndPoint];
35    };
36
37    const [start, end] = getControlPoints();
38    start[0] *= width;
39    end[0] *= width;
40    start[1] *= height;
41    end[1] *= height;
42    const py = end[1] - start[1];
43    const px = end[0] - start[0];
44
45    return 90 + (Math.atan2(py, px) * 180) / Math.PI;
46  }, [width, height, startPoint, endPoint]);
47
48  const gradientColors = React.useMemo(() => {
49    return colors.map((color: number, index: number): string => {
50      const hexColor = normalizeColor(color);
51      let output = hexColor;
52      if (locations && locations[index]) {
53        const location = Math.max(0, Math.min(1, locations[index]));
54        // Convert 0...1 to 0...100
55        const percentage = location * 100;
56        output += ` ${percentage}%`;
57      }
58      return output;
59    });
60  }, [colors, locations]);
61
62  const colorStyle = gradientColors.join(',');
63  const backgroundImage = `linear-gradient(${pseudoAngle}deg, ${colorStyle})`;
64  // TODO(Bacon): In the future we could consider adding `backgroundRepeat: "no-repeat"`. For more
65  // browser support.
66  return (
67    <View
68      {...props}
69      style={[
70        props.style,
71        // @ts-ignore: [ts] Property 'backgroundImage' does not exist on type 'ViewStyle'.
72        { backgroundImage },
73      ]}
74      onLayout={event => {
75        const { x, y, width, height } = event.nativeEvent.layout;
76        const oldLayout = layout ?? { x: 0, y: 0, width: 1, height: 1 };
77        // don't set new layout state unless the layout has actually changed
78        if (
79          x !== oldLayout.x ||
80          y !== oldLayout.y ||
81          width !== oldLayout.width ||
82          height !== oldLayout.height
83        ) {
84          setLayout({ x, y, width, height });
85        }
86
87        if (props.onLayout) {
88          props.onLayout(event);
89        }
90      }}
91    />
92  );
93}
94