diff --git a/js/main.js b/js/main.js index 06a44c79..a8018175 100644 --- a/js/main.js +++ b/js/main.js @@ -344,6 +344,21 @@ document.addEventListener("click", function (event) { if (event.target.classList.contains("pfp_character")) { toggleBigPicture(); } + + // Handle sidebar clicks on mobile + if (isMobile()) { + // Check if the click did NOT originate from any of the specified toggle buttons or elements + if ( + target.closest("#navigation-toggle") !== navigationToggle && + target.closest("#past-chats-toggle") !== pastChatsToggle && + target.closest("#chat-controls-toggle") !== chatControlsToggle && + target.closest(".header_bar") !== headerBar && + target.closest("#past-chats-row") !== pastChatsRow && + target.closest("#chat-controls") !== chatControlsRow + ) { + handleIndividualSidebarClose(event); + } + } }); //------------------------------------------------ @@ -656,7 +671,26 @@ headerBar.appendChild(navigationToggle); const pastChatsToggle = document.getElementById("past-chats-toggle"); const chatControlsToggle = document.getElementById("chat-controls-toggle"); -function toggleSidebar(sidebar, toggle) { +function handleIndividualSidebarClose(event) { + const target = event.target; + + // Close navigation bar if click is outside and it is open + if (!headerBar.contains(target) && !headerBar.classList.contains("sidebar-hidden")) { + toggleSidebar(headerBar, navigationToggle, true); + } + + // Close past chats row if click is outside and it is open + if (!pastChatsRow.contains(target) && !pastChatsRow.classList.contains("sidebar-hidden")) { + toggleSidebar(pastChatsRow, pastChatsToggle, true); + } + + // Close chat controls row if click is outside and it is open + if (!chatControlsRow.contains(target) && !chatControlsRow.classList.contains("sidebar-hidden")) { + toggleSidebar(chatControlsRow, chatControlsToggle, true); + } +} + +function toggleSidebar(sidebar, toggle, forceClose = false) { const isCurrentlyHidden = sidebar.classList.contains("sidebar-hidden"); const shouldClose = !isCurrentlyHidden;