forked from oku-ui/primitives
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(use-composable): add new useEscapeKeydown (oku-ui#248)
* feat: add useEscapeKeydown * add a cleanup callback * add test * cleanup
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
import { useEscapeKeydown } from './useEscapeKeydown' | ||
|
||
describe('useEscapeKeydown', () => { | ||
let onEscapeKeyDown: any | ||
let ownerDocument: Document | ||
|
||
beforeEach(() => { | ||
onEscapeKeyDown = vi.fn() | ||
ownerDocument = globalThis.document | ||
}) | ||
|
||
it('should call onEscapeKeyDown when the escape key is pressed', () => { | ||
onEscapeKeyDown = vi.fn() | ||
ownerDocument = globalThis.document | ||
|
||
useEscapeKeydown(onEscapeKeyDown, ownerDocument) | ||
|
||
const escapeKeyEvent = new KeyboardEvent('keydown', { key: 'Escape' }) | ||
ownerDocument.dispatchEvent(escapeKeyEvent) | ||
|
||
expect(onEscapeKeyDown).toHaveBeenCalledTimes(1) | ||
expect(onEscapeKeyDown).toHaveBeenCalledWith(escapeKeyEvent) | ||
}) | ||
|
||
it('should not call onEscapeKeyDown for other key presses', () => { | ||
useEscapeKeydown(onEscapeKeyDown, ownerDocument) | ||
|
||
const otherKeyEvent = new KeyboardEvent('keydown', { key: 'Enter' }) | ||
ownerDocument.dispatchEvent(otherKeyEvent) | ||
|
||
expect(onEscapeKeyDown).not.toHaveBeenCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { watchEffect } from 'vue' | ||
import { useCallbackRef } from './useCallbackRef' | ||
|
||
/** | ||
* Listens for when the escape key is down | ||
*/ | ||
function useEscapeKeydown( | ||
onEscapeKeyDownProp?: (event: KeyboardEvent) => void, | ||
ownerDocument: Document = globalThis?.document, | ||
) { | ||
const onEscapeKeyDown = useCallbackRef(onEscapeKeyDownProp) | ||
|
||
watchEffect((onInvalidate) => { | ||
const handleKeyDown = (event: KeyboardEvent) => { | ||
if (event.key === 'Escape') | ||
onEscapeKeyDown(event) | ||
} | ||
ownerDocument.addEventListener('keydown', handleKeyDown) | ||
onInvalidate(() => ownerDocument.removeEventListener('keydown', handleKeyDown)) | ||
}) | ||
} | ||
|
||
export { useEscapeKeydown } |