Skip to content

Commit bfb6688

Browse files
committed
Add monocomponent
1 parent 0b7caa1 commit bfb6688

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@
7272
},
7373
{
7474
"path": "./dist/component.js",
75-
"maxSize": "0.6 kb"
75+
"maxSize": "0.7 kb"
76+
},
77+
{
78+
"path": "./dist/monolithic.js",
79+
"maxSize": "1.0 kb"
7680
}
7781
]
7882
}

src/monocomponent.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const h = require('react').createElement
2+
const PropTypes = require('prop-types')
3+
const cxs = require('./monolithic')
4+
5+
module.exports = C => (...args) => {
6+
const Comp = (props, context = {}) => {
7+
const stylePropKeys = Object.keys(Comp.propTypes || {})
8+
const styleProps = Object.assign({ theme: context.theme || {} }, props)
9+
10+
const next = {}
11+
for (let key in props) {
12+
if (stylePropKeys.includes(key)) continue
13+
next[key] = props[key]
14+
}
15+
next.className = [
16+
next.className,
17+
...args.map(a => typeof a === 'function' ? a(styleProps) : a)
18+
.filter(s => s !== null)
19+
.map(s => cxs(s))
20+
].join(' ').trim()
21+
22+
return h(C, next)
23+
}
24+
25+
Comp.contextTypes = {
26+
theme: PropTypes.oneOfType([
27+
PropTypes.object,
28+
PropTypes.func
29+
])
30+
}
31+
32+
return Comp
33+
}
34+
35+
module.exports.css = cxs.css
36+
module.exports.reset = cxs.reset

src/monolithic.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ let cache = {}
22
let prefix = '_cxs'
33
const cssRules = []
44
let insert = rule => cssRules.push(rule)
5+
56
const hyph = s => s.replace(/[A-Z]|^ms/g, '-$&').toLowerCase()
67
const mx = (rule, media) => media ? `${media}{${rule}}` : rule
7-
const rx = (cn, prop, val) => `.${cn}{${hyph(prop)}:${val}}`
88
const noAnd = s => s.replace(/&/g, '')
9-
10-
const isMedia = key => /^@/.test(key)
119
const createDeclaration = (key, value) => hyph(key) + ':' + value
1210
const createRule = ({
1311
className,
@@ -26,7 +24,7 @@ const parseRules = (obj, child = '', media) => {
2624
if (value === null) continue
2725

2826
if (typeof value === 'object') {
29-
const _media = isMedia(key) ? key : null
27+
const _media = /^@/.test(key) ? key : null
3028
const _child = _media ? child : child + noAnd(key)
3129
parseRules(value, _child, _media)
3230
.forEach(r => rules.push(r))

x.js

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

0 commit comments

Comments
 (0)