Skip to content

Commit e5efd0a

Browse files
crisbetovivian-hu-zz
authored andcommitted
docs(drag-drop): add docs and live example for boundary functionality (#14438)
Adds a live example and some docs for the new `cdkDragBoundary` input.
1 parent 35fd998 commit e5efd0a

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ changed by setting the `orientation` property to `"horizontal".
134134

135135
<!-- example(cdk-drag-drop-horizontal-sorting) -->
136136

137+
### Restricting movement within an element
138+
139+
If you want to stop the user from being able to drag a `cdkDrag` element outside of another element,
140+
you can pass a CSS selector to the `cdkDragBoundary` attribute. The attribute works by accepting a
141+
selector and looking up the DOM until it finds an element that matches it. If a match is found,
142+
it'll be used as the boundary outside of which the element can't be dragged. `cdkDragBoundary` can
143+
also be used when `cdkDrag` is placed inside a `cdkDropList`.
144+
145+
<!-- example(cdk-drag-drop-boundary) -->
146+
137147
### Restricting movement along an axis
138148
By default, `cdkDrag` allows free movement in all directions. To restrict dragging to a
139149
specific axis, you can set `cdkDragLockAxis` on `cdkDrag` or `lockAxis` on `cdkDropList`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.example-box {
2+
width: 200px;
3+
height: 200px;
4+
border: solid 1px #ccc;
5+
color: rgba(0, 0, 0, 0.87);
6+
cursor: move;
7+
display: inline-flex;
8+
justify-content: center;
9+
align-items: center;
10+
text-align: center;
11+
background: #fff;
12+
border-radius: 4px;
13+
margin-right: 25px;
14+
position: relative;
15+
z-index: 1;
16+
box-sizing: border-box;
17+
padding: 10px;
18+
transition: box-shadow 200ms cubic-bezier(0, 0, 0.2, 1);
19+
box-shadow: 0 3px 1px -2px rgba(0, 0, 0, 0.2),
20+
0 2px 2px 0 rgba(0, 0, 0, 0.14),
21+
0 1px 5px 0 rgba(0, 0, 0, 0.12);
22+
}
23+
24+
.example-box:active {
25+
box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
26+
0 8px 10px 1px rgba(0, 0, 0, 0.14),
27+
0 3px 14px 2px rgba(0, 0, 0, 0.12);
28+
}
29+
30+
.example-boundary {
31+
width: 400px;
32+
height: 400px;
33+
max-width: 100%;
34+
border: dotted #ccc 2px;
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="example-boundary">
2+
<div class="example-box" cdkDragBoundary=".example-boundary" cdkDrag>
3+
I can only be dragged within the dotted container
4+
</div>
5+
</div>
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {Component} from '@angular/core';
2+
3+
/**
4+
* @title Drag&Drop boundary
5+
*/
6+
@Component({
7+
selector: 'cdk-drag-drop-boundary-example',
8+
templateUrl: 'cdk-drag-drop-boundary-example.html',
9+
styleUrls: ['cdk-drag-drop-boundary-example.css'],
10+
})
11+
export class CdkDragDropBoundaryExample {}

0 commit comments

Comments
 (0)