1+ import type { FC } from "react" ;
12import { useMemo } from "react" ;
23import { View } from "react-native" ;
34import type { ViewProps } from "react-native" ;
@@ -15,22 +16,21 @@ export interface LinearGradientProps extends ViewProps {
1516
1617type RequiredProps = Required < LinearGradientProps > ;
1718
18- function getCssColors (
19+ const getCssColors = (
1920 colors : RequiredProps [ "colors" ] ,
2021 locations : RequiredProps [ "locations" ] ,
21- ) {
22- return colors
22+ ) =>
23+ colors
2324 . map ( ( color , i ) =>
2425 locations ?. [ i ] != null ? `${ color } ${ locations [ i ] * 100 } %` : color ,
2526 )
2627 . join ( "," ) ;
27- }
2828
29- function getSvgColors (
29+ const getSvgColors = (
3030 colors : RequiredProps [ "colors" ] ,
3131 locations : RequiredProps [ "locations" ] ,
32- ) {
33- return colors
32+ ) =>
33+ colors
3434 . map ( ( color , i ) => {
3535 const offsetValue =
3636 locations ?. [ i ] != null
@@ -42,40 +42,39 @@ function getSvgColors(
4242 return `<stop offset="${ normalizedOffset * 100 } %" stop-color="${ color } " />` ;
4343 } )
4444 . join ( "" ) ;
45- }
4645
4746const toPercent = ( v : number ) : string => v * 100 + "%" ;
4847
49- export function getLinearGradientBackgroundImage (
48+ export const getLinearGradientBackgroundImage = (
5049 start : RequiredProps [ "start" ] ,
5150 end : RequiredProps [ "end" ] ,
5251 locations : RequiredProps [ "locations" ] ,
5352 colors : RequiredProps [ "colors" ] ,
5453 useAngle : LinearGradientProps [ "useAngle" ] ,
5554 angle : RequiredProps [ "angle" ] ,
56- ) {
55+ ) => {
5756 if ( useAngle ) {
5857 return `linear-gradient(${ angle } deg, ${ getCssColors ( colors , locations ) } )` ;
5958 }
6059
6160 const svgColors = getSvgColors ( colors , locations ) ;
6261 const svgGradient = `<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%"><defs><linearGradient id="g" gradientUnits="userSpaceOnUse" x1="${ toPercent ( start . x ) } " y1="${ toPercent ( start . y ) } " x2="${ toPercent ( end . x ) } " y2="${ toPercent ( end . y ) } ">${ svgColors } </linearGradient></defs><rect width="100%" height="100%" fill="url(#g)"/></svg>` ;
6362 return `url("data:image/svg+xml,${ encodeURIComponent ( svgGradient ) } ")` ;
64- }
63+ } ;
6564
6665const emptyArray : [ ] = [ ] ;
6766const defaultStart = { x : 0.5 , y : 0 } ;
6867const defaultEnd = { x : 0.5 , y : 1 } ;
6968
70- function LinearGradientWeb ( {
69+ const LinearGradientWeb : FC < LinearGradientProps > = ( {
7170 start = defaultStart ,
7271 end = defaultEnd ,
7372 locations = emptyArray ,
7473 colors = emptyArray ,
7574 useAngle,
7675 angle = 0 ,
7776 ...viewProps
78- } : LinearGradientProps ) {
77+ } ) => {
7978 "use memo" ;
8079
8180 const bgStyle = useMemo ( ( ) : ViewProps [ "style" ] => {
@@ -95,6 +94,8 @@ function LinearGradientWeb({
9594 } , [ start , end , locations , colors , useAngle , angle ] ) ;
9695
9796 return < View { ...viewProps } style = { [ viewProps . style , bgStyle ] } /> ;
98- }
97+ } ;
98+
99+ LinearGradientWeb . displayName = "LinearGradientWeb" ;
99100
100101export default LinearGradientWeb ;
0 commit comments