Skip to content

Commit 36c0a7d

Browse files
obladorfacebook-github-bot
authored andcommitted
Support user-defined PlatformColors on iOS (#31258)
Summary: Original PR: #31258 ## Imported PR from Github: This is a continuation of #29683. I've talked to danilobuerger who does not intend on continue work on it and is OK with me picking up where he left. This PR is identical besides adding a test case in the RN Tester app as requested in the original PR. In summary it gives iOS feature parity with Android in the sense that one can use user-defined native colors, something even the docs claim is possible. It's useful as it enables accessibility features such as high contrast colors and makes implementing dark mode simple. For an example on how it can be used, see https://github.com/klarna-incubator/platform-colors ## Changelog [iOS] [Added] - Allow PlatformColor to return user-defined named asset color Pull Request resolved: #31258 Test Plan: Test case added to RN Tester. Reviewed By: sammy-SC Differential Revision: D28803206 Pulled By: p-sun fbshipit-source-id: e0f0690274799bd2d09c9f9d1a6a95ac0f979498
1 parent b3a715f commit 36c0a7d

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

React/Base/RCTConvert.m

+10-2
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,11 @@ + (UIColor *)UIColor:(id)json
850850
if ((value = [dictionary objectForKey:@"semantic"])) {
851851
if ([value isKindOfClass:[NSString class]]) {
852852
NSString *semanticName = value;
853-
UIColor *color = RCTColorFromSemanticColorName(semanticName);
853+
UIColor *color = [UIColor colorNamed:semanticName];
854+
if (color != nil) {
855+
return color;
856+
}
857+
color = RCTColorFromSemanticColorName(semanticName);
854858
if (color == nil) {
855859
RCTLogConvertError(
856860
json,
@@ -859,7 +863,11 @@ + (UIColor *)UIColor:(id)json
859863
return color;
860864
} else if ([value isKindOfClass:[NSArray class]]) {
861865
for (id name in value) {
862-
UIColor *color = RCTColorFromSemanticColorName(name);
866+
UIColor *color = [UIColor colorNamed:name];
867+
if (color != nil) {
868+
return color;
869+
}
870+
color = RCTColorFromSemanticColorName(name);
863871
if (color != nil) {
864872
return color;
865873
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"colors" : [
3+
{
4+
"color" : {
5+
"color-space" : "srgb",
6+
"components" : {
7+
"alpha" : "1.000",
8+
"blue" : "1.000",
9+
"green" : "1.000",
10+
"red" : "0.516"
11+
}
12+
},
13+
"idiom" : "universal"
14+
},
15+
{
16+
"appearances" : [
17+
{
18+
"appearance" : "luminosity",
19+
"value" : "dark"
20+
}
21+
],
22+
"color" : {
23+
"color-space" : "srgb",
24+
"components" : {
25+
"alpha" : "1.000",
26+
"blue" : "0.746",
27+
"green" : "0.195",
28+
"red" : "0.080"
29+
}
30+
},
31+
"idiom" : "universal"
32+
}
33+
],
34+
"info" : {
35+
"author" : "xcode",
36+
"version" : 1
37+
}
38+
}

packages/rn-tester/js/examples/PlatformColor/PlatformColorExample.js

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ function PlatformColorsExample() {
110110
{label: 'systemGray6', color: PlatformColor('systemGray6')},
111111
// Transparent Color
112112
{label: 'clear', color: PlatformColor('clear')},
113+
{label: 'customColor', color: PlatformColor('customColor')},
113114
];
114115
} else if (Platform.OS === 'android') {
115116
colors = [

0 commit comments

Comments
 (0)