1
1
2
+ function getFocusableChildren ( element : HTMLElement ) {
3
+ return Array . from ( element . querySelectorAll < HTMLElement > ( 'button:not([tabindex="-1"]), [href]:not([tabindex="-1"]), input:not([tabindex="-1"]), select:not([tabindex="-1"]), textarea:not([tabindex="-1"]), [tabindex]:not([tabindex="-1"])' ) )
4
+ }
5
+
2
6
// Exports
3
- export function focusfirst ( element : HTMLElement ) {
4
- const focusable = element . querySelectorAll < HTMLElement > ( 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])' )
5
- if ( focusable . length ) focusable [ 0 ] . focus ( )
6
- return { }
7
+ export function focusFirstChild ( element : HTMLElement ) {
8
+ const focusable = getFocusableChildren ( element )
9
+ if ( focusable . length ) {
10
+ focusable [ 0 ] . focus ( )
11
+ console . log ( 'FocusFirstChild focused' )
12
+ }
7
13
}
8
14
9
- export function focusthis ( element : HTMLElement ) {
15
+ export function focusOnLoad ( element : HTMLElement ) {
10
16
element . focus ( )
11
- return { }
17
+ console . log ( 'FocusOnLoad focused' )
12
18
}
13
19
14
- export function focusonhover ( element : HTMLElement ) {
20
+ export function focusOnHover ( element : HTMLElement ) {
15
21
function onHover ( ) {
16
22
element . focus ( )
23
+ console . log ( 'FocusOnHover focused' )
17
24
}
18
25
19
26
element . addEventListener ( 'mouseenter' , onHover )
@@ -25,30 +32,24 @@ export function focusonhover(element: HTMLElement) {
25
32
}
26
33
}
27
34
28
- export function losefocus ( element : HTMLElement , callback : ( ) => void ) {
29
- function checkFocus ( ) {
30
- let had_focus = has_focus
31
- has_focus = element . contains ( document . activeElement ) ||
32
- element === document . activeElement ||
33
- document . activeElement === document . body
34
- if ( had_focus && ! has_focus ) callback ( )
35
+ export function focusOnKeydown ( element : HTMLElement , key ?: string ) {
36
+ function onKeyDown ( event : KeyboardEvent ) {
37
+ if ( event . key === 'Tab' || event . key === 'Enter' || key && event . key !== key ) return
38
+ element . focus ( )
39
+ console . log ( 'FocusOnKeydown focused' )
35
40
}
36
41
37
- let has_focus = element . contains ( document . activeElement )
38
- document . addEventListener ( 'focusin' , checkFocus )
39
- document . addEventListener ( 'focusout' , checkFocus )
42
+ document . addEventListener ( 'keydown' , onKeyDown )
40
43
41
44
return {
42
45
destroy ( ) {
43
- document . removeEventListener ( 'focusin' , checkFocus )
44
- document . removeEventListener ( 'focusout' , checkFocus )
46
+ document . removeEventListener ( 'keydown' , onKeyDown )
45
47
}
46
48
}
47
49
}
48
50
49
- export function loopfocus ( element : HTMLElement ) {
50
- const focusable = Array . from ( element . querySelectorAll < HTMLElement > ( 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])' ) )
51
- if ( ! focusable . length ) return { }
51
+ export function loopFocus ( element : HTMLElement ) {
52
+ let focusable = getFocusableChildren ( element )
52
53
let index = focusable . findIndex ( e => e === document . activeElement )
53
54
54
55
function onFocusIn ( event : FocusEvent ) {
@@ -61,15 +62,12 @@ export function loopfocus(element: HTMLElement) {
61
62
62
63
event . preventDefault ( )
63
64
64
- // Go to the next focusable element that still exists in the dom
65
- let next = ( index + ( event . shiftKey ? - 1 : 1 ) + focusable . length ) % focusable . length
66
- while ( ! element . contains ( focusable [ next ] ) ) {
67
- next = ( next + ( event . shiftKey ? - 1 : 1 ) + focusable . length ) % focusable . length
68
- }
69
-
70
- // Focus
71
- index = next
65
+ // Go to the next focusable element
66
+ index = ( index + ( event . shiftKey ? - 1 : 1 ) + focusable . length ) % focusable . length
67
+ while ( ! element . contains ( focusable [ index ] ) )
68
+ index = ( index + ( event . shiftKey ? - 1 : 1 ) + focusable . length ) % focusable . length
72
69
focusable [ index ] . focus ( )
70
+ console . log ( 'LoopFocus focused' )
73
71
}
74
72
}
75
73
@@ -83,3 +81,23 @@ export function loopfocus(element: HTMLElement) {
83
81
}
84
82
}
85
83
}
84
+
85
+ export function preventFocus ( element : HTMLElement ) {
86
+ let last_focus = document . activeElement as HTMLElement | null
87
+
88
+ function onFocus ( event : FocusEvent ) {
89
+ if ( element === event . target || element . contains ( event . target as HTMLElement ) ) {
90
+ last_focus ?. focus ( )
91
+ } else {
92
+ last_focus = document . activeElement as HTMLElement | null
93
+ }
94
+ }
95
+
96
+ document . addEventListener ( 'focus' , onFocus )
97
+
98
+ return {
99
+ destroy ( ) {
100
+ document . removeEventListener ( 'focus' , onFocus )
101
+ }
102
+ }
103
+ }
0 commit comments