Skip to content
Closed
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
6 changes: 3 additions & 3 deletions goldens/cdk/overlay/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import { TrackByFunction } from '@angular/core';
import { Type } from '@angular/core';
import { ViewContainerRef } from '@angular/core';

// @public
export const attachedOverlays: i0.Signal<readonly OverlayRef[]>;

// @public
export class BlockScrollStrategy implements ScrollStrategy {
constructor(_viewportRuler: ViewportRuler, document: any);
Expand Down Expand Up @@ -332,9 +335,6 @@ export class FullscreenOverlayContainer extends OverlayContainer implements OnDe
static ɵprov: i0.ɵɵInjectableDeclaration<any>;
}

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

// @public
export class GlobalPositionStrategy implements PositionStrategy {
apply(): void;
Expand Down
16 changes: 8 additions & 8 deletions src/cdk/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
NgZone,
Renderer2,
afterNextRender,
signal,
} from '@angular/core';
import {Observable, Subject, Subscription, SubscriptionLike} from 'rxjs';
import {Direction, Directionality} from '../bidi';
Expand All @@ -37,12 +38,10 @@ export function isElement(value: any): value is Element {
return value && (value as Element).nodeType === 1;
}

const attachedOverlays = new Set<OverlayRef>();
const attachedOverlaysInternal = signal<readonly OverlayRef[]>([]);

/** Gets all overlays that are currently attached. */
export function getAttachedOverlays(): OverlayRef[] {
return Array.from(attachedOverlays);
}
/** Signal with all of the overlays that are currently attached. */
export const attachedOverlays = attachedOverlaysInternal.asReadonly();

/**
* Reference to an overlay that has been created with the Overlay service.
Expand Down Expand Up @@ -149,7 +148,8 @@ export class OverlayRef implements PortalOutlet {
this._updateStackingOrder();
this._updateElementSize();
this._updateElementDirection();
attachedOverlays.add(this);

attachedOverlaysInternal.update(value => (value.includes(this) ? value : [...value, this]));

if (this._scrollStrategy) {
this._scrollStrategy.enable();
Expand Down Expand Up @@ -256,7 +256,7 @@ export class OverlayRef implements PortalOutlet {
this._detachContentWhenEmpty();
this._locationChanges.unsubscribe();
this._outsideClickDispatcher.remove(this);
attachedOverlays.delete(this);
attachedOverlaysInternal.update(value => value.filter(current => current !== this));
return detachmentResult;
}

Expand Down Expand Up @@ -293,7 +293,7 @@ export class OverlayRef implements PortalOutlet {
this._detachments.complete();
this._completeDetachContent();
this._disposed = true;
attachedOverlays.delete(this);
attachedOverlaysInternal.update(value => value.filter(current => current !== this));
}

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

describe('Overlay', () => {
Expand Down Expand Up @@ -481,24 +481,24 @@ describe('Overlay', () => {

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

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

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

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

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

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

describe('positioning', () => {
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, getAttachedOverlays} from './overlay-ref';
export {OverlayRef, OverlaySizeConfig, attachedOverlays} from './overlay-ref';
export {ViewportRuler} from '../scrolling';
export {ComponentType} from '../portal';
export {OverlayPositionBuilder} from './position/overlay-position-builder';
Expand Down
Loading