Skip to content

Commit b81259e

Browse files
authored
refactor(material/menu): use separate signature for deprecated constructor parameters (#24116)
Moves the constructor deprecations out into a separate signature in order to make it easier to remove later on and to make the autocompletion in IDEs more accurate. This is something we've wanted to do for a while, but VIewEngine didn't support multiple constructor signatures. Also this is meant as a test to check if anything comes up during the presubmit. If it passes, we can roll this approach out to all constructors.
1 parent 040ef57 commit b81259e

File tree

4 files changed

+52
-26
lines changed

4 files changed

+52
-26
lines changed

src/material/menu/menu-content.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ export abstract class _MatMenuContentBase implements OnDestroy {
3737
/** Emits when the menu content has been attached. */
3838
readonly _attached = new Subject<void>();
3939

40+
/**
41+
* @deprecated `changeDetectorRef` is now a required parameter.
42+
* @breaking-change 9.0.0
43+
*/
44+
constructor(
45+
template: TemplateRef<any>,
46+
componentFactoryResolver: ComponentFactoryResolver,
47+
appRef: ApplicationRef,
48+
injector: Injector,
49+
viewContainerRef: ViewContainerRef,
50+
document: any,
51+
changeDetectorRef?: ChangeDetectorRef,
52+
);
53+
4054
constructor(
4155
private _template: TemplateRef<any>,
4256
private _componentFactoryResolver: ComponentFactoryResolver,
@@ -80,10 +94,7 @@ export abstract class _MatMenuContentBase implements OnDestroy {
8094
// not be updated by Angular. By explicitly marking for check here, we tell Angular that
8195
// it needs to check for new menu items and update the `@ContentChild` in `MatMenu`.
8296
// @breaking-change 9.0.0 Make change detector ref required
83-
if (this._changeDetectorRef) {
84-
this._changeDetectorRef.markForCheck();
85-
}
86-
97+
this._changeDetectorRef?.markForCheck();
8798
this._portal.attach(this._outlet, context);
8899
this._attached.next();
89100
}

src/material/menu/menu-item.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,28 @@ export class MatMenuItem
7575
/** Whether the menu item acts as a trigger for a sub-menu. */
7676
_triggersSubmenu: boolean = false;
7777

78+
/**
79+
* @deprecated `document` parameter to be removed, `changeDetectorRef` and
80+
* `focusMonitor` to become required.
81+
* @breaking-change 12.0.0
82+
*/
83+
constructor(
84+
elementRef: ElementRef<HTMLElement>,
85+
document?: any,
86+
focusMonitor?: FocusMonitor,
87+
parentMenu?: MatMenuPanel<MatMenuItem>,
88+
changeDetectorRef?: ChangeDetectorRef,
89+
);
90+
7891
constructor(
7992
private _elementRef: ElementRef<HTMLElement>,
80-
/**
81-
* @deprecated `_document` parameter is no longer being used and will be removed.
82-
* @breaking-change 12.0.0
83-
*/
8493
@Inject(DOCUMENT) _document?: any,
8594
private _focusMonitor?: FocusMonitor,
8695
@Inject(MAT_MENU_PANEL) @Optional() public _parentMenu?: MatMenuPanel<MatMenuItem>,
87-
/**
88-
* @deprecated `_changeDetectorRef` to become a required parameter.
89-
* @breaking-change 14.0.0
90-
*/
9196
private _changeDetectorRef?: ChangeDetectorRef,
9297
) {
93-
// @breaking-change 8.0.0 make `_focusMonitor` and `document` required params.
9498
super();
95-
96-
if (_parentMenu && _parentMenu.addItem) {
97-
_parentMenu.addItem(this);
98-
}
99+
_parentMenu?.addItem?.(this);
99100
}
100101

101102
/** Focuses the menu item. */
@@ -171,7 +172,7 @@ export class MatMenuItem
171172
// We need to mark this for check for the case where the content is coming from a
172173
// `matMenuContent` whose change detection tree is at the declaration position,
173174
// not the insertion position. See #23175.
174-
// @breaking-change 14.0.0 Remove null check for `_changeDetectorRef`.
175+
// @breaking-change 12.0.0 Remove null check for `_changeDetectorRef`.
175176
this._highlighted = isHighlighted;
176177
this._changeDetectorRef?.markForCheck();
177178
}

src/material/menu/menu-trigger.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,21 @@ export abstract class _MatMenuTriggerBase implements AfterContentInit, OnDestroy
185185
// tslint:disable-next-line:no-output-on-prefix
186186
@Output() readonly onMenuClose: EventEmitter<void> = this.menuClosed;
187187

188+
/**
189+
* @deprecated `focusMonitor` will become a required parameter.
190+
* @breaking-change 8.0.0
191+
*/
192+
constructor(
193+
overlay: Overlay,
194+
element: ElementRef<HTMLElement>,
195+
viewContainerRef: ViewContainerRef,
196+
scrollStrategy: any,
197+
parentMenu: MatMenuPanel,
198+
menuItemInstance: MatMenuItem,
199+
dir: Directionality,
200+
focusMonitor?: FocusMonitor | null,
201+
);
202+
188203
constructor(
189204
private _overlay: Overlay,
190205
private _element: ElementRef<HTMLElement>,
@@ -195,9 +210,7 @@ export abstract class _MatMenuTriggerBase implements AfterContentInit, OnDestroy
195210
// tslint:disable-next-line: lightweight-tokens
196211
@Optional() @Self() private _menuItemInstance: MatMenuItem,
197212
@Optional() private _dir: Directionality,
198-
// TODO(crisbeto): make the _focusMonitor required when doing breaking changes.
199-
// @breaking-change 8.0.0
200-
private _focusMonitor?: FocusMonitor,
213+
private _focusMonitor: FocusMonitor | null,
201214
) {
202215
this._scrollStrategy = scrollStrategy;
203216
this._parentMaterialMenu = parentMenu instanceof _MatMenuBase ? parentMenu : undefined;

tools/public_api_guard/material/menu.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ export class MatMenuContent extends _MatMenuContentBase {
167167

168168
// @public (undocumented)
169169
export abstract class _MatMenuContentBase implements OnDestroy {
170-
constructor(_template: TemplateRef<any>, _componentFactoryResolver: ComponentFactoryResolver, _appRef: ApplicationRef, _injector: Injector, _viewContainerRef: ViewContainerRef, _document: any, _changeDetectorRef?: ChangeDetectorRef | undefined);
170+
// @deprecated
171+
constructor(template: TemplateRef<any>, componentFactoryResolver: ComponentFactoryResolver, appRef: ApplicationRef, injector: Injector, viewContainerRef: ViewContainerRef, document: any, changeDetectorRef?: ChangeDetectorRef);
171172
attach(context?: any): void;
172173
readonly _attached: Subject<void>;
173174
detach(): void;
@@ -191,9 +192,8 @@ export interface MatMenuDefaultOptions {
191192

192193
// @public
193194
export class MatMenuItem extends _MatMenuItemBase implements FocusableOption, CanDisable, CanDisableRipple, AfterViewInit, OnDestroy {
194-
constructor(_elementRef: ElementRef<HTMLElement>,
195-
_document?: any, _focusMonitor?: FocusMonitor | undefined, _parentMenu?: MatMenuPanel<MatMenuItem> | undefined,
196-
_changeDetectorRef?: ChangeDetectorRef | undefined);
195+
// @deprecated
196+
constructor(elementRef: ElementRef<HTMLElement>, document?: any, focusMonitor?: FocusMonitor, parentMenu?: MatMenuPanel<MatMenuItem>, changeDetectorRef?: ChangeDetectorRef);
197197
_checkDisabled(event: Event): void;
198198
focus(origin?: FocusOrigin, options?: FocusOptions): void;
199199
readonly _focused: Subject<MatMenuItem>;
@@ -279,7 +279,8 @@ export class MatMenuTrigger extends _MatMenuTriggerBase {
279279

280280
// @public (undocumented)
281281
export abstract class _MatMenuTriggerBase implements AfterContentInit, OnDestroy {
282-
constructor(_overlay: Overlay, _element: ElementRef<HTMLElement>, _viewContainerRef: ViewContainerRef, scrollStrategy: any, parentMenu: MatMenuPanel, _menuItemInstance: MatMenuItem, _dir: Directionality, _focusMonitor?: FocusMonitor | undefined);
282+
// @deprecated
283+
constructor(overlay: Overlay, element: ElementRef<HTMLElement>, viewContainerRef: ViewContainerRef, scrollStrategy: any, parentMenu: MatMenuPanel, menuItemInstance: MatMenuItem, dir: Directionality, focusMonitor?: FocusMonitor | null);
283284
closeMenu(): void;
284285
// @deprecated (undocumented)
285286
get _deprecatedMatMenuTriggerFor(): MatMenuPanel;

0 commit comments

Comments
 (0)