1
1
const ION_FOCUSED = 'ion-focused' ;
2
2
const ION_FOCUSABLE = 'ion-focusable' ;
3
- const FOCUS_KEYS = [
4
- 'Tab' ,
5
- 'ArrowDown' ,
6
- 'Space' ,
7
- 'Escape' ,
8
- ' ' ,
9
- 'Shift' ,
10
- 'Enter' ,
11
- 'ArrowLeft' ,
12
- 'ArrowRight' ,
13
- 'ArrowUp' ,
14
- 'Home' ,
15
- 'End' ,
16
- ] ;
17
3
18
4
export interface FocusVisibleUtility {
19
5
destroy : ( ) => void ;
@@ -22,7 +8,6 @@ export interface FocusVisibleUtility {
22
8
23
9
export const startFocusVisible = ( rootEl ?: HTMLElement ) : FocusVisibleUtility => {
24
10
let currentFocus : Element [ ] = [ ] ;
25
- let keyboardMode = true ;
26
11
27
12
const ref = rootEl ? rootEl . shadowRoot ! : document ;
28
13
const root = rootEl ? rootEl : document . body ;
@@ -32,43 +17,31 @@ export const startFocusVisible = (rootEl?: HTMLElement): FocusVisibleUtility =>
32
17
elements . forEach ( ( el ) => el . classList . add ( ION_FOCUSED ) ) ;
33
18
currentFocus = elements ;
34
19
} ;
20
+
35
21
const pointerDown = ( ) => {
36
- keyboardMode = false ;
37
22
setFocus ( [ ] ) ;
38
23
} ;
39
24
40
- const onKeydown = ( ev : Event ) => {
41
- keyboardMode = FOCUS_KEYS . includes ( ( ev as KeyboardEvent ) . key ) ;
42
- if ( ! keyboardMode ) {
43
- setFocus ( [ ] ) ;
44
- }
45
- } ;
46
25
const onFocusin = ( ev : Event ) => {
47
- if ( keyboardMode && ev . composedPath !== undefined ) {
48
- const toFocus = ev . composedPath ( ) . filter ( ( el : any ) => {
49
- // TODO(FW-2832): type
50
- if ( el . classList ) {
51
- return el . classList . contains ( ION_FOCUSABLE ) ;
52
- }
53
- return false ;
54
- } ) as Element [ ] ;
55
- setFocus ( toFocus ) ;
56
- }
26
+ const toFocus = ev . composedPath ( ) . filter ( ( el ) : el is HTMLElement => {
27
+ return el instanceof HTMLElement && el . classList . contains ( ION_FOCUSABLE ) ;
28
+ } ) ;
29
+
30
+ setFocus ( toFocus ) ;
57
31
} ;
32
+
58
33
const onFocusout = ( ) => {
59
34
if ( ref . activeElement === root ) {
60
35
setFocus ( [ ] ) ;
61
36
}
62
37
} ;
63
38
64
- ref . addEventListener ( 'keydown' , onKeydown ) ;
65
39
ref . addEventListener ( 'focusin' , onFocusin ) ;
66
40
ref . addEventListener ( 'focusout' , onFocusout ) ;
67
41
ref . addEventListener ( 'touchstart' , pointerDown , { passive : true } ) ;
68
42
ref . addEventListener ( 'mousedown' , pointerDown ) ;
69
43
70
44
const destroy = ( ) => {
71
- ref . removeEventListener ( 'keydown' , onKeydown ) ;
72
45
ref . removeEventListener ( 'focusin' , onFocusin ) ;
73
46
ref . removeEventListener ( 'focusout' , onFocusout ) ;
74
47
ref . removeEventListener ( 'touchstart' , pointerDown ) ;
0 commit comments