0

I have an Angular web application that has a page that needs to be interrupted by a modal dialogue in some situations. However, when I press the button that does that, the modal appears at the bottom of the page, not covering it as I want it to.

This particular page is a logout form. We have a few different flavors, and some of those flavors need to give our users more information via a modal dialogue when they sign out.

<main>
  <p class="question-header">Did you do the thing before signing out?</p>

  <button class="btn btn-lg btn-success w-100" (click)="logout(1)" [disabled]="processing">
    <span *ngIf="processing === 'logout'" class="spinner-grow-sm spinner-grow text-white"></span>
    Yes
  </button>
  <button class="btn btn-lg btn-primary w-100" (click)="logout(-1)" [disabled]="processing">
    <span *ngIf="processing === 'logout'" class="spinner-grow-sm spinner-grow text-white"></span>
    N/A
  </button>
  <hr class="my-3"> <!-- This separator is here so our users don't press "No" by mistake. -->
  <button class="btn btn-lg btn-danger w-100" (click)="logout(0)" [disabled]="processing">
    <span *ngIf="processing === 'logout'" class="spinner-grow-sm spinner-grow text-white"></span>
    No
  </button>
  <!-- Trigger Button (this will eventually replace the button above, but is here for testing.) -->
  <button class="btn btn-primary" (click)="openModal(template)">Open Modal</button>

  <!-- Modal Template -->
  <ng-template #template>
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">Overlay Modal</h4>
        </div>
        <div class="modal-body">
          This modal should cover the page content.
        </div>
        <div class="modal-footer">
          <button class="btn btn-primary" (click)="modalRef?.hide()">OK</button>
        </div>
      </div>
    </div>
  </ng-template>
</main>

Here is my .scss file for this component.

@import '../../../../../../variables.scss';

.question-header {
  font-weight: 300;
  font-size: $h2-font-size;
  padding: 0.5em;
  margin: 0;
}
.question-part-two {
  font-weight: 300;
  font-size: $h4-font-size;
  padding: 0.5em;
  margin: 0;
}
::ng-deep .modal-dialog {
  max-width: 500px;
  margin: 1.75rem auto;
}

::ng-deep .modal-content {
  border-radius: 10px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
  padding: 1rem;
}

::ng-deep .modal-header {
  border-bottom: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

::ng-deep .modal-footer {
  border-top: none;
  display: flex;
  justify-content: flex-end;
}
6
  • I tried to create a sample of your code and found that the modal is centered on the page, so the code you showed us is not affected. Please show us more of the page's structure. Commented Jul 18 at 19:01
  • 2
    Not your question, but just a warning: be careful with "unprotected" use of ::ng-deep like that. With how you have it set up, you're loading globally-applicable styles when the component is loaded. That means you may change the appearance of other components that use the same class names accidentally only after this component is loaded, which can be very surprising (I say that from experience). You should use :host before ::ng-deep to prevent issues like that. Commented Jul 18 at 19:56
  • you seem to be using bootstrap, and if so, modal markup is incorrect: you're missing entire modal parent container. so, add the container, or provide fully reproducible example Commented Jul 18 at 20:28
  • @Razzer When I say "bottom of the page" I mean "below other components" as they don't reach the center of the page. Commented Jul 18 at 22:22
  • Thank you all for your comments. These should prove helpful. Commented Jul 18 at 22:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.