Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor dropdown's hide functionality #33466

Merged
merged 4 commits into from
Apr 18, 2021
Merged
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
88 changes: 35 additions & 53 deletions js/src/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,28 +212,7 @@ class Dropdown extends BaseComponent {
relatedTarget: this._element
}

const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE, relatedTarget)

if (hideEvent.defaultPrevented) {
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', noop))
}

if (this._popper) {
this._popper.destroy()
}

this._menu.classList.toggle(CLASS_NAME_SHOW)
this._element.classList.toggle(CLASS_NAME_SHOW)
this._element.setAttribute('aria-expanded', 'false')
Manipulator.removeDataAttribute(this._menu, 'popper')
EventHandler.trigger(this._element, EVENT_HIDDEN, relatedTarget)
this._completeHide(relatedTarget)
}

dispose() {
Expand Down Expand Up @@ -263,6 +242,30 @@ class Dropdown extends BaseComponent {
})
}

_completeHide(relatedTarget) {
const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE, relatedTarget)
if (hideEvent.defaultPrevented) {
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', noop))
}

if (this._popper) {
this._popper.destroy()
}

this._menu.classList.remove(CLASS_NAME_SHOW)
this._element.classList.remove(CLASS_NAME_SHOW)
this._element.setAttribute('aria-expanded', 'false')
Manipulator.removeDataAttribute(this._menu, 'popper')
EventHandler.trigger(this._element, EVENT_HIDDEN, relatedTarget)
}

_getConfig(config) {
config = {
...this.constructor.Default,
Expand Down Expand Up @@ -421,56 +424,35 @@ class Dropdown extends BaseComponent {

for (let i = 0, len = toggles.length; i < len; i++) {
const context = Data.get(toggles[i], DATA_KEY)
const relatedTarget = {
relatedTarget: toggles[i]
}

if (event && event.type === 'click') {
relatedTarget.clickEvent = event
}

if (!context) {
continue
}

const dropdownMenu = context._menu
if (!toggles[i].classList.contains(CLASS_NAME_SHOW)) {
if (!context._element.classList.contains(CLASS_NAME_SHOW)) {
continue
rohit2sharma95 marked this conversation as resolved.
Show resolved Hide resolved
}

const relatedTarget = {
relatedTarget: context._element
}

if (event) {
// Don't close the menu if the clicked element or one of its parents is the dropdown button
if ([context._element].some(element => event.composedPath().includes(element))) {
continue
}

// Tab navigation through the dropdown menu shouldn't close the menu
if (event.type === 'keyup' && event.key === TAB_KEY && dropdownMenu.contains(event.target)) {
if (event.type === 'keyup' && event.key === TAB_KEY && context._menu.contains(event.target)) {
continue
}
}

const hideEvent = EventHandler.trigger(toggles[i], EVENT_HIDE, relatedTarget)
if (hideEvent.defaultPrevented) {
continue
}

// 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', noop))
}

if (context._popper) {
context._popper.destroy()
if (event.type === 'click') {
relatedTarget.clickEvent = event
}
}

dropdownMenu.classList.remove(CLASS_NAME_SHOW)
toggles[i].classList.remove(CLASS_NAME_SHOW)
toggles[i].setAttribute('aria-expanded', 'false')
Manipulator.removeDataAttribute(dropdownMenu, 'popper')
EventHandler.trigger(toggles[i], EVENT_HIDDEN, relatedTarget)
context._completeHide(relatedTarget)
}
}

Expand Down