Skip to content

Commit f1b5dc5

Browse files
committed
fix(material/autocomplete): allow overlay backdrop when opened
Currently when we open the panel, backdrop is not allowed and is not inline with `mat-select`, `mat-menu`, etc. This fix will align autocomplete with those components Fixes #30457
1 parent 75713b0 commit f1b5dc5

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,8 @@ export class MatAutocompleteTrigger
900900
scrollStrategy: this._scrollStrategy(),
901901
width: this._getPanelWidth(),
902902
direction: this._dir ?? undefined,
903+
hasBackdrop: this._defaults?.hasBackdrop,
904+
backdropClass: this._defaults?.backdropClass || 'cdk-overlay-transparent-backdrop',
903905
panelClass: this._defaults?.overlayPanelClass,
904906
});
905907
}

src/material/autocomplete/autocomplete.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3014,6 +3014,66 @@ describe('MatAutocomplete', () => {
30143014
});
30153015
});
30163016

3017+
describe('with backdrop in default options', () => {
3018+
it('should contain backdrop by default', fakeAsync(() => {
3019+
const fixture = createComponent(SimpleAutocomplete, []);
3020+
fixture.detectChanges();
3021+
fixture.componentInstance.trigger.openPanel();
3022+
fixture.detectChanges();
3023+
3024+
tick(500);
3025+
3026+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeTruthy();
3027+
}));
3028+
3029+
it('should be able to remove the backdrop', fakeAsync(() => {
3030+
const fixture = createComponent(SimpleAutocomplete, [
3031+
{
3032+
provide: MAT_AUTOCOMPLETE_DEFAULT_OPTIONS,
3033+
useValue: {hasBackdrop: false},
3034+
},
3035+
]);
3036+
fixture.detectChanges();
3037+
fixture.componentInstance.trigger.openPanel();
3038+
fixture.detectChanges();
3039+
3040+
tick(500);
3041+
3042+
expect(overlayContainerElement.querySelector('.cdk-overlay-backdrop')).toBeFalsy();
3043+
}));
3044+
});
3045+
3046+
describe('with backdropClass in default options', () => {
3047+
it('should contain default backdropClass', fakeAsync(() => {
3048+
const fixture = createComponent(SimpleAutocomplete, []);
3049+
fixture.detectChanges();
3050+
fixture.componentInstance.trigger.openPanel();
3051+
fixture.detectChanges();
3052+
3053+
tick(500);
3054+
3055+
const cdkPanelElement = overlayContainerElement.querySelector('.cdk-overlay-backdrop');
3056+
expect(cdkPanelElement?.classList).toContain('cdk-overlay-transparent-backdrop');
3057+
}));
3058+
3059+
it('should be able to remove the backdrop', fakeAsync(() => {
3060+
const fixture = createComponent(SimpleAutocomplete, [
3061+
{
3062+
provide: MAT_AUTOCOMPLETE_DEFAULT_OPTIONS,
3063+
useValue: {backdropClass: 'my-custom-backdrop-class', hasBackdrop: true},
3064+
},
3065+
]);
3066+
fixture.detectChanges();
3067+
fixture.componentInstance.trigger.openPanel();
3068+
fixture.detectChanges();
3069+
3070+
tick(500);
3071+
3072+
const cdkPanelElement = overlayContainerElement.querySelector('.cdk-overlay-backdrop');
3073+
expect(cdkPanelElement?.classList).toContain('my-custom-backdrop-class');
3074+
}));
3075+
});
3076+
30173077
describe('misc', () => {
30183078
it('should allow basic use without any forms directives', () => {
30193079
expect(() => {

src/material/autocomplete/autocomplete.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ export interface MatAutocompleteDefaultOptions {
7070
*/
7171
requireSelection?: boolean;
7272

73+
/** Class to be applied to the autocomplete's backdrop. */
74+
backdropClass?: string;
75+
76+
/** Whether the autocomplete has a backdrop. */
77+
hasBackdrop?: boolean;
78+
7379
/** Class or list of classes to be applied to the autocomplete's overlay panel. */
7480
overlayPanelClass?: string | string[];
7581

@@ -97,6 +103,8 @@ export function MAT_AUTOCOMPLETE_DEFAULT_OPTIONS_FACTORY(): MatAutocompleteDefau
97103
autoSelectActiveOption: false,
98104
hideSingleSelectionIndicator: false,
99105
requireSelection: false,
106+
hasBackdrop: true,
107+
backdropClass: 'cdk-overlay-transparent-backdrop',
100108
};
101109
}
102110

tools/public_api_guard/material/autocomplete.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ export interface MatAutocompleteActivatedEvent {
135135
export interface MatAutocompleteDefaultOptions {
136136
autoActiveFirstOption?: boolean;
137137
autoSelectActiveOption?: boolean;
138+
backdropClass?: string;
139+
hasBackdrop?: boolean;
138140
hideSingleSelectionIndicator?: boolean;
139141
overlayPanelClass?: string | string[];
140142
requireSelection?: boolean;

0 commit comments

Comments
 (0)