Skip to content

fix(autocomplete): provide horizontal fallback positions #18906

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,32 +694,30 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, AfterViewIn

/** Sets the positions on a position strategy based on the directive's input state. */
private _setStrategyPositions(positionStrategy: FlexibleConnectedPositionStrategy) {
const belowPosition: ConnectedPosition = {
originX: 'start',
originY: 'bottom',
overlayX: 'start',
overlayY: 'top'
};
const abovePosition: ConnectedPosition = {
originX: 'start',
originY: 'top',
overlayX: 'start',
overlayY: 'bottom',

// The overlay edge connected to the trigger should have squared corners, while
// the opposite end has rounded corners. We apply a CSS class to swap the
// border-radius based on the overlay position.
panelClass: 'mat-autocomplete-panel-above'
};
// Note that we provide horizontal fallback positions, even though by default the dropdown
// width matches the input, because consumers can override the width. See #18854.
const belowPositions: ConnectedPosition[] = [
{originX: 'start', originY: 'bottom', overlayX: 'start', overlayY: 'top'},
{originX: 'end', originY: 'bottom', overlayX: 'end', overlayY: 'top'}
];

// The overlay edge connected to the trigger should have squared corners, while
// the opposite end has rounded corners. We apply a CSS class to swap the
// border-radius based on the overlay position.
const panelClass = 'mat-autocomplete-panel-above';
const abovePositions: ConnectedPosition[] = [
{originX: 'start', originY: 'top', overlayX: 'start', overlayY: 'bottom', panelClass},
{originX: 'end', originY: 'top', overlayX: 'end', overlayY: 'bottom', panelClass}
];

let positions: ConnectedPosition[];

if (this.position === 'above') {
positions = [abovePosition];
positions = abovePositions;
} else if (this.position === 'below') {
positions = [belowPosition];
positions = belowPositions;
} else {
positions = [belowPosition, abovePosition];
positions = [...belowPositions, ...abovePositions];
}

positionStrategy.withPositions(positions);
Expand Down