mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-14 01:11:25 +08:00
20 lines
526 B
TypeScript
20 lines
526 B
TypeScript
|
import { onMounted, onBeforeUnmount } from 'vue'
|
||
|
import { on, off } from '@element-plus/utils/dom'
|
||
|
import { EVENT_CODE } from '@element-plus/utils/aria'
|
||
|
|
||
|
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)
|
||
|
})
|
||
|
}
|