-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
[core] Remove RootRef usage #15347
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
[core] Remove RootRef usage #15347
Changes from all commits
79cb073
60cf4d5
0cc1b3f
0f363f8
d94635a
a6571d3
07ef2e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,32 @@ | ||
/* eslint-disable consistent-return, jsx-a11y/no-noninteractive-tabindex */ | ||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import PropTypes from 'prop-types'; | ||
import warning from 'warning'; | ||
import RootRef from '../RootRef'; | ||
import ownerDocument from '../utils/ownerDocument'; | ||
import { useForkRef } from '../utils/reactHelpers'; | ||
|
||
function TrapFocus(props) { | ||
const { | ||
children, | ||
disableAutoFocus, | ||
disableEnforceFocus, | ||
disableRestoreFocus, | ||
getDoc, | ||
isEnabled, | ||
open, | ||
} = props; | ||
const rootRef = React.useRef(); | ||
const ignoreNextEnforceFocus = React.useRef(); | ||
const sentinelStart = React.useRef(); | ||
const sentinelEnd = React.useRef(); | ||
const lastFocus = React.useRef(); | ||
const rootRef = React.useRef(); | ||
// can be removed once we drop support for non ref forwarding class components | ||
const handleOwnRef = React.useCallback(ref => { | ||
rootRef.current = ReactDOM.findDOMNode(ref); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we can add the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Actually every findDOMNode that get's an instance from a ref should be strict-mode. |
||
}, []); | ||
const handleRef = useForkRef(children.ref, handleOwnRef); | ||
|
||
// ⚠️ You may rely on React.useMemo as a performance optimization, not as a semantic guarantee. | ||
// https://reactjs.org/docs/hooks-reference.html#usememo | ||
|
@@ -61,7 +68,7 @@ function TrapFocus(props) { | |
return; | ||
} | ||
|
||
if (!rootRef.current.contains(doc.activeElement)) { | ||
if (rootRef.current && !rootRef.current.contains(doc.activeElement)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we warn if rootRef.current is not an instance of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. Probably better than checking for |
||
rootRef.current.focus(); | ||
} | ||
}; | ||
|
@@ -109,7 +116,7 @@ function TrapFocus(props) { | |
return ( | ||
<React.Fragment> | ||
<div tabIndex={0} ref={sentinelStart} data-test="sentinelStart" /> | ||
<RootRef rootRef={rootRef}>{props.children}</RootRef> | ||
{React.cloneElement(children, { ref: handleRef })} | ||
<div tabIndex={0} ref={sentinelEnd} data-test="sentinelEnd" /> | ||
</React.Fragment> | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Rather use
instance
.A ref would be the the pointer-type i.e.