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

Solution 1:

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

Solution 2:

I'm struggling with this problem myself and I'm using a crude workaround for now. Forcing the z-index of .cdk-overlay-container to 1000 in your global styles (styles.scss) should get you the result you want. It's not best practice though.

Add this in styles.scss:

.cdk-overlay-container {
  z-index: 1000 !important;
}

Stackblitz here

To my knowledge, it's not possible to force a z-index on the drag preview ("dragging" element) because the cdk sets its z-index dynamically as inline style. The Nebular library you are using seems to be setting the z-index of the overlay container to 1040. The Angular Material library sets the drag preview's z-index as 1000, that's why it goes behind the overlay. Vanilla Angular Material sets the z-index of cdk overlay to 1000, so drag & drop will work in that scenario.

Solution 3:

For previous angular material versions than 9.

In the html dragable element:

<div ... cdkDrag (cdkDragStarted)="onDragStarted($event)"</div>  

In the compoonent ts:

export class ...{
  zIndexSerial: number = 1000;

  onDragStarted(event: CdkDragEnd): void {
    event.source.element.nativeElement.style.zIndex=this.zIndexSerial+"";
    this.zIndexSerial = this.zIndexSerial+1;
  }