192f6de58SWill Schurmanimport { requireNativeViewManager } from '@unimodules/core';
2b6f8472aSBrent Vatneimport React from 'react';
3b6f8472aSBrent Vatneimport { StyleSheet, View } from 'react-native';
4b6f8472aSBrent Vatne
5b6f8472aSBrent Vatnetype Props = {
6b6f8472aSBrent Vatne  colors: number[];
7b6f8472aSBrent Vatne  locations?: number[] | null;
8b6f8472aSBrent Vatne  startPoint?: Point | null;
9b6f8472aSBrent Vatne  endPoint?: Point | null;
10b6f8472aSBrent Vatne} & React.ComponentProps<typeof View>;
11b6f8472aSBrent Vatne
12b6f8472aSBrent Vatnetype Point = [number, number];
13b6f8472aSBrent Vatne
14b6f8472aSBrent Vatneexport default class NativeLinearGradient extends React.Component<Props> {
15b6f8472aSBrent Vatne  render() {
16*b91f886fSJames Ide    const { colors, locations, startPoint, endPoint, children, style, ...props } = this.props;
17b6f8472aSBrent Vatne
18b6f8472aSBrent Vatne    // TODO: revisit whether we need to inherit the container's borderRadius since this issue has
19b6f8472aSBrent Vatne    // been resolved: https://github.com/facebook/react-native/issues/3198
20*b91f886fSJames Ide    const flatStyle = StyleSheet.flatten(style) || {};
21*b91f886fSJames Ide    const borderRadius = flatStyle.borderRadius || 0;
22b6f8472aSBrent Vatne
23b6f8472aSBrent Vatne    // This is the format from:
24b6f8472aSBrent Vatne    // https://developer.android.com/reference/android/graphics/Path.html#addRoundRect(android.graphics.RectF,%20float[],%20android.graphics.Path.Direction)
25*b91f886fSJames Ide    const borderRadiiPerCorner = [
26b6f8472aSBrent Vatne      flatStyle.borderTopLeftRadius || borderRadius,
27b6f8472aSBrent Vatne      flatStyle.borderTopLeftRadius || borderRadius,
28b6f8472aSBrent Vatne      flatStyle.borderTopRightRadius || borderRadius,
29b6f8472aSBrent Vatne      flatStyle.borderTopRightRadius || borderRadius,
30b6f8472aSBrent Vatne      flatStyle.borderBottomRightRadius || borderRadius,
31b6f8472aSBrent Vatne      flatStyle.borderBottomRightRadius || borderRadius,
32b6f8472aSBrent Vatne      flatStyle.borderBottomLeftRadius || borderRadius,
33b6f8472aSBrent Vatne      flatStyle.borderBottomLeftRadius || borderRadius,
34b6f8472aSBrent Vatne    ];
35b6f8472aSBrent Vatne
36b6f8472aSBrent Vatne    return (
37b6f8472aSBrent Vatne      <View {...props} style={style}>
38b6f8472aSBrent Vatne        <BaseNativeLinearGradient
39b6f8472aSBrent Vatne          style={{ position: 'absolute', top: 0, left: 0, bottom: 0, right: 0 }}
40b6f8472aSBrent Vatne          colors={colors}
41b6f8472aSBrent Vatne          startPoint={startPoint}
42b6f8472aSBrent Vatne          endPoint={endPoint}
43b6f8472aSBrent Vatne          locations={locations}
44b6f8472aSBrent Vatne          borderRadii={borderRadiiPerCorner}
45b6f8472aSBrent Vatne        />
46b6f8472aSBrent Vatne        {children}
47b6f8472aSBrent Vatne      </View>
48b6f8472aSBrent Vatne    );
49b6f8472aSBrent Vatne  }
50b6f8472aSBrent Vatne}
51b6f8472aSBrent Vatne
52b6f8472aSBrent Vatneconst BaseNativeLinearGradient = requireNativeViewManager('ExpoLinearGradient');
53