Skip to content
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

feat(theme): referencing values #398

Merged
merged 2 commits into from
May 22, 2019
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
1 change: 1 addition & 0 deletions src/framework/theme/application/application.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2657,6 +2657,7 @@ exports[`@app: application wrapper check * renders properly 1`] = `
"gray-light": "#DDE1EB",
"gray-primary": "#A6AEBD",
"pink-primary": "#FF3D71",
"referencing": "$gray-100",
"text-primary": "#000000",
"text-primary-inverse": "#ffffff",
}
Expand Down
4 changes: 3 additions & 1 deletion src/framework/theme/support/tests/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
"gray-100": "#f7f8fa",
"gray-200": "#edf0f5",
"gray-300": "#c8cedb",
"gray-400": "#a6aebd"
"gray-400": "#a6aebd",

"referencing": "$gray-100"
}
30 changes: 29 additions & 1 deletion src/framework/theme/theme/theme.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ThemeType } from './type';

const SYMBOL_REFERENCE: string = '$';

/**
* @param name: string - theme property name, like `backgroundColor`
* @param theme: ThemeType - theme
Expand All @@ -8,5 +10,31 @@ import { ThemeType } from './type';
* @return any. Theme property value if it presents in theme, fallback otherwise
*/
export function getThemeValue(name: string, theme: ThemeType, fallback?: any): any | undefined {
return theme[name] || fallback;
return findThemeValue(name, theme) || fallback;
}

function findThemeValue(name: string, theme: ThemeType): any | undefined {
const value: any = theme[name];

if (isReferenceKey(value)) {
const themeKey: string = toThemeKey(value);

return findThemeValue(themeKey, theme);
}

return value;
}

/**
* @returns true if theme value references to another
*/
function isReferenceKey(value: any): boolean {
return `${value}`.startsWith(SYMBOL_REFERENCE);
}

/**
* Transforms reference key to theme key
*/
function toThemeKey(value: any): string {
return `${value}`.substring(1);
}
6 changes: 6 additions & 0 deletions src/framework/theme/theme/theme.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ describe('@theme: service method checks', () => {
expect(undefinedValue).toBeUndefined();
});

it('finds referencing theme value properly', async () => {
const themeValue = getThemeValue('referencing', theme);

expect(themeValue).toEqual(theme['gray-100']);
});

});

describe('@theme: ui component checks', () => {
Expand Down