9
9
import { EmbeddedViewRef , ElementRef , NgZone , ViewContainerRef , TemplateRef } from '@angular/core' ;
10
10
import { ViewportRuler } from '@angular/cdk/scrolling' ;
11
11
import { Direction } from '@angular/cdk/bidi' ;
12
- import { normalizePassiveListenerOptions } from '@angular/cdk/platform' ;
12
+ import { normalizePassiveListenerOptions , _getShadowRoot } from '@angular/cdk/platform' ;
13
13
import { coerceBooleanProperty , coerceElement } from '@angular/cdk/coercion' ;
14
14
import { Subscription , Subject , Observable } from 'rxjs' ;
15
15
import { DropListRefInternal as DropListRef } from './drop-list-ref' ;
@@ -227,6 +227,13 @@ export class DragRef<T = any> {
227
227
/** Layout direction of the item. */
228
228
private _direction : Direction = 'ltr' ;
229
229
230
+ /**
231
+ * Cached shadow root that the element is placed in. `null` means that the element isn't in
232
+ * the shadow DOM and `undefined` means that it hasn't been resolved yet. Should be read via
233
+ * `_getShadowRoot`, not directly.
234
+ */
235
+ private _cachedShadowRoot : ShadowRoot | null | undefined ;
236
+
230
237
/** Axis along which dragging is locked. */
231
238
lockAxis : 'x' | 'y' ;
232
239
@@ -743,6 +750,9 @@ export class DragRef<T = any> {
743
750
const placeholder = this . _placeholder = this . _createPlaceholderElement ( ) ;
744
751
const anchor = this . _anchor = this . _anchor || this . _document . createComment ( '' ) ;
745
752
753
+ // Needs to happen before the root element is moved.
754
+ const shadowRoot = this . _getShadowRoot ( ) ;
755
+
746
756
// Insert an anchor node so that we can restore the element's position in the DOM.
747
757
parent . insertBefore ( anchor , element ) ;
748
758
@@ -751,7 +761,7 @@ export class DragRef<T = any> {
751
761
// from the DOM completely, because iOS will stop firing all subsequent events in the chain.
752
762
toggleVisibility ( element , false ) ;
753
763
this . _document . body . appendChild ( parent . replaceChild ( placeholder , element ) ) ;
754
- getPreviewInsertionPoint ( this . _document ) . appendChild ( preview ) ;
764
+ getPreviewInsertionPoint ( this . _document , shadowRoot ) . appendChild ( preview ) ;
755
765
this . started . next ( { source : this } ) ; // Emit before notifying the container.
756
766
dropContainer . start ( ) ;
757
767
this . _initialContainer = dropContainer ;
@@ -1319,6 +1329,20 @@ export class DragRef<T = any> {
1319
1329
return cachedPosition ? cachedPosition . scrollPosition :
1320
1330
this . _viewportRuler . getViewportScrollPosition ( ) ;
1321
1331
}
1332
+
1333
+ /**
1334
+ * Lazily resolves and returns the shadow root of the element. We do this in a function, rather
1335
+ * than saving it in property directly on init, because we want to resolve it as late as possible
1336
+ * in order to ensure that the element has been moved into the shadow DOM. Doing it inside the
1337
+ * constructor might be too early if the element is inside of something like `ngFor` or `ngIf`.
1338
+ */
1339
+ private _getShadowRoot ( ) : ShadowRoot | null {
1340
+ if ( this . _cachedShadowRoot === undefined ) {
1341
+ this . _cachedShadowRoot = _getShadowRoot ( this . _rootElement ) as ShadowRoot | null ;
1342
+ }
1343
+
1344
+ return this . _cachedShadowRoot ;
1345
+ }
1322
1346
}
1323
1347
1324
1348
/**
@@ -1356,11 +1380,12 @@ function isTouchEvent(event: MouseEvent | TouchEvent): event is TouchEvent {
1356
1380
}
1357
1381
1358
1382
/** Gets the element into which the drag preview should be inserted. */
1359
- function getPreviewInsertionPoint ( documentRef : any ) : HTMLElement {
1383
+ function getPreviewInsertionPoint ( documentRef : any , shadowRoot : ShadowRoot | null ) : HTMLElement {
1360
1384
// We can't use the body if the user is in fullscreen mode,
1361
1385
// because the preview will render under the fullscreen element.
1362
1386
// TODO(crisbeto): dedupe this with the `FullscreenOverlayContainer` eventually.
1363
- return documentRef . fullscreenElement ||
1387
+ return shadowRoot ||
1388
+ documentRef . fullscreenElement ||
1364
1389
documentRef . webkitFullscreenElement ||
1365
1390
documentRef . mozFullScreenElement ||
1366
1391
documentRef . msFullscreenElement ||
0 commit comments