Manage dialogs throughout the react application. React Dialog Manager
gives you a clear declarative API to easily handle the dialog's behaviour.
using npm:
npm install --save react-dialog-manager
React Dialog Manager
gives you great functionalities and flexibility to handle the dialogs of your react projects. Let's start with the following implementaiton.
import React from 'react';
import {createDialogManager, DialogManagerComponent} from 'react-dialog-manager';
const dialogManager = createDialogManager();
const dialog = dialogManager.createDialog({
title: 'The Dialog Title',
body: <TestForm />
});
class App extends React.Component{
constructor(props){
super(props);
}
onOpenBtnClicked(){
dialog.open();
}
onCloseBtnClicked(){
dialog.close();
}
render(){
return (
<div className="App">
<div>
<button onClick={this.onOpenBtnClicked.bind(this)}>Open Dialog</button>
<button onClick={this.onCloseBtnClicked.bind(this)}>Close Dialog</button>
</div>
<DialogManagerComponent manager={dialogManager} />
</div>
);
}
}
function TestForm(props){
/* dialog is passed within props */
let {dialog} = props;
return (
<form>
<input type="text" />
</form>
);
}
export default App;
From the previous implementation we can get some points;
<DialogManagerComponent />
this ReactComponent is imported fromreact-dialog-manager
and it's recommended to be at higher level in the application as shown in previous snippet. This component takes only one prop which is manager.
// dialogManager comes from createDialogManager or getLastDMInstance
<DialogManagerComponent manager={dialogManager} />
createDialogManager
this function is also imported fromreact-dialog-manager
to create aDialogManager
instace with two main params.
let setting = {}; // setting for manager
let instanceName = 'base_dialogs_manager'; // acts as id or key. just used for identification.
const dialogManager = createDialogManager(setting, instanceName);
Note, Sometimes, we use dialogManager
in different places through our app to create dialogs so instead of making a seperate singleton object to handle this process, we can use getLastDMInstance
function which also is exported from react-dialog-manager
.
createDialog
once you have already created adialogManager
, you can create as much as you need of dialogs. You can, easily, interact withdialog
throuth API.
const dialogManager = createDialogManager();
let definition = {}; // definition of the dialog like its title, its header, its body ... etc.
const dialog = dialogManager.createDialog(definition);
// open the dialog
dialog.open();
// close the dialog
dialog.close();
Let's go throght some examples.
- Basic Example
import React from 'react';
import {
createDialogManager,
DialogManagerComponent
} from 'react-dialog-manager';
const dialogManager = createDialogManager({}, 'BASE_DM');
const dialogA = dialogManager.createDialog({
title: 'The Dialog Title',
body: (
<div>
<p>Hello World</p>
</div>
)
});
class App extends React.Component{
constructor(props){
super(props);
}
onBtnAClicked(){
dialogA.open();
}
onBtnBClicked(){
let definition = {
title: 'Dialog B',
body: 'the body content'
};
let dialogInstanceName = 'DIALOG_B';
/* will get dialogInstance of 'DIALOG_B'. In case of not existing -> create new one */
dialogManager.getLastDialogInstance(definition, dialogInstanceName).open();
}
render(){
return (
<div className="App">
<div>
<button onClick={this.onBtnAClicked.bind(this)}>Open Dialog A</button>
<button onClick={this.onBtnBClicked.bind(this)}>Open Dialog B</button>
</div>
<DialogManagerComponent manager={dialogManager} />
</div>
);
}
}
export default App;
- Nested Dialogs Example
const dialogManager = createDialogManager({}, 'BASE_DM');
const dialogA = dialogManager.createDialog({
title: 'The Dialog Title',
body: <DialogBody />,
close_by_overlay: false /* will not close when clicking on overlay layer */
});
class App extends React.Component{
onBtnAClicked(){
dialogA.open();
}
render(){
return (
<div className="App">
<div>
<button onClick={this.onBtnAClicked.bind(this)}>Open Dialog</button>
</div>
<DialogManagerComponent manager={dialogManager} />
</div>
);
}
}
function DialogBody(props){
return (
<div>
<button
onClick={() => {
dialogManager.getLastDialogInstance({
title: 'I am nested dialog',
body: <div>Awesome!</div>
}, 'NESTED_DAILOG').open();
}}
>
Open Nested Dialog
</button>
</div>
);
}
export default App;
- Open - Close Handlers Examples
const dialogManager = createDialogManager({}, 'BASE_DM');
/* will not open beacuse of the before_open handler returns false */
const dialogA = dialogManager.createDialog({
before_open: (d)=>{
return false;
}
});
/* will console.log 'on dialog opened' after dialog opening */
const dialogB = dialogManager.createDialog({
on_open: (d)=>{
console.log('on dialog opened');
}
});
/* will not close after clicking on done button beacuse of the before_done handler returns false */
const dialogC = dialogManager.createDialog({
before_done: (d)=>{
console.log('e.g. form validation');
return false;
}
});
- react-dialog-manager
This module is responsible for handling the instantiation of DialogManager and also, exporting the DialogManagerComponent
- DialogManager
- Dialog
- new Dialog(definition, dialogMananger, [instanceName])
- .before(eventName, handler)
- .close()
- .done()
- .getBeforeHandlersOf(eventName) ⇒
function
|Array.<function()>
- .getBody() ⇒
ReactComponent
|string
- .getHandlersOf(eventName) ⇒
function
|Array.<function()>
- .getInstanceName() ⇒
string
- .getOverlayStyle() ⇒
Object
- .getStyle() ⇒
Object
- .getTitle() ⇒
string
- .getZIndex() ⇒
int
- .isOpened() ⇒
boolean
- .on(eventName, handler)
- .open()
- .set(definition, [value])
- .setBody(body)
- .setStyle(style)
- .setTitle(title)
- .setZIndex(zIndex)
This module is responsible for handling the instantiation of DialogManager and also, exporting the DialogManagerComponent
Params
- DialogManagerComponent
ReactComponent
Example
const dm = createDialogManager();
<DialogManagerComponent dialogManager="dm" />
react-dialog-manager.createDialogManager(setting, instanceName) ⇒ DialogManager
It will instantiate a new instance of DialogManager and store it as last instance.
Returns: DialogManager
- dialogManager
Params
- setting
Object
- instanceName
Sting
- The name used to identify the DialogManager instance. It's helpful wheu you call getLastDMInstance with the same instanceName
Returns: Array
- intances
react-dialog-manager.getLastDMInstance(setting, instanceName) ⇒ DialogManager
Returns the last instance of DialogManger with the same provided instanceName param. In case of, there is no previous instance, it instantiates new one and returns it.
Returns: DialogManager
- dialogManager
Params
- setting
Object
- instanceName
Sting
= ''
The instantiation of this class is done by createDialogManager
Params
- setting
Object
- Setting for the dialogs manager.- [.initial_z_index]
int
= 1000
- The start point where the z-indices of dialogs with begin. Each new dialog theinitial_z_index
will be incremented. For example; first dialog will haveinitial_z_index
, second one will haveinitial_z_index + 1
and so on.
- [.initial_z_index]
- [instanceName]
string
- The name of DialogManager instance
Example
import {createDialogManager} from 'react-dialog-manager';
const dm = createDialogManager({}, 'baseDM');
dialogManager.createDialog(definition, [instanceName]) ⇒ Dialog
Creates new dialog instance, and stores (pushs) it into the dialogs.
Returns: Dialog
- dialog
Params
- definition
Object
- The definition of the new Dialog - [instanceName]
string
- The (id) name of the new dialog instance
Retireve the dialogs that have been created.
Returns: Array
- dialogs
Returns: string
- instanceName - The name of dialogManager instance
dialogManager.getLastDialogInstance(definition, [instanceName]) ⇒ Dialog
Returns the last dialog instance of the provided instanceName. In case of there is no previous one with the provided instnaceName, we will create new dialog instance with the provide instancName.
Returns: Dialog
- dialog
Params
- definition
Object
- The definition of the new Dialog - [instanceName]
string
- The (id) name of the new dialog instance
Removes (splices) the dialog instance from dialogs,
Params
- dialog
Dialog
- Dialog
- new Dialog(definition, dialogMananger, [instanceName])
- dialog.before(eventName, handler)
- dialog.close()
- dialog.done()
- dialog.getBeforeHandlersOf(eventName) ⇒
function
|Array.<function()>
- dialog.getBody() ⇒
ReactComponent
|string
- dialog.getHandlersOf(eventName) ⇒
function
|Array.<function()>
- dialog.getInstanceName() ⇒
string
- dialog.getOverlayStyle() ⇒
Object
- dialog.getStyle() ⇒
Object
- dialog.getTitle() ⇒
string
- dialog.getZIndex() ⇒
int
- dialog.isOpened() ⇒
boolean
- dialog.on(eventName, handler)
- dialog.open()
- dialog.set(definition, [value])
- dialog.setBody(body)
- dialog.setStyle(style)
- dialog.setTitle(title)
- dialog.setZIndex(zIndex)
The instantiation of this class is done by createDialog of DialogManager
Params
- definition
Object
- The definition of the dialog. By definition, we mean the properties like title, header, body ... etc.- .title
string
- The dialog's title. - [.allow_header]
boolean
= false
- Whether allowing the header part of the dialog or not. - [.header]
ReactElement
- Sometimes, you might want to set custom header component. In this case the title will be ignore. You are responsible to set the title within the custom header. - [.body]
ReactElement
|string
- The body component of the dialog. This ReactElement body will recieve the dialog within its props. - [.before_open]
function
|Array.<function()>
- The handler(s) that will be invoked before dialog opening. It must return true to open the dialog, otherwise it will not. - [.on_open]
function
|Array.<function()>
- The handler(s) that will be invoked once the dialog has been opened. - [.before_done]
function
|Array.<function()>
- The handler(s) that will be invoked before dialog done (clicking on done button). It must return true to done the dialog, otherwise it will not. - [.on_done]
function
|Array.<function()>
- The handler(s) that will be invoked once the dialog has been done. - [.before_close]
function
|Array.<function()>
- The handler(s) that will be invoked before dialog closing. It must return true to close the dialog, otherwise it will not. - [.on_close]
function
|Array.<function()>
- The handler(s) that will be invoked once the dialog has been closed. - [.close_by_overlay]
boolean
= true
- Close the dialog when clicking on overlay layer or not. - [.style]
Object
- The style object for the dialog.
- .title
- dialogMananger
DialogManager
- This param is being passed by outter dialogManager duringdm.createDialog({})
. - [instanceName]
string
- The (id) name of the dialog instance.
Example
import {createDialogManager} from 'react-dialog-manager';
const dm = createDialogManager({}, 'baseDM');
const dialog = dm.createDialog({});
Add event handler(s) before (open - done - close) actions.
Params
- eventName
string
- One of (open - done - close) event names. - handler
function
|Array.<function()>
- Event handler(s) that will be invoked with the dialog parameter before performing the corresponding action. Note, each handler must returntrue
to go farther and execute the next steps.
Example
dialog.before('open', (dialog) => true );
dialog.before('done', [(dialog) => true, (dialog) => true] );
First, we invoke all attached-event-handlers of before_close. All handlers should return true to go furhter and close (hide) the dialog.
Done with the dialog as if you press the done button of the dialog.
Returns: function
| Array.<function()>
- handler - Event handler(s) that will be invoked with the dialog parameter before performing the corresponding action.
Params
- eventName
string
- One of (open - done - close) event names.
Returns: ReactComponent
| string
- body
Returns: function
| Array.<function()>
- handler - Event handler(s) that will be invoked with the dialog parameter once the corresponding action is performed.
Params
- eventName
string
- One of (open - done - close) event names.
Returns: string
- instanceName - The name of dialog instance
Returns: Object
- overlayStyle
Returns: Object
- style - The (z-index) of the dialog.
Returns: string
- title - The dialog's title.
Returns: int
- zIndex - The (z-index) of the dialog.
retrieve the (open/shown) state of dialog
Returns: boolean
- IS_OPENED
Add event handler(s) once (open - done - close) actions are performed.
Params
- eventName
string
- One of (open - done - close) event names. - handler
function
|Array.<function()>
- Event handler(s) that will be invoked with the dialog parameter once the corresponding action is performed.
Example
dialog.on('done', (dialog) => console.log('dialog has been done') );
dialog.on('close', [(dialog) => console.log('dialog has been closed'), (dialog) => true] );
First, we invoke all attached-event-handlers of before_open. All handlers should return true to go furhter and open (show) the dialog.
set definition for the dialog
Params
- definition
Object
|string
- Set the definition of the Dialog. You can set one property with the corresponding value. - [value]
*
- In case of setting one property of definition, you could add the corresponding value.
Example
let dialogManager = createDialogsManager();
let dialog = dialogManager.createDialog();
dialog.set({
title: 'Hello World!',
body: 'good morning'
});
dialog.set('title', 'New Title Here');
Params
- body
ReactComponent
|string
- Set dialog's body.
Params
- style
Object
- The style of the dialog.
Params
- title
string
- Set the dialog's title.
Params
- zIndex
int
- The (z-index) of the dialog. Usually, manipulated by dialogManager.
- manage WAI & scrolling
- manage keyboard interaction
- add more functionalities and options
- improve docs
- write some examples & demos
- make some styles & css
- add effects & animation features
- design logo & add some demo pics
All of contribution will be appreciated. Remember to write test cases and documented code.