@@ -28,8 +28,9 @@ import {
28
28
OnChanges ,
29
29
SimpleChanges ,
30
30
ChangeDetectorRef ,
31
+ isDevMode ,
31
32
} from '@angular/core' ;
32
- import { coerceBooleanProperty , coerceNumberProperty } from '@angular/cdk/coercion' ;
33
+ import { coerceBooleanProperty , coerceNumberProperty , coerceElement } from '@angular/cdk/coercion' ;
33
34
import { Observable , Observer , Subject , merge } from 'rxjs' ;
34
35
import { startWith , take , map , takeUntil , switchMap , tap } from 'rxjs/operators' ;
35
36
import {
@@ -100,12 +101,27 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
100
101
*/
101
102
@Input ( 'cdkDragRootElement' ) rootElementSelector : string ;
102
103
104
+ /**
105
+ * Node or selector that will be used to determine the element to which the draggable's
106
+ * position will be constrained. If a string is passed in, it'll be used as a selector that
107
+ * will be matched starting from the element's parent and going up the DOM until a match
108
+ * has been found.
109
+ */
110
+ @Input ( 'cdkDragBoundary' ) boundaryElement : string | ElementRef < HTMLElement > | HTMLElement ;
111
+
103
112
/**
104
113
* Selector that will be used to determine the element to which the draggable's position will
105
114
* be constrained. Matching starts from the element's parent and goes up the DOM until a matching
106
- * element has been found.
115
+ * element has been found
116
+ * @deprecated Use `boundaryElement` instead.
117
+ * @breaking -change 9.0.0
107
118
*/
108
- @Input ( 'cdkDragBoundary' ) boundaryElementSelector : string ;
119
+ get boundaryElementSelector ( ) : string {
120
+ return typeof this . boundaryElement === 'string' ? this . boundaryElement : undefined ! ;
121
+ }
122
+ set boundaryElementSelector ( selector : string ) {
123
+ this . boundaryElement = selector ;
124
+ }
109
125
110
126
/**
111
127
* Amount of milliseconds to wait after the user has put their
@@ -292,10 +308,25 @@ export class CdkDrag<T = any> implements AfterViewInit, OnChanges, OnDestroy {
292
308
this . _dragRef . withRootElement ( rootElement || element ) ;
293
309
}
294
310
295
- /** Gets the boundary element, based on the `boundaryElementSelector` . */
311
+ /** Gets the boundary element, based on the `boundaryElement` value . */
296
312
private _getBoundaryElement ( ) {
297
- const selector = this . boundaryElementSelector ;
298
- return selector ? getClosestMatchingAncestor ( this . element . nativeElement , selector ) : null ;
313
+ const boundary = this . boundaryElement ;
314
+
315
+ if ( ! boundary ) {
316
+ return null ;
317
+ }
318
+
319
+ if ( typeof boundary === 'string' ) {
320
+ return getClosestMatchingAncestor ( this . element . nativeElement , boundary ) ;
321
+ }
322
+
323
+ const element = coerceElement ( boundary ) ;
324
+
325
+ if ( isDevMode ( ) && ! element . contains ( this . element . nativeElement ) ) {
326
+ throw Error ( 'Draggable element is not inside of the node passed into cdkDragBoundary.' ) ;
327
+ }
328
+
329
+ return element ;
299
330
}
300
331
301
332
/** Syncs the inputs of the CdkDrag with the options of the underlying DragRef. */
0 commit comments