Skip to content

Commit

Permalink
Remove empty mouseover listeners added for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit2sharma95 committed Mar 24, 2021
1 parent 7c26426 commit 31f508a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions js/src/dropdown.js
Expand Up @@ -218,6 +218,13 @@ class Dropdown extends BaseComponent {
return
}

// If this is a touch-enabled device we remove the extra
// empty mouseover listeners we added for iOS support
if ('ontouchstart' in document.documentElement) {
[].concat(...document.body.children)
.forEach(elem => EventHandler.off(elem, 'mouseover', null, noop()))
}

if (this._popper) {
this._popper.destroy()
}
Expand Down
33 changes: 33 additions & 0 deletions js/tests/unit/dropdown.spec.js
Expand Up @@ -895,6 +895,39 @@ describe('Dropdown', () => {
done()
})
})

it('should remove event listener on touch-enabled device that was added in show method', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Dropdwon item</a>',
' </div>',
'</div>'
].join('')

const defaultValueOnTouchStart = document.documentElement.ontouchstart
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown)

document.documentElement.ontouchstart = () => {}
spyOn(EventHandler, 'off')

btnDropdown.addEventListener('shown.bs.dropdown', () => {
dropdown.hide()
})

btnDropdown.addEventListener('hidden.bs.dropdown', () => {
expect(btnDropdown.classList.contains('show')).toEqual(false)
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('false')
expect(EventHandler.off).toHaveBeenCalled()

document.documentElement.ontouchstart = defaultValueOnTouchStart
done()
})

dropdown.show()
})
})

describe('dispose', () => {
Expand Down

0 comments on commit 31f508a

Please sign in to comment.