-
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 #27670 from software-mansion-labs/@kosmydel/add-fo…
…cus-trap-2 Add focus trap to the RHP (v2)
- Loading branch information
Showing
18 changed files
with
205 additions
and
37 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* The FocusTrap is only used on web and desktop | ||
*/ | ||
import FocusTrapViewProps from './types'; | ||
|
||
function FocusTrapView({children}: FocusTrapViewProps) { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* The FocusTrap is only used on web and desktop | ||
*/ | ||
import FocusTrap from 'focus-trap-react'; | ||
import React, {useRef} from 'react'; | ||
import {View} from 'react-native'; | ||
import viewRef from '@src/types/utils/viewRef'; | ||
import FocusTrapViewProps from './types'; | ||
|
||
function FocusTrapView({isEnabled = true, isActive = true, shouldEnableAutoFocus = false, ...props}: FocusTrapViewProps) { | ||
/** | ||
* 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<HTMLDivElement>(null); | ||
|
||
return isEnabled ? ( | ||
<FocusTrap | ||
active={isActive} | ||
focusTrapOptions={{ | ||
initialFocus: () => (shouldEnableAutoFocus && ref.current) ?? false, | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
fallbackFocus: () => ref.current!, | ||
clickOutsideDeactivates: true, | ||
}} | ||
> | ||
<View | ||
ref={viewRef(ref)} | ||
tabIndex={0} | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...props} | ||
/> | ||
</FocusTrap> | ||
) : ( | ||
props.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {ViewProps} from 'react-native'; | ||
import ChildrenProps from '@src/types/utils/ChildrenProps'; | ||
|
||
type FocusTrapViewProps = ChildrenProps & { | ||
/** | ||
* Whether to enable the FocusTrap. | ||
* If the FocusTrap is disabled, we just pass the children through. | ||
*/ | ||
isEnabled?: boolean; | ||
|
||
/** | ||
* Whether to disable auto focus | ||
* It is used when the component inside the FocusTrap have their own auto focus logic | ||
*/ | ||
shouldEnableAutoFocus?: boolean; | ||
|
||
/** Whether the FocusTrap is active (listening for events) */ | ||
isActive?: boolean; | ||
} & ViewProps; | ||
|
||
export default FocusTrapViewProps; |
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
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
Oops, something went wrong.