Skip to content

Commit 6d98b41

Browse files
committed
refactor(core): allow passing target to useKeyPress
1 parent af93fd2 commit 6d98b41

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

packages/core/src/composables/useKeyPress.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type { MaybeRefOrGetter } from 'vue'
2-
import { ref, toValue, watch } from 'vue'
2+
import { onMounted, ref, toValue, watch } from 'vue'
33
import type { KeyFilter, KeyPredicate } from '@vueuse/core'
44
import { onKeyStroke, useEventListener } from '@vueuse/core'
55
import { useWindow } from './useWindow'
66

77
export interface UseKeyPressOptions {
88
actInsideInputWithModifier?: MaybeRefOrGetter<boolean>
9+
target?: MaybeRefOrGetter<EventTarget>
910
}
1011

1112
export function isInputDOMNode(event: KeyboardEvent): boolean {
@@ -68,22 +69,19 @@ function useKeyOrCode(code: string, keysToWatch: string | string[]) {
6869
return keysToWatch.includes(code) ? 'code' : 'key'
6970
}
7071

72+
const window = useWindow()
73+
7174
/**
72-
* Reactive key press state
75+
* Composable that returns a boolean value if a key is pressed
7376
*
74-
* todo: make this public?
7577
* @internal
76-
* @param keyFilter - Can be a boolean, a string or an array of strings. If it's a boolean, it will always return that value. If it's a string, it will return true if the key is pressed. If it's an array of strings, it will return true if any of the keys are pressed, or a combination is pressed (e.g. ['ctrl+a', 'ctrl+b'])
77-
* @param onChange - Callback function that will be called when the key state changes
78+
* @param keyFilter - Can be a boolean, a string, an array of strings or a function that returns a boolean. If it's a boolean, it will act as if the key is always pressed. If it's a string, it will return true if a key matching that string is pressed. If it's an array of strings, it will return true if any of the strings match a key being pressed, or a combination (e.g. ['ctrl+a', 'ctrl+b'])
7879
* @param options - Options object
7980
*/
8081
export function useKeyPress(
8182
keyFilter: MaybeRefOrGetter<KeyFilter | null>,
82-
onChange?: (keyPressed: boolean) => void,
83-
options: UseKeyPressOptions = { actInsideInputWithModifier: true },
83+
options: UseKeyPressOptions = { actInsideInputWithModifier: true, target: window },
8484
) {
85-
const window = useWindow()
86-
8785
const isPressed = ref(toValue(keyFilter) === true)
8886

8987
let modifierPressed = false
@@ -92,12 +90,6 @@ export function useKeyPress(
9290

9391
let currentFilter = createKeyFilterFn(toValue(keyFilter))
9492

95-
watch(isPressed, (isKeyPressed, wasPressed) => {
96-
if (isKeyPressed !== wasPressed) {
97-
onChange?.(isKeyPressed)
98-
}
99-
})
100-
10193
watch(
10294
() => toValue(keyFilter),
10395
(nextKeyFilter, previousKeyFilter) => {
@@ -113,10 +105,8 @@ export function useKeyPress(
113105
},
114106
)
115107

116-
useEventListener(window, 'blur', () => {
117-
if (toValue(keyFilter) !== true) {
118-
isPressed.value = false
119-
}
108+
onMounted(() => {
109+
useEventListener(window, ['blur', 'contextmenu'], reset)
120110
})
121111

122112
onKeyStroke(
@@ -134,7 +124,7 @@ export function useKeyPress(
134124

135125
isPressed.value = true
136126
},
137-
{ eventName: 'keydown' },
127+
{ eventName: 'keydown', target: options.target },
138128
)
139129

140130
onKeyStroke(
@@ -150,11 +140,9 @@ export function useKeyPress(
150140
reset()
151141
}
152142
},
153-
{ eventName: 'keyup' },
143+
{ eventName: 'keyup', target: options.target },
154144
)
155145

156-
return isPressed
157-
158146
function reset() {
159147
modifierPressed = false
160148

@@ -184,4 +172,6 @@ export function useKeyPress(
184172

185173
return keyFilter
186174
}
175+
176+
return isPressed
187177
}

packages/core/src/composables/useWindow.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ type UseWindow = Window & typeof globalThis & { chrome?: any }
88
export function useWindow(): UseWindow {
99
if (typeof window !== 'undefined') {
1010
return window as UseWindow
11-
} else {
12-
return {
13-
chrome: false,
14-
addEventListener(..._: Parameters<Window['addEventListener']>) {
15-
// do nothing
16-
},
17-
} as UseWindow
1811
}
12+
13+
return {
14+
chrome: false,
15+
addEventListener(..._: Parameters<Window['addEventListener']>) {
16+
// do nothing
17+
},
18+
} as UseWindow
1919
}

0 commit comments

Comments
 (0)