Created
August 25, 2019 14:20
-
-
Save pfftdammitchris/40b8ce9e5ccac2d497d0d4d71c69934e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 UserContext = React.createContext({ | |
user: { | |
firstName: 'Kelly', | |
email: '[email protected]', | |
}, | |
activated: true, | |
}) | |
const VisibilityControl = ({ children, opened, close }) => { | |
const ctx = React.useContext(UserContext) | |
return React.cloneElement(children, { | |
opened: ctx.activated ? opened : false, | |
onClick: close, | |
}) | |
} | |
export const useModal = ({ urls } = {}) => { | |
const [opened, setOpened] = React.useState(false) | |
const open = () => setOpened(true) | |
const close = () => setOpened(false) | |
return { | |
opened, | |
open, | |
close, | |
} | |
} | |
const App = ({ children }) => { | |
const modal = useModal() | |
return ( | |
<div> | |
<button type="button" onClick={modal.opened ? modal.close : modal.open}> | |
{modal.opened ? 'Close' : 'Open'} the Modal | |
</button> | |
<VisibilityControl {...modal}>{children}</VisibilityControl> | |
</div> | |
) | |
} | |
const Window = ({ opened }) => { | |
if (!opened) return null | |
return ( | |
<div style={{ border: '1px solid teal', padding: 12 }}> | |
<h2>I am a window</h2> | |
</div> | |
) | |
} | |
export default () => ( | |
<App> | |
<Window /> | |
</App> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment