-
Notifications
You must be signed in to change notification settings - Fork 7
/
createStyle.js
116 lines (97 loc) · 3.02 KB
/
createStyle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const getDefaultExport = require('./lib/getDefaultExport')
const PropTypes = require('prop-types')
const { createRenderer } = require('fela')
const { Provider: FelaProvider, ThemeProvider: FelaThemeProvider } = require('react-fela')
const h = require('react-hyperscript')
const MuiThemeProvider = getDefaultExport(require('@material-ui/core/styles/MuiThemeProvider'))
const createMuiTheme = getDefaultExport(require('@material-ui/core/styles/createMuiTheme'))
// TODO publish preset `fela-preset-dogstack`
// plugins and enhancers from https://github.com/cloudflare/cf-ui/blob/master/packages/cf-style-provider/src/index.js#L40
const fallbackValue = getDefaultExport(require('fela-plugin-fallback-value'))
const lvha = getDefaultExport(require('fela-plugin-lvha'))
const validator = getDefaultExport(require('fela-plugin-validator'))
const beautifier = getDefaultExport(require('fela-beautifier'))
const monolithic = getDefaultExport(require('fela-monolithic'))
const prefixer = getDefaultExport(require('fela-plugin-prefixer'))
module.exports = {
createStyleRenderer,
StyleProvider
}
function createStyleRenderer (options) {
const {
plugins: userPlugins = [],
enhancers: userEnhancers = [],
setup = noop,
prod = process.env.NODE_ENV === 'production',
dev = !prod,
selectorPrefix
} = options
// plugin order matters!
// https://github.com/rofrischmann/fela/blob/master/docs/advanced/Plugins.md#order-matters
var defaultPlugins = []
var defaultEnhancers = []
if (dev) {
defaultPlugins = [lvha(), fallbackValue(), validator()]
defaultEnhancers = [beautifier(), monolithic()]
}
if (prod) {
defaultPlugins = [lvha(), prefixer(), fallbackValue()]
}
const plugins = [...defaultPlugins, ...userPlugins]
const enhancers = [...defaultEnhancers, ...userEnhancers]
const renderer = createRenderer({
plugins,
enhancers,
selectorPrefix
})
setup(renderer)
return renderer
}
function StyleProvider (options) {
const {
renderer,
theme,
children
} = options
const muiTheme = themeToMuiTheme(theme)
return (
h(FelaProvider, {
renderer
}, [
h(FelaThemeProvider, {
theme
}, [
h(MuiThemeProvider, {
theme: muiTheme
}, children)
])
])
)
}
StyleProvider.defaultProps = {
theme: {}
}
StyleProvider.propTypes = {
renderer: PropTypes.object,
theme: PropTypes.object,
children: PropTypes.node.isRequired
}
function noop () {}
function themeToMuiTheme (theme) {
return createMuiTheme({
fontFamily: theme.fontFamily,
palette: {
primary1Color: theme.colors.primary1,
primary2Color: theme.colors.primary2,
primary3Color: theme.colors.primary3,
accent1Color: theme.colors.accent1,
accent2Color: theme.colors.accent2,
accent3Color: theme.colors.accent3,
textColor: theme.colors.text,
alternateTextColor: theme.colors.alternateText,
canvasColor: theme.colors.canvas,
borderColor: theme.colors.border,
shadowColor: theme.colors.shadow
}
})
}