X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/e711290d8b1ce06b38e0560248806e8de2077870..refs/pull/4794/head:/resources/js/components/dropdown.js diff --git a/resources/js/components/dropdown.js b/resources/js/components/dropdown.js index 8444bf038..4efd428ac 100644 --- a/resources/js/components/dropdown.js +++ b/resources/js/components/dropdown.js @@ -34,6 +34,7 @@ export class Dropdown extends Component { let heightOffset = 0; const toggleHeight = this.toggle.getBoundingClientRect().height; const dropUpwards = menuOriginalRect.bottom > window.innerHeight; + const containerRect = this.container.getBoundingClientRect(); // If enabled, Move to body to prevent being trapped within scrollable sections if (this.moveMenu) { @@ -41,22 +42,30 @@ export class Dropdown extends Component { this.menu.style.position = 'fixed'; this.menu.style.width = `${menuOriginalRect.width}px`; this.menu.style.left = `${menuOriginalRect.left}px`; - heightOffset = dropUpwards ? (window.innerHeight - menuOriginalRect.top - toggleHeight / 2) : menuOriginalRect.top; + if (dropUpwards) { + heightOffset = (window.innerHeight - menuOriginalRect.top - toggleHeight / 2); + } else { + heightOffset = menuOriginalRect.top; + } } // Adjust menu to display upwards if near the bottom of the screen if (dropUpwards) { this.menu.style.top = 'initial'; this.menu.style.bottom = `${heightOffset}px`; + const maxHeight = (window.innerHeight - 40) - (window.innerHeight - containerRect.bottom); + this.menu.style.maxHeight = `${Math.floor(maxHeight)}px`; } else { this.menu.style.top = `${heightOffset}px`; this.menu.style.bottom = 'initial'; + const maxHeight = (window.innerHeight - 40) - containerRect.top; + this.menu.style.maxHeight = `${Math.floor(maxHeight)}px`; } // Set listener to hide on mouse leave or window click this.menu.addEventListener('mouseleave', this.hide); - window.addEventListener('click', event => { - if (!this.menu.contains(event.target)) { + window.addEventListener('click', clickEvent => { + if (!this.menu.contains(clickEvent.target)) { this.hide(); } }); @@ -87,6 +96,7 @@ export class Dropdown extends Component { this.toggle.setAttribute('aria-expanded', 'false'); this.menu.style.top = ''; this.menu.style.bottom = ''; + this.menu.style.maxHeight = ''; if (this.moveMenu) { this.menu.style.position = ''; @@ -128,6 +138,7 @@ export class Dropdown extends Component { onSelect(this.toggle, event => { event.stopPropagation(); + event.preventDefault(); this.show(event); if (event instanceof KeyboardEvent) { keyboardNavHandler.focusNext();