Skip to content

Commit

Permalink
[added] trigger onAfterOpen callback when available.
Browse files Browse the repository at this point in the history
  • Loading branch information
diasbruno authored and Bruno Dias committed Apr 9, 2016
1 parent eee9431 commit 386d662
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Accessible modal dialog component for React.JS
```xml
<Modal
isOpen={bool}
onRequestClose={fn}
onAfterOpen={afterOpenFn}
onRequestClose={requestOpenFn}
closeTimeoutMS={n}
style={customStyle}>

Expand Down Expand Up @@ -95,6 +96,11 @@ var App = React.createClass({
this.setState({modalIsOpen: true});
},

afterOpenModal: function() {
// references are now sync'd and can be accessed.
this.refs.subtitle.style.color = '#f00';
},

closeModal: function() {
this.setState({modalIsOpen: false});
},
Expand All @@ -105,10 +111,11 @@ var App = React.createClass({
<button onClick={this.openModal}>Open Modal</button>
<Modal
isOpen={this.state.modalIsOpen}
onAfterOpen={this.afterOpenModal}
onRequestClose={this.closeModal}
style={customStyles} >

<h2>Hello</h2>
<h2 ref="subtitle">Hello</h2>
<button onClick={this.closeModal}>close</button>
<div>I am a modal</div>
<form>
Expand Down Expand Up @@ -137,7 +144,8 @@ pass the 'shouldCloseOnOverlayClick' prop with 'false' value.
```xml
<Modal
isOpen={bool}
onRequestClose={fn}
onAfterOpen={afterOpenFn}
onRequestClose={requestCloseFn}
closeTimeoutMS={n}
shouldCloseOnOverlayClick={false}
style={customStyle}>
Expand Down
9 changes: 8 additions & 1 deletion examples/basic/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,24 @@ var App = React.createClass({
this.setState({foo: 'bar'});
},

handleOnAfterOpenModal: function() {
// when ready, we can access the available refs.
this.refs.title.style.color = '#F00';
},

render: function() {
return (
<div>
<button onClick={this.openModal}>Open Modal</button>
<button onClick={this.openModal2}>Open Modal 2</button>
<Modal
ref="mymodal"
closeTimeoutMS={150}
isOpen={this.state.modalIsOpen}
onAfterOpen={this.handleOnAfterOpenModal}
onRequestClose={this.handleModalCloseRequest}
>
<h1>Hello</h1>
<h1 ref="title">Hello</h1>
<button onClick={this.closeModal}>close</button>
<div>I am a modal</div>
<form>
Expand Down
1 change: 1 addition & 0 deletions lib/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var Modal = React.createClass({
overlay: React.PropTypes.object
}),
appElement: React.PropTypes.instanceOf(SafeHTMLElement),
onAfterOpen: React.PropTypes.func,
onRequestClose: React.PropTypes.func,
closeTimeoutMS: React.PropTypes.number,
ariaHideApp: React.PropTypes.bool,
Expand Down
1 change: 1 addition & 0 deletions lib/components/ModalPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ var ModalPortal = module.exports = React.createClass({
focusManager.markForFocusLater();
this.setState({isOpen: true}, function() {
this.setState({afterOpen: true});
(this.props.isOpen && this.props.onAfterOpen) && this.props.onAfterOpen();
}.bind(this));
},

Expand Down

0 comments on commit 386d662

Please sign in to comment.