Skip to content

Commit 4e02a95

Browse files
authored
fix(drag-drop): allow preview z-index to be changed (#18914)
The CDK drag preview has a `z-index` that works with the CDK overlay, but might be too low for other libraries. These changes add an option that allows consumers to override it. Fixes #18902.
1 parent 3e984c7 commit 4e02a95

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

src/cdk/drag-drop/directives/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface DragDropConfig extends Partial<DragRefConfig> {
4242
sortingDisabled?: boolean;
4343
listAutoScrollDisabled?: boolean;
4444
listOrientation?: DropListOrientation;
45+
zIndex?: number;
4546
}
4647

4748
/**

src/cdk/drag-drop/directives/drag.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,7 @@ describe('CdkDrag', () => {
19291929
expect(previewRect.height).toBe(itemRect.height, 'Expected preview height to match element');
19301930
expect(preview.style.pointerEvents)
19311931
.toBe('none', 'Expected pointer events to be disabled on the preview');
1932+
expect(preview.style.zIndex).toBe('1000', 'Expected preview to have a high default zIndex.');
19321933
// Use a regex here since some browsers normalize 0 to 0px, but others don't.
19331934
expect(preview.style.margin).toMatch(/^0(px)?$/, 'Expected the preview margin to be reset.');
19341935

@@ -1942,6 +1943,22 @@ describe('CdkDrag', () => {
19421943
expect(preview.parentNode).toBeFalsy('Expected preview to be removed from the DOM');
19431944
}));
19441945

1946+
it('should be able to configure the preview z-index', fakeAsync(() => {
1947+
const fixture = createComponent(DraggableInDropZone, [{
1948+
provide: CDK_DRAG_CONFIG,
1949+
useValue: {
1950+
dragStartThreshold: 0,
1951+
zIndex: 3000
1952+
}
1953+
}]);
1954+
fixture.detectChanges();
1955+
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;
1956+
startDraggingViaMouse(fixture, item);
1957+
1958+
const preview = document.querySelector('.cdk-drag-preview')! as HTMLElement;
1959+
expect(preview.style.zIndex).toBe('3000');
1960+
}));
1961+
19451962
it('should create the preview inside the fullscreen element when in fullscreen mode',
19461963
fakeAsync(() => {
19471964
// Provide a limited stub of the document since we can't trigger fullscreen

src/cdk/drag-drop/directives/drag.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
198198
dragStartThreshold: config && config.dragStartThreshold != null ?
199199
config.dragStartThreshold : 5,
200200
pointerDirectionChangeThreshold: config && config.pointerDirectionChangeThreshold != null ?
201-
config.pointerDirectionChangeThreshold : 5
201+
config.pointerDirectionChangeThreshold : 5,
202+
zIndex: config?.zIndex
202203
});
203204
this._dragRef.data = this;
204205

src/cdk/drag-drop/drag-ref.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ export interface DragRefConfig {
3131
* considers them to have changed the drag direction.
3232
*/
3333
pointerDirectionChangeThreshold: number;
34+
35+
/** `z-index` for the absolutely-positioned elements that are created by the drag item. */
36+
zIndex?: number;
3437
}
3538

3639
/** Options that can be used to bind a passive event listener. */
@@ -909,7 +912,7 @@ export class DragRef<T = any> {
909912
position: 'fixed',
910913
top: '0',
911914
left: '0',
912-
zIndex: '1000'
915+
zIndex: `${this._config.zIndex || 1000}`
913916
});
914917

915918
toggleNativeDragInteractions(preview, false);

tools/public_api_guard/cdk/drag-drop.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ export interface DragDropConfig extends Partial<DragRefConfig> {
216216
previewClass?: string | string[];
217217
rootElementSelector?: string;
218218
sortingDisabled?: boolean;
219+
zIndex?: number;
219220
}
220221

221222
export declare class DragDropModule {
@@ -317,6 +318,7 @@ export declare class DragRef<T = any> {
317318
export interface DragRefConfig {
318319
dragStartThreshold: number;
319320
pointerDirectionChangeThreshold: number;
321+
zIndex?: number;
320322
}
321323

322324
export declare type DragStartDelay = number | {

0 commit comments

Comments
 (0)