Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #19 from mmartinsky/master
Browse files Browse the repository at this point in the history
add support for prop value in mapToTheme
  • Loading branch information
Scott authored May 15, 2019
2 parents faf8a5c + 6009242 commit 040170b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,27 @@ const Button = styled.button`

`styled-map` will then look at the Button's `type` prop for a matching value.

This also works in `mapToTheme`:
```js
import styledMap, { mapToTheme as theme } from 'styled-map';

const myTheme = {
buttonColor: {
primary: 'orange',
warning: 'red',
info: 'blue',
default: 'white',
},
...
};

const Button = styled.button`
color: ${theme('buttonColor', 'kind')};
`;

<Button kind='warning'>Click</Button> // will be red
```

**Note: This currently doesn't work doesn't work with the pseudo-CSS syntax. This functionality should arrive by v4.0. PRs welcome!**:

## Typings
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const _dotProp = (string, object) => string
.split('.')
.reduce((acc, key) => acc[key], object);

export const mapToTheme = (key) =>
(props) => styledMap(_dotProp(key, props.theme));
export const mapToTheme = (key, prop) =>
(props) => prop ? styledMap(prop, _dotProp(key, props.theme)) : styledMap(_dotProp(key, props.theme));

export default styledMap;
6 changes: 6 additions & 0 deletions lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ describe('mapToTheme', () => {
});
expect(result).toEqual(_dotProp('button.foreground.primary', nestedTheme));
});
it('uses the prop value when passed a second argument', () => {
const result = mapToTheme('buttonColor', 'kind')({theme})({
kind: 'other'
});
expect(result).toEqual('#aab');
})
});

describe('_convertToObject', () => {
Expand Down
2 changes: 1 addition & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare const styledMap: (...params: any[]) => (props: any) => any;

export const mapToTheme: (key: string) => (props: any) => any;
export const mapToTheme: (key: string, prop?: string) => (props: any) => any;

export default styledMap;

0 comments on commit 040170b

Please sign in to comment.