11const ION_FOCUSED = 'ion-focused' ;
22const 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- ] ;
173
184export interface FocusVisibleUtility {
195 destroy : ( ) => void ;
@@ -22,7 +8,6 @@ export interface FocusVisibleUtility {
228
239export const startFocusVisible = ( rootEl ?: HTMLElement ) : FocusVisibleUtility => {
2410 let currentFocus : Element [ ] = [ ] ;
25- let keyboardMode = true ;
2611
2712 const ref = rootEl ? rootEl . shadowRoot ! : document ;
2813 const root = rootEl ? rootEl : document . body ;
@@ -32,43 +17,31 @@ export const startFocusVisible = (rootEl?: HTMLElement): FocusVisibleUtility =>
3217 elements . forEach ( ( el ) => el . classList . add ( ION_FOCUSED ) ) ;
3318 currentFocus = elements ;
3419 } ;
20+
3521 const pointerDown = ( ) => {
36- keyboardMode = false ;
3722 setFocus ( [ ] ) ;
3823 } ;
3924
40- const onKeydown = ( ev : Event ) => {
41- keyboardMode = FOCUS_KEYS . includes ( ( ev as KeyboardEvent ) . key ) ;
42- if ( ! keyboardMode ) {
43- setFocus ( [ ] ) ;
44- }
45- } ;
4625 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 ) ;
5731 } ;
32+
5833 const onFocusout = ( ) => {
5934 if ( ref . activeElement === root ) {
6035 setFocus ( [ ] ) ;
6136 }
6237 } ;
6338
64- ref . addEventListener ( 'keydown' , onKeydown ) ;
6539 ref . addEventListener ( 'focusin' , onFocusin ) ;
6640 ref . addEventListener ( 'focusout' , onFocusout ) ;
6741 ref . addEventListener ( 'touchstart' , pointerDown , { passive : true } ) ;
6842 ref . addEventListener ( 'mousedown' , pointerDown ) ;
6943
7044 const destroy = ( ) => {
71- ref . removeEventListener ( 'keydown' , onKeydown ) ;
7245 ref . removeEventListener ( 'focusin' , onFocusin ) ;
7346 ref . removeEventListener ( 'focusout' , onFocusout ) ;
7447 ref . removeEventListener ( 'touchstart' , pointerDown ) ;
0 commit comments