1

Is it possible to make a Angular Material Dialog draggable? because after a loosing a lot of time for searching i didn't found a very clear answer.

Wassim Makni
  • 471
  • 2
  • 9
  • 21

1 Answers1

1

Yes and this was included in Angular Material version 7+ update, by using the cdkDragRootElement

Here's a sample copied from material.angular.io

HTML:

<button (click)="openDialog()">Open a draggable dialog</button>

<ng-template>
  <div class="example-dialog-content" cdkDrag cdkDragRootElement=".cdk-overlay-pane">
    Drag the dialog around!
  </div>
</ng-template>

TS:

export class CdkDragDropRootElementExample implements AfterViewInit, OnDestroy {
  @ViewChild(TemplateRef) _dialogTemplate: TemplateRef<any>;
  private _overlayRef: OverlayRef;
  private _portal: TemplatePortal;

  constructor(private _overlay: Overlay, private _viewContainerRef: ViewContainerRef) {}

  ngAfterViewInit() {
    this._portal = new TemplatePortal(this._dialogTemplate, this._viewContainerRef);
    this._overlayRef = this._overlay.create({
      positionStrategy: this._overlay.position().global().centerHorizontally().centerVertically(),
      hasBackdrop: true
    });
    this._overlayRef.backdropClick().subscribe(() => this._overlayRef.detach());
  }

  ngOnDestroy() {
    this._overlayRef.dispose();
  }

  openDialog() {
    this._overlayRef.attach(this._portal);
  }
}

Stackblitz: Draggable Dialog

KimCindy
  • 1,159
  • 1
  • 11
  • 15
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/21363672) – Clock Slave Nov 09 '18 at 03:21
  • 2
    Noted. I'll update the answer. Thank you! – KimCindy Nov 09 '18 at 03:41