Skip to content

Commit

Permalink
[added] ref for overlay and content
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomaskin Ilya authored and diasbruno committed Feb 15, 2018
1 parent 61b141d commit 0809958
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ You can use this to remove scrolling on the body while the modal is open.
}
```

### Refs

You can use ref callbacks to get overlay and content DOM nodes:

```jsx
<Modal
...
overlayRef={node => this.overlayRef = node}
contentRef={node => this.contentRef = node}
...
>
<p>Modal Content.</p>
</Modal>
```

## Examples

Inside an app:
Expand Down
8 changes: 8 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ import ReactModal from 'react-modal';
labelledby: "heading",
describedby: "full_description"
}}
/*
Overlay ref callback.
*/
overlayRef={setOverlayRef}
/*
Content ref callback.
*/
contentRef={setContentRef}
/>
```

Expand Down
16 changes: 16 additions & 0 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,4 +601,20 @@ export default () => {
Modal.setAppElement(node);
ReactDOM.render(<App />, node);
});

it("use overlayRef and contentRef", () => {
let overlay = null;
let content = null;

renderModal({
isOpen: true,
overlayRef: node => (overlay = node),
contentRef: node => (content = node)
});

overlay.should.be.instanceOf(HTMLElement);
content.should.be.instanceOf(HTMLElement);
overlay.classList.contains("ReactModal__Overlay");
content.classList.contains("ReactModal__Content");
});
};
4 changes: 3 additions & 1 deletion src/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export default class Modal extends Component {
aria: PropTypes.object,
role: PropTypes.string,
contentLabel: PropTypes.string,
shouldCloseOnEsc: PropTypes.bool
shouldCloseOnEsc: PropTypes.bool,
overlayRef: PropTypes.func,
contentRef: PropTypes.func
};
/* eslint-enable react/no-unused-prop-types */

Expand Down
6 changes: 5 additions & 1 deletion src/components/ModalPortal.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export default class ModalPortal extends Component {
contentLabel: PropTypes.string,
aria: PropTypes.object,
children: PropTypes.node,
shouldCloseOnEsc: PropTypes.bool
shouldCloseOnEsc: PropTypes.bool,
overlayRef: PropTypes.func,
contentRef: PropTypes.func
};

constructor(props) {
Expand Down Expand Up @@ -110,10 +112,12 @@ export default class ModalPortal extends Component {

setOverlayRef = overlay => {
this.overlay = overlay;
this.props.overlayRef && this.props.overlayRef(overlay);
};

setContentRef = content => {
this.content = content;
this.props.contentRef && this.props.contentRef(content);
};

beforeOpen() {
Expand Down

0 comments on commit 0809958

Please sign in to comment.