Skip to content

Commit 2d28807

Browse files
committed
refactor(material/autocomplete): switch to tree shakeable overlay APIs
Reworks the module to use the new tree-shakeable APIs for creating overlays.
1 parent 2a70c98 commit 2d28807

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

goldens/material/autocomplete/index.api.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { Observable } from 'rxjs';
2323
import { OnChanges } from '@angular/core';
2424
import { OnDestroy } from '@angular/core';
2525
import { OnInit } from '@angular/core';
26-
import { Overlay } from '@angular/cdk/overlay';
2726
import { QueryList } from '@angular/core';
2827
import { ScrollStrategy } from '@angular/cdk/overlay';
2928
import { SimpleChanges } from '@angular/core';
@@ -43,12 +42,12 @@ export function MAT_AUTOCOMPLETE_DEFAULT_OPTIONS_FACTORY(): MatAutocompleteDefau
4342
export const MAT_AUTOCOMPLETE_SCROLL_STRATEGY: InjectionToken<() => ScrollStrategy>;
4443

4544
// @public @deprecated
46-
export function MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY(overlay: Overlay): () => ScrollStrategy;
45+
export function MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY(_overlay: unknown): () => ScrollStrategy;
4746

4847
// @public @deprecated
4948
export const MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY_PROVIDER: {
5049
provide: InjectionToken<() => ScrollStrategy>;
51-
deps: (typeof Overlay)[];
50+
deps: any[];
5251
useFactory: typeof MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY;
5352
};
5453

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import {DOWN_ARROW, ENTER, ESCAPE, TAB, UP_ARROW, hasModifierKey} from '@angular
1212
import {BreakpointObserver, Breakpoints} from '@angular/cdk/layout';
1313
import {
1414
ConnectedPosition,
15+
createFlexibleConnectedPositionStrategy,
16+
createOverlayRef,
17+
createRepositionScrollStrategy,
1518
FlexibleConnectedPositionStrategy,
16-
Overlay,
1719
OverlayConfig,
1820
OverlayRef,
1921
PositionStrategy,
@@ -29,6 +31,7 @@ import {
2931
ElementRef,
3032
EnvironmentInjector,
3133
InjectionToken,
34+
Injector,
3235
Input,
3336
NgZone,
3437
OnChanges,
@@ -88,8 +91,8 @@ export const MAT_AUTOCOMPLETE_SCROLL_STRATEGY = new InjectionToken<() => ScrollS
8891
{
8992
providedIn: 'root',
9093
factory: () => {
91-
const overlay = inject(Overlay);
92-
return () => overlay.scrollStrategies.reposition();
94+
const injector = inject(Injector);
95+
return () => createRepositionScrollStrategy(injector);
9396
},
9497
},
9598
);
@@ -99,8 +102,9 @@ export const MAT_AUTOCOMPLETE_SCROLL_STRATEGY = new InjectionToken<() => ScrollS
99102
* @deprecated No longer used, will be removed.
100103
* @breaking-change 21.0.0
101104
*/
102-
export function MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY(overlay: Overlay): () => ScrollStrategy {
103-
return () => overlay.scrollStrategies.reposition();
105+
export function MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY(_overlay: unknown): () => ScrollStrategy {
106+
const injector = inject(Injector);
107+
return () => createRepositionScrollStrategy(injector);
104108
}
105109

106110
/**
@@ -110,7 +114,7 @@ export function MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY(overlay: Overlay): () =
110114
*/
111115
export const MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY_PROVIDER = {
112116
provide: MAT_AUTOCOMPLETE_SCROLL_STRATEGY,
113-
deps: [Overlay],
117+
deps: [] as any[],
114118
useFactory: MAT_AUTOCOMPLETE_SCROLL_STRATEGY_FACTORY,
115119
};
116120

@@ -142,7 +146,7 @@ export class MatAutocompleteTrigger
142146
{
143147
private _environmentInjector = inject(EnvironmentInjector);
144148
private _element = inject<ElementRef<HTMLInputElement>>(ElementRef);
145-
private _overlay = inject(Overlay);
149+
private _injector = inject(Injector);
146150
private _viewContainerRef = inject(ViewContainerRef);
147151
private _zone = inject(NgZone);
148152
private _changeDetectorRef = inject(ChangeDetectorRef);
@@ -794,7 +798,7 @@ export class MatAutocompleteTrigger
794798
this._portal = new TemplatePortal(this.autocomplete.template, this._viewContainerRef, {
795799
id: this._formField?.getLabelId(),
796800
});
797-
overlayRef = this._overlay.create(this._getOverlayConfig());
801+
overlayRef = createOverlayRef(this._injector, this._getOverlayConfig());
798802
this._overlayRef = overlayRef;
799803
this._viewportSubscription = this._viewportRuler.change().subscribe(() => {
800804
if (this.panelOpen && overlayRef) {
@@ -916,9 +920,10 @@ export class MatAutocompleteTrigger
916920

917921
private _getOverlayPosition(): PositionStrategy {
918922
// Set default Overlay Position
919-
const strategy = this._overlay
920-
.position()
921-
.flexibleConnectedTo(this._getConnectedElement())
923+
const strategy = createFlexibleConnectedPositionStrategy(
924+
this._injector,
925+
this._getConnectedElement(),
926+
)
922927
.withFlexibleDimensions(false)
923928
.withPush(false);
924929

src/material/autocomplete/autocomplete.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Directionality} from '@angular/cdk/bidi';
22
import {DOWN_ARROW, ENTER, ESCAPE, SPACE, TAB, UP_ARROW} from '@angular/cdk/keycodes';
3-
import {Overlay, OverlayContainer, OverlayModule} from '@angular/cdk/overlay';
3+
import {createCloseScrollStrategy, OverlayContainer, OverlayModule} from '@angular/cdk/overlay';
44
import {_supportsShadowDom} from '@angular/cdk/platform';
55
import {ScrollDispatcher} from '@angular/cdk/scrolling';
66
import {
@@ -16,6 +16,7 @@ import {
1616
ChangeDetectionStrategy,
1717
Component,
1818
ElementRef,
19+
Injector,
1920
OnDestroy,
2021
OnInit,
2122
Provider,
@@ -3203,8 +3204,7 @@ describe('MatAutocomplete', () => {
32033204
},
32043205
{
32053206
provide: MAT_AUTOCOMPLETE_SCROLL_STRATEGY,
3206-
useFactory: (overlay: Overlay) => () => overlay.scrollStrategies.close(),
3207-
deps: [Overlay],
3207+
useFactory: () => () => createCloseScrollStrategy(TestBed.inject(Injector)),
32083208
},
32093209
]);
32103210

0 commit comments

Comments
 (0)