Skip to content

Commit 510f958

Browse files
authored
Close Palette Menu on Outside Click (#4719)
Signed-off-by: Subham Raj <[email protected]>
1 parent 3b48b2c commit 510f958

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

js/palette.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@ class Palette {
863863
this.fadedUpButton = null;
864864
this.fadedDownButton = null;
865865
this.count = 0;
866+
this._outsideClickListener = null;
866867
}
867868

868869
hide() {
@@ -948,6 +949,25 @@ class Palette {
948949
}
949950

950951
this._showMenuItems();
952+
953+
// Close palette menu on outside click
954+
if (this._outsideClickListener) {
955+
// Remove any existing listener before attaching a new one
956+
document.removeEventListener("click", this._outsideClickListener);
957+
}
958+
959+
this._outsideClickListener = (event) => {
960+
if (!this.menuContainer.contains(event.target)) {
961+
this.hideMenu(); // Calls your existing hideMenu() → _hideMenuItems()
962+
document.removeEventListener("click", this._outsideClickListener);
963+
this._outsideClickListener = null;
964+
}
965+
};
966+
967+
// Delay listener to avoid capturing the click that opened the menu
968+
setTimeout(() => {
969+
document.addEventListener("click", this._outsideClickListener);
970+
}, 0);
951971
}
952972

953973
_hideMenuItems() {

0 commit comments

Comments
 (0)