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

default variant #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
*.log
example/bundle.js
dist
.idea
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,24 @@ Button.defaultProps = {
<Button variant="success"/>
<Button variant="warning"/>
```

if an undefined variant arrives, the default variant is used.

```js
import styled from 'styled-components';
import theme from 'styled-theming';

const backgroundColor = theme.variants('mode', 'variant', {
default: { light: 'gray', dark: 'darkgray' },
primary: { light: 'blue', dark: 'darkblue' },
success: { light: 'green', dark: 'darkgreen' },
warning: { light: 'orange', dark: 'darkorange' },
});

const Button = styled.button`
background-color: ${backgroundColor};
`;

<Button variant="noVariant"/> // default
<Button variant="default"/> // default
```
1 change: 1 addition & 0 deletions example/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default class App extends React.Component {
<Button kind="success" onClick={this.handleToggleMode}>Toggle Mode</Button>
<Button kind="warning" onClick={this.handleToggleSize}>Toggle Size</Button>
<Button kind="danger" onClick={this.handleToggleMode}>Toggle Mode</Button>
<Button kind="noKind" onClick={this.handleToggleMode}>Toggle Size</Button>
</Box>
</ThemeProvider>
);
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ function theme(name, values) {
theme.variants = function(name, prop, values) {
return function(props) {
var variant = props[prop] && values[props[prop]];
return variant && getThemeValue(name, props, variant);
var defaultVariant = props[prop] && values['default'];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that there is 'default' key in the variants object. In few use cases we may not be having 'default' as one of the item in the list of variants. In such case, I suggest to use the first item from the list of variants as a fallback.


return variant
? getThemeValue(name, props, variant)
: getThemeValue(name, props, defaultVariant)
};
};

Expand Down
33 changes: 33 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,36 @@ describe('theme.variants()', () => {
expect(fn({ kind: 'fancy', theme: { mode: themes => themes.dark } })).toBe('#0f0');
});
});

describe('theme.variants()', () => {
const fn = theme.variants('mode', 'kind', {
default: { light: '#fff', dark: '#000' },
fancy: { light: '#f0f', dark: '#0f0' },
});

it('should create a function that returns a matching prop when passed a string', () => {
expect(fn({ kind: 'default', theme: { mode: 'light' } })).toBe('#fff');
expect(fn({ kind: 'default', theme: { mode: 'dark' } })).toBe('#000');
expect(fn({ kind: 'fancy', theme: { mode: 'light' } })).toBe('#f0f');
expect(fn({ kind: 'fancy', theme: { mode: 'dark' } })).toBe('#0f0');
});

it('should create a function that returns calls a passed function', () => {
expect(fn({ kind: 'default', theme: { mode: themes => themes.light } })).toBe('#fff');
expect(fn({ kind: 'default', theme: { mode: themes => themes.dark } })).toBe('#000');
expect(fn({ kind: 'fancy', theme: { mode: themes => themes.light } })).toBe('#f0f');
expect(fn({ kind: 'fancy', theme: { mode: themes => themes.dark } })).toBe('#0f0');
});
});

describe('defaultVariant', () => {
const fn = theme.variants('mode', 'kind', {
default: { light: '#fff', dark: '#000' },
fancy: { light: '#f0f', dark: '#0f0' },
});

it('should create a function that returns a matching prop when passed a string', () => {
expect(fn({ kind: 'test', theme: { mode: 'light' } })).toBe('#fff');
expect(fn({ kind: 'test', theme: { mode: 'dark' } })).toBe('#000');
});
});