Skip to content

Commit e8d6454

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

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
@@ -16,6 +16,7 @@ import {
1616
effect,
1717
ElementRef,
1818
inject,
19+
InjectionToken,
1920
Injector,
2021
input,
2122
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`.
@@ -101,6 +114,7 @@ export class MatTimepicker<D> implements OnDestroy, MatOptionParentComponent {
101114
private _defaultConfig = inject(MAT_TIMEPICKER_CONFIG, {optional: true});
102115
private _dateAdapter = inject<DateAdapter<D>>(DateAdapter, {optional: true})!;
103116
private _dateFormats = inject(MAT_DATE_FORMATS, {optional: true})!;
117+
private _scrollStrategyFactory = inject(MAT_TIMEPICKER_SCROLL_STRATEGY);
104118

105119
private _isOpen = signal(false);
106120
private _activeDescendant = signal<string | null>(null);
@@ -314,7 +328,7 @@ export class MatTimepicker<D> implements OnDestroy, MatOptionParentComponent {
314328

315329
this._overlayRef = this._overlay.create({
316330
positionStrategy,
317-
scrollStrategy: this._overlay.scrollStrategies.reposition(),
331+
scrollStrategy: this._scrollStrategyFactory(),
318332
direction: this._dir || 'ltr',
319333
hasBackdrop: false,
320334
});

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)