Skip to content
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
3 changes: 3 additions & 0 deletions goldens/cdk/overlay/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ export class FullscreenOverlayContainer extends OverlayContainer implements OnDe
static ɵprov: i0.ɵɵInjectableDeclaration<FullscreenOverlayContainer>;
}

// @public
export function getAttachedOverlays(): OverlayRef[];

// @public
export class GlobalPositionStrategy implements PositionStrategy {
apply(): void;
Expand Down
10 changes: 10 additions & 0 deletions src/cdk/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export function isElement(value: any): value is Element {
return value && (value as Element).nodeType === 1;
}

const attachedOverlays = new Set<OverlayRef>();

/** Gets all overlays that are currently attached. */
export function getAttachedOverlays(): OverlayRef[] {
return Array.from(attachedOverlays);
}

/**
* Reference to an overlay that has been created with the Overlay service.
* Used to manipulate or dispose of said overlay.
Expand Down Expand Up @@ -142,6 +149,7 @@ export class OverlayRef implements PortalOutlet {
this._updateStackingOrder();
this._updateElementSize();
this._updateElementDirection();
attachedOverlays.add(this);

if (this._scrollStrategy) {
this._scrollStrategy.enable();
Expand Down Expand Up @@ -248,6 +256,7 @@ export class OverlayRef implements PortalOutlet {
this._detachContentWhenEmpty();
this._locationChanges.unsubscribe();
this._outsideClickDispatcher.remove(this);
attachedOverlays.delete(this);
return detachmentResult;
}

Expand Down Expand Up @@ -284,6 +293,7 @@ export class OverlayRef implements PortalOutlet {
this._detachments.complete();
this._completeDetachContent();
this._disposed = true;
attachedOverlays.delete(this);
}

/** Whether the overlay has attached content. */
Expand Down
23 changes: 23 additions & 0 deletions src/cdk/overlay/overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
PositionStrategy,
ScrollStrategy,
createOverlayRef,
getAttachedOverlays,
} from './index';

describe('Overlay', () => {
Expand Down Expand Up @@ -478,6 +479,28 @@ describe('Overlay', () => {
expect(document.querySelector('.cdk-overlay-pane')).toBeFalsy();
});

it('should track when an overlay is attached and detached', () => {
const overlayRef = createOverlayRef(injector);
expect(getAttachedOverlays()).toEqual([]);

overlayRef.attach(componentPortal);
expect(getAttachedOverlays()).toEqual([overlayRef]);

overlayRef.detach();
expect(getAttachedOverlays()).toEqual([]);
});

it('should track when an overlay is attached and disposed', () => {
const overlayRef = createOverlayRef(injector);
expect(getAttachedOverlays()).toEqual([]);

overlayRef.attach(componentPortal);
expect(getAttachedOverlays()).toEqual([overlayRef]);

overlayRef.dispose();
expect(getAttachedOverlays()).toEqual([]);
});

describe('positioning', () => {
let config: OverlayConfig;

Expand Down
2 changes: 1 addition & 1 deletion src/cdk/overlay/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export {
CDK_CONNECTED_OVERLAY_DEFAULT_CONFIG,
} from './overlay-directives';
export {FullscreenOverlayContainer} from './fullscreen-overlay-container';
export {OverlayRef, OverlaySizeConfig} from './overlay-ref';
export {OverlayRef, OverlaySizeConfig, getAttachedOverlays} from './overlay-ref';
export {ViewportRuler} from '../scrolling';
export {ComponentType} from '../portal';
export {OverlayPositionBuilder} from './position/overlay-position-builder';
Expand Down
Loading