Skip to content

Commit

Permalink
メニュー以外の場所をクリックしても閉じるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
oki07 committed Nov 23, 2024
1 parent 04e1ca8 commit 348f9d0
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/components/dropdownAction/sp-dropdown-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export class SpDropdownAction extends HTMLElement {
this.#menuElement.appendChild(this.#menuSlotElement);

this.#menuSlotElement.addEventListener("click", this.#closeMenu.bind(this));
window.addEventListener("click", this.#closeMenu.bind(this));

this.#baseElement.appendChild(this.#menuElement);
this.#baseElement.classList.add("base");
Expand All @@ -116,6 +117,7 @@ export class SpDropdownAction extends HTMLElement {

disconnectedCallback() {
this.#menuSlotElement.removeEventListener("click", this.#closeMenu.bind(this));
window.removeEventListener("click", this.#closeMenu.bind(this));
}

attributeChangedCallback(name: string, oldValue: string, newValue: string) {
Expand All @@ -140,12 +142,18 @@ export class SpDropdownAction extends HTMLElement {
}
}

#toggleMenuVisibility() {
#toggleMenuVisibility(event: MouseEvent) {
event.stopPropagation();

this.open = !this.open;
this.#updateAriaExpandedAttribute();
}

#closeMenu() {
if (!this.open) {
return
};

this.open = false;
this.#updateAriaExpandedAttribute();
}
Expand Down

0 comments on commit 348f9d0

Please sign in to comment.