Skip to content

Commit

Permalink
Fix backdrop for dropdown menu on mobile (#21578)
Browse files Browse the repository at this point in the history
- Create backdrop only if the menu is actually open (do not create it if the show event is prevented)
- Drop the backdrop only when the corresponding menu is closed (do not remove if there is no menu to close or if the hide event is prevented)
  • Loading branch information
pvdlg authored and mdo committed Mar 19, 2017
1 parent 5a5ab4c commit f2f8051
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions js/src/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,6 @@ const Dropdown = (($) => {
return false
}

if ('ontouchstart' in document.documentElement &&
!$(parent).closest(Selector.NAVBAR_NAV).length) {

// if mobile we use a backdrop because click events don't delegate
const dropdown = document.createElement('div')
dropdown.className = ClassName.BACKDROP
$(dropdown).insertBefore(this)
$(dropdown).on('click', Dropdown._clearMenus)
}

const relatedTarget = {
relatedTarget : this
}
Expand All @@ -118,6 +108,17 @@ const Dropdown = (($) => {
return false
}

// set the backdrop only if the dropdown menu will be opened
if ('ontouchstart' in document.documentElement &&
!$(parent).closest(Selector.NAVBAR_NAV).length) {

// if mobile we use a backdrop because click events don't delegate
const dropdown = document.createElement('div')
dropdown.className = ClassName.BACKDROP
$(dropdown).insertBefore(this)
$(dropdown).on('click', Dropdown._clearMenus)
}

this.focus()
this.setAttribute('aria-expanded', true)

Expand Down Expand Up @@ -166,11 +167,6 @@ const Dropdown = (($) => {
return
}

const backdrop = $(Selector.BACKDROP)[0]
if (backdrop) {
backdrop.parentNode.removeChild(backdrop)
}

const toggles = $.makeArray($(Selector.DATA_TOGGLE))

for (let i = 0; i < toggles.length; i++) {
Expand All @@ -195,6 +191,12 @@ const Dropdown = (($) => {
continue
}

// remove backdrop only if the dropdown menu will be hidden
const backdrop = $(parent).find(Selector.BACKDROP)[0]
if (backdrop) {
backdrop.parentNode.removeChild(backdrop)
}

toggles[i].setAttribute('aria-expanded', 'false')

$(parent)
Expand Down

0 comments on commit f2f8051

Please sign in to comment.