Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change z-index of Angular CDK Drag and Drop?

I am using the Angular Material CDK Drag and Drop Functionality in my application. The drag and drop functionality is working fine, unless I am using it within a dialog (for most components I am using Nebular, in this case the Nebular dialog). The problem I am encountering is, as soon as I drag a draggable element within the dialog, the element disappears behind the dialog. After dropping it, it reappears on the correct position. In the screenshot, I am dragging the "AAAA" element away from the list - it disappears behind the dialog.

draggable button disappears behind dialog

Stackblitz: https://stackblitz.com/edit/angular-znqckb

I am using the following implementation:

 <div cdkDropList cdkDropListOrientation="horizontal" class="example-list" [cdkDropListData]="techs"
     (cdkDropListDropped)="drop($event)">
     <button *ngFor="let tech of techs" nbButton cdkDrag>{{tech}}</button>
 </div>

Component.ts:

drop(event: CdkDragDrop<string[]>) {
    moveItemInArray(this.techs, event.previousIndex, event.currentIndex);
}

I did not modify the style sheet. I assume this issue can be solved somehow by modifying the z-index but I don't know how to apply it to the "dragging" element.

like image 267
Tobias Kaufmann Avatar asked Apr 03 '20 15:04

Tobias Kaufmann


Video Answer


1 Answers

You can change the DragRefConfig injecting the right config (with the desired z-index) in your component. For example:

const DragConfig = {
  dragStartThreshold: 0,
  pointerDirectionChangeThreshold: 5,
  zIndex: 10000
};

providers: [{ provide: CDK_DRAG_CONFIG, useValue: DragConfig }]

The z-index of the preview element will be 10000 ;-)

For more infos: https://material.angular.io/cdk/drag-drop/api#DragRefConfig

like image 79
Fabrizio Rizzi Avatar answered Sep 20 '22 15:09

Fabrizio Rizzi