element-plus/packages/hooks/use-escape-keydown/index.ts
三咲智子 6503e55277
refactor(utils): migrate utils (#5949)
* refactor(utils-v2): migrate utils

* refactor(utils-v2): migrate utils

* refactor(utils-v2): migrate utils

* refactor(utils): remove

* refactor(utils): rename

* refactor(utils): move EVENT_CODE to constants

* refactor: remove generic
2022-02-11 11:03:15 +08:00

20 lines
521 B
TypeScript

import { onMounted, onBeforeUnmount } from 'vue'
import { on, off } from '@element-plus/utils'
import { EVENT_CODE } from '@element-plus/constants'
export const useEscapeKeydown = (handler?: (e: KeyboardEvent) => void) => {
const cachedHandler = (e: Event) => {
const event = e as KeyboardEvent
if (event.key === EVENT_CODE.esc) {
handler?.(event)
}
}
onMounted(() => {
on(document, 'keydown', cachedHandler)
})
onBeforeUnmount(() => {
off(document, 'keydown', cachedHandler)
})
}