1
1
import type { MaybeRefOrGetter } from 'vue'
2
- import { ref , toRef , toValue , watch } from 'vue'
2
+ import { computed , shallowRef , toValue , watch } from 'vue'
3
3
import type { KeyFilter , KeyPredicate } from '@vueuse/core'
4
4
import { onKeyStroke , useEventListener } from '@vueuse/core'
5
5
@@ -14,6 +14,8 @@ export interface UseKeyPressOptions {
14
14
15
15
const inputTags = [ 'INPUT' , 'SELECT' , 'TEXTAREA' ]
16
16
17
+ const defaultDoc = typeof document !== 'undefined' ? document : null
18
+
17
19
export function isInputDOMNode ( event : KeyboardEvent ) : boolean {
18
20
const target = ( event . composedPath ?.( ) ?. [ 0 ] || event . target ) as HTMLElement
19
21
@@ -87,13 +89,9 @@ function useKeyOrCode(code: string, keysToWatch: string | string[]): KeyOrCode {
87
89
* @param options - Options object
88
90
*/
89
91
export function useKeyPress ( keyFilter : MaybeRefOrGetter < KeyFilter | boolean | null > , options ?: UseKeyPressOptions ) {
90
- const actInsideInputWithModifier = toRef ( ( ) => toValue ( options ?. actInsideInputWithModifier ) ?? false )
91
-
92
- const target = toRef ( ( ) => toValue ( options ?. target ) ?? window )
93
-
94
- const preventDefault = toRef ( ( ) => toValue ( options ?. preventDefault ) ?? true )
92
+ const target = computed ( ( ) => toValue ( options ?. target ) ?? defaultDoc )
95
93
96
- const isPressed = ref ( toValue ( keyFilter ) === true )
94
+ const isPressed = shallowRef ( toValue ( keyFilter ) === true )
97
95
98
96
let modifierPressed = false
99
97
@@ -121,9 +119,12 @@ export function useKeyPress(keyFilter: MaybeRefOrGetter<KeyFilter | boolean | nu
121
119
onKeyStroke (
122
120
( ...args ) => currentFilter ( ...args ) ,
123
121
( e ) => {
122
+ const actInsideInputWithModifier = toValue ( options ?. actInsideInputWithModifier ) ?? true
123
+ const preventDefault = toValue ( options ?. preventDefault ) ?? false
124
+
124
125
modifierPressed = wasModifierPressed ( e )
125
126
126
- const preventAction = ( ! modifierPressed || ( modifierPressed && ! actInsideInputWithModifier . value ) ) && isInputDOMNode ( e )
127
+ const preventAction = ( ! modifierPressed || ( modifierPressed && ! actInsideInputWithModifier ) ) && isInputDOMNode ( e )
127
128
128
129
if ( preventAction ) {
129
130
return
@@ -132,7 +133,7 @@ export function useKeyPress(keyFilter: MaybeRefOrGetter<KeyFilter | boolean | nu
132
133
const target = ( e . composedPath ?.( ) ?. [ 0 ] || e . target ) as Element | null
133
134
const isInteractiveElement = target ?. nodeName === 'BUTTON' || target ?. nodeName === 'A'
134
135
135
- if ( ! preventDefault . value && ( modifierPressed || ! isInteractiveElement ) ) {
136
+ if ( ! preventDefault && ( modifierPressed || ! isInteractiveElement ) ) {
136
137
e . preventDefault ( )
137
138
}
138
139
@@ -144,8 +145,10 @@ export function useKeyPress(keyFilter: MaybeRefOrGetter<KeyFilter | boolean | nu
144
145
onKeyStroke (
145
146
( ...args ) => currentFilter ( ...args ) ,
146
147
( e ) => {
148
+ const actInsideInputWithModifier = toValue ( options ?. actInsideInputWithModifier ) ?? true
149
+
147
150
if ( isPressed . value ) {
148
- const preventAction = ( ! modifierPressed || ( modifierPressed && ! actInsideInputWithModifier . value ) ) && isInputDOMNode ( e )
151
+ const preventAction = ( ! modifierPressed || ( modifierPressed && ! actInsideInputWithModifier ) ) && isInputDOMNode ( e )
149
152
150
153
if ( preventAction ) {
151
154
return
0 commit comments