Created
June 24, 2019 16:14
-
-
Save inodaf/6753f1b55eac4684033d06b12684f8e8 to your computer and use it in GitHub Desktop.
Promise Based Dialog Actions
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 { Button } from './Button' | |
export const Dialog = props => h('article', { | |
id: 'my-dialog', | |
content: Button({ | |
textContent: 'Hey', | |
onclick: props.onSuccessClick | |
}) | |
}) |
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 { h } from '../uihelpers' | |
const withContext = (fn) => { | |
const rootID = '__dialog-context__' | |
const root = h('div', { id: rootID }) | |
const query = document.querySelector(`#${rootID}`) | |
if (query) { | |
return query | |
} else { | |
return fn(document.body.appendChild(root)) | |
} | |
} | |
export const makeDialog = Dialog => withContext(DialogContext => { | |
return new Promise(resolve => { | |
DialogContext.appendChild(Dialog({ | |
onSuccessClick: e => resolve(true) | |
})) | |
}) | |
}) |
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 { makeDialog } from '../hofs/make-dialog.hof' | |
import { Dialog } from '../components/' | |
async function MyView() { | |
const confirm = await makeDialog(Dialog) | |
return confirm ? console.log('Hello') : console.log('Nope') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment