Skip to content

Commit 5b10bd6

Browse files
authored
Support using PlatformColor for designTokens (#1939)
* Support using PlatformColor for designTokens * Avoid loading dark scheme tokens * Fix scheme platformColor logic * Fix how we consume platform color from Android * Fix constants import to fix tests
1 parent b20750a commit 5b10bd6

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/style/colors.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Color from 'color';
44
import tinycolor from 'tinycolor2';
55
import {colorsPalette, themeColors} from './colorsPalette';
66
import DesignTokens from './designTokens';
7-
import DesignTokensDM from './designTokensDM';
7+
// import DesignTokensDM from './designTokensDM';
88
//@ts-ignore
99
import ColorName from './colorName';
1010
import Scheme, {Schemes, SchemeType} from './scheme';
@@ -14,8 +14,11 @@ export class Colors {
1414
private shouldSupportDarkMode = false;
1515

1616
constructor() {
17-
const colors = Object.assign(colorsPalette, DesignTokens, themeColors);
17+
const colors = Object.assign(colorsPalette, themeColors);
1818
Object.assign(this, colors);
19+
// TODO: For now we load the same design tokens for both schemes to not force it until it's ready
20+
this.loadSchemes({light: DesignTokens, dark: DesignTokens});
21+
// this.loadSchemes({light: DesignTokens, dark: DesignTokensDM});
1922

2023
Scheme.addChangeListener(() => {
2124
Object.assign(this, Scheme.getScheme());
@@ -62,9 +65,14 @@ export class Colors {
6265
* and change the design tokens accordingly
6366
*/
6467
supportDarkMode() {
65-
const designTokensColors = Scheme.getSchemeType() === 'dark' ? DesignTokensDM : DesignTokens;
6668
this.shouldSupportDarkMode = true;
67-
Object.assign(this, designTokensColors);
69+
}
70+
71+
/**
72+
* Should use RN PlatformColor API for retrieving design token colors from native
73+
*/
74+
enablePlatformColors() {
75+
Scheme.enablePlatformColors();
6876
}
6977

7078
/**

src/style/scheme.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {Appearance} from 'react-native';
2-
import {remove, xor, isEmpty, merge} from 'lodash';
1+
import {Appearance, PlatformColor} from 'react-native';
2+
import {remove, xor, isEmpty, merge, forEach, cloneDeep} from 'lodash';
3+
import Constants from '../commons/Constants';
34

45
export type Schemes = {light: {[key: string]: string}; dark: {[key: string]: string}};
56
export type SchemeType = 'default' | 'light' | 'dark';
@@ -9,6 +10,7 @@ class Scheme {
910
currentScheme: SchemeType = 'default';
1011
schemes: Schemes = {light: {}, dark: {}};
1112
changeListeners: SchemeChangeListener[] = [];
13+
private usePlatformColors = false;
1214

1315
constructor() {
1416
Appearance.addChangeListener(() => {
@@ -61,7 +63,29 @@ class Scheme {
6163
throw new Error(`There is a mismatch in scheme keys: ${missingKeys.join(', ')}`);
6264
}
6365

64-
merge(this.schemes, schemes);
66+
const platformColorsSchemes: Schemes = cloneDeep(schemes);
67+
68+
forEach(schemes, (scheme, schemeKey) => {
69+
forEach(scheme, (colorValue, colorKey) => {
70+
// @ts-expect-error
71+
Object.defineProperty(platformColorsSchemes[schemeKey], colorKey, {
72+
get: () => {
73+
if (this.usePlatformColors) {
74+
if (Constants.isAndroid) {
75+
// Remove the $ prefix cause it's not allowed in Android and add the @color prefix
76+
return PlatformColor(`@color/${colorKey.replace(/^[$]/, '')}`);
77+
} else {
78+
return PlatformColor(colorKey);
79+
}
80+
} else {
81+
return colorValue;
82+
}
83+
}
84+
});
85+
});
86+
});
87+
88+
merge(this.schemes, platformColorsSchemes);
6589
}
6690

6791
/**
@@ -71,6 +95,13 @@ class Scheme {
7195
return this.schemes[this.getSchemeType()];
7296
}
7397

98+
/**
99+
* Should use RN PlatformColor API for retrieving design token colors from native
100+
*/
101+
enablePlatformColors() {
102+
this.usePlatformColors = true;
103+
}
104+
74105
/**
75106
* Add a change scheme event listener
76107
*/

0 commit comments

Comments
 (0)