5

I've got an issue using ng2-bootstrap's modals. I'm doing the whole viewContainerRef hack mentioned here. I'm making a modal in a deeply nested component (4 levels deep through routing). I copied the modal code from the above link. Whenever I try to open it, it opens like this: The bs-modal-backdrop is totally covering the screen, including the modal, preventing me from interacting with it. Any idea why this is happening?enter image description here

Alex Kibler
  • 4,674
  • 9
  • 44
  • 74

2 Answers2

2

It's a specific situation with a z-index. At least one of the parent components created a stacking context with a z-index less than backdrop z-index 1040. And now all HTML elements will be stacked in it's context.

That's why a backdrop overlaps a modal no matter what z-index a modal has.

To solve the issue:

  1. Create a modal in the same context as backdrop so z-index will work. Somewhere outside app header component.

  2. Raise parent component z-index so it will be higher than backdrop's 1040.

It's fully described here

norekhov
  • 3,915
  • 25
  • 45
  • Not sure whether (2) raising parent components z-index will help. I suppose then the backdrop won't be visible and/or won't catch click events outside the modal dialog. – hgoebl Apr 22 '17 at 11:08
  • Why not? I remember at least it's visible. Though I won't recommend spoiling bootstrap theme that much. As I understand header stacking context has it's own z-index relative to parent context and if it's bigger than a backdrop than it's perfectly ok. Please note I'm speaking about header z-index, not modal z-index. – norekhov Apr 22 '17 at 15:01
0

This works for me though it's not optimal:

Implement onShow() and onHide() events like this for modal code:

<div class="modal fade"
     bsModal #uploadModal="bs-modal"
     [config]="{backdrop:'static'}"
     tabindex="-1"
     (onShow)="handler('onShow', $event)"
     (onHide)="handler('onHide', $event)"
     role="dialog"
     aria-hidden="true">

Then define handler like so:

private handler(type: string, event: any) {

    if (this.containerId) {

      let elem:Element = document.getElementById(this.containerId);

      if (!elem)
        return;

      switch (type) {

        case 'onShow':
          elem.classList.add('modal-front');
          break;
        case 'onHide':
          elem.classList.remove('modal-front');
          break;
      }

    }
  }

Pass in containerId via some mechanism such as openModal(). Finally, add globally scoped class modal-front that just sets a high z-index.

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
J. Reynolds
  • 151
  • 3
  • 14