|
614 | 614 | const {xMax, yMax, left, top, width} = overlay.getBoundaries();
|
615 | 615 | // Reuse saved x and y event values, in order to this method be used async
|
616 | 616 | // in the `vaadin-overlay-change` which guarantees that overlay is ready
|
617 |
| - const x = this.__x || left; |
| 617 | + let x = this.__x || left; |
618 | 618 | const y = this.__y || top;
|
619 | 619 |
|
620 | 620 | // Select one overlay corner and move to the event x/y position.
|
|
625 | 625 | // Align to the parent menu overlay, if any.
|
626 | 626 | const parent = overlay.parentOverlay;
|
627 | 627 | let alignedToParent = false;
|
| 628 | + let parentContentRect; |
628 | 629 | if (parent) {
|
629 |
| - const parentContentRect = parent.$.overlay.getBoundingClientRect(); |
| 630 | + parentContentRect = parent.$.overlay.getBoundingClientRect(); |
630 | 631 | if (parent.hasAttribute('right-aligned')) {
|
631 | 632 | const parentStyle = getComputedStyle(parent);
|
632 |
| - |
633 | 633 | const getPadding = (el, direction) => {
|
634 | 634 | return parseFloat(getComputedStyle(el.$.content)['padding' + direction]);
|
635 | 635 | };
|
|
639 | 639 | // Preserve right-aligned, if possible.
|
640 | 640 | if ((wdthVport - (right - padding)) > width) {
|
641 | 641 | overlay.setAttribute('right-aligned', '');
|
642 |
| - style.right = right - padding + 'px'; |
| 642 | + style.right = right + 'px'; |
643 | 643 | alignedToParent = true;
|
644 | 644 | }
|
| 645 | + } else if (x < parentContentRect.x) { |
| 646 | + // Check if sub menu opens on the left side and the parent menu is not right aligned. |
| 647 | + // If so, use actual width of the submenu content instead of the parent menu content. |
| 648 | + x = x - (width - parentContentRect.width); |
645 | 649 | }
|
646 | 650 | }
|
647 | 651 |
|
648 | 652 | if (!alignedToParent) {
|
649 |
| - if (x < wdthVport / 2 || x < xMax) { |
| 653 | + // Sub-menu is displayed in the right side of root menu |
| 654 | + if ((x < wdthVport / 2 || x < xMax) && !parent) { |
650 | 655 | style.left = x + 'px';
|
651 |
| - } else { |
| 656 | + } else if ((parent && (wdthVport - parentContentRect.width - parentContentRect.left |
| 657 | + >= parentContentRect.width))) { // Sub-menu is displayed in the right side of root menu If it is nested menu |
| 658 | + style.left = parentContentRect.left + parentContentRect.width + 'px'; |
| 659 | + } else if (parent) { // Sub-menu is displayed in the left side of root menu If it is nested menu |
| 660 | + style.right = 'auto'; |
| 661 | + style.left = Math.max(overlay.getBoundingClientRect().left, |
| 662 | + parentContentRect.left - overlay.getBoundingClientRect().width) + 'px'; |
| 663 | + overlay.setAttribute('right-aligned', ''); |
| 664 | + } else { // Sub-menu is displayed in the left side of root menu |
652 | 665 | style.right = Math.max(0, (wdthVport - x)) + 'px';
|
653 | 666 | overlay.setAttribute('right-aligned', '');
|
654 | 667 | }
|
|
0 commit comments