Skip to content

Commit 9c196f8

Browse files
ethansharlidord-wix
authored andcommitted
Feat/config (#1969)
* Create Config class * Create a standalone package for the config file * Use config in schemes class * Add description * Avoid declaring default config values twice
1 parent 68eafa1 commit 9c196f8

File tree

5 files changed

+37
-17
lines changed

5 files changed

+37
-17
lines changed

scripts/buildPackages.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const packages = [
66
filename: 'keyboard.js',
77
content: `module.exports = require('./lib/components/Keyboard').default;\n`
88
},
9+
{
10+
filename: 'config.js',
11+
content: `module.exports = require('./src/commons/Config').default;\n`
12+
},
913
{
1014
filename: 'core.js',
1115
components: ['View', 'Text', 'Image', 'TouchableOpacity', 'Button'],
@@ -52,19 +56,15 @@ packages.forEach((package) => {
5256
content += 'module.exports = {\n';
5357
_.forEach(package.components, (component) => {
5458
content += `get ${component}() {\n`;
55-
content += `return require('./src/components/${_.camelCase(
56-
component
57-
)}').default;`;
59+
content += `return require('./src/components/${_.camelCase(component)}').default;`;
5860
content += `},\n`;
5961

6062
typings = addTyping(typings, component);
6163
});
6264

6365
_.forEach(package.styleComponents, (component) => {
6466
content += `get ${component}() {\n`;
65-
content += `return require('./src/style/${_.camelCase(
66-
component
67-
)}').default;`;
67+
content += `return require('./src/style/${_.camelCase(component)}').default;`;
6868
content += `},\n`;
6969

7070
typings = addTyping(typings, component);
@@ -88,15 +88,11 @@ fs.readdir(path, (err, files) => {
8888
files
8989
.filter((f) => f !== 'index.js')
9090
.forEach((file) => {
91-
fs.writeFileSync(
92-
`${file}.js`,
93-
`module.exports = require('${path}/${file}').default;\n`
94-
);
91+
fs.writeFileSync(`${file}.js`,
92+
`module.exports = require('${path}/${file}').default;\n`);
9593
const componentName = _.upperFirst(file);
96-
fs.writeFileSync(
97-
`${file}.d.ts`,
98-
`import {${componentName}} from './generatedTypes';\nexport default ${componentName};\n`
99-
);
94+
fs.writeFileSync(`${file}.d.ts`,
95+
`import {${componentName}} from './generatedTypes';\nexport default ${componentName};\n`);
10096
});
10197
}
10298
});

src/commons/Config.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
interface ConfigOptions {
2+
/**
3+
* Should use platform colors for design tokens
4+
*/
5+
usePlatformColors?: boolean;
6+
}
7+
8+
class Config {
9+
usePlatformColors?: boolean;
10+
11+
constructor() {
12+
this.setConfig({});
13+
}
14+
15+
public setConfig(options: ConfigOptions) {
16+
const {usePlatformColors = false} = options;
17+
this.usePlatformColors = usePlatformColors;
18+
}
19+
}
20+
21+
export default new Config();

src/commons/new.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export {default as forwardRef, ForwardRefInjectedProps} from './forwardRef';
55
export {default as withScrollEnabler, WithScrollEnablerProps} from './withScrollEnabler';
66
export {default as withScrollReached, WithScrollReachedProps} from './withScrollReached';
77
export {default as Constants} from './Constants';
8+
export {default as Config} from './Config';
89

910
export {
1011
ContainerModifiers,

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './services';
55
export * from '../lib/components';
66
export {
77
asBaseComponent,
8+
Config,
89
Constants,
910
forwardRef,
1011
withScrollEnabler,

src/style/scheme.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Appearance, PlatformColor} from 'react-native';
22
import {remove, xor, isEmpty, merge, forEach, cloneDeep} from 'lodash';
33
import Constants from '../commons/Constants';
4+
import Config from '../commons/Config';
45

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

1515
constructor() {
1616
Appearance.addChangeListener(() => {
@@ -71,7 +71,7 @@ class Scheme {
7171
Object.defineProperty(platformColorsSchemes[schemeKey], colorKey, {
7272
get: () => {
7373
let color: any = colorValue;
74-
if (this.usePlatformColors) {
74+
if (Config.usePlatformColors) {
7575
if (Constants.isAndroid) {
7676
// Remove the $ prefix cause it's not allowed in Android and add the @color prefix
7777
color = PlatformColor(`@color/${colorKey.replace(/^[$]/, '')}`);
@@ -97,11 +97,12 @@ class Scheme {
9797
return this.schemes[this.getSchemeType()];
9898
}
9999

100+
// TODO: Remove this method, use config instead
100101
/**
101102
* Should use RN PlatformColor API for retrieving design token colors from native
102103
*/
103104
enablePlatformColors() {
104-
this.usePlatformColors = true;
105+
// this.usePlatformColors = true;
105106
}
106107

107108
/**

0 commit comments

Comments
 (0)