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

add support for prop value in mapToTheme #19

Merged
merged 3 commits into from
May 15, 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
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));
scf4 marked this conversation as resolved.
Show resolved Hide resolved

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;