Skip to content

Commit f96a064

Browse files
committed
Add ThemeProvider, fix Readme
1 parent 5d02fc0 commit f96a064

3 files changed

Lines changed: 67 additions & 29 deletions

File tree

README.md

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,23 @@ const ButtonContainer = styled(Container)({
5353
`styled-jss` has out of the box support for theme customization with the unified [theming](https://github.com/cssinjs/theming) package.
5454

5555
```js
56-
import {ThemeProvider} from 'theming'
57-
import styled from 'styled-jss'
56+
import styled, {ThemeProvider} from 'styled-jss'
5857

59-
const Button = styled('button')(({theme}) => ({
58+
const Button = styled('button')(({margin, theme}) => ({
59+
margin,
6060
color: theme.color,
61-
'background-color': theme.backgroundColor,
62-
margin: props.margin,
61+
backgroundColor: theme.backgroundColor,
6362
}))
6463

65-
const currentTheme = {
66-
primary: {
64+
const themes = {
65+
light: {
6766
color: 'black',
6867
backgroundColor: 'yellow',
6968
},
7069
}
7170

7271
const App = () => (
73-
<ThemeProvider theme={currentTheme}>
72+
<ThemeProvider theme={themes.light}>
7473
<Button margin={20}>This is themed Button</Button>
7574
</ThemeProvider>
7675
)
@@ -80,46 +79,83 @@ export default App
8079

8180
## Composable styles
8281

83-
You can compose your style-objects and style-functions.
82+
Example on the [CodeSandbox](https://codesandbox.io/s/y0162p38lv)
8483

85-
```js
86-
import colors from 'my-colors'
84+
You can compose your style-objects and style-functions.
8785

88-
/* let's declare some abstract mixins for example */
86+
Let's say this is our **mods.js**:
8987

90-
const theme = ({theme}) => ({
91-
color: colors[theme],
92-
backgroundColor: colors.accent[theme],
88+
```js
89+
export const theme = ({ theme }) => ({
90+
color: theme.colors.primary,
91+
backgroundColor: theme.colors.secondary,
9392
})
9493

95-
const font = ({bold}) => ({
96-
font: {weight: bold ? 'bold' : 'normal', family: 'Arial'}
94+
export const font = ({ bold }) => ({
95+
font: {
96+
weight: bold ? 'bold' : 'normal',
97+
family: 'Arial',
98+
},
9799
})
98100

99-
const size = ({size}) => ({
101+
export const size = ({ size = 'm' }) => ({
100102
s: {
101103
fontSize: 12,
102-
lineHeight: 15,
104+
lineHeight: 1.2,
103105
},
104106
m: {
105107
fontSize: 16,
106-
lineHeight: 18
108+
lineHeight: 1.5
107109
}
108110
})[size]
109111

110-
const rounded = ({rounded}) => rounded && {borderRadius: 5}
112+
export const rounded = ({ rounded }) => rounded && { borderRadius: 5 }
113+
```
111114

112-
/* now we can mix them to our Button Component */
115+
Now we can mix them to our **Button** Component:
113116

114-
const Button = styled('button')(theme, size, font, rounded)
117+
```js
118+
import styled from 'styled-jss'
119+
import {theme, font, size, rounded} from 'mods'
115120

116-
/* and that's it */
121+
const Button = styled('button')(
122+
{
123+
border: 0,
124+
padding: [5, 10],
125+
display: 'inline-block',
126+
},
127+
theme,
128+
font,
129+
size,
130+
rounded,
131+
)
117132

118-
<Button theme="action" size="s" rounded />
133+
export default Button
134+
```
119135

120-
/* we can also compose object-styles as well */
136+
And Usage:
121137

122-
const Button = styled('button')({margin: props => props.margin}, theme, size)
138+
```js
139+
import {ThemeProvider} from 'styled-jss'
140+
import Button from './components/Button'
141+
142+
const theme = {
143+
dark: {
144+
colors: {
145+
primary: 'white',
146+
secondary: 'purple'
147+
}
148+
}
149+
}
150+
151+
export default () => (
152+
<ThemeProvider theme={theme.dark}>
153+
<Button>normal button</Button>
154+
<Button bold>bold button</Button>
155+
<Button size="s">small button</Button>
156+
<Button rounded>rounded button</Button>
157+
</ThemeProvider>
158+
)
123159
```
124160

125161
## Base Style Sheet

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import createStyled from './createStyled'
55

66
const jss: Function = createJss(preset())
77

8+
export {ThemeProvider} from 'theming'
9+
810
export const Styled = createStyled(jss)
911
export default Styled()
1012

src/tests/functional.spec.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'react-dom'
22
import React from 'react'
33
import Observable from 'zen-observable'
4-
import {ThemeProvider} from 'theming'
54
import Enzyme, {mount} from 'enzyme'
65
import Adapter from 'enzyme-adapter-react-16'
76

@@ -16,6 +15,7 @@ Enzyme.configure({adapter: new Adapter()})
1615

1716
let Styled
1817
let styled
18+
let ThemeProvider
1919

2020
const mockNameGenerators = () => {
2121
let styledCounter = 0
@@ -46,7 +46,7 @@ describe('functional tests', () => {
4646
beforeEach(() => {
4747
mockNameGenerators()
4848

49-
Styled = require('../').Styled
49+
;({ThemeProvider, Styled} = require('../'))
5050
styled = Styled()
5151
})
5252

0 commit comments

Comments
 (0)