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

fixes #456 dropdown open/close bug #462

Merged
merged 1 commit into from
Mar 8, 2024
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
29 changes: 17 additions & 12 deletions src/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,24 @@ export class Dropdown extends Component<DropdownOptions> implements Openable {
}

_setupTemporaryEventHandlers() {
// Use capture phase event handler to prevent click
document.body.addEventListener('click', this._handleDocumentClick, true);
document.body.addEventListener('click', this._handleDocumentClick);
document.body.addEventListener('touchmove', this._handleDocumentTouchmove);
this.dropdownEl.addEventListener('keydown', this._handleDropdownKeydown);
}

_removeTemporaryEventHandlers() {
// Use capture phase event handler to prevent click
document.body.removeEventListener('click', this._handleDocumentClick, true);
document.body.removeEventListener('click', this._handleDocumentClick);
document.body.removeEventListener('touchmove', this._handleDocumentTouchmove);
this.dropdownEl.removeEventListener('keydown', this._handleDropdownKeydown);
}

_handleClick = (e: MouseEvent) => {
e.preventDefault();
this.open();
if (this.isOpen) {
this.close();
} else {
this.open();
}
}

_handleMouseEnter = () => {
Expand Down Expand Up @@ -245,17 +247,18 @@ export class Dropdown extends Component<DropdownOptions> implements Openable {
!this.isTouchMoving
) {
// isTouchMoving to check if scrolling on mobile.
//setTimeout(() => {
this.close();
//}, 0);
}
else if (
target.closest('.dropdown-trigger') ||
!target.closest('.dropdown-content')
) {
//setTimeout(() => {
this.close();
//}, 0);
// Do this one frame later so that if the element clicked also triggers _handleClick
// For example, if a label for a select was clicked, that we don't close/open the dropdown
setTimeout(() => {
if (this.isOpen) {
this.close();
}
}, 0);
}
this.isTouchMoving = false;
}
Expand Down Expand Up @@ -592,7 +595,9 @@ export class Dropdown extends Component<DropdownOptions> implements Openable {
this.dropdownEl.style.display = 'block';
this._placeDropdown();
this._animateIn();
this._setupTemporaryEventHandlers();
// Do this one frame later so that we don't bind an event handler that's immediately
// called when the event bubbles up to the document and closes the dropdown
setTimeout(() => this._setupTemporaryEventHandlers(), 0);
}

/**
Expand Down
Loading