Last active
April 28, 2018 10:11
-
-
Save casesandberg/d8dc577bc3b6b7f0dc648ecb7ef3c930 to your computer and use it in GitHub Desktop.
Popups Managed Via Redux:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import reactCSS from 'reactcss' | |
import { connect } from 'react-redux' | |
import { actions, selectors } from './reducer' | |
export const Popup = ({ children, visible, onClose, name }) => { | |
const styles = reactCSS({ | |
'default': { | |
wrap: { | |
position: 'absolute', | |
top: 0, | |
right: 0, | |
bottom: 0, | |
left: 0, | |
}, | |
cover: { | |
position: 'absolute', | |
top: 0, | |
right: 0, | |
bottom: 0, | |
left: 0, | |
background: 'rgba(39, 36, 41, 0.7)', | |
}, | |
box: { | |
position: 'absolute', | |
left: '50%', | |
top: '50%', | |
transform: 'translate(-50%, -50%)', | |
}, | |
}, | |
}) | |
const handleClose = () => onClose(name) | |
return visible ? ( | |
<div style={ styles.wrap }> | |
<div style={ styles.cover } onClick={ handleClose } /> | |
<div style={ styles.box }>{ children }</div> | |
</div> | |
) : null | |
} | |
export default connect( | |
(state, ownProps) => ({ | |
visible: selectors.getPopupVisibility(state, ownProps.name), | |
}), | |
actions | |
)(Popup) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const OPEN_POPUP = 'POPUP/OPEN_POPUP' | |
export const CLOSE_POPUP = 'POPUP/CLOSE_POPUP' | |
export const reducer = (state = {}, action) => { | |
switch (action.type) { | |
case OPEN_POPUP: | |
return { ...state, [action.name]: true } | |
case CLOSE_POPUP: | |
return { ...state, [action.name]: false } | |
default: return state | |
} | |
} | |
export const actions = { | |
onClose: name => ({ type: CLOSE_POPUP, name }), | |
onOpen: name => ({ type: OPEN_POPUP, name }), | |
} | |
export const selectors = { | |
getPopupVisibility: (state, name) => state[name] || false, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment