Last active
August 13, 2018 12:59
-
-
Save iamandrewluca/9785b5d89db7e1371f9570085c160b46 to your computer and use it in GitHub Desktop.
Modal Toggler
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 { MyModal } from 'my-modal' | |
import { MyModalToggleButton } from 'modal-toggle-button' | |
const App = () => ( | |
<React.Fragment> | |
<MyModalToggleButton /> | |
<MyModal /> | |
</React.Fragment> | |
) | |
export default App |
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 ReactDOM from 'react-dom' | |
import App from './app' | |
import { ModalTogglerProvider } from './modal-toggler' | |
ReactDOM.render( | |
<ModalTogglerProvider> | |
<App /> | |
</ModalTogglerProvider>, | |
document.getElementById('app') | |
) |
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' | |
const { Provider, Consumer } = React.createContext() | |
export class ModalTogglerProvider extends React.Component { | |
state = {} | |
toggle = (modal) => (e) => this.setState((state) => ({ [modal]: !state[modal] })) | |
render() { | |
return ( | |
<Provider value={{ on: this.state, toggle: this.toggle }}> | |
{this.props.children} | |
</Provider> | |
) | |
} | |
} | |
export const ModalToggler = ({ modal, children }) => ( | |
<Consumer> | |
{({ on, toggle }) => children(on[modal] || false, toggle(modal))} | |
</Consumer> | |
) |
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 { ModalToggler } from './modal-toggler' | |
import { Modal } from '...' | |
export const MyModal = () => ( | |
<ModalToggler modal="my-modal"> | |
{(on, toggle) => ( | |
<Modal isOpen={on}> | |
My Modal content | |
</Modal> | |
)} | |
</ModalToggler> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment