Skip to content

Commit 23dc148

Browse files
committedMay 6, 2024·
fix(cdk/menu): allow for scroll strategy to be configured (#29005)
Adds an injection token that allows users to configure the scroll strategy of a CDK menu. Fixes #28965. (cherry picked from commit d2aeb8b)
1 parent 72baa7c commit 23dc148

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed
 

‎src/cdk/menu/context-menu-trigger.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class CdkContextMenuTrigger extends CdkMenuTriggerBase implements OnDestr
139139
private _getOverlayConfig(coordinates: ContextMenuCoordinates) {
140140
return new OverlayConfig({
141141
positionStrategy: this._getOverlayPositionStrategy(coordinates),
142-
scrollStrategy: this._overlay.scrollStrategies.reposition(),
142+
scrollStrategy: this.menuScrollStrategy(),
143143
direction: this._directionality || undefined,
144144
});
145145
}

‎src/cdk/menu/menu-trigger-base.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,25 @@ import {
1818
} from '@angular/core';
1919
import {Menu} from './menu-interface';
2020
import {MENU_STACK, MenuStack} from './menu-stack';
21-
import {ConnectedPosition, OverlayRef} from '@angular/cdk/overlay';
21+
import {ConnectedPosition, Overlay, OverlayRef, ScrollStrategy} from '@angular/cdk/overlay';
2222
import {TemplatePortal} from '@angular/cdk/portal';
2323
import {merge, Subject} from 'rxjs';
2424

2525
/** Injection token used for an implementation of MenuStack. */
2626
export const MENU_TRIGGER = new InjectionToken<CdkMenuTriggerBase>('cdk-menu-trigger');
2727

28+
/** Injection token used to configure the behavior of the menu when the page is scrolled. */
29+
export const MENU_SCROLL_STRATEGY = new InjectionToken<() => ScrollStrategy>(
30+
'cdk-menu-scroll-strategy',
31+
{
32+
providedIn: 'root',
33+
factory: () => {
34+
const overlay = inject(Overlay);
35+
return () => overlay.scrollStrategies.reposition();
36+
},
37+
},
38+
);
39+
2840
/**
2941
* Abstract directive that implements shared logic common to all menu triggers.
3042
* This class can be extended to create custom menu trigger types.
@@ -46,6 +58,9 @@ export abstract class CdkMenuTriggerBase implements OnDestroy {
4658
/** The menu stack in which this menu resides. */
4759
protected readonly menuStack: MenuStack = inject(MENU_STACK);
4860

61+
/** Function used to configure the scroll strategy for the menu. */
62+
protected readonly menuScrollStrategy = inject(MENU_SCROLL_STRATEGY);
63+
4964
/**
5065
* A list of preferred menu positions to be used when constructing the
5166
* `FlexibleConnectedPositionStrategy` for this trigger's menu.

‎src/cdk/menu/menu-trigger.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export class CdkMenuTrigger extends CdkMenuTriggerBase implements OnDestroy {
249249
private _getOverlayConfig() {
250250
return new OverlayConfig({
251251
positionStrategy: this._getOverlayPositionStrategy(),
252-
scrollStrategy: this._overlay.scrollStrategies.reposition(),
252+
scrollStrategy: this.menuScrollStrategy(),
253253
direction: this._directionality || undefined,
254254
});
255255
}

‎tools/public_api_guard/cdk/menu.md

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { OnDestroy } from '@angular/core';
2222
import { Optional } from '@angular/core';
2323
import { OverlayRef } from '@angular/cdk/overlay';
2424
import { QueryList } from '@angular/core';
25+
import { ScrollStrategy } from '@angular/cdk/overlay';
2526
import { Subject } from 'rxjs';
2627
import { TemplatePortal } from '@angular/cdk/portal';
2728
import { TemplateRef } from '@angular/core';
@@ -225,6 +226,7 @@ export abstract class CdkMenuTriggerBase implements OnDestroy {
225226
isOpen(): boolean;
226227
menuData: unknown;
227228
menuPosition: ConnectedPosition[];
229+
protected readonly menuScrollStrategy: () => ScrollStrategy;
228230
protected readonly menuStack: MenuStack;
229231
menuTemplateRef: TemplateRef<unknown> | null;
230232
// (undocumented)
@@ -296,6 +298,9 @@ export interface Menu extends MenuStackItem {
296298
// @public
297299
export const MENU_AIM: InjectionToken<MenuAim>;
298300

301+
// @public
302+
export const MENU_SCROLL_STRATEGY: InjectionToken<() => ScrollStrategy>;
303+
299304
// @public
300305
export const MENU_STACK: InjectionToken<MenuStack>;
301306

0 commit comments

Comments
 (0)
Please sign in to comment.