-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24316 from kosmydel/@swm/add-focus-trap
Add focus trap to the RHP
- Loading branch information
Showing
9 changed files
with
169 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,7 @@ | |
"domhandler": "^4.3.0", | ||
"expensify-common": "git+ssh://[email protected]/Expensify/expensify-common.git#35bff866a8d345b460ea6256f0a0f0a8a7f81086", | ||
"fbjs": "^3.0.2", | ||
"focus-trap-react": "^10.2.1", | ||
"htmlparser2": "^7.2.0", | ||
"idb-keyval": "^6.2.1", | ||
"jest-when": "^3.5.2", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* The FocusTrap is only used on web and desktop | ||
*/ | ||
import React, {useEffect, useRef} from 'react'; | ||
import FocusTrap from 'focus-trap-react'; | ||
import {View} from 'react-native'; | ||
import {PropTypes} from 'prop-types'; | ||
import {useIsFocused} from '@react-navigation/native'; | ||
|
||
const propTypes = { | ||
/** Children to wrap with FocusTrap */ | ||
children: PropTypes.node.isRequired, | ||
|
||
/** Whether to enable the FocusTrap */ | ||
enabled: PropTypes.bool, | ||
|
||
/** | ||
* Whether to disable auto focus | ||
* It is used when the component inside the FocusTrap have their own auto focus logic | ||
*/ | ||
shouldEnableAutoFocus: PropTypes.bool, | ||
}; | ||
|
||
const defaultProps = { | ||
enabled: true, | ||
shouldEnableAutoFocus: false, | ||
}; | ||
|
||
function FocusTrapView({enabled, shouldEnableAutoFocus, ...props}) { | ||
const isFocused = useIsFocused(); | ||
|
||
/** | ||
* Focus trap always needs a focusable element. | ||
* In case that we don't have any focusable elements in the modal, | ||
* the FocusTrap will use fallback View element using this ref. | ||
*/ | ||
const ref = useRef(null); | ||
|
||
/** | ||
* We have to set the 'tabindex' attribute to 0 to make the View focusable. | ||
* Currently, it is not possible to set this through props. | ||
* After the upgrade of 'react-native-web' to version 0.19 we can use 'tabIndex={0}' prop instead. | ||
*/ | ||
useEffect(() => { | ||
if (!ref.current) { | ||
return; | ||
} | ||
ref.current.setAttribute('tabindex', '0'); | ||
}, []); | ||
|
||
return enabled ? ( | ||
<FocusTrap | ||
active={isFocused} | ||
focusTrapOptions={{ | ||
initialFocus: () => shouldEnableAutoFocus && ref.current, | ||
fallbackFocus: () => ref.current, | ||
clickOutsideDeactivates: true, | ||
}} | ||
> | ||
<View | ||
ref={ref} | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
/> | ||
</FocusTrap> | ||
) : ( | ||
props.children | ||
); | ||
} | ||
|
||
FocusTrapView.displayName = 'FocusTrapView'; | ||
FocusTrapView.propTypes = propTypes; | ||
FocusTrapView.defaultProps = defaultProps; | ||
|
||
export default FocusTrapView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* The FocusTrap is only used on web and desktop | ||
*/ | ||
|
||
function FocusTrapView({children}) { | ||
return children; | ||
} | ||
|
||
FocusTrapView.displayName = 'FocusTrapView'; | ||
|
||
export default FocusTrapView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters