Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
feat: expose mapThemeProps
Browse files Browse the repository at this point in the history
expose mapThemeProps to allow all themer-based decorators to apply theme and variants props
consistently
  • Loading branch information
Biavati, Alessandro authored and Biavati, Alessandro committed Feb 28, 2017
1 parent ecd4b3d commit 91ef19a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/decorator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export function createDecorator(customThemer) {
const themerInstance = customThemer || themer;
return rawTheme => inputSnippet => {
const { snippet, theme } = themerInstance.resolveAttributes(inputSnippet, [rawTheme]);
return mapThemeProps(snippet, theme);
return (props) => snippet(mapThemeProps(props, theme));
};
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* of the MIT license. See the LICENSE file for details.
*/

import { createThemer as create } from './utils';
import { createThemer as create, mapThemeProps } from './utils';
import { createDecorator } from './decorator';

const themer = create();

export { themer, create, createDecorator };
export { themer, create, createDecorator, mapThemeProps };

export default createDecorator(themer);
14 changes: 6 additions & 8 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,10 @@ export function applyVariantsProps(props, resolvedTheme) {
return { ...mappedProps, theme: mappedTheme };
}

export function mapThemeProps(snippet, resolvedTheme) {
return (props) => {
if (resolvedTheme.variants && resolvedTheme.styles) {
const variantsProps = applyVariantsProps(props, resolvedTheme);
return snippet(variantsProps);
}
return snippet({ ...props, theme: resolvedTheme });
};
export function mapThemeProps(props, resolvedTheme) {
if (resolvedTheme.variants && resolvedTheme.styles) {
const variantsProps = applyVariantsProps(props, resolvedTheme);
return variantsProps;
}
return { ...props, theme: resolvedTheme };
}
8 changes: 7 additions & 1 deletion tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* of the MIT license. See the LICENSE file for details.
*/

import themerDecorator, { create, themer as themerInstance } from '../src';
import themerDecorator, { create, themer as themerInstance, mapThemeProps } from '../src';
import Themer from '../src/Themer';
import { testThemeSimple, snippet } from './fixtures';

Expand Down Expand Up @@ -39,4 +39,10 @@ describe('exports', () => {
expect(mockMiddleware).toHaveBeenCalledTimes(1);
});
});

describe('mapThemeProps', () => {
it('should export mapThemeProps function that correctly maps resolved theme to snippet props', () => {
expect(typeof mapThemeProps).toBe('function');
});
});
});
16 changes: 11 additions & 5 deletions tests/utils/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
applyVariantsProps,
mapThemeProps,
} from '../../src/utils';
import { snippet } from '../fixtures';

describe('utils', () => {
describe('createThemer', () => {
Expand Down Expand Up @@ -109,15 +108,22 @@ describe('utils', () => {
it('should be a funciton', () => {
expect(typeof mapThemeProps).toBe('function');
});

it('should map theme prop', () => {
const testResolvedTheme = { styles: { root: 'root-class-123' } };
const mappedSnippet = mapThemeProps(snippet, testResolvedTheme);
expect(mappedSnippet({ content: 'Hello' })).toBe('<h1 class="root-class-123">Hello</h1>');
const testProps = { content: 'Hello' };
const mappedProps = mapThemeProps(testProps, testResolvedTheme);
expect(mappedProps).toEqual({ ...testProps, theme: testResolvedTheme });
});

it('should map variants props, if defined', () => {
const testResolvedTheme = { styles: { root: 'root-class-123', test: 'test-123' }, variants: { test: true } };
const mappedSnippet = mapThemeProps(snippet, testResolvedTheme);
expect(mappedSnippet({ content: 'Hello', test: true })).toBe('<h1 class="root-class-123 test-123">Hello</h1>');
const testProps = { content: 'Hello', test: true };
const mappedProps = mapThemeProps(testProps, testResolvedTheme);
expect(mappedProps).toEqual({
content: 'Hello',
theme: { styles: { root: 'root-class-123 test-123', test: 'test-123' }, variants: { test: true } },
});
});
});
});

0 comments on commit 91ef19a

Please sign in to comment.