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 348f9d0 commit f5eb0ea
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/components/dropdownAction/sp-dropdown-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class SpDropdownAction extends HTMLElement {
connectedCallback() {
this.#buttonElement.addEventListener(
"click",
this.#toggleMenuVisibility.bind(this),
this.#handleClickButton.bind(this),
);

this.#baseElement.appendChild(this.#buttonElement);
Expand All @@ -103,8 +103,8 @@ export class SpDropdownAction extends HTMLElement {
this.#menuElement.role = "menu";
this.#menuElement.appendChild(this.#menuSlotElement);

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

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

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

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

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

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

#closeMenu() {
if (!this.open) {
return
};
#handleClickMenu(event: MouseEvent) {
event.stopPropagation();

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

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

if (!this.contains(event.target as Node)) {
this.open = false;
this.#updateAriaExpandedAttribute();
}
}

#updateMenuDisplay() {
this.#menuElement.style.display = this.open && !this.disabled ? "block" : "none";
}
Expand Down

0 comments on commit f5eb0ea

Please sign in to comment.