You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by hn...@apache.org on 2025/03/05 07:37:07 UTC

(myfaces-tobago) 01/02: fix(dropdown): improve DropdownMenu class

This is an automated email from the ASF dual-hosted git repository.

hnoeth pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git

commit 9f2a723a70859cc5c16231d0b5c97cf39e74cb58
Author: Henning Noeth <hn...@apache.org>
AuthorDate: Fri Feb 21 14:04:18 2025 +0100

    fix(dropdown): improve DropdownMenu class
    
    * The dropdown menu now use max-height instead of height. So the menu can be smaller if there are not many entries.
    * The offset is now set by a CSS variable.
    * The margin to the borders of the window is now set by a CSS variable.
    
    Issue: TOBAGO-2389, TOBAGO-2390
---
 tobago-theme/src/main/scss/_tobago.scss            |  18 +++-
 .../src/main/ts/tobago-dropdown-menu.ts            | 106 ++++++++++++++-------
 2 files changed, 83 insertions(+), 41 deletions(-)

diff --git a/tobago-theme/src/main/scss/_tobago.scss b/tobago-theme/src/main/scss/_tobago.scss
index df517f8b70..3428c45ca1 100644
--- a/tobago-theme/src/main/scss/_tobago.scss
+++ b/tobago-theme/src/main/scss/_tobago.scss
@@ -45,6 +45,8 @@
 ) !default;
 
 :root {
+  --tobago-dropdown-menu-offset: 1rem;
+  --tobago-dropdown-menu-component-offset: 2px; //offset between dropdown menu and component
   --tobago-dropdown-menu-z-index: 1100; //usually the highest z-index value
   --tobago-bs-input-padding-x: #{$input-padding-x};
   --tobago-page-padding-top: 1rem;
@@ -92,6 +94,16 @@ @mixin buttonLinkDisabledCursor() {
   }
 }
 
