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 v
const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
width: '250px',
data: { name: this.name, animal: this.animal },
position: {
top: '0px',
left: '0px'
}
});
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 :)
The styles from your component won't affect its parents. Since the dialog-container is the parent of your ThankYouComponent, it won't override its position.
As a workaround, you can add the positioning to your global styles.css
.
⚠️ Caution though, this affects all of your dialogs! ⚠️
mat-dialog-container {
position: fixed;
left: 20%;
top: 0%
background-color: white;
z-index: 100000;
}
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);
});
}