53

Is it possible to make a Angular Material Dialog draggable? I installed angular2-draggable and can of course use the functionality on all other elements.

But because the dialogs are dynamically created i can not use ngDraggable on a special element or can use a template variable.

Edric
  • 24,639
  • 13
  • 81
  • 91
HansDampfHH
  • 575
  • 1
  • 4
  • 10

4 Answers4

114

Update since Angular Material 7

You can simply use cdkDrag directive from @angular/cdk/drag-drop

dialog.html

<h1 mat-dialog-title 
   cdkDrag
   cdkDragRootElement=".cdk-overlay-pane" 
   cdkDragHandle>
     Hi {{data.name}}
</h1>

Stackblitz Example

Previous answer:

Since there is no official solution for that, I'm going to write custom directive that will be applied on a dialog title and do all job for us:

dialog.html

@Component({
  selector: 'app-simple-dialog',
  template: `
    <h1 mat-dialog-title mat-dialog-draggable-title>Hi {{data.name}}</h1>
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    <div mat-dialog-content>
      ...
    </div>
    <div mat-dialog-actions>
      ...
    </div>
  `
})
export class SimpleDialogComponent {

Ng-run Example

enter image description here

The basic idea here is to use MatDialogRef.updatePosition method for updating dialog position. Under the hood this method changes margin-top|margin-left values and someone can argue that it's not the best option here and it would be better if we used transform but I simply want to show an example of how we can do it without some tricks and with the help of the built-in services.

We also need to inject MatDialogContainer in our directive so that we can get initial position of dialog container. We have to calculate initial offset because Angular material library uses flex to center dialog and it doesn't get us specific top/left values.

dialog-draggable-title.directive.ts

import { Directive, HostListener, OnInit } from '@angular/core';
import { MatDialogContainer, MatDialogRef } from '@angular/material';
import { Subscription } from 'rxjs/Subscription';
import { Observable } from 'rxjs/Observable';
import { takeUntil } from 'rxjs/operators/takeUntil';
import 'rxjs/add/observable/fromEvent';
import { take } from 'rxjs/operators/take';

@Directive({
  selector: '[mat-dialog-draggable-title]'
})
export class DialogDraggableTitleDirective implements OnInit {

  private _subscription: Subscription;

  mouseStart: Position;

  mouseDelta: Position;

  offset: Position;

  constructor(
    private matDialogRef: MatDialogRef<any>,
    private container: MatDialogContainer) {}

  ngOnInit() {
    this.offset = this._getOffset();
  }

  @HostListener('mousedown', ['$event'])
  onMouseDown(event: MouseEvent) {
    this.mouseStart = {x: event.pageX, y: event.pageY};

    const mouseup$ = Observable.fromEvent(document, 'mouseup');
    this._subscription = mouseup$.subscribe(() => this.onMouseup());

    const mousemove$ = Observable.fromEvent(document, 'mousemove')
      .pipe(takeUntil(mouseup$))
      .subscribe((e: MouseEvent) => this.onMouseMove(e));

    this._subscription.add(mousemove$);
  }

  onMouseMove(event: MouseEvent) {
      this.mouseDelta = {x: (event.pageX - this.mouseStart.x), y: (event.pageY - this.mouseStart.y)};

      this._updatePosition(this.offset.y + this.mouseDelta.y, this.offset.x + this.mouseDelta.x);
  }

  onMouseup() {
    if (this._subscription) {
      this._subscription.unsubscribe();
      this._subscription = undefined;
    }

    if (this.mouseDelta) {
      this.offset.x += this.mouseDelta.x;
      this.offset.y += this.mouseDelta.y;
    }
  }

  private _updatePosition(top: number, left: number) {
    this.matDialogRef.updatePosition({
      top: top + 'px',
      left: left + 'px'
    });
  }

  private _getOffset(): Position {
    const box = this.container['_elementRef'].nativeElement.getBoundingClientRect();
    return {
      x: box.left + pageXOffset,
      y: box.top + pageYOffset
    };
  }
}


export interface Position {
  x: number;
  y: number;
}

Remember location

Since @Rolando asked:

I want to 'remember' where the modal was positioned so that when the button to open the modal is hit, the modal opens up where 'it was last located'.

let's try to support it.

In order to do that you can create some service where you will store dialog positions:

modal-position.cache.ts

@Injectable()
export class ModalPositionCache {
  private _cache = new Map<Type<any>, Position>();

  set(dialog: Type<any>, position: Position) {
    this._cache.set(dialog, position);
  }

  get(dialog: Type<any>): Position|null {
    return this._cache.get(dialog);
  }
}

now you need to inject this service in our directive:

dialog-draggable-title.directive.ts

export class DialogDraggableTitleDirective implements OnInit {
  ...

