Skip to content

Commit 530c0eb

Browse files
crisbetommalerba
authored andcommitted
docs(drag-drop): add doc section and live example for customizing the placeholder (#13777)
Adds a paragraph about the `cdkDragPlaceholder` and sets up a live example. Fixes #13765.
1 parent b30ec56 commit 530c0eb

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

src/cdk/drag-drop/drag-drop.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ This preview can be customized, though, by providing a custom template via `*cdk
104104

105105
<!-- example(cdk-drag-drop-custom-preview) -->
106106

107+
### Customizing the drag placeholder
108+
While a `cdkDrag` element is being dragged, the CDK will create a placeholder element that will
109+
show where it will be placed when it's dropped. By default the placeholder is a clone of the element
110+
that is being dragged, however you can replace it with a custom one using the `*cdkDragPlaceholder`
111+
directive:
112+
113+
<!-- example(cdk-drag-drop-custom-placeholder) -->
114+
107115
### List orientation
108116
The `cdkDropList` directive assumes that lists are vertical by default. This can be
109117
changed by setting the `orientation` property to `"horizontal".
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
.example-list {
2+
width: 500px;
3+
max-width: 100%;
4+
border: solid 1px #ccc;
5+
min-height: 60px;
6+
display: block;
7+
background: white;
8+
border-radius: 4px;
9+
overflow: hidden;
10+
}
11+
12+
.example-box {
13+
padding: 20px 10px;
14+
border-bottom: solid 1px #ccc;
15+
color: rgba(0, 0, 0, 0.87);
16+
display: flex;
17+
flex-direction: row;
18+
align-items: center;
19+
justify-content: space-between;
20+
box-sizing: border-box;
21+
cursor: move;
22+
background: white;
23+
font-size: 14px;
24+
}
25+
26+
.cdk-drag-preview {
27+
box-sizing: border-box;
28+
border-radius: 4px;
29+
box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
30+
0 8px 10px 1px rgba(0, 0, 0, 0.14),
31+
0 3px 14px 2px rgba(0, 0, 0, 0.12);
32+
}
33+
34+
.cdk-drag-animating {
35+
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
36+
}
37+
38+
.example-box:last-child {
39+
border: none;
40+
}
41+
42+
.example-list.cdk-drop-list-dragging .example-box:not(.cdk-drag-placeholder) {
43+
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
44+
}
45+
46+
.example-custom-placeholder {
47+
background: #ccc;
48+
border: dotted 3px #999;
49+
min-height: 60px;
50+
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
51+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
2+
<div class="example-box" *ngFor="let movie of movies" cdkDrag>
3+
<div class="example-custom-placeholder" *cdkDragPlaceholder></div>
4+
{{movie}}
5+
</div>
6+
</div>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {Component} from '@angular/core';
2+
import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop';
3+
4+
/**
5+
* @title Drag&Drop custom placeholer
6+
*/
7+
@Component({
8+
selector: 'cdk-drag-drop-custom-placeholder-example',
9+
templateUrl: 'cdk-drag-drop-custom-placeholder-example.html',
10+
styleUrls: ['cdk-drag-drop-custom-placeholder-example.css'],
11+
})
12+
export class CdkDragDropCustomPlaceholderExample {
13+
movies = [
14+
'Episode I - The Phantom Menace',
15+
'Episode II - Attack of the Clones',
16+
'Episode III - Revenge of the Sith',
17+
'Episode IV - A New Hope',
18+
'Episode V - The Empire Strikes Back',
19+
'Episode VI - Return of the Jedi',
20+
'Episode VII - The Force Awakens',
21+
'Episode VIII - The Last Jedi'
22+
];
23+
24+
drop(event: CdkDragDrop<string[]>) {
25+
moveItemInArray(this.movies, event.previousIndex, event.currentIndex);
26+
}
27+
}

0 commit comments

Comments
 (0)