Skip to content

Commit 5244a9f

Browse files
authored
feat(menu): add input to add overlay pane classes (#19547)
* feat(menu): add option to add overlay pane classes * api golden
1 parent a290168 commit 5244a9f

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

src/material/menu/menu-panel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export interface MatMenuPanel<T = any> {
3636
setElevation?(depth: number): void;
3737
lazyContent?: MatMenuContent;
3838
backdropClass?: string;
39+
overlayPanelClass?: string|string[];
3940
hasBackdrop?: boolean;
4041
readonly panelId?: string;
4142

src/material/menu/menu-trigger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
405405
.withLockedPosition()
406406
.withTransformOriginOn('.mat-menu-panel, .mat-mdc-menu-panel'),
407407
backdropClass: this.menu.backdropClass || 'cdk-overlay-transparent-backdrop',
408+
panelClass: this.menu.overlayPanelClass,
408409
scrollStrategy: this._scrollStrategy(),
409410
direction: this._dir
410411
});

src/material/menu/menu.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,41 @@ describe('MatMenu', () => {
245245
expect(backdrop.classList).toContain('custom-backdrop');
246246
}));
247247

248+
it('should be able to set a custom class on the overlay panel', fakeAsync(() => {
249+
const optionsProvider = {
250+
provide: MAT_MENU_DEFAULT_OPTIONS,
251+
useValue: {overlayPanelClass: 'custom-panel-class'}
252+
};
253+
const fixture = createComponent(SimpleMenu, [optionsProvider], [FakeIcon]);
254+
255+
fixture.detectChanges();
256+
fixture.componentInstance.trigger.openMenu();
257+
fixture.detectChanges();
258+
tick(500);
259+
260+
const overlayPane = <HTMLElement>overlayContainerElement.querySelector('.cdk-overlay-pane');
261+
262+
expect(overlayPane.classList).toContain('custom-panel-class');
263+
}));
264+
265+
it('should be able to set a custom classes on the overlay panel', fakeAsync(() => {
266+
const optionsProvider = {
267+
provide: MAT_MENU_DEFAULT_OPTIONS,
268+
useValue: {overlayPanelClass: ['custom-panel-class-1', 'custom-panel-class-2']}
269+
};
270+
const fixture = createComponent(SimpleMenu, [optionsProvider], [FakeIcon]);
271+
272+
fixture.detectChanges();
273+
fixture.componentInstance.trigger.openMenu();
274+
fixture.detectChanges();
275+
tick(500);
276+
277+
const overlayPane = <HTMLElement>overlayContainerElement.querySelector('.cdk-overlay-pane');
278+
279+
expect(overlayPane.classList).toContain('custom-panel-class-1');
280+
expect(overlayPane.classList).toContain('custom-panel-class-2');
281+
}));
282+
248283
it('should restore focus to the root trigger when the menu was opened by mouse', fakeAsync(() => {
249284
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
250285
fixture.detectChanges();

src/material/menu/menu.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ export interface MatMenuDefaultOptions {
6464
/** Class to be applied to the menu's backdrop. */
6565
backdropClass: string;
6666

67+
/** Class or list of classes to be applied to the menu's overlay panel. */
68+
overlayPanelClass?: string | string[];
69+
6770
/** Whether the menu has a backdrop. */
6871
hasBackdrop?: boolean;
6972
}
@@ -129,6 +132,9 @@ export class _MatMenuBase implements AfterContentInit, MatMenuPanel<MatMenuItem>
129132
/** Layout direction of the menu. */
130133
direction: Direction;
131134

135+
/** Class or list of classes to be added to the overlay panel. */
136+
overlayPanelClass: string|string[] = this._defaultOptions.overlayPanelClass || '';
137+
132138
/** Class to be added to the backdrop element. */
133139
@Input() backdropClass: string = this._defaultOptions.backdropClass;
134140

tools/public_api_guard/material/menu.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export declare class _MatMenuBase implements AfterContentInit, MatMenuPanel<MatM
2727
lazyContent: MatMenuContent;
2828
get overlapTrigger(): boolean;
2929
set overlapTrigger(value: boolean);
30+
overlayPanelClass: string | string[];
3031
set panelClass(classes: string);
3132
readonly panelId: string;
3233
parentMenu: MatMenuPanel | undefined;
@@ -94,6 +95,7 @@ export interface MatMenuDefaultOptions {
9495
backdropClass: string;
9596
hasBackdrop?: boolean;
9697
overlapTrigger: boolean;
98+
overlayPanelClass?: string | string[];
9799
xPosition: MenuPositionX;
98100
yPosition: MenuPositionY;
99101
}
@@ -134,6 +136,7 @@ export interface MatMenuPanel<T = any> {
134136
hasBackdrop?: boolean;
135137
lazyContent?: MatMenuContent;
136138
overlapTrigger: boolean;
139+
overlayPanelClass?: string | string[];
137140
readonly panelId?: string;
138141
parentMenu?: MatMenuPanel | undefined;
139142
removeItem?: (item: T) => void;

0 commit comments

Comments
 (0)