Skip to content

Commit

Permalink
Merge pull request #30 from quarkly/fix/theme-props
Browse files Browse the repository at this point in the history
fix: theme props
  • Loading branch information
Mobydack authored May 27, 2021
2 parents f1f901e + 4588e6b commit 635d243
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/atomic-styles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ export const makeComponent = (styled, tag, styles, config, other) => {
const rules = isArray(other) ? other : [];
const cleanProps = typeof tag === 'string';

const denieList = ['cssObject'];
if (cleanProps) denieList.push('theme');

const Component = normalize(
styled(tag).withConfig({
shouldForwardProp: prop => !['cssObject'].includes(prop),
shouldForwardProp: prop => !denieList.includes(prop),
})(rules, props => {
const { cssObject } = props;
return cssObject;
Expand Down
4 changes: 3 additions & 1 deletion src/utils/normalize-props.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { isString, isObject } from 'lodash/fp';
import { useTheme } from 'styled-components';

const normalizeMap = {
as(value) {
Expand All @@ -22,7 +23,8 @@ export const normalizer = props => {

export default (Tag, cb, cleanProps) =>
React.forwardRef((props, ref) => {
const { cssObject, cleanedProps } = cb(props);
const theme = useTheme(props);
const { cssObject, cleanedProps } = cb({ theme, ...props });
return React.createElement(Tag, {
ref,
...normalizer(cleanProps ? cleanedProps : props),
Expand Down
55 changes: 55 additions & 0 deletions test/sc-integration/clear-props.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import renderer from 'react-test-renderer';
import 'jest-styled-components';
import { ThemeProvider } from 'styled-components';
import atomize from '../../src';

const Element = atomize('div')({
Expand Down Expand Up @@ -69,4 +70,58 @@ describe('atomize filter props keys', () => {
/>
`);
});
test('test props theme pass', () => {
const tree = renderer
.create(
<ThemeProvider theme={{ breakpoints: { kek: [{ type: 'max-width', value: '1280' }] } }}>
<ElementWithComponent kek-color="blue" color="red" color_passed="red" />
</ThemeProvider>,
)
.toJSON();

expect(tree).toMatchInlineSnapshot(`
.c0 {
color: red;
}
@media (max-width:1280px) {
.c0 {
color: blue;
}
}
<div
className="c0"
color="red"
color_passed="red"
kek-color="blue"
/>
`);
});
test('test props theme', () => {
const tree = renderer
.create(
<ThemeProvider theme={{ breakpoints: { kek: [{ type: 'max-width', value: '1280' }] } }}>
<Element kek-color="blue" color="red" color_passed="red" />
</ThemeProvider>,
)
.toJSON();

expect(tree).toMatchInlineSnapshot(`
.c0 {
color: red;
}
@media (max-width:1280px) {
.c0 {
color: blue;
}
}
<div
className="c0"
color_passed="red"
/>
`);
});
});

0 comments on commit 635d243

Please sign in to comment.