Skip to content

Instantly share code, notes, and snippets.

@inodaf
Created June 24, 2019 16:14
Show Gist options
  • Save inodaf/6753f1b55eac4684033d06b12684f8e8 to your computer and use it in GitHub Desktop.
Save inodaf/6753f1b55eac4684033d06b12684f8e8 to your computer and use it in GitHub Desktop.
Promise Based Dialog Actions
import { Button } from './Button'
export const Dialog = props => h('article', {
id: 'my-dialog',
content: Button({
textContent: 'Hey',
onclick: props.onSuccessClick
})
})
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)
}))
})
})
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