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