Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(use-composable): add new useEscapeKeydown #248

Merged
merged 4 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/use-composable/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { usePrevious } from './usePrevious'
export { useRef } from './useRef'
export { unrefElement } from './unrefElement'
export { useId } from './useId'
export { useEscapeKeydown } from './useEscapeKeydown'
export type { MaybeComputedElementRef } from './unrefElement'

export { computedEager, syncRef }
25 changes: 25 additions & 0 deletions packages/core/use-composable/src/useEscapeKeydown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { watchEffect } from 'vue'

// import { useCallbackRef } from '@oku-ui/use-composable'
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(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape')
onEscapeKeyDown(event)
}
ownerDocument.addEventListener('keydown', handleKeyDown)
return () => ownerDocument.removeEventListener('keydown', handleKeyDown)
Cr0zy07 marked this conversation as resolved.
Show resolved Hide resolved
})
}

export { useEscapeKeydown }
Loading