Skip to content

Commit a2a009d

Browse files
committed
fix: live preview mode button dropdown not getting closed on second click
1 parent d56266a commit a2a009d

File tree

1 file changed

+41
-8
lines changed
  • src/extensionsIntegrated/Phoenix-live-preview

1 file changed

+41
-8
lines changed

src/extensionsIntegrated/Phoenix-live-preview/main.js

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ define(function (require, exports, module) {
8989
// live preview mode pref
9090
const PREFERENCE_LIVE_PREVIEW_MODE = "livePreviewMode";
9191

92+
// holds the dropdown instance
93+
let $dropdown = null;
94+
9295
/**
9396
* Get the appropriate default mode based on whether edit features are active
9497
* @returns {string} "highlight" if edit features inactive, "edit" if active
@@ -309,7 +312,7 @@ define(function (require, exports, module) {
309312
// this is to take care of invalid values in the pref file
310313
const currentMode = ["preview", "highlight", "edit"].includes(rawMode) ? rawMode : _getDefaultMode();
311314

312-
const dropdown = new DropdownButton.DropdownButton("", items, function(item, index) {
315+
$dropdown = new DropdownButton.DropdownButton("", items, function(item, index) {
313316
if (item === Strings.LIVE_PREVIEW_MODE_PREVIEW) {
314317
// using empty spaces to keep content aligned
315318
return currentMode === "preview" ? `✓ ${item}` : `${'\u00A0'.repeat(4)}${item}`;
@@ -333,25 +336,25 @@ define(function (require, exports, module) {
333336
});
334337

335338
// Append to document body for absolute positioning
336-
$("body").append(dropdown.$button);
339+
$("body").append($dropdown.$button);
337340

338341
// Position the dropdown at the mouse coordinates
339-
dropdown.$button.css({
342+
$dropdown.$button.css({
340343
position: "absolute",
341344
left: event.pageX + "px",
342345
top: event.pageY + "px",
343346
zIndex: 1000
344347
});
345348

346349
// Add a custom class to override the max-height
347-
dropdown.dropdownExtraClasses = "mode-context-menu";
350+
$dropdown.dropdownExtraClasses = "mode-context-menu";
348351

349-
dropdown.showDropdown();
352+
$dropdown.showDropdown();
350353

351354
$(".mode-context-menu").css("max-height", "300px");
352355

353356
// handle the option selection
354-
dropdown.on("select", function (e, item, index) {
357+
$dropdown.on("select", function (e, item, index) {
355358
// here we just set the preference
356359
// as the preferences listener will automatically handle the required changes
357360
if (index === 0) {
@@ -384,11 +387,28 @@ define(function (require, exports, module) {
384387
});
385388

386389
// Remove the button after the dropdown is hidden
387-
dropdown.$button.css({
390+
$dropdown.$button.css({
388391
display: "none"
389392
});
390393
}
391394

395+
/**
396+
* to close the overflow button's dropdown
397+
*/
398+
function _closeDropdown() {
399+
if ($dropdown) {
400+
if ($dropdown.$button) {
401+
$dropdown.$button.remove();
402+
}
403+
$dropdown = null;
404+
}
405+
}
406+
407+
function _handleLPModeBtnClick(e) {
408+
e.stopPropagation();
409+
$dropdown ? _closeDropdown() : _showModeSelectionDropdown(e);
410+
}
411+
392412
function _getTrustProjectPage() {
393413
const trustProjectMessage = StringUtils.format(Strings.TRUST_PROJECT,
394414
path.basename(ProjectManager.getProjectRoot().fullPath));
@@ -692,7 +712,7 @@ define(function (require, exports, module) {
692712
_popoutLivePreview("firefox");
693713
});
694714

695-
$modeBtn.on("click", _showModeSelectionDropdown);
715+
$modeBtn.on("click", _handleLPModeBtnClick);
696716
$previewBtn.on("click", _handlePreviewBtnClick);
697717

698718
_showOpenBrowserIcons();
@@ -1046,6 +1066,18 @@ define(function (require, exports, module) {
10461066
});
10471067
}
10481068

1069+
function _registerHandlers() {
1070+
// when clicked anywhere on the page we want to close the dropdown
1071+
$("html").on("click", function (e) {
1072+
if ($(e.target).closest("#livePreviewModeBtn").length) { return; }
1073+
_closeDropdown();
1074+
});
1075+
1076+
$(document).on("click", "#livePreviewModeBtn", function (e) {
1077+
_handleLPModeBtnClick(e);
1078+
});
1079+
}
1080+
10491081
AppInit.appReady(function () {
10501082
if(Phoenix.isSpecRunnerWindow){
10511083
return;
@@ -1088,6 +1120,7 @@ define(function (require, exports, module) {
10881120
Menus.AFTER, Commands.FILE_LIVE_FILE_PREVIEW);
10891121
fileMenu.addMenuDivider(Menus.BEFORE, Commands.FILE_LIVE_FILE_PREVIEW);
10901122

1123+
_registerHandlers();
10911124
// init live preview mode from saved preferences
10921125
_initializeMode();
10931126
// listen for pref changes

0 commit comments

Comments
 (0)