aboutsummaryrefslogtreecommitdiff
path: root/js/src
diff options
context:
space:
mode:
authorPierre Vanduynslager <[email protected]>2017-03-18 20:41:13 -0400
committerMark Otto <[email protected]>2017-03-18 17:41:13 -0700
commitf2f805128508e82f0adc6e57b421dfb46d65a434 (patch)
treef09c68d3ad52e8d60c83335a3c2f388ae9d517fd /js/src
parent5a5ab4c0de0981680fa1764e33967c8d3d4cacf5 (diff)
downloadbootstrap-f2f805128508e82f0adc6e57b421dfb46d65a434.tar.xz
bootstrap-f2f805128508e82f0adc6e57b421dfb46d65a434.zip
Fix backdrop for dropdown menu on mobile (#21578)
- 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)
Diffstat (limited to 'js/src')
-rw-r--r--js/src/dropdown.js32
1 files changed, 17 insertions, 15 deletions
diff --git a/js/src/dropdown.js b/js/src/dropdown.js
index 1660d4257..644273a0a 100644
--- a/js/src/dropdown.js
+++ b/js/src/dropdown.js
@@ -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
}
@@ -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)
@@ -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++) {
@@ -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)