Skip to content

Commit

Permalink
feat(dropdown): close dropdown with a click outside the element or wi…
Browse files Browse the repository at this point in the history
…th pressing escape (#146)
  • Loading branch information
daenub authored Mar 18, 2024
1 parent fadafde commit 8b07146
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/components/dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,29 @@ export class LeuDropdown extends LitElement {
this.menuItems = []
}

connectedCallback() {
super.connectedCallback()
this.addEventListener("keyup", this._keyUpHandler)
document.addEventListener("click", this._documentClickHandler)
}

disconnectedCallback() {
super.disconnectedCallback()
this._removeMenuItemListeners()
this.removeEventListener("keyup", this._keyUpHandler)
document.removeEventListener("click", this._documentClickHandler)
}

_documentClickHandler = (event) => {
if (!this.contains(event.target)) {
this.expanded = false
}
}

_keyUpHandler(event) {
if (event.key === "Escape") {
this.expanded = false
}
}

_handleSlotChange() {
Expand Down

0 comments on commit 8b07146

Please sign in to comment.