Handle highlight sidebar and popover as one unit for closing them when

clicking elsewhere

Resolves MS-867 and MS-868 #Complete
This commit is contained in:
Ramon Wenger 2024-01-31 18:03:14 +01:00
parent 2f6589680e
commit 8506392c3f
2 changed files with 17 additions and 1 deletions

View File

@ -27,8 +27,17 @@ export default {
const mountEl = document.createElement('div'); const mountEl = document.createElement('div');
app?.appendChild(mountEl); app?.appendChild(mountEl);
const clickOutsideElementListener = (event: Event) => {
const isInsidePopover = document.querySelector('.highlight-popover')?.contains(event.target as Node);
const isInsideSidebar = mountEl.contains(event.target as Node);
if (!(isInsidePopover || isInsideSidebar)) {
cleanUp();
}
};
const cleanUp = () => { const cleanUp = () => {
mountEl?.parentNode?.removeChild(mountEl); mountEl?.parentNode?.removeChild(mountEl);
document.removeEventListener('click', clickOutsideElementListener);
sidebar.unmount(); sidebar.unmount();
}; };
@ -48,5 +57,9 @@ export default {
sidebar.directive('auto-grow', autoGrow); // todo: can this be done a different way? maybe use this declaration inside the component itself sidebar.directive('auto-grow', autoGrow); // todo: can this be done a different way? maybe use this declaration inside the component itself
sidebar.mount(mountEl); sidebar.mount(mountEl);
setTimeout(() => {
document.addEventListener('click', clickOutsideElementListener);
}, 1);
}, },
}; };

View File

@ -25,7 +25,10 @@ export default {
const clickOutsideElementListener = (event: Event) => { const clickOutsideElementListener = (event: Event) => {
// inspired by https://stackoverflow.com/a/3028037/6071058 // inspired by https://stackoverflow.com/a/3028037/6071058
if (!mountEl.contains(event.target as Node)) { const isInsidePopover = mountEl.contains(event.target as Node); // the click happened inside the Popover
// const isInsideWrapper = wrapper.contains(event.target as Node); // the click happened inside the wrapper
const isInsideSidebar = document.querySelector('.highlight-sidebar')?.contains(event.target as Node); // the click happened inside the HighlightSidebar
if (!(isInsidePopover || isInsideSidebar)) {
_reject(); _reject();
cleanUp(); cleanUp();
} }