File tree 3 files changed +12
-9
lines changed
packages/orbit-components/src
3 files changed +12
-9
lines changed Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ const Dialog = ({
43
43
const ref = React . useRef < HTMLDivElement | null > ( null ) ;
44
44
const theme = useTheme ( ) ;
45
45
46
- useFocusTrap ( ref ) ;
46
+ useFocusTrap ( ref , true ) ;
47
47
48
48
React . useEffect ( ( ) => {
49
49
const transitionLength = parseFloat ( theme . orbit . durationFast ) * 1000 ;
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ const Component = (props: Props) => {
21
21
22
22
The table below contains all parameters available to the ` useFocusTrap ` hook.
23
23
24
- | Name | Type | Description |
25
- | :--- | :----- | :------------------ |
26
- | ref | ` func ` | Component reference |
24
+ | Name | Type | Description |
25
+ | :--------------- | :-------- | :-------------------------------------------- |
26
+ | ref | ` func ` | Component reference |
27
+ | triggeredDefault | ` boolean ` | Whether to trigger the focus trap by default. |
Original file line number Diff line number Diff line change 1
1
import * as React from "react" ;
2
2
3
3
import FOCUSABLE_ELEMENT_SELECTORS from "./consts" ;
4
- import KEY_CODE_MAP from "../../common/keyMaps" ;
5
4
6
5
interface FocusElements {
7
6
first : HTMLElement | null ;
8
7
last : HTMLElement | null ;
9
8
}
10
9
11
- type UseFocusTrap = < T extends HTMLElement > ( ref : React . RefObject < T > ) => void ;
10
+ type UseFocusTrap = < T extends HTMLElement > (
11
+ ref : React . RefObject < T > ,
12
+ triggeredDefault ?: boolean ,
13
+ ) => void ;
12
14
13
15
const manageFocus = ( ref , triggered ) : FocusElements => {
14
16
if ( triggered && ref . current ) {
@@ -25,12 +27,12 @@ const manageFocus = (ref, triggered): FocusElements => {
25
27
return { first : null , last : null } ;
26
28
} ;
27
29
28
- const useFocusTrap : UseFocusTrap = ref => {
29
- const [ triggered , setTriggered ] = React . useState ( false ) ;
30
+ const useFocusTrap : UseFocusTrap = ( ref , triggeredDefault = false ) => {
31
+ const [ triggered , setTriggered ] = React . useState ( triggeredDefault ) ;
30
32
31
33
React . useEffect ( ( ) => {
32
34
const handleKeyDown = ( ev : KeyboardEvent ) => {
33
- if ( ev . keyCode === KEY_CODE_MAP . TAB ) {
35
+ if ( ev . key === "Tab" ) {
34
36
if ( ! triggered ) {
35
37
setTriggered ( true ) ;
36
38
}
You can’t perform that action at this time.
0 commit comments