Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I position a dialog above the triggering button?

I'm about to put a fist through my PC screen. I have a dialog box that refuses to budge a hot inch above my submit button at the bottom of the screen -- I want it at the very top.

I have made sure my theme is loading. That was validated here: ide loading of CSS

I have tried:

this.dialogBox.open(thankYouModal, {position: {top: '0%', left: '20%'}}); 

I've tried negative and positive numbers for top. The best I get is that I can shift the modal further down so the user has to scroll down to close it. However, that sucker won't budge a hair past the submit button

I've attempted to put a style sheet into the component with the following:

div#cdk-overlay-0.cdk-overlay-container{     position: fixed !important;     left: 20%;     top: 0%;     background-color: white;     pointer-events: auto; } 

I cannot get this thing to budge an inch above that submit button. I will give someone my first born child if they help me figure out what I'm doing wrong.

(yes I know I'm dramatic, but I've fought with it and searched the web for 3 hours; I am a defeated man ... )

Edit

here is the thankYouModalComponent code:

import {Component} from "@angular/core"; import {MdDialogRef, MdDialogConfig} from "@angular/material";  @Component({              selector: 'thank-you-modal',              template: `                                 <h3>Thank You for Your Submission</h3>                                 <p>Your Feedback has been Saved.</p>                                 <button md-raised-button (click)="dialogRef.close()">Close</button>                          `,             styles: [`                         .md-dialog-container {                                             position: fixed !important;                                             left: 20%;                                             top: 0%;                                             background-color: white;                                             z-index: 100000;                     }`] })  export class ThankYouComponent{     constructor(public dialogRef: MdDialogRef<any>, ) {} } 

Here's the calling method and constructor for the component:

constructor(@Inject(FormBuilder) fb : FormBuilder,                  private feedbackService: feedbackService,                  private dialog : MdDialog){  -------<irrelevant code here>------------  }        submitFeedback(group : FormGroup){             const feedback = new Feedback(  group.get('nameBox').value,                                             group.get('productBox').value,                                             group.get('upsBox').value,                                             group.get('downsBox').value);              if(confirm("Is the data entered what you want to submit?")){                 this.dialog.open(ThankYouComponent, {position: {top: '0%', left:'20%'}});      } 

Resulting dialog at bottom of form

Resulting dialog box at the bottom of the form despite requesting is show at top

like image 335
Tom L'esperance Avatar asked Apr 18 '17 15:04

Tom L'esperance


People also ask

How do you change mat dialog position?

The MatDialog popup may be positioned relative to an element. In the example below, the dialog is opened below and to the left of the clicked button based on the button's bounding client rectangle. The element may be referenced via a template reference variable. Then use MatDialogRef method updatePosition .

How do you align a mat button to the right?

To right align the mat dialog actions buttons, use align="end" attribute on mat-dialog-actions container.


2 Answers

A better way to do this is to inject the button element and pass it into the dialog. This way, the dialog has the full context on how to size it up/position it (and leverage the element.getBoundingClientRect() function):

Dialog Component:

import { Component, ElementRef, Inject, OnInit } from '@angular/core'; import { MAT_DIALOG_DATA, MatDialogConfig, MatDialogRef } from '@angular/material';  @Component({   selector: 'app-dialog-component',   templateUrl: './dialog.component.html',   styleUrls: ['./dialog.component.scss'] }) export class MyDialogComponent implements OnInit {   private readonly _matDialogRef: MatDialogRef<MyDialogComponent>;   private readonly triggerElementRef: ElementRef;   constructor(_matDialogRef: MatDialogRef<MyDialogComponent>,               @Inject(MAT_DIALOG_DATA) data: { trigger: ElementRef }) {     this._matDialogRef = _matDialogRef;     this.triggerElementRef = data.trigger;   }    ngOnInit() {     const matDialogConfig: MatDialogConfig = new MatDialogConfig();     const rect = this.triggerElementRef.nativeElement.getBoundingClientRect();     matDialogConfig.position = { left: `${rect.left}px`, top: `${rect.bottom - 50}px` };     matDialogConfig.width = '300px';     matDialogConfig.height = '400px';     this._matDialogRef.updateSize(matDialogConfig.width, matDialogConfig.height);     this._matDialogRef.updatePosition(matDialogConfig.position);   }   cancel(): void {     this._matDialogRef.close(null);   } } 

Usage:

onShowDialog(evt: MouseEvent): void {   const target = new ElementRef(evt.currentTarget);   const dialogRef = this._matDialog.open(MyDialogComponent, {     data: { trigger: target }   });   dialogRef.afterClosed().subscribe( _res => {     console.log(_res);   }); } 
like image 118
mwilson Avatar answered Oct 06 '22 03:10

mwilson


You can adjust position of dialog component using updatePosition() method of MdDialogRef. first import this line in your dialog component

import { MdDialog, MdDialogConfig, MdDialogRef } from '@angular/material'; 

in constructor use this

constructor(public dialModalRef: MdDialogRef<any>) { } 

then from inside dialog modal component call this method anywhere you want

 changePosition() {         this.dialModalRef.updatePosition({ top: '50px', left: '50px' });     } 

read more about it here https://material.angular.io/components/component/dialog

i hope this will help :)

like image 30
Amit kumar Avatar answered Oct 06 '22 04:10

Amit kumar