Skip to content

Feat/config #1969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions scripts/buildPackages.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const packages = [
filename: 'keyboard.js',
content: `module.exports = require('./lib/components/Keyboard').default;\n`
},
{
filename: 'config.js',
content: `module.exports = require('./src/commons/Config').default;\n`
},
{
filename: 'core.js',
components: ['View', 'Text', 'Image', 'TouchableOpacity', 'Button'],
Expand Down Expand Up @@ -52,19 +56,15 @@ packages.forEach((package) => {
content += 'module.exports = {\n';
_.forEach(package.components, (component) => {
content += `get ${component}() {\n`;
content += `return require('./src/components/${_.camelCase(
component
)}').default;`;
content += `return require('./src/components/${_.camelCase(component)}').default;`;
content += `},\n`;

typings = addTyping(typings, component);
});

_.forEach(package.styleComponents, (component) => {
content += `get ${component}() {\n`;
content += `return require('./src/style/${_.camelCase(
component
)}').default;`;
content += `return require('./src/style/${_.camelCase(component)}').default;`;
content += `},\n`;

typings = addTyping(typings, component);
Expand All @@ -88,15 +88,11 @@ fs.readdir(path, (err, files) => {
files
.filter((f) => f !== 'index.js')
.forEach((file) => {
fs.writeFileSync(
`${file}.js`,
`module.exports = require('${path}/${file}').default;\n`
);
fs.writeFileSync(`${file}.js`,
`module.exports = require('${path}/${file}').default;\n`);
const componentName = _.upperFirst(file);
fs.writeFileSync(
`${file}.d.ts`,
`import {${componentName}} from './generatedTypes';\nexport default ${componentName};\n`
);
fs.writeFileSync(`${file}.d.ts`,
`import {${componentName}} from './generatedTypes';\nexport default ${componentName};\n`);
});
}
});
21 changes: 21 additions & 0 deletions src/commons/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
interface ConfigOptions {
/**
* Should use platform colors for design tokens
*/
usePlatformColors?: boolean;
}

class Config {
usePlatformColors?: boolean;

constructor() {
this.setConfig({});
}

public setConfig(options: ConfigOptions) {
const {usePlatformColors = false} = options;
this.usePlatformColors = usePlatformColors;
}
}

export default new Config();
1 change: 1 addition & 0 deletions src/commons/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export {default as forwardRef, ForwardRefInjectedProps} from './forwardRef';
export {default as withScrollEnabler, WithScrollEnablerProps} from './withScrollEnabler';
export {default as withScrollReached, WithScrollReachedProps} from './withScrollReached';
export {default as Constants} from './Constants';
export {default as Config} from './Config';

export {
ContainerModifiers,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './services';
export * from '../lib/components';
export {
asBaseComponent,
Config,
Constants,
forwardRef,
withScrollEnabler,
Expand Down
7 changes: 4 additions & 3 deletions src/style/scheme.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Appearance, PlatformColor} from 'react-native';
import {remove, xor, isEmpty, merge, forEach, cloneDeep} from 'lodash';
import Constants from '../commons/Constants';
import Config from '../commons/Config';

export type Schemes = {light: {[key: string]: string}; dark: {[key: string]: string}};
export type SchemeType = 'default' | 'light' | 'dark';
Expand All @@ -10,7 +11,6 @@ class Scheme {
private currentScheme: SchemeType = 'default';
private schemes: Schemes = {light: {}, dark: {}};
private changeListeners: SchemeChangeListener[] = [];
private usePlatformColors = false;

constructor() {
Appearance.addChangeListener(() => {
Expand Down Expand Up @@ -71,7 +71,7 @@ class Scheme {
Object.defineProperty(platformColorsSchemes[schemeKey], colorKey, {
get: () => {
let color: any = colorValue;
if (this.usePlatformColors) {
if (Config.usePlatformColors) {
if (Constants.isAndroid) {
// Remove the $ prefix cause it's not allowed in Android and add the @color prefix
color = PlatformColor(`@color/${colorKey.replace(/^[$]/, '')}`);
Expand All @@ -97,11 +97,12 @@ class Scheme {
return this.schemes[this.getSchemeType()];
}

// TODO: Remove this method, use config instead
/**
* Should use RN PlatformColor API for retrieving design token colors from native
*/
enablePlatformColors() {
this.usePlatformColors = true;
// this.usePlatformColors = true;
}

/**
Expand Down