Skip to content

Commit 6a80dd8

Browse files
committed
Create JSS-styled prototype
1 parent 6e432fe commit 6a80dd8

5 files changed

Lines changed: 175 additions & 0 deletions

File tree

src/index.jsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React from 'react'
2+
import injectSheetDefault from 'react-jss'
3+
import { pure } from 'recompose'
4+
5+
6+
type StyledElementType = Function & { Tag: string, styles: Object }
7+
8+
9+
export const prepareStyled = (injectSheet: Function = injectSheetDefault) =>
10+
(
11+
Element: string | StyledElementType,
12+
styles: Object,
13+
baseStyles: ?Object = {},
14+
): StyledElementType => {
15+
const {
16+
Tag,
17+
styles: inheritStyles = {},
18+
}: {
19+
Tag: string,
20+
styles?: Object
21+
} = typeof Element === 'string' ? { Tag: Element } : Element
22+
23+
const elementStyles = { ...inheritStyles, ...styles }
24+
25+
const StyledElement = pure(
26+
injectSheet({ [Tag]: elementStyles, ...baseStyles })(({ classes, children }) =>
27+
<Tag className={classes[Tag]}>
28+
{children}
29+
</Tag>,
30+
),
31+
)
32+
33+
return Object.assign(StyledElement, { Tag, styles: elementStyles })
34+
}
35+
36+
export const styled = prepareStyled()
37+
38+
export const setStyledCreator = (styledFunction: Function = styled) =>
39+
(baseStyles: Object) => Object.assign(
40+
(Element: string | StyledElementType, styles: Object) =>
41+
styledFunction(Element, styles, baseStyles),
42+
{ styles: baseStyles },
43+
)
44+
45+
export default setStyledCreator()

src/tests/.eslintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
rules:
2+
import/no-extraneous-dependencies:
3+
- error
4+
- devDependencies: true

src/tests/App.jsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from 'react'
2+
3+
4+
export default (styled: Function) => {
5+
const App = styled('div', {
6+
margin: '50px',
7+
})
8+
9+
const Header = styled('header', {
10+
padding: '10px',
11+
})
12+
13+
const Section = styled('section', {
14+
color: 'red',
15+
})
16+
17+
const AnotherSection = styled(Section, {
18+
color: 'yellow',
19+
})
20+
21+
const Title = styled('h1', {
22+
color: 'red',
23+
})
24+
25+
const Button = styled('button', {
26+
margin: ({ margin = 0 }) => `${margin}px`,
27+
})
28+
29+
30+
return () => (
31+
<App>
32+
<Header>
33+
<Title>Title</Title>
34+
</Header>
35+
36+
<Section data-name="content">
37+
<Button>primitive test</Button>
38+
<Button _margin={10}>dynamic primitive test</Button>
39+
</Section>
40+
41+
<AnotherSection>Another section</AnotherSection>
42+
</App>
43+
)
44+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
exports[`test renders correctly App with default Styled 1`] = `
2+
<div
3+
className="div-0-8">
4+
<header
5+
className="header-0-9">
6+
<h1
7+
className="h1-0-10">
8+
Title
9+
</h1>
10+
</header>
11+
<section
12+
className="section-0-11">
13+
<button
14+
className="button-0-13 button-0-12">
15+
primitive test
16+
</button>
17+
<button
18+
className="button-0-14 button-0-12">
19+
dynamic primitive test
20+
</button>
21+
</section>
22+
<section
23+
className="section-0-15">
24+
Another section
25+
</section>
26+
</div>
27+
`;
28+
29+
exports[`test renders correctly App with default styled 1`] = `
30+
<div
31+
className="div-0-0">
32+
<header
33+
className="header-0-1">
34+
<h1
35+
className="h1-0-2">
36+
Title
37+
</h1>
38+
</header>
39+
<section
40+
className="section-0-3">
41+
<button
42+
className="button-0-5 button-0-4">
43+
primitive test
44+
</button>
45+
<button
46+
className="button-0-6 button-0-4">
47+
dynamic primitive test
48+
</button>
49+
</section>
50+
<section
51+
className="section-0-7">
52+
Another section
53+
</section>
54+
</div>
55+
`;

src/tests/index.spec.jsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import renderer from 'react-test-renderer'
2+
import React from 'react'
3+
4+
import Styled, { styled } from '../'
5+
6+
import CreateApp from './App'
7+
8+
9+
it('renders correctly App with default styled', () => {
10+
const App = CreateApp(styled)
11+
const tree = renderer.create(<App />).toJSON()
12+
13+
expect(tree).toMatchSnapshot()
14+
})
15+
16+
it('renders correctly App with default Styled', () => {
17+
const customStyled = Styled({
18+
baseButton: {
19+
color: 'red',
20+
},
21+
})
22+
23+
const App = CreateApp(customStyled)
24+
const tree = renderer.create(<App />).toJSON()
25+
26+
expect(tree).toMatchSnapshot()
27+
})

0 commit comments

Comments
 (0)