Skip to content

Commit 4e6929c

Browse files
committed
[#77] Finally drop use of findDOMNode()
Fixes #77 Fixes #704 For the longest time, since before I took over, we were filtering specified containers through `ReactDOM.findDOMNode()`, but why? The configured trap containers (via the `containerElements` prop) must be elements already, which means they must already be rendered, which means there's no point in passing them through `findDOMNode()` to find an underlying DOM element because they aren't React elements in the first place. The removal of this call should mean that React Strict Mode will finally be OK with focus-trap-react. Note it's still possible to render a focus trap with a single React element child. That works fine, and never needed `findDOMNode()` anyway. We were already using a callback ref to get its element to then auto-configure that element as the single container for the focus trap. Just to be sure, however, this will be released as a new __major__ version.
1 parent 9947461 commit 4e6929c

3 files changed

Lines changed: 11 additions & 19 deletions

File tree

.changeset/shy-terms-fold.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'focus-trap-react': major
3+
---
4+
5+
Stop using the infamous `findDOMNode()` on provided `containerElements`. There seems to have been no good reason for this as this prop, if specified, is already required to be an array of HTMLElement references, which means these nodes have already been rendered (if they were once React elements). There appears to have been no remaining need for this API. Furthermore, the minimum supported version of React is now 16.3 as it technically has been for a while now since that is the version that introduced callback refs, which we've been using for quite some time now (so this bump shouldn't cause any ripples).

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
},
100100
"peerDependencies": {
101101
"prop-types": "^15.8.1",
102-
"react": ">=16.0.0",
103-
"react-dom": ">=16.0.0"
102+
"react": ">=16.3.0",
103+
"react-dom": ">=16.3.0"
104104
}
105105
}

src/focus-trap-react.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
const React = require('react');
2-
const ReactDOM = require('react-dom');
32
const PropTypes = require('prop-types');
43
const { createFocusTrap } = require('focus-trap');
54
const { isFocusable } = require('tabbable');
65

7-
// TODO: These issues are related to older React features which we'll likely need
8-
// to fix in order to move the code forward to the next major version of React.
9-
// @see https://github.com/davidtheclark/focus-trap-react/issues/77
10-
/* eslint-disable react/no-find-dom-node */
11-
126
class FocusTrap extends React.Component {
137
constructor(props) {
148
super(props);
@@ -277,18 +271,11 @@ class FocusTrap extends React.Component {
277271

278272
setupFocusTrap() {
279273
if (!this.focusTrap) {
280-
const focusTrapElementDOMNodes = this.focusTrapElements.map(
281-
// NOTE: `findDOMNode()` does not support CSS selectors; it'll just return
282-
// a new text node with the text wrapped in it instead of treating the
283-
// string as a selector and resolving it to a node in the DOM
284-
ReactDOM.findDOMNode
285-
);
286-
287-
const nodesExist = focusTrapElementDOMNodes.some(Boolean);
274+
const nodesExist = this.focusTrapElements.some(Boolean);
288275
if (nodesExist) {
289276
// eslint-disable-next-line react/prop-types -- _createFocusTrap is an internal prop
290277
this.focusTrap = this.props._createFocusTrap(
291-
focusTrapElementDOMNodes,
278+
this.focusTrapElements,
292279
this.internalOptions
293280
);
294281

@@ -378,7 +365,7 @@ class FocusTrap extends React.Component {
378365
);
379366
}
380367

381-
const composedRefCallback = (element) => {
368+
const callbackRef = (element) => {
382369
const { containerElements } = this.props;
383370

384371
if (child) {
@@ -395,7 +382,7 @@ class FocusTrap extends React.Component {
395382
};
396383

397384
const childWithRef = React.cloneElement(child, {
398-
ref: composedRefCallback,
385+
ref: callbackRef,
399386
});
400387

401388
return childWithRef;

0 commit comments

Comments
 (0)