+@mixin dropdownMenuPositioning() {
+  /*.tobago-dropdown-menu extends .dropdown-menu which causes conflicts when used inside a .navbar, because the
+   "position" value would be overwritten. Hence the use of !important. */
+  position: fixed !important;
+  overflow: auto;
+  overscroll-behavior: none;
+  margin: var(--tobago-dropdown-menu-offset);
+  z-index: var(--tobago-dropdown-menu-z-index);
+}
+
 @mixin imageTextSpacing() {
   > img + span {
     margin-left: .4em;
@@ -299,11 +311,7 @@ @mixin selectListOptions() {
       /* Use .tobago-dropdown-menu instead of .dropdown-menu to prevent Bootstrap from creating a dropdown instance.
          This leads to javascript error if arrow up/down is pressed. */
       @extend .dropdown-menu;
-
-      position: fixed;
-      overflow: auto;
-      overscroll-behavior: none;
-      z-index: var(--tobago-dropdown-menu-z-index);
+      @include dropdownMenuPositioning();
     }
 
     &.list-group-item {
diff --git a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-dropdown-menu.ts b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-dropdown-menu.ts
index 14d49fe822..78c55a68ad 100644
--- a/tobago-theme/tobago-theme-standard/src/main/ts/tobago-dropdown-menu.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/ts/tobago-dropdown-menu.ts
@@ -55,12 +55,24 @@ export class DropdownMenu {
 
   private scrollEventListenerEvent(event: Event): void {
     const target = event.target as HTMLElement;
-    if (!target.classList || !target.classList.contains(Css.TOBAGO_DROPDOWN_MENU)
-        || target.getAttribute("name") !== this.dropdownMenuElementId) {
+    if (!this.insideDropdownMenuElement(target)) {
       this.hide();
     }
   }
 
+  private insideDropdownMenuElement(element: HTMLElement): boolean {
+    if (element && element.classList) {
+      if (element.classList.contains(Css.TOBAGO_DROPDOWN_MENU)
+          && element.getAttribute("name") === this.dropdownMenuElementId) {
+        return true;
+      } else {
+        return this.insideDropdownMenuElement(element.parentElement);
+      }
+    } else {
+      return false;
+    }
+  }
+
   /**
    * Call this in the disconnectedCallback() method.
    */
@@ -103,54 +115,71 @@ export class DropdownMenu {
   }
 
   private updatePosition(): void {
-    const offset = 2; // 2px offset to every border
-    const windowWidth = window.innerWidth;
-    const windowHeight = window.innerHeight;
     const refElementRect = this.referenceElement.getBoundingClientRect();
 
+    //calc horizontal positioning and max-width
+    const rightBorder = this.body.offsetWidth;
+    switch (this.alignment) {
+      case DropdownMenuAlignment.start:
+      case DropdownMenuAlignment.centerFullWidth:
+        this.dropdownMenuElement.style.left = refElementRect.left + "px";
+        this.dropdownMenuElement.style.right = null;
+        this.dropdownMenuElement.style.marginLeft = "0px";
+        this.dropdownMenuElement.style.marginRight = null;
+        this.dropdownMenuElement.style.maxWidth = rightBorder - refElementRect.left
+            - parseFloat(getComputedStyle(this.dropdownMenuElement).marginRight) + "px";
+        break;
+      case DropdownMenuAlignment.end:
+        this.dropdownMenuElement.style.left = null;
+        this.dropdownMenuElement.style.right = rightBorder - refElementRect.right + "px";
+        this.dropdownMenuElement.style.marginLeft = null;
+        this.dropdownMenuElement.style.marginRight = "0px";
+        this.dropdownMenuElement.style.maxWidth = refElementRect.right
+            - parseFloat(getComputedStyle(this.dropdownMenuElement).marginLeft) + "px";
+        break;
+    }
+
     //calc width
-    this.dropdownMenuElement.style.width = null;
-    const naturalWidth = this.dropdownMenuElement.offsetWidth;
-    const maxWidth = windowWidth - offset - offset;
     switch (this.alignment) {
       case DropdownMenuAlignment.start:
       case DropdownMenuAlignment.end:
-        if (naturalWidth > maxWidth) {
-          this.dropdownMenuElement.style.width = maxWidth + "px";
-        }
+        this.dropdownMenuElement.style.width = null;
         break;
       case DropdownMenuAlignment.centerFullWidth:
-        if (refElementRect.width <= maxWidth) {
-          this.dropdownMenuElement.style.width = refElementRect.width + "px";
-        }
+        this.dropdownMenuElement.style.width = refElementRect.width + "px";
         break;
     }
 
-    //calc height
-    const topSpace = refElementRect.top - offset - offset;
-    const bottomSpace = windowHeight - refElementRect.bottom - offset - offset;
-    this.dropdownMenuElement.style.maxHeight = bottomSpace >= topSpace ? bottomSpace + "px" : topSpace + "px";
-
-    //calc top
-    if (bottomSpace >= topSpace) {
-      this.dropdownMenuElement.style.top = refElementRect.bottom + offset + "px";
+    //calc vertical positioning and max-height
+    const upperBorder = this.stickyHeader ? this.stickyHeader.offsetHeight : 0;
+    const lowerBorder = this.fixedFooter ? this.fixedFooter.offsetTop : window.innerHeight;
+    const spaceAbove = refElementRect.top - upperBorder;
+    const spaceBelow = lowerBorder - refElementRect.bottom;
+    if (spaceBelow >= spaceAbove) {
+      this.dropdownMenuElement.style.top = refElementRect.bottom + "px";
+      this.dropdownMenuElement.style.bottom = null;
+      this.dropdownMenuElement.style.marginTop = "var(--tobago-dropdown-menu-component-offset)";
+      this.dropdownMenuElement.style.marginBottom = null;
+      this.dropdownMenuElement.style.maxHeight = spaceBelow
+          - parseFloat(getComputedStyle(this.dropdownMenuElement).marginBottom) + "px";
     } else {
-      this.dropdownMenuElement.style.top = offset + "px";
+      this.dropdownMenuElement.style.top = null;
+      this.dropdownMenuElement.style.bottom = (window.innerHeight - refElementRect.top) + "px";
+      this.dropdownMenuElement.style.marginTop = null;
+      this.dropdownMenuElement.style.marginBottom = "var(--tobago-dropdown-menu-component-offset)";
+      this.dropdownMenuElement.style.maxHeight = spaceAbove
+          - parseFloat(getComputedStyle(this.dropdownMenuElement).marginTop) + "px";
     }
+  }
 
-    //calc left
-    const currentWidth = this.dropdownMenuElement.offsetWidth;
-    if (this.alignment === DropdownMenuAlignment.start) {
-      const naturalLeft = refElementRect.left;
-      const alternateLeft = window.innerWidth - offset - currentWidth;
-      this.dropdownMenuElement.style.left = naturalLeft <= alternateLeft ? naturalLeft + "px" : alternateLeft + "px";
-    } else if (this.alignment === DropdownMenuAlignment.centerFullWidth) {
-      const refElementCenter = refElementRect.left + (refElementRect.width / 2);
-      this.dropdownMenuElement.style.left = refElementCenter - (currentWidth / 2) + "px";
-    } else if (this.alignment === DropdownMenuAlignment.end) {
-      const naturalLeft = refElementRect.right - currentWidth;
-      this.dropdownMenuElement.style.left = naturalLeft >= offset ? naturalLeft + "px" : offset + "px";
-    }
+  private get body(): HTMLElement {
+    const root = document.getRootNode() as ShadowRoot | Document;
+    return root.querySelector("body");
+  }
+
+  private get stickyHeader(): HTMLElement {
+    const root = document.getRootNode() as ShadowRoot | Document;
+    return root.querySelector("tobago-header.sticky-top");
   }
 
   private get menuStore(): HTMLDivElement {
@@ -162,4 +191,9 @@ export class DropdownMenu {
     const root = document.getRootNode() as ShadowRoot | Document;
     return root.querySelector(`.${Css.TOBAGO_DROPDOWN_MENU}[name='${this.dropdownMenuElementId}']`);
   }
+
+  private get fixedFooter(): HTMLElement {
+    const root = document.getRootNode() as ShadowRoot | Document;
+    return root.querySelector("tobago-footer.fixed-bottom");
+  }
 }