Skip to content

Instantly share code, notes, and snippets.

@pfftdammitchris
Created August 25, 2019 14:20
Show Gist options
  • Save pfftdammitchris/40b8ce9e5ccac2d497d0d4d71c69934e to your computer and use it in GitHub Desktop.
Save pfftdammitchris/40b8ce9e5ccac2d497d0d4d71c69934e to your computer and use it in GitHub Desktop.
import React from 'react'
const UserContext = React.createContext({
user: {
firstName: 'Kelly',
email: 'frogLover123@gmail.com',
},
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