Skip to content

Commit af0280f

Browse files
authored
Merge pull request #7971 from nextcloud-libraries/backport/7966/stable8
2 parents 1b828cc + 823f014 commit af0280f

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/composables/useHotKey/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ function shouldIgnoreEvent(event: KeyboardEvent): boolean {
5858
|| event.target.isContentEditable) {
5959
return true
6060
}
61-
/** Abort if any modal/dialog opened */
62-
return document.getElementsByClassName('modal-mask').length !== 0
61+
62+
/** Abort if any modal/dialog is opened AND visible */
63+
return Array.from(document.getElementsByClassName('modal-mask'))
64+
.filter((el) => el.checkVisibility())
65+
.length > 0
6366
}
6467

6568
type KeyboardEventHandler = (event: KeyboardEvent) => void

tests/unit/composables/useHotKey.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('useHotKey', () => {
1919

2020
afterEach(() => {
2121
mockCallback.mockReset()
22+
document.body.innerHTML = ''
2223
})
2324

2425
it('should register listener and invoke callback', () => {
@@ -39,6 +40,33 @@ describe('useHotKey', () => {
3940
expect(mockCallback).not.toHaveBeenCalled()
4041
})
4142

43+
it('should not invoke callback by default, when a modal is shown', () => {
44+
const modal = document.createElement('div')
45+
modal.className = 'modal-mask'
46+
document.body.appendChild(modal)
47+
modal.checkVisibility = () => true
48+
49+
const stop = useHotKey(true, mockCallback)
50+
triggerKeyDown({ key: 'a', code: 'KeyA' })
51+
stop()
52+
53+
expect(mockCallback).not.toHaveBeenCalled()
54+
})
55+
56+
it('should invoke callback by default, when a modal present but hidden', () => {
57+
const modal = document.createElement('div')
58+
modal.className = 'modal-mask'
59+
modal.style.display = 'none'
60+
document.body.appendChild(modal)
61+
modal.checkVisibility = () => false
62+
63+
const stop = useHotKey(true, mockCallback)
64+
triggerKeyDown({ key: 'a', code: 'KeyA' })
65+
stop()
66+
67+
expect(mockCallback).toHaveBeenCalled()
68+
})
69+
4270
describe('options', () => {
4371
it('should accept array of keys and invoke callback for all of them', () => {
4472
const stop = useHotKey(['a', 'b'], mockCallback)

0 commit comments

Comments
 (0)