Skip to content

Commit c585fdd

Browse files
committed
fix(material/autocomplete): Prevent Autocomplete overlap with outlined form-field label when overlay is positioned above
Fixed by preventing Autocomplete overlap with outlined form-field label when overlay is positioned above. Implemented a function to calculate the necessary vertical offset for outlined appearance, ensuring label legibility. Fix #27476
1 parent 24202a8 commit c585fdd

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,9 +830,20 @@ export class MatAutocompleteTrigger
830830
// the opposite end has rounded corners. We apply a CSS class to swap the
831831
// border-radius based on the overlay position.
832832
const panelClass = this._aboveClass;
833+
834+
// Calculate vertical overlay offset with outline, or set to 0.
835+
const offsetY = this._formField?._getOverlayOffsetOutlined() || 0;
836+
833837
const abovePositions: ConnectedPosition[] = [
834-
{originX: 'start', originY: 'top', overlayX: 'start', overlayY: 'bottom', panelClass},
835-
{originX: 'end', originY: 'top', overlayX: 'end', overlayY: 'bottom', panelClass},
838+
{
839+
originX: 'start',
840+
originY: 'top',
841+
overlayX: 'start',
842+
overlayY: 'bottom',
843+
panelClass,
844+
offsetY,
845+
},
846+
{originX: 'end', originY: 'top', overlayX: 'end', overlayY: 'bottom', panelClass, offsetY},
836847
];
837848

838849
let positions: ConnectedPosition[];

src/material/form-field/form-field.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,16 @@ export class MatFormField
503503
return this.appearance === 'outline';
504504
}
505505

506+
/**
507+
* Calculate the vertical offset for the overlay when the input field has an outline appearance.
508+
* In the outline appearance, the overlay should have extra space to display the label correctly.
509+
* This function calculates the vertical offset needed.
510+
*/
511+
_getOverlayOffsetOutlined() {
512+
const outlineHeight = this._floatingLabel?.element.getBoundingClientRect().height || 0;
513+
return this._hasOutline() ? -outlineHeight / 2 : 0;
514+
}
515+
506516
/**
507517
* Whether the label should display in the infix. Labels in the outline appearance are
508518
* displayed as part of the notched-outline and are horizontally offset to account for

0 commit comments

Comments
 (0)