Skip to content

Commit f3cbba3

Browse files
committed
Add theming support
1 parent 71d141f commit f3cbba3

File tree

7 files changed

+64
-101
lines changed

7 files changed

+64
-101
lines changed

ThemeProvider.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dist/ThemeProvider')

docs/App.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
import React from 'react'
22
import cxs from 'cxs/component'
3+
import ThemeProvider from 'cxs/ThemeProvider'
4+
import theme from './theme'
35
import Container from './Container'
46
import Header from './Header'
57
import About from './About'
68
import Usage from './Usage'
79
import CTA from './CTA'
810
import Footer from './Footer'
911

10-
const Root = cxs('div')({
12+
const Root = cxs('div')(props => ({
1113
fontFamily: '"Roboto Mono", Menlo, monospace',
14+
color: typeof props.theme,
1215
lineHeight: '1.5'
13-
})
16+
}))
1417

1518
const App = props => (
16-
<Root>
17-
<Container>
18-
<Header />
19-
<About />
20-
<Usage />
21-
<CTA />
22-
<Footer />
23-
</Container>
24-
</Root>
19+
<ThemeProvider theme={theme}>
20+
<Root>
21+
<Container>
22+
<Header />
23+
<About />
24+
<Usage />
25+
<CTA />
26+
<Footer />
27+
</Container>
28+
</Root>
29+
</ThemeProvider>
2530
)
2631

2732
export default App

docs/Box.js

+2-88
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,11 @@
11
import cxs from 'cxs/component'
22
import colors from './colors'
3-
4-
const reg = /^[mp][trblxy]?$/
5-
6-
const scale = [
7-
0,
8-
4,
9-
8,
10-
16,
11-
32,
12-
64,
13-
128
14-
]
15-
16-
const scaleVal = n => {
17-
if (typeof n !== 'number') return n
18-
const neg = n < 0 ? -1 : 1
19-
const i = Math.abs(n)
20-
return neg * (scale[i] || i)
21-
}
22-
23-
const space = props => {
24-
const decs = []
25-
26-
for (let key in props) {
27-
if (!reg.test(key)) continue
28-
const [ a, b ] = key
29-
const prop = [ properties[a], directions[b] ]
30-
.filter(n => !!n)
31-
.join('')
32-
const val = scaleVal(props[key])
33-
decs.push({ [prop]: val })
34-
}
35-
36-
return decs.reduce((a, b) => Object.assign(a, b), {})
37-
return decs.join(';')
38-
}
39-
40-
const properties = {
41-
m: 'margin',
42-
p: 'padding'
43-
}
44-
45-
const directions = {
46-
t: 'Top',
47-
r: 'Right',
48-
b: 'Bottom',
49-
l: 'Left',
50-
}
51-
52-
const per = n => typeof n !== 'number' || n > 1 ? n : (n * 100) + '%'
53-
54-
const width = props => props.w
55-
? { width: per(props.w) }
56-
: null
57-
58-
const color = props => props.color
59-
? { color: colors[props.color] || props.color }
60-
: null
61-
62-
const bg = props => props.bg
63-
? { backgroundColor: colors[props.bg] || props.bg }
64-
: null
65-
66-
const opts = {
67-
removeProps: [
68-
'w',
69-
'color',
70-
'bg',
71-
'm',
72-
'mt',
73-
'mr',
74-
'mb',
75-
'ml',
76-
'p',
77-
'pt',
78-
'pr',
79-
'pb',
80-
'pl',
81-
'f',
82-
'bold',
83-
'wrap',
84-
'column',
85-
'justify',
86-
'align',
87-
]
88-
}
3+
import { space, width, color } from 'styled-system'
894

905
const Box = cxs('div')(
916
space,
927
width,
93-
color,
94-
bg
8+
color
959
)
9610

9711
export default Box

docs/theme.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const space = [
2+
0,
3+
4,
4+
8,
5+
16,
6+
32,
7+
64,
8+
128
9+
]
10+
11+
module.exports = {
12+
space
13+
}

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"react-test-renderer": "^15.6.1",
3939
"standard": "^10.0.2",
4040
"static-react": "^4.0.0-2",
41+
"styled-system": "^1.0.0-12",
4142
"webpack": "^3.3.0",
4243
"webpack-dev-server": "^2.6.0"
4344
},
@@ -68,6 +69,10 @@
6869
{
6970
"path": "./dist/index.js",
7071
"maxSize": "0.8 kb"
72+
},
73+
{
74+
"path": "./dist/component.js",
75+
"maxSize": "0.6 kb"
7176
}
7277
],
7378
"static-react": {

src/ThemeProvider.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const React = require('react')
2+
const PropTypes = require('prop-types')
3+
const h = React.createElement
4+
5+
class ThemeProvider extends React.Component {
6+
getChildContext () {
7+
return {
8+
theme: this.props.theme
9+
}
10+
}
11+
12+
render () {
13+
return (
14+
h('div', null, this.props.children)
15+
)
16+
}
17+
}
18+
19+
ThemeProvider.childContextTypes = {
20+
theme: PropTypes.object
21+
}
22+
23+
module.exports = ThemeProvider

src/component.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ const PropTypes = require('prop-types')
33
const cxs = require('./index')
44

55
module.exports = C => (...args) => {
6-
const Comp = props =>
6+
const Comp = (props, context) =>
77
h(C, Object.assign({}, props, {
88
className: [
99
props.className,
10-
...args.map(a => typeof a === 'function' ? a(props) : a)
10+
...args.map(a => typeof a === 'function'
11+
? a(Object.assign({ theme: context.theme }, props))
12+
: a)
1113
.filter(s => s !== null)
1214
.map(s => cxs(s))
1315
].join(' ')

0 commit comments

Comments
 (0)