Skip to content

Commit

Permalink
fix(drag-drop): allow preview z-index to be changed
Browse files Browse the repository at this point in the history
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 angular#18902.
  • Loading branch information
crisbeto committed Mar 25, 2020
1 parent f2d2c19 commit 429c13d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/cdk/drag-drop/directives/config.ts
Expand Up @@ -42,6 +42,7 @@ export interface DragDropConfig extends Partial<DragRefConfig> {
sortingDisabled?: boolean;
listAutoScrollDisabled?: boolean;
listOrientation?: DropListOrientation;
zIndex?: number;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/cdk/drag-drop/directives/drag.spec.ts
Expand Up @@ -1929,6 +1929,7 @@ describe('CdkDrag', () => {
expect(previewRect.height).toBe(itemRect.height, 'Expected preview height to match element');
expect(preview.style.pointerEvents)
.toBe('none', 'Expected pointer events to be disabled on the preview');
expect(preview.style.zIndex).toBe('1000', 'Expected preview to have a high default zIndex.');
// Use a regex here since some browsers normalize 0 to 0px, but others don't.
expect(preview.style.margin).toMatch(/^0(px)?$/, 'Expected the preview margin to be reset.');

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

it('should be able to configure the preview z-index', fakeAsync(() => {
const fixture = createComponent(DraggableInDropZone, [{
provide: CDK_DRAG_CONFIG,
useValue: {
dragStartThreshold: 0,
zIndex: 3000
}
}]);
fixture.detectChanges();
const item = fixture.componentInstance.dragItems.toArray()[1].element.nativeElement;
startDraggingViaMouse(fixture, item);

const preview = document.querySelector('.cdk-drag-preview')! as HTMLElement;
expect(preview.style.zIndex).toBe('3000');
}));

it('should create the preview inside the fullscreen element when in fullscreen mode',
fakeAsync(() => {
// Provide a limited stub of the document since we can't trigger fullscreen
Expand Down
3 changes: 2 additions & 1 deletion src/cdk/drag-drop/directives/drag.ts
Expand Up @@ -198,7 +198,8 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
dragStartThreshold: config && config.dragStartThreshold != null ?
config.dragStartThreshold : 5,
pointerDirectionChangeThreshold: config && config.pointerDirectionChangeThreshold != null ?
config.pointerDirectionChangeThreshold : 5
config.pointerDirectionChangeThreshold : 5,
zIndex: config?.zIndex
});
this._dragRef.data = this;

Expand Down
5 changes: 4 additions & 1 deletion src/cdk/drag-drop/drag-ref.ts
Expand Up @@ -31,6 +31,9 @@ export interface DragRefConfig {
* considers them to have changed the drag direction.
*/
pointerDirectionChangeThreshold: number;

/** `z-index` for the absolutely-positioned elements that are created by the drag item. */
zIndex?: number;
}

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

toggleNativeDragInteractions(preview, false);
Expand Down
2 changes: 2 additions & 0 deletions tools/public_api_guard/cdk/drag-drop.d.ts
Expand Up @@ -217,6 +217,7 @@ export interface DragDropConfig extends Partial<DragRefConfig> {
previewClass?: string | string[];
rootElementSelector?: string;
sortingDisabled?: boolean;
zIndex?: number;
}

export declare class DragDropModule {
Expand Down Expand Up @@ -318,6 +319,7 @@ export declare class DragRef<T = any> {
export interface DragRefConfig {
dragStartThreshold: number;
pointerDirectionChangeThreshold: number;
zIndex?: number;
}

export declare type DragStartDelay = number | {
Expand Down

0 comments on commit 429c13d

Please sign in to comment.