Skip to content

[change] deprecate getParentElement in favor of local element. #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,8 @@ object will apply to all instances of the modal.

### Appended to custom node

You can choose an element for the modal to be appended to, rather than using
body tag. To do this, provide a function to `parentSelector` prop that return
the element to be used.

```jsx

function getParent() {
return document.querySelector('#root');
}

<Modal
...
parentSelector={getParent}
...
>
<p>Modal Content.</p>
</Modal>
```
`parentSelector` is now deprecated. `<Modal />` can be appended on any place
and it will correctly manage it's clean up.

### Body class

Expand Down
4 changes: 0 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ import ReactModal from 'react-modal';
String indicating the role of the modal, allowing the 'dialog' role to be applied if desired.
*/
role="dialog"
/*
Function that will be called to get the parent element that the modal will be attached to.
*/
parentSelector={() => document.body}
/*
Additional aria attributes (optional).
*/
Expand Down
24 changes: 11 additions & 13 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,24 @@ export default () => {
ReactDOM.unmountComponentAtNode(node);
});

it('renders into the body, not in context', () => {
const node = document.createElement('div');
it('renders in context, never in document.body', function() {
var node = document.createElement('div');
var realRef = null;
class App extends Component {
render() {
return (
<div>
<Modal isOpen>
<span>hello</span>
</Modal>
return (
<div ref={ref => { realRef = ref; }}>
<Modal isOpen={true}>
<span>hello</span>
</Modal>
</div>
);
);
}
}
Modal.setAppElement(node);
ReactDOM.render(<App />, node);
document.body.querySelector(
'.ReactModalPortal'
).parentNode.should.be.eql(
document.body
);
var modalParent = node.querySelector('.ReactModalPortal').parentNode;
expect(modalParent).toEqual(realRef);
ReactDOM.unmountComponentAtNode(node);
});

Expand Down
33 changes: 10 additions & 23 deletions src/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ export const bodyOpenClassName = 'ReactModal__Body--open';

const renderSubtreeIntoContainer = ReactDOM.unstable_renderSubtreeIntoContainer;

function getParentElement(parentSelector) {
return parentSelector();
}

export default class Modal extends Component {
static setAppElement(element) {
ariaAppHider.setElement(element);
Expand Down Expand Up @@ -53,7 +49,6 @@ export default class Modal extends Component {
ariaHideApp: PropTypes.bool,
shouldFocusAfter: PropTypes.bool,
shouldCloseOnOverlayClick: PropTypes.bool,
parentSelector: PropTypes.func,
aria: PropTypes.object,
role: PropTypes.string,
contentLabel: PropTypes.string
Expand All @@ -68,7 +63,6 @@ export default class Modal extends Component {
closeTimeoutMS: 0,
shouldFocusAfterRender: true,
shouldCloseOnOverlayClick: true,
parentSelector() { return document.body; }
};

static defaultStyles = {
Expand Down Expand Up @@ -97,12 +91,6 @@ export default class Modal extends Component {
};

componentDidMount() {
this.node = document.createElement('div');
this.node.className = this.props.portalClassName;

const parent = getParentElement(this.props.parentSelector);
parent.appendChild(this.node);

this.renderPortal(this.props);
}

Expand All @@ -111,14 +99,6 @@ export default class Modal extends Component {
// Stop unnecessary renders if modal is remaining closed
if (!this.props.isOpen && !isOpen) return;

const currentParent = getParentElement(this.props.parentSelector);
const newParent = getParentElement(newProps.parentSelector);

if (newParent !== currentParent) {
currentParent.removeChild(this.node);
newParent.appendChild(this.node);
}

this.renderPortal(newProps);
}

Expand Down Expand Up @@ -148,10 +128,13 @@ export default class Modal extends Component {
}
}

setNodeRef = ref => {
this.node = ref;
}

removePortal = () => {
ReactDOM.unmountComponentAtNode(this.node);
const parent = getParentElement(this.props.parentSelector);
parent.removeChild(this.node);
this.portal = null;
}

renderPortal = props => {
Expand All @@ -161,6 +144,10 @@ export default class Modal extends Component {
}

render() {
return null;
return (
<div
ref={this.setNodeRef}
className={this.props.portalClassName} />
);
}
}