Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cdk/menu): allow user to configure scroll strategy on menu triggers #28965

Closed
goetzrobin opened this issue Apr 25, 2024 · 1 comment · Fixed by #29005
Closed

feat(cdk/menu): allow user to configure scroll strategy on menu triggers #28965

goetzrobin opened this issue Apr 25, 2024 · 1 comment · Fixed by #29005
Assignees
Labels
area: cdk/menu feature This issue represents a new feature or feature request rather than a bug or bug fix P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@goetzrobin
Copy link

Feature Description

I'd like to change the scroll strategy of a cdk menu to lock the screen instead of repositioning it.
This is possible in Material by providing a custom MAT_MENU_SCROLL_STRATEGY injection token.

/** Injection token that determines the scroll handling while the menu is open. */
export const MAT_MENU_SCROLL_STRATEGY = new InjectionToken<() => ScrollStrategy>(
  'mat-menu-scroll-strategy',
  {
    providedIn: 'root',
    factory: () => {
      const overlay = inject(Overlay);
      return () => overlay.scrollStrategies.reposition();
    },
  },
);

/** @docs-private */
export function MAT_MENU_SCROLL_STRATEGY_FACTORY(overlay: Overlay): () => ScrollStrategy {
  return () => overlay.scrollStrategies.reposition();
}

/** @docs-private */
export const MAT_MENU_SCROLL_STRATEGY_FACTORY_PROVIDER = {
  provide: MAT_MENU_SCROLL_STRATEGY,
  deps: [Overlay],
  useFactory: MAT_MENU_SCROLL_STRATEGY_FACTORY,
};

The customizable scrollStrategy is then provided through DI in the constructor:

this._scrollStrategy = scrollStrategy;

However, in the cdk version of the menu the scroll strategy is hard coded and does not allow for any customization:

  /** Get the configuration object used to create the overlay. */
  private _getOverlayConfig() {
    return new OverlayConfig({
      positionStrategy: this._getOverlayPositionStrategy(),
      scrollStrategy: this._overlay.scrollStrategies.reposition(),
      direction: this._directionality || undefined,
    });
  }

scrollStrategy: this._overlay.scrollStrategies.reposition(),

Use Case

I am building a component library on top of the Angular CDK. I'd love to allow users to choose if they'd like to lock scrolling upon opening of a dropdown menu like the default of Angular Material's Menu: https://material.angular.io/components/menu/overview
Or keep the repositioning strategy that is currently the only option for the CDK.
I am specifically trying to implement this request from one of my users: goetzrobin/spartan#277

I think Angular Material supporting a custom strategy and the CDK not supporting a strategy is confusing to a lot of end users and I'd love for both to be customizable!

@goetzrobin goetzrobin added feature This issue represents a new feature or feature request rather than a bug or bug fix needs triage This issue needs to be triaged by the team labels Apr 25, 2024
@alcaidio
Copy link

alcaidio commented May 1, 2024

You can use the cdk overlay until it's available

@crisbeto crisbeto added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent area: cdk/menu and removed needs triage This issue needs to be triaged by the team labels May 3, 2024
crisbeto added a commit to crisbeto/material2 that referenced this issue May 6, 2024
Adds an injection token that allows users to configure the scroll strategy of a CDK menu.

Fixes angular#28965.
crisbeto added a commit to crisbeto/material2 that referenced this issue May 6, 2024
Adds an injection token that allows users to configure the scroll strategy of a CDK menu.

Fixes angular#28965.
@crisbeto crisbeto self-assigned this May 6, 2024
crisbeto added a commit that referenced this issue May 6, 2024
Adds an injection token that allows users to configure the scroll strategy of a CDK menu.

Fixes #28965.
crisbeto added a commit that referenced this issue May 6, 2024
Adds an injection token that allows users to configure the scroll strategy of a CDK menu.

Fixes #28965.

(cherry picked from commit d2aeb8b)
crisbeto added a commit that referenced this issue May 6, 2024
Adds an injection token that allows users to configure the scroll strategy of a CDK menu.

Fixes #28965.

(cherry picked from commit d2aeb8b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: cdk/menu feature This issue represents a new feature or feature request rather than a bug or bug fix P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants