Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions src/composables/useHotKey/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ function shouldIgnoreEvent(event: KeyboardEvent, options: UseHotKeyOptions): boo
return false
}

/** Abort if any modal/dialog opened */
return document.getElementsByClassName('modal-mask').length !== 0
/** Abort if any modal/dialog is opened AND visible */
return Array.prototype.filter.call(
document.getElementsByClassName('modal-mask'),
(el) => el.checkVisibility(),
).length > 0
}

type KeyboardEventHandler = (event: KeyboardEvent) => void
Expand Down
17 changes: 16 additions & 1 deletion tests/unit/composables/useHotKey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ describe('useHotKey', () => {
expect(mockCallback).not.toHaveBeenCalled()
})

it('should not invoke callback by default, when a modal is shown', () => {
it('should not invoke callback by default, when a modal is shown', async () => {
const modal = document.createElement('div')
modal.className = 'modal-mask'
document.body.appendChild(modal)
modal.checkVisibility = () => true

const stop = useHotKey(true, mockCallback)
triggerKeyDown({ key: 'a', code: 'KeyA' })
Expand All @@ -52,6 +53,20 @@ describe('useHotKey', () => {
expect(mockCallback).not.toHaveBeenCalled()
})

it('should invoke callback by default, when a modal present but hidden', async () => {
const modal = document.createElement('div')
modal.className = 'modal-mask'
modal.style.display = 'none'
document.body.appendChild(modal)
modal.checkVisibility = () => false

const stop = useHotKey(true, mockCallback)
triggerKeyDown({ key: 'a', code: 'KeyA' })
stop()

expect(mockCallback).toHaveBeenCalled()
})

it('should invoke callback if modals are allowed, when a modal is shown', () => {
const modal = document.createElement('div')
modal.className = 'modal-mask'
Expand Down
Loading