Skip to content

Commit 0925cf5

Browse files
authored
fix(material/timepicker): allow scroll strategy to be customized (#30473)
Adds an injection token to the timepicker that allows the scroll strategy to be customized, similar to other components. Fixes #30421.
1 parent 6434841 commit 0925cf5

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/material/timepicker/timepicker.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
effect,
1818
ElementRef,
1919
inject,
20+
InjectionToken,
2021
Injector,
2122
input,
2223
InputSignal,
@@ -41,7 +42,7 @@ import {
4142
MatOptionParentComponent,
4243
} from '@angular/material/core';
4344
import {Directionality} from '@angular/cdk/bidi';
44-
import {Overlay, OverlayRef} from '@angular/cdk/overlay';
45+
import {Overlay, OverlayRef, ScrollStrategy} from '@angular/cdk/overlay';
4546
import {TemplatePortal} from '@angular/cdk/portal';
4647
import {_getEventTarget} from '@angular/cdk/platform';
4748
import {ENTER, ESCAPE, hasModifierKey, TAB} from '@angular/cdk/keycodes';
@@ -62,6 +63,18 @@ export interface MatTimepickerSelected<D> {
6263
source: MatTimepicker<D>;
6364
}
6465

66+
/** Injection token used to configure the behavior of the timepicker dropdown while scrolling. */
67+
export const MAT_TIMEPICKER_SCROLL_STRATEGY = new InjectionToken<() => ScrollStrategy>(
68+
'MAT_TIMEPICKER_SCROLL_STRATEGY',
69+
{
70+
providedIn: 'root',
71+
factory: () => {
72+
const overlay = inject(Overlay);
73+
return () => overlay.scrollStrategies.reposition();
74+
},
75+
},
76+
);
77+
6578
/**
6679
* Renders out a listbox that can be used to select a time of day.
6780
* Intended to be used together with `MatTimepickerInput`.
@@ -89,6 +102,7 @@ export class MatTimepicker<D> implements OnDestroy, MatOptionParentComponent {
89102
private _defaultConfig = inject(MAT_TIMEPICKER_CONFIG, {optional: true});
90103
private _dateAdapter = inject<DateAdapter<D>>(DateAdapter, {optional: true})!;
91104
private _dateFormats = inject(MAT_DATE_FORMATS, {optional: true})!;
105+
private _scrollStrategyFactory = inject(MAT_TIMEPICKER_SCROLL_STRATEGY);
92106
protected _animationsDisabled =
93107
inject(ANIMATION_MODULE_TYPE, {optional: true}) === 'NoopAnimations';
94108

@@ -321,7 +335,7 @@ export class MatTimepicker<D> implements OnDestroy, MatOptionParentComponent {
321335

322336
this._overlayRef = this._overlay.create({
323337
positionStrategy,
324-
scrollStrategy: this._overlay.scrollStrategies.reposition(),
338+
scrollStrategy: this._scrollStrategyFactory(),
325339
direction: this._dir || 'ltr',
326340
hasBackdrop: false,
327341
});

tools/public_api_guard/material/timepicker.md

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { MatOptionParentComponent } from '@angular/material/core';
1717
import { ModelSignal } from '@angular/core';
1818
import { OnDestroy } from '@angular/core';
1919
import { OutputEmitterRef } from '@angular/core';
20+
import { ScrollStrategy } from '@angular/cdk/overlay';
2021
import { Signal } from '@angular/core';
2122
import { TemplateRef } from '@angular/core';
2223
import { ValidationErrors } from '@angular/forms';
@@ -25,6 +26,9 @@ import { Validator } from '@angular/forms';
2526
// @public
2627
export const MAT_TIMEPICKER_CONFIG: InjectionToken<MatTimepickerConfig>;
2728

29+
// @public
30+
export const MAT_TIMEPICKER_SCROLL_STRATEGY: InjectionToken<() => ScrollStrategy>;
31+
2832
// @public
2933
export class MatTimepicker<D> implements OnDestroy, MatOptionParentComponent {
3034
constructor();

0 commit comments

Comments
 (0)