Skip to content

Commit

Permalink
feat(design-system-utils): Enable color to accept a path
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoreis authored and mrmartineau committed Feb 27, 2019
1 parent 0b3e88d commit 4ef20e1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ This is a short-hand for the `tokens.get()` function.
// Get values like this:
tokens.color('bright') // #F9FAFB - the `base` key is the default, so it is not needed
tokens.color('bright', 'dark')
tokens.color('background.extra.dark') // Accepts a path (in this case the second `variant` argument is ignored)
```

#### `tokens.brand()` - Get brand palette values
Expand Down
2 changes: 2 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ describe('design-system-utils methods', () => {
test('color', () => {
expect(ds1.color('primary')).toBe('#181830')
expect(ds1.color('secondary', 'light')).toBe('#fea04c')
expect(ds1.color('v2.secondary.light')).toBe('#fea04c')
expect(ds1.color('v2.secondary.deep.nested.light')).toBe('#fea04c')

// Errors
expect(() => ds1.color('text', 'dark')).toThrow(
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ export default class DesignSystem<T extends System, K extends SystemOptions> {
/**
* color()
* get a color from your color palette
* `hue`: can contain a path to be traversed (eg: 'base.background.light'),
* in that case the `variant` argument is ignored
*/
public color(hue: string, variant: string = 'base'): string {
const location = 'colors.colorPalette'
Expand All @@ -220,7 +222,11 @@ export default class DesignSystem<T extends System, K extends SystemOptions> {
throw new Error(MissingParent(location))
}

const value: string | undefined = this.ds.colors.colorPalette[hue][variant]
const isMultiPathHue = hue.split('.').length > 1;

const value: string | undefined = isMultiPathHue ?
this.get(hue, this.ds.colors.colorPalette) :
this.ds.colors.colorPalette[hue][variant]

if (value === undefined) {
throw new Error(MissingKey(location, hue, variant))
Expand Down
11 changes: 11 additions & 0 deletions src/testData/ds1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ const DesignSystem1: MySystem = {
light: '#fea04c',
dark: '#d26401',
},

v2: {
secondary: {
light: '#fea04c',
deep: {
nested: {
light: '#fea04c',
}
}
}
}
},

brand: {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface SystemBreakpoints {

export interface SystemColorPalette {
[name: string]: {
[variant: string]: string
[variant: string]: string | object
}
}

Expand Down

0 comments on commit 4ef20e1

Please sign in to comment.