  constructor(
    private matDialogRef: MatDialogRef<any>,
    private container: MatDialogContainer,
    private positionCache: ModalPositionCache
  ) {}

  ngOnInit() {
    const dialogType = this.matDialogRef.componentInstance.constructor;
    const cachedValue = this.positionCache.get(dialogType);
    this.offset = cachedValue || this._getOffset();
    this._updatePosition(this.offset.y, this.offset.x);

    this.matDialogRef.beforeClose().pipe(take(1))
      .subscribe(() => this.positionCache.set(dialogType, this.offset));
  }

As you can as soon as dialog is going to be closed i save last offset.

Ng-run Example

This way dialog remembers where it was closed

enter image description here

yurzui
  • 205,937
  • 32
  • 433
  • 399
  • I had to set the offset in `ngAfterViewInit()` instead of `ngOnInit()` - now it work fine! – TmTron Jan 08 '19 at 15:05
  • I tried your demo, but how can I drag from one screen to another screen when I have 2 monitors? – Hien Nguyen Mar 15 '19 at 01:50
  • 1
    I've tried your updated solution with Angular 9 but the whole dialog acts as a drag handle, not just the title. Any thoughts? – Xander Apr 07 '20 at 14:21
  • @Xander Can you please reproduce it in stackblitz? – yurzui Apr 07 '20 at 14:25
  • @yurzui Unfortunately I cannot. I used the Material 9 stackblitz example for dialogs and added these 3 directives and it works fine. So it's probably something specific to my app but I have no idea what.. at my wit's end here. – Xander Apr 07 '20 at 14:28
  • I just converted this application to Angular 9 and 10, and it stops dragging. The real issue is that there aren't any errors, warnings or other problems that show it. It just doesn't work. The cdk directives do not work if they are on a component that is opened with DialogRef. The fix is to remove cdkDragHandle from the `h1` and leave it off because cdkDrag and cdkDragHandle cant be on the same element. It worked before due to a bug. – Andrew T Finnell Jun 26 '20 at 15:40
  • @Xander I have the same situation. – Mitulát báti Jul 02 '20 at 12:56
  • hi, can someone answer this question? thanks https://stackoverflow.com/questions/62807800/angular-material-popup-windows-allow-windows-to-go-between-front-and-back –  Jul 09 '20 at 05:09
  • 2
    Note that [in some cases](https://stackoverflow.com/a/62235999/4321) it seems necessary to move the cdkDrag and cdkDragRootElement to a parent element of cdkDragHandle, to prevent the whole dialog from being draggable. – JW. Aug 25 '20 at 21:33
  • I suggest adding style="cursor: move" to the

    so the user understands the element is draggable.

    – yglodt Mar 13 '21 at 17:50
  • They need to add an award, "This author is smarter than chatGPT". – AppDreamer Jun 05 '23 at 14:29
17

in your module, import the cdk drag

import { DragDropModule } from '@angular/cdk/drag-drop';

and in html where dialog is for example, just add to any html element. i have added to first element and then i can drag the dialog anywhere i pick.

<mat-dialog-content cdkDrag cdkDragRootElement=".cdk-overlay-pane" cdkDragHandle>
  content...
</mat-dialog-content>
pinarella
  • 805
  • 8
  • 16
  • Do you know how to do this when I am opening a dialog with this.dialog.open... I do not have mat-dialog-content in my HTML. It is managed in the background. – Abhishek Mittal Apr 08 '23 at 11:08
  • could you post an example on stackblitz or wherever? when you do this.dialog.open, you specify a component which should be open, and in that component you add those directives, will make an example and post it. – pinarella Apr 10 '23 at 10:59
  • here is the example: https://stackblitz.com/edit/angular-jtnajz?file=src%2Fapp%2Fdialog-animations-example-dialog.html – pinarella Apr 10 '23 at 11:18
1

In case anyone else runs into this, it's actually a noop to use cdkDrag and cdkDragHandle on the same element as is done in the examples here. The relevant GH Issue can be found here:

https://github.com/angular/components/issues/18984

Luminoth
  • 15
  • 3
0

in angular2-draggable, you use ngDraggable to make the element draggable. where ngDraggable is a directive and in your situation you need to attach this ngDraggable directive dynamically with your dialog which is created dynamically.

Though officially, there is no way to add directive dynamically but some dirty tricks have been discussed in the following questions to add directive dynamically.

How to dynamically add a directive?

Use Angular2 Directive in host of another Directive

WasiF
  • 26,101
  • 16
  • 120
  • 128