@fiscozen/composables 0.1.9 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fiscozen/composables",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "Design System utility composables",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
@@ -26,8 +26,8 @@
26
26
  "vitest": "^1.2.0",
27
27
  "vue-tsc": "^1.8.25",
28
28
  "@fiscozen/tsconfig": "^0.1.0",
29
- "@fiscozen/eslint-config": "^0.1.0",
30
- "@fiscozen/prettier-config": "^0.1.0"
29
+ "@fiscozen/prettier-config": "^0.1.0",
30
+ "@fiscozen/eslint-config": "^0.1.0"
31
31
  },
32
32
  "license": "ISC",
33
33
  "scripts": {
@@ -2,3 +2,4 @@ export * from './useFloating'
2
2
  export * from './useMediaQuery'
3
3
  export * from './useBreakpoints'
4
4
  export * from './useClickOutside'
5
+ export * from './useKeyDown'
@@ -0,0 +1,26 @@
1
+ import { onBeforeUnmount, onMounted, Ref } from 'vue'
2
+
3
+ function useKeyDown(component: Ref<HTMLElement | undefined>, callback: (event: KeyboardEvent) => void) {
4
+ // fail early if any of the required params is missing
5
+ if (!component) {
6
+ throw new Error('A target component has to be provided.')
7
+ }
8
+
9
+ if (!callback || typeof callback !== 'function') {
10
+ throw new Error('A callback has to be provided.')
11
+ }
12
+
13
+ const listener = (event: KeyboardEvent) => {
14
+ callback(event)
15
+ }
16
+
17
+ onMounted(() => {
18
+ component.value!.addEventListener('keydown', listener)
19
+ })
20
+
21
+ onBeforeUnmount(() => {
22
+ component.value!.removeEventListener('keydown', listener)
23
+ })
24
+ }
25
+
26
+ export { useKeyDown }