Skip to content

Commit

Permalink
feat(theme): referencing values (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
artyorsh authored and 32penkin committed May 22, 2019
1 parent 1b90281 commit cbe5056
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
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

0 comments on commit cbe5056

Please sign in to comment.