chunkai1312 / muibox

Promise-based dialog boxes (alert, confirm, prompt) using Material-UI
https://chunkai1312.github.io/muibox
MIT License
20 stars 6 forks source link

muibox

NPM version Build Status

Promise-based dialog boxes (alert, confirm, prompt) using Material-UI

Demo

Install

$ npm install muibox --save

Usage

Simply wrap all components that should display dialog boxes with the DialogProvider component, e.g. by wrapping your router with it.

import { DialogProvider } from 'muibox'

// somewhere at the root of your app
<DialogProvider>
  {/* the rest of your app belongs here, e.g. the router */}
</DialogProvider>

You can then display dialog boxes with the withDialog HOC and the injected dialog prop in your components.

import React from 'react'
import { withDialog } from 'muibox'

class MyComponent extends React.Component {
  render () {
    const { dialog } = this.props
    return (
      <div>
        <button onClick={() => dialog.alert('Warning!')}>
          Click me
        </button>
      </div>
    )
  }
}

export default withDialog()(MyComponent)

If use React 16.8+, you can import useDialog hook to get dialog context directly.

import React from 'react'
import { useDialog } from 'muibox'

function MyComponent () {
  const dialog = useDialog()
  return (
    <div>
      <button onClick={() => dialog.alert('Warning!')}>
        Click me
      </button>
    </div>
  )
}

export default MyComponent

Alert

dialog.alert('Warning!')
  .then(() => console.log('clicked ok'))

API

dialog.alert(options)

Confirm

dialog.confirm('Are you sure?')
  .then(() => console.log('clicked ok'))
  .catch(() => console.log('clicked cancel'))

API

dialog.confirm(options)

Prompt

dialog.prompt('Enter your name:')
  .then((value) => console.log('clicked ok', value))
  .catch(() => console.log('clicked cancel'))

API

dialog.prompt(options)

License

MIT