Skip to content

Instantly share code, notes, and snippets.

@bradennapier
Created June 17, 2017 20:40
Show Gist options
  • Save bradennapier/d046c903cf6cb5d90fe781460aefd83c to your computer and use it in GitHub Desktop.
Save bradennapier/d046c903cf6cb5d90fe781460aefd83c to your computer and use it in GitHub Desktop.
redux-css simple example
import { createStore, applyMiddleware, compose } from 'redux'
import reduxCSS from 'redux-css'
import reducers from './reducers'
/*
initial styles are the variables that we should set immediately.
Note that '--' is optional, it will be added for you if you don't
include it.
Objects are flattened { myKey: { myNested: 'red' } } -> --myKeymyNested: red
*/
const initialStyles = {
primary: {
BG: '#303641',
},
navbar: {
Height: '55px',
PaddingTop: '0px'
}
}
const styleReducer = (vars = initialStyles, action, state) => {
switch(action.type) {
case 'DEVICE_ORIENTATION': {
return {
...vars,
navbar: {
...vars.navbar,
Height: action.orientation === 'portrait'
? '55px'
: '50px',
PaddingTop: action.orientation === 'portrait'
: '5px'
: '0px'
}
}
}
}
return vars
}
const configureStore = (initialState = {}) => {
// exposes: css.getVariable, css.setVariable, css.removeVariable, css.middleware
const {
middleware: cssMiddleware,
...css
} = reduxCSS(
// our style reducers
styleReducer
)
const enhancers = compose(
applyMiddleware(
cssMiddleware
)
)
const store = createStore(
reducers,
initialState,
enhancers
)
return { store, css }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment