Skip to content

fix(drag-drop): allow preview z-index to be changed #18914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cdk/drag-drop/directives/config.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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