Skip to content

Commit 1b01381

Browse files
authored
feat: remove extractColor in favor of RN impl (#1726)
PR removing extractColor.ts in favor of using the processColor implementation straight from react-native. It should handle all the cases from the previous implementation and the cases with PlatformColor and DynamicColorIOS. Normally we would just send the returned value to the native side, but in react-native-svg, e.g. fill prop can have more values than just color, e.g. currentColor. Because of it, we cannot use UIColor on iOS, NSColor on macOS and customType="Color" on Android as a prop type there and therefore we need to prepare the custom values on the JS side. It is done by passing the prop as an array with specific first element. In case of colors, it is 0. (hopefully I understood the code right).
1 parent d859083 commit 1b01381

File tree

13 files changed

+63
-518
lines changed

13 files changed

+63
-518
lines changed

Example/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ PODS:
347347
- React-jsi (= 0.68.0-rc.1)
348348
- React-logger (= 0.68.0-rc.1)
349349
- React-perflogger (= 0.68.0-rc.1)
350-
- RNSVG (12.2.0):
350+
- RNSVG (12.3.0):
351351
- React-Core
352352
- SocketRocket (0.6.0)
353353
- Yoga (1.14.0)
@@ -501,7 +501,7 @@ SPEC CHECKSUMS:
501501
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
502502
DoubleConversion: 831926d9b8bf8166fd87886c4abab286c2422662
503503
FBLazyVector: bcdeff523be9f87a135b7c6fde8736db94904716
504-
FBReactNativeSpec: 0c3f104f594b34d7b3a923cd12e03b0d4e12eaf5
504+
FBReactNativeSpec: c01fba40ea819e68030d0d9b9094a173b2a3d127
505505
Flipper: 26fc4b7382499f1281eb8cb921e5c3ad6de91fe0
506506
Flipper-Boost-iOSX: fd1e2b8cbef7e662a122412d7ac5f5bea715403c
507507
Flipper-DoubleConversion: 57ffbe81ef95306cc9e69c4aa3aeeeeb58a6a28c
@@ -540,7 +540,7 @@ SPEC CHECKSUMS:
540540
React-RCTVibration: 5cab6419b68d5750482b0fc34dbb27af550469cc
541541
React-runtimeexecutor: 10cda3a396b14a764a5f86088e7e3810b9c66cec
542542
ReactCommon: 73a01eb83f22c84a6b09dfb60f3463888ebd4975
543-
RNSVG: 4ecc2e8f38b6ebe7889909570c26f3abe8059767
543+
RNSVG: 302bfc9905bd8122f08966dc2ce2d07b7b52b9f8
544544
SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608
545545
Yoga: d29dba374c1a582b40cfb736647b5e1b5ed35dba
546546
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

TestsExample/App.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* eslint-disable no-unused-vars */
22
import React from 'react';
3-
import SampleTest from './src/SampleTest';
3+
import ColorTest from './src/ColorTest';
4+
import Test1718 from './src/Test1718';
45

56
export default function App() {
6-
return <SampleTest />;
7+
return <ColorTest />;
78
}

TestsExample/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ PODS:
347347
- React-jsi (= 0.68.0-rc.1)
348348
- React-logger (= 0.68.0-rc.1)
349349
- React-perflogger (= 0.68.0-rc.1)
350-
- RNSVG (12.2.0):
350+
- RNSVG (12.3.0):
351351
- React-Core
352352
- SocketRocket (0.6.0)
353353
- Yoga (1.14.0)
@@ -501,7 +501,7 @@ SPEC CHECKSUMS:
501501
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
502502
DoubleConversion: 831926d9b8bf8166fd87886c4abab286c2422662
503503
FBLazyVector: bcdeff523be9f87a135b7c6fde8736db94904716
504-
FBReactNativeSpec: 96fb2991e5748dcf1c627822cbf41253f1fffe31
504+
FBReactNativeSpec: 137186498f03934daf999cec565fbc3d291f294c
505505
Flipper: 26fc4b7382499f1281eb8cb921e5c3ad6de91fe0
506506
Flipper-Boost-iOSX: fd1e2b8cbef7e662a122412d7ac5f5bea715403c
507507
Flipper-DoubleConversion: 57ffbe81ef95306cc9e69c4aa3aeeeeb58a6a28c
@@ -540,7 +540,7 @@ SPEC CHECKSUMS:
540540
React-RCTVibration: 5cab6419b68d5750482b0fc34dbb27af550469cc
541541
React-runtimeexecutor: 10cda3a396b14a764a5f86088e7e3810b9c66cec
542542
ReactCommon: 73a01eb83f22c84a6b09dfb60f3463888ebd4975
543-
RNSVG: 4ecc2e8f38b6ebe7889909570c26f3abe8059767
543+
RNSVG: 302bfc9905bd8122f08966dc2ce2d07b7b52b9f8
544544
SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608
545545
Yoga: d29dba374c1a582b40cfb736647b5e1b5ed35dba
546546
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
import React from 'react';
2-
import { PlatformColor, Platform, Button } from 'react-native';
2+
import { PlatformColor, Platform, Button, DynamicColorIOS } from 'react-native';
33
import {
44
Svg,
55
Circle,
66
Rect,
77
Text,
88
TSpan,
9-
SvgUri
109
} from 'react-native-svg';
1110

1211
const color = PlatformColor(Platform.select({
13-
ios: 'label',
14-
android: '@android:color/primary_text_light',
12+
ios: 'systemTealColor',
13+
android: '@android:color/holo_blue_bright',
1514
default: 'black',
1615
}))
1716

17+
// const customContrastDynamicTextColor = DynamicColorIOS({
18+
// dark: 'hsla(360, 40%, 30%, 1.0)',
19+
// light: '#ff00ff55',
20+
// highContrastDark: 'black',
21+
// highContrastLight: 'white',
22+
// });
23+
1824
export default () => {
1925
const [test, setTest] = React.useState(50);
20-
const [uri, setUri] = React.useState('https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/not_existing.svg')
2126

2227
return (
2328
<>
@@ -46,12 +51,6 @@ export default () => {
4651
</Text>
4752
</Svg>
4853
<Button title="Click me" onPress={()=> setTest(test + 1)}/>
49-
<SvgUri
50-
onError={() => setUri('https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/ruby.svg')}
51-
width="100"
52-
height="100"
53-
uri={uri}
54-
/>
5554
</>
5655
);
5756
}

TestsExample/src/Test1718.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react';
2+
import { SvgUri } from 'react-native-svg';
3+
4+
5+
export default () => {
6+
const [uri, setUri] = React.useState('https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/not_existing.svg')
7+
8+
return (
9+
<>
10+
<SvgUri
11+
onError={() => setUri('https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/ruby.svg')}
12+
width="100"
13+
height="100"
14+
uri={uri}
15+
/>
16+
</>
17+
);
18+
}

src/elements/Svg.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import React, { Component } from 'react';
22
import {
3+
ColorValue,
34
findNodeHandle,
45
MeasureInWindowOnSuccessCallback,
56
MeasureLayoutOnSuccessCallback,
67
MeasureOnSuccessCallback,
78
NativeModules,
9+
processColor,
810
StyleSheet,
911
ViewStyle,
1012
} from 'react-native';
1113
import {
1214
ClipProps,
13-
Color,
1415
extractedProps,
1516
FillProps,
1617
NumberProp,
@@ -21,7 +22,6 @@ import {
2122
} from '../lib/extract/types';
2223
import extractResponder from '../lib/extract/extractResponder';
2324
import extractViewBox from '../lib/extract/extractViewBox';
24-
import extractColor from '../lib/extract/extractColor';
2525
import Shape from './Shape';
2626
import G from './G';
2727
import { RNSVGSvg } from './NativeComponents';
@@ -38,7 +38,7 @@ const defaultStyle = styles.svg;
3838

3939
export default class Svg extends Shape<
4040
{
41-
color?: Color;
41+
color?: ColorValue;
4242
viewBox?: string;
4343
opacity?: NumberProp;
4444
onLayout?: () => void;
@@ -183,7 +183,7 @@ export default class Svg extends Shape<
183183

184184
extractResponder(props, props, this as ResponderInstanceProps);
185185

186-
const tint = extractColor(color);
186+
const tint = processColor(color);
187187
if (tint != null) {
188188
props.color = tint;
189189
props.tintColor = tint;

src/index.d.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as React from 'react';
22
import * as ReactNative from 'react-native';
33
import {
4+
ColorValue,
45
GestureResponderEvent,
56
TransformsStyle,
6-
OpaqueColorValue,
77
} from 'react-native';
88

99
// Common props
@@ -97,15 +97,7 @@ export interface ResponderProps extends ReactNative.GestureResponderHandlers {
9797
pointerEvents?: 'box-none' | 'none' | 'box-only' | 'auto';
9898
}
9999

100-
// rgba values inside range 0 to 1 inclusive
101-
// rgbaArray = [r, g, b, a]
102-
export type rgbaArray = ReadonlyArray<number>;
103-
104-
// argb values inside range 0x00 to 0xff inclusive
105-
// int32ARGBColor = 0xaarrggbb
106-
export type int32ARGBColor = number;
107-
108-
export type Color = int32ARGBColor | rgbaArray | OpaqueColorValue | string;
100+
export type Color = ColorValue;
109101

110102
export interface FillProps {
111103
fill?: Color;

src/index.js.flow

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import * as React from "react";
88
import * as ReactNative from "react-native";
99
import {
10+
ColorValue,
1011
GestureResponderEvent,
1112
TransformsStyle,
12-
OpaqueColorValue,
1313
} from "react-native";
1414
export type NumberProp = string | number;
1515
export type NumberArray = NumberProp[] | NumberProp;
@@ -95,9 +95,7 @@ export type ResponderProps = {
9595
pointerEvents?: "box-none" | "none" | "box-only" | "auto",
9696
...
9797
} & ReactNative.GestureResponderHandlers;
98-
export type rgbaArray = $ReadOnlyArray<number>;
99-
export type int32ARGBColor = number;
100-
export type Color = int32ARGBColor | rgbaArray | OpaqueColorValue | string;
98+
export type Color = ColorValue;
10199
export interface FillProps {
102100
fill?: Color;
103101
fillOpacity?: NumberProp;

src/lib/extract/extractBrush.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
import extractColor, { integerColor } from './extractColor';
2-
import { Color } from './types';
1+
import { ColorValue, processColor } from 'react-native';
32

43
const urlIdPattern = /^url\(#(.+)\)$/;
54

65
const currentColorBrush = [2];
76
const contextFillBrush = [3];
87
const contextStrokeBrush = [4];
98

10-
export default function extractBrush(color?: Color) {
11-
if (typeof color === 'number') {
12-
if (color >>> 0 === color && color >= 0 && color <= 0xffffffff) {
13-
return integerColor(color);
14-
}
15-
}
9+
export default function extractBrush(color?: ColorValue) {
1610

1711
if (!color || color === 'none') {
1812
return null;
@@ -35,23 +29,18 @@ export default function extractBrush(color?: Color) {
3529
return [1, brush[1]];
3630
}
3731

38-
const int32ARGBColor = extractColor(color);
39-
if (typeof int32ARGBColor === 'number') {
40-
return int32ARGBColor;
32+
const processedColor = processColor(color);
33+
if (typeof processedColor === 'number') {
34+
return processedColor;
4135
}
4236

43-
if (typeof color === 'object' && color !== null) {
44-
// iOS PlatformColor
45-
if ('semantic' in color) {
46-
return [0, color];
47-
}
48-
49-
// Android PlatformColor
50-
if ('resource_paths' in color) {
51-
return [0, color];
52-
}
37+
if (typeof processedColor === 'object' && processedColor !== null) {
38+
// if we got an object, it should be `PlatformColor` or `DynamicColorIOS`,
39+
// so we pass it as an array with `0` value as first item, which is interpreted
40+
// on the native side as color to be managed by `RCTConvert`.
41+
return [0, processedColor];
5342
}
5443

55-
console.warn(`"${color}" is not a valid color or brush`);
44+
console.warn(`"${String(color)}" is not a valid color or brush`);
5645
return null;
5746
}

0 commit comments

Comments
 (0)