Skip to content

Commit 568abd5

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

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

goldens/material/datepicker/index.api.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import { Observable } from 'rxjs';
3333
import { OnChanges } from '@angular/core';
3434
import { OnDestroy } from '@angular/core';
3535
import { OnInit } from '@angular/core';
36-
import { Overlay } from '@angular/cdk/overlay';
3736
import { Portal } from '@angular/cdk/portal';
3837
import { ScrollStrategy } from '@angular/cdk/overlay';
3938
import { SimpleChanges } from '@angular/core';
@@ -94,12 +93,12 @@ export const MAT_DATE_RANGE_SELECTION_STRATEGY: InjectionToken<MatDateRangeSelec
9493
export const MAT_DATEPICKER_SCROLL_STRATEGY: InjectionToken<() => ScrollStrategy>;
9594

9695
// @public @deprecated
97-
export function MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY(overlay: Overlay): () => ScrollStrategy;
96+
export function MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY(_overlay: unknown): () => ScrollStrategy;
9897

9998
// @public @deprecated
10099
export const MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY_PROVIDER: {
101100
provide: InjectionToken<() => ScrollStrategy>;
102-
deps: (typeof Overlay)[];
101+
deps: any[];
103102
useFactory: typeof MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY;
104103
};
105104

src/material/datepicker/datepicker-base.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ import {
2121
UP_ARROW,
2222
} from '@angular/cdk/keycodes';
2323
import {
24+
createBlockScrollStrategy,
25+
createFlexibleConnectedPositionStrategy,
26+
createGlobalPositionStrategy,
27+
createOverlayRef,
28+
createRepositionScrollStrategy,
2429
FlexibleConnectedPositionStrategy,
25-
Overlay,
2630
OverlayConfig,
2731
OverlayRef,
2832
ScrollStrategy,
@@ -82,8 +86,8 @@ export const MAT_DATEPICKER_SCROLL_STRATEGY = new InjectionToken<() => ScrollStr
8286
{
8387
providedIn: 'root',
8488
factory: () => {
85-
const overlay = inject(Overlay);
86-
return () => overlay.scrollStrategies.reposition();
89+
const injector = inject(Injector);
90+
return () => createRepositionScrollStrategy(injector);
8791
},
8892
},
8993
);
@@ -93,8 +97,9 @@ export const MAT_DATEPICKER_SCROLL_STRATEGY = new InjectionToken<() => ScrollStr
9397
* @deprecated No longer used, will be removed.
9498
* @breaking-change 21.0.0
9599
*/
96-
export function MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY(overlay: Overlay): () => ScrollStrategy {
97-
return () => overlay.scrollStrategies.reposition();
100+
export function MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY(_overlay: unknown): () => ScrollStrategy {
101+
const injector = inject(Injector);
102+
return () => createRepositionScrollStrategy(injector);
98103
}
99104

100105
/** Possible positions for the datepicker dropdown along the X axis. */
@@ -110,7 +115,7 @@ export type DatepickerDropdownPositionY = 'above' | 'below';
110115
*/
111116
export const MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY_PROVIDER = {
112117
provide: MAT_DATEPICKER_SCROLL_STRATEGY,
113-
deps: [Overlay],
118+
deps: [] as any[],
114119
useFactory: MAT_DATEPICKER_SCROLL_STRATEGY_FACTORY,
115120
};
116121

@@ -390,7 +395,7 @@ export abstract class MatDatepickerBase<
390395
>
391396
implements MatDatepickerPanel<C, S, D>, OnDestroy, OnChanges
392397
{
393-
private _overlay = inject(Overlay);
398+
private _injector = inject(Injector);
394399
private _viewContainerRef = inject(ViewContainerRef);
395400
private _dateAdapter = inject<DateAdapter<D>>(DateAdapter, {optional: true})!;
396401
private _dir = inject(Directionality, {optional: true});
@@ -565,8 +570,6 @@ export abstract class MatDatepickerBase<
565570
/** Emits when the datepicker's state changes. */
566571
readonly stateChanges = new Subject<void>();
567572

568-
private _injector = inject(Injector);
569-
570573
private readonly _changeDetectorRef = inject(ChangeDetectorRef);
571574

572575
constructor(...args: unknown[]);
@@ -760,7 +763,8 @@ export abstract class MatDatepickerBase<
760763
MatDatepickerContent,
761764
this._viewContainerRef,
762765
);
763-
const overlayRef = (this._overlayRef = this._overlay.create(
766+
const overlayRef = (this._overlayRef = createOverlayRef(
767+
this._injector,
764768
new OverlayConfig({
765769
positionStrategy: isDialog ? this._getDialogStrategy() : this._getDropdownStrategy(),
766770
hasBackdrop: true,
@@ -769,7 +773,9 @@ export abstract class MatDatepickerBase<
769773
this._backdropHarnessClass,
770774
],
771775
direction: this._dir || 'ltr',
772-
scrollStrategy: isDialog ? this._overlay.scrollStrategies.block() : this._scrollStrategy(),
776+
scrollStrategy: isDialog
777+
? createBlockScrollStrategy(this._injector)
778+
: this._scrollStrategy(),
773779
panelClass: `mat-datepicker-${isDialog ? 'dialog' : 'popup'}`,
774780
disableAnimations: this._animationsDisabled,
775781
}),
@@ -825,14 +831,15 @@ export abstract class MatDatepickerBase<
825831

826832
/** Gets a position strategy that will open the calendar as a dropdown. */
827833
private _getDialogStrategy() {
828-
return this._overlay.position().global().centerHorizontally().centerVertically();
834+
return createGlobalPositionStrategy(this._injector).centerHorizontally().centerVertically();
829835
}
830836

831837
/** Gets a position strategy that will open the calendar as a dropdown. */
832838
private _getDropdownStrategy() {
833-
const strategy = this._overlay
834-
.position()
835-
.flexibleConnectedTo(this.datepickerInput.getConnectedOverlayOrigin())
839+
const strategy = createFlexibleConnectedPositionStrategy(
840+
this._injector,
841+
this.datepickerInput.getConnectedOverlayOrigin(),
842+
)
836843
.withTransformOriginOn('.mat-datepicker-content')
837844
.withFlexibleDimensions(false)
838845
.withViewportMargin(8)

src/material/datepicker/datepicker.spec.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
RIGHT_ARROW,
1010
UP_ARROW,
1111
} from '@angular/cdk/keycodes';
12-
import {Overlay} from '@angular/cdk/overlay';
12+
import {createCloseScrollStrategy} from '@angular/cdk/overlay';
1313
import {_supportsShadowDom} from '@angular/cdk/platform';
1414
import {ScrollDispatcher} from '@angular/cdk/scrolling';
1515
import {
@@ -20,7 +20,15 @@ import {
2020
dispatchMouseEvent,
2121
typeInElement,
2222
} from '@angular/cdk/testing/private';
23-
import {Component, Directive, Provider, Type, ViewChild, ViewEncapsulation} from '@angular/core';
23+
import {
24+
Component,
25+
Directive,
26+
Injector,
27+
Provider,
28+
Type,
29+
ViewChild,
30+
ViewEncapsulation,
31+
} from '@angular/core';
2432
import {ComponentFixture, TestBed, fakeAsync, flush, tick} from '@angular/core/testing';
2533
import {
2634
FormControl,
@@ -508,8 +516,7 @@ describe('MatDatepicker', () => {
508516
},
509517
{
510518
provide: MAT_DATEPICKER_SCROLL_STRATEGY,
511-
deps: [Overlay],
512-
useFactory: (overlay: Overlay) => () => overlay.scrollStrategies.close(),
519+
useFactory: () => () => createCloseScrollStrategy(TestBed.inject(Injector)),
513520
},
514521
],
515522
);

0 commit comments

Comments
 (0)