Skip to content

Commit

Permalink
Properly close the hover menu on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
oobabooga committed Sep 13, 2023
1 parent 9fc46d3 commit 7cd437e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
6 changes: 1 addition & 5 deletions css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,11 @@ div.svelte-362y77>*, div.svelte-362y77>.form>* {
bottom: 80%;
left: 0;
background-color: var(--background-fill-secondary);
z-index: 100;
z-index: 10000;
min-width: 300px;
flex-direction: column;
}

.hover-element:hover .hover-menu {
display: flex;
}

.hover-menu button {
width: 100%;
background: transparent !important;
Expand Down
64 changes: 56 additions & 8 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,24 +208,72 @@ for(i = 0; i < noBackgroundelements.length; i++) {

//------------------------------------------------
// Create the hover menu in the chat tab
// The show/hide events were adapted from:
// https://github.com/SillyTavern/SillyTavern/blob/6c8bd06308c69d51e2eb174541792a870a83d2d6/public/script.js
//------------------------------------------------
const buttonsInChat = document.getElementById("chat-tab").querySelectorAll("button");
const hoverMenu = document.getElementById('hover-menu');
var button = document.getElementById('hover-element-button');
var menu = document.getElementById('hover-menu');

function showMenu() {
menu.style.display = 'flex'; // Show the menu
}

function hideMenu() {
menu.style.display = 'none'; // Hide the menu
}

for (let i = 14; i >= 2; i--) {
const button = buttonsInChat[i];
hoverMenu.appendChild(button);
const thisButton = buttonsInChat[i];
menu.appendChild(thisButton);

if(i != 10) {
button.addEventListener("click", () => {
hoverMenu.style.display = 'none';
setTimeout(() => {
hoverMenu.style.display = '';
}, 2000);
thisButton.addEventListener("click", () => {
hideMenu();
});
}
}

function isMouseOverButtonOrMenu() {
return menu.matches(':hover') || button.matches(':hover');
}

button.addEventListener('mouseenter', function () {
showMenu();
});

button.addEventListener('click', function () {
showMenu();
});

// Add event listener for mouseleave on the button
button.addEventListener('mouseleave', function () {
// Delay to prevent menu hiding when the mouse leaves the button into the menu
setTimeout(function () {
if (!isMouseOverButtonOrMenu()) {
hideMenu();
}
}, 100);
});

// Add event listener for mouseleave on the menu
menu.addEventListener('mouseleave', function () {
// Delay to prevent menu hide when the mouse leaves the menu into the button
setTimeout(function () {
if (!isMouseOverButtonOrMenu()) {
hideMenu();
}
}, 100);
});

// Add event listener for click anywhere in the document
document.addEventListener('click', function (event) {
// Check if the click is outside the button/menu and the menu is visible
if (!isMouseOverButtonOrMenu() && menu.style.display === 'flex') {
hideMenu();
}
});

//------------------------------------------------
// Relocate the "Show controls" checkbox
//------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion modules/ui_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create_ui():

with gr.Row():
with gr.Column(scale=1):
gr.HTML(value='<div class="hover-element" onclick="void(0)"><span style="width: 100px; display: block">&#9776;</span><div class="hover-menu" id="hover-menu"></div>', elem_id='gr-hover')
gr.HTML(value='<div class="hover-element" onclick="void(0)"><span style="width: 100px; display: block" id="hover-element-button">&#9776;</span><div class="hover-menu" id="hover-menu"></div>', elem_id='gr-hover')

with gr.Column(scale=10):
shared.gradio['textbox'] = gr.Textbox(label='', placeholder='Send a message', elem_id='chat-input')
Expand Down

1 comment on commit 7cd437e

@Touch-Night
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend you to use button/menu.addEventListener('touchend', function () {}) instead of click, so touching the menu icon can also close the menu when the menu is flex

Please sign in to comment.