Skip to content

feat(overlay): allow width/height when using point as flexible origin #16739

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
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,23 @@ describe('FlexibleConnectedPositionStrategy', () => {
expect(Math.floor(overlayRect.left)).toBe(50);
});

it('should be able to position relative to a point with width and height', () => {
positionStrategy
.setOrigin({x: 100, y: 200, width: 100, height: 50})
.withPositions([{
originX: 'end',
originY: 'bottom',
overlayX: 'end',
overlayY: 'top'
}]);

attachOverlay({positionStrategy});

const overlayRect = overlayRef.overlayElement.getBoundingClientRect();
expect(Math.floor(overlayRect.top)).toBe(250);
expect(Math.floor(overlayRect.right)).toBe(200);
});

});

it('should account for the `offsetX` pushing the overlay out of the screen', () => {
Expand Down
16 changes: 11 additions & 5 deletions src/cdk/overlay/position/flexible-connected-position-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import {OverlayContainer} from '../overlay-container';
const boundingBoxClass = 'cdk-overlay-connected-position-bounding-box';

/** Possible values that can be set as the origin of a FlexibleConnectedPositionStrategy. */
export type FlexibleConnectedPositionStrategyOrigin = ElementRef | HTMLElement | Point;
export type FlexibleConnectedPositionStrategyOrigin = ElementRef | HTMLElement | Point & {
width?: number;
height?: number;
};

/**
* A strategy for positioning overlays. Using this strategy, an overlay is given an
Expand Down Expand Up @@ -1094,14 +1097,17 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
return origin.getBoundingClientRect();
}

const width = origin.width || 0;
const height = origin.height || 0;

// If the origin is a point, return a client rect as if it was a 0x0 element at the point.
return {
top: origin.y,
bottom: origin.y,
bottom: origin.y + height,
left: origin.x,
right: origin.x,
height: 0,
width: 0
right: origin.x + width,
height,
width
};
}
}
Expand Down
1 change: 1 addition & 0 deletions src/cdk/overlay/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ export {ConnectedPositionStrategy} from './position/connected-position-strategy'
export {
ConnectedPosition,
FlexibleConnectedPositionStrategy,
FlexibleConnectedPositionStrategyOrigin,
} from './position/flexible-connected-position-strategy';
export {VIEWPORT_RULER_PROVIDER} from '@angular/cdk/scrolling';
5 changes: 5 additions & 0 deletions tools/public_api_guard/cdk/overlay.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ export declare class FlexibleConnectedPositionStrategy implements PositionStrate
withViewportMargin(margin: number): this;
}

export declare type FlexibleConnectedPositionStrategyOrigin = ElementRef | HTMLElement | Point & {
width?: number;
height?: number;
};

export declare class FullscreenOverlayContainer extends OverlayContainer implements OnDestroy {
constructor(_document: any);
protected _createContainer(): void;
Expand Down