promodal
v1.0.6
Published
Library which create promise-based modal dialogs
Readme
Promise modal (promodal)
This is React wrapper for your modal dialogs to use them like promises. You can use any UI library what you like. It's really convenient to create modals especially confirmation modals
Installation
Yarn:
yarn add promodalnpm:
npm install --save promodalExample
- Insert container for your modals into you app
import React from "react";
import { ModalContainer } from "promodal";
class App extends React.Component {
render () {
return (
<div>
<ModalContainer />
...
</div>
);
}
};- Then create modal and wrap it with 'createModal'. Now you have promise function that invoke your modal dialog
import React from 'react';
import { createModal } from 'promodal';
class Modal extends React.Component {
render () {
const { submit, cancel } = this.props;
return (
<div>
Modal content
<button onClick={() => submit("Hello I'm resolved!")}>Submit</button>
<button onClick={() => cancel("Do'h!")}>Cancel</button>
</div>
);
}
};
export default createModal(Modal, options);You can pass options like second argument, but it's not necessary;
This options can contains the following properties:
parentNodeis any valid DOM node where modal window should be located, by default it will be<ModalContainer/>component location
- Now you can import your promise-based modal and invoke it
import React from "react";
import modal from "./modal";
class Component extends React.Component {
showModal = () => {
modal().then((data) => {
console.log(data); // Hello I'm resolved!
}, (data) => {
console.log(data); // Do'h!
});
};
render () {
return (
<div>
<button onClick={this.showModal}>Show modal</button>
</div>
);
}
};
export default createModal(Modal);