Skip to content

Commit 5851f61

Browse files
authored
DynamicColorMacOS fixes (#1028)
* Use initWithDynamicProvider + Add HC Support * Update log error
1 parent 291c44d commit 5851f61

File tree

7 files changed

+69
-252
lines changed

7 files changed

+69
-252
lines changed

Libraries/StyleSheet/PlatformColorValueTypes.macos.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export opaque type NativeColorValue = {
1818
dynamic?: {
1919
light: ?(ColorValue | ProcessedColorValue),
2020
dark: ?(ColorValue | ProcessedColorValue),
21+
highContrastLight?: ?(ColorValue | ProcessedColorValue),
22+
highContrastDark?: ?(ColorValue | ProcessedColorValue),
2123
},
2224
colorWithSystemEffect?: {
2325
baseColor: ?(ColorValue | ProcessedColorValue),
@@ -51,12 +53,21 @@ export const ColorWithSystemEffectMacOSPrivate = (
5153
export type DynamicColorMacOSTuplePrivate = {
5254
light: ColorValue,
5355
dark: ColorValue,
56+
highContrastLight?: ColorValue,
57+
highContrastDark?: ColorValue,
5458
};
5559

5660
export const DynamicColorMacOSPrivate = (
5761
tuple: DynamicColorMacOSTuplePrivate,
5862
): ColorValue => {
59-
return {dynamic: {light: tuple.light, dark: tuple.dark}};
63+
return {
64+
dynamic: {
65+
light: tuple.light,
66+
dark: tuple.dark,
67+
highContrastLight: tuple.highContrastLight,
68+
highContrastDark: tuple.highContrastDark,
69+
},
70+
};
6071
};
6172

6273
export const normalizeColorObject = (
@@ -74,6 +85,8 @@ export const normalizeColorObject = (
7485
dynamic: {
7586
light: normalizeColor(dynamic.light),
7687
dark: normalizeColor(dynamic.dark),
88+
highContrastLight: normalizeColor(dynamic.highContrastLight),
89+
highContrastDark: normalizeColor(dynamic.highContrastDark),
7790
},
7891
};
7992
return dynamicColor;
@@ -104,6 +117,8 @@ export const processColorObject = (
104117
dynamic: {
105118
light: processColor(dynamic.light),
106119
dark: processColor(dynamic.dark),
120+
highContrastLight: processColor(dynamic.highContrastLight),
121+
highContrastDark: processColor(dynamic.highContrastDark),
107122
},
108123
};
109124
return dynamicColor;

Libraries/StyleSheet/PlatformColorValueTypesMacOS.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import type {ColorValue} from './StyleSheet';
1515
export type DynamicColorMacOSTuple = {
1616
light: ColorValue,
1717
dark: ColorValue,
18+
highContrastLight?: ColorValue,
19+
highContrastDark?: ColorValue,
1820
};
1921

2022
export const DynamicColorMacOS = (

Libraries/StyleSheet/PlatformColorValueTypesMacOS.macos.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@ import {
1919
export type DynamicColorMacOSTuple = {
2020
light: ColorValue,
2121
dark: ColorValue,
22+
highContrastLight?: ColorValue,
23+
highContrastDark?: ColorValue,
2224
};
2325

2426
export const DynamicColorMacOS = (
2527
tuple: DynamicColorMacOSTuple,
2628
): ColorValue => {
27-
return DynamicColorMacOSPrivate({light: tuple.light, dark: tuple.dark});
29+
return DynamicColorMacOSPrivate({
30+
light: tuple.light,
31+
dark: tuple.dark,
32+
highContrastLight: tuple.highContrastLight,
33+
highContrastDark: tuple.highContrastDark,
34+
});
2835
};
2936

3037
export type SystemEffectMacOS =

React/Base/RCTConvert.m

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
#import "RCTParserUtils.h"
1717
#import "RCTUtils.h"
1818

19-
#if TARGET_OS_OSX // [TODO(macOS GH#774)
20-
#import "RCTDynamicColor.h"
21-
#endif // ]TODO(macOS GH#774)
22-
2319
@implementation RCTConvert
2420

2521
RCT_CONVERTER(id, id, self)
@@ -1043,7 +1039,28 @@ + (RCTUIColor *)UIColor:(id)json // TODO(macOS GH#750)
10431039
RCTUIColor *highContrastDarkColor = [RCTConvert UIColor:highContrastDark]; // TODO(macOS GH#750)
10441040
if (lightColor != nil && darkColor != nil) {
10451041
#if TARGET_OS_OSX
1046-
RCTDynamicColor *color = [[RCTDynamicColor alloc] initWithAquaColor:lightColor darkAquaColor:darkColor];
1042+
NSColor *color = [NSColor colorWithName:nil dynamicProvider:^NSColor * _Nonnull(NSAppearance * _Nonnull appearance) {
1043+
NSMutableArray<NSAppearanceName> *appearances = [NSMutableArray arrayWithArray:@[NSAppearanceNameAqua,NSAppearanceNameDarkAqua]];
1044+
if (highContrastLightColor != nil) {
1045+
[appearances addObject:NSAppearanceNameAccessibilityHighContrastAqua];
1046+
}
1047+
if (highContrastDarkColor != nil) {
1048+
[appearances addObject:NSAppearanceNameAccessibilityHighContrastDarkAqua];
1049+
}
1050+
NSAppearanceName bestMatchingAppearance = [appearance bestMatchFromAppearancesWithNames:appearances];
1051+
if (bestMatchingAppearance == NSAppearanceNameAqua) {
1052+
return lightColor;
1053+
} else if (bestMatchingAppearance == NSAppearanceNameDarkAqua) {
1054+
return darkColor;
1055+
} else if (bestMatchingAppearance == NSAppearanceNameAccessibilityHighContrastAqua) {
1056+
return highContrastLightColor;
1057+
} else if (bestMatchingAppearance == NSAppearanceNameAccessibilityHighContrastDarkAqua) {
1058+
return highContrastDarkColor;
1059+
} else {
1060+
RCTLogConvertError(json, @"an NSColorColor. Could not resolve current appearance. Defaulting to light.");
1061+
return lightColor;
1062+
}
1063+
}];
10471064
return color;
10481065
#else
10491066
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000

React/Base/macOS/RCTDynamicColor.h

Lines changed: 0 additions & 28 deletions
This file was deleted.

React/Base/macOS/RCTDynamicColor.m

Lines changed: 0 additions & 217 deletions
This file was deleted.

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,27 @@ function DynamicColorsExample() {
266266
}}
267267
/>
268268
</View>
269+
<View style={styles.row}>
270+
<Text style={styles.labelCell}>
271+
DynamicColorMacOS({'{\n'}
272+
{' '}light: 'red',{'\n'}
273+
{' '}dark: 'blue',{'\n'}
274+
{' '}highContrastLight: 'green',{'\n'}
275+
{' '}highContrastDark: 'orange',{'\n'}
276+
{'}'})
277+
</Text>
278+
<View
279+
style={{
280+
...styles.colorCell,
281+
backgroundColor: DynamicColorMacOS({
282+
light: 'red',
283+
dark: 'blue',
284+
highContrastLight: 'green',
285+
highContrastDark: 'orange',
286+
}),
287+
}}
288+
/>
289+
</View>
269290
</View>
270291
) : // ]TODO(macOS GH#774)
271292
Platform.OS === 'ios' ? (

0 commit comments

Comments
 (0)