Skip to content

Commit b98a3bd

Browse files
crisbetoandrewseguin
authored andcommitted
feat(overlay): allow width/height when using point as flexible origin (#16739)
Adds the ability to set an optional width and height when passing in a point to the `FlexibleConnectedPositionStrategy`. This allows for cases like a floating panel in a text editor with a `textarea` to be handled where the overlay should be positioned relative to a region, but the region might not be tied to a particular DOM node. Fixes #16160.
1 parent 89c784c commit b98a3bd

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

src/cdk/overlay/position/flexible-connected-position-strategy.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,23 @@ describe('FlexibleConnectedPositionStrategy', () => {
748748
expect(Math.floor(overlayRect.left)).toBe(50);
749749
});
750750

751+
it('should be able to position relative to a point with width and height', () => {
752+
positionStrategy
753+
.setOrigin({x: 100, y: 200, width: 100, height: 50})
754+
.withPositions([{
755+
originX: 'end',
756+
originY: 'bottom',
757+
overlayX: 'end',
758+
overlayY: 'top'
759+
}]);
760+
761+
attachOverlay({positionStrategy});
762+
763+
const overlayRect = overlayRef.overlayElement.getBoundingClientRect();
764+
expect(Math.floor(overlayRect.top)).toBe(250);
765+
expect(Math.floor(overlayRect.right)).toBe(200);
766+
});
767+
751768
});
752769

753770
it('should account for the `offsetX` pushing the overlay out of the screen', () => {

src/cdk/overlay/position/flexible-connected-position-strategy.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ import {OverlayContainer} from '../overlay-container';
3030
const boundingBoxClass = 'cdk-overlay-connected-position-bounding-box';
3131

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

3538
/**
3639
* A strategy for positioning overlays. Using this strategy, an overlay is given an
@@ -1095,14 +1098,17 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
10951098
return origin.getBoundingClientRect();
10961099
}
10971100

1101+
const width = origin.width || 0;
1102+
const height = origin.height || 0;
1103+
10981104
// If the origin is a point, return a client rect as if it was a 0x0 element at the point.
10991105
return {
11001106
top: origin.y,
1101-
bottom: origin.y,
1107+
bottom: origin.y + height,
11021108
left: origin.x,
1103-
right: origin.x,
1104-
height: 0,
1105-
width: 0
1109+
right: origin.x + width,
1110+
height,
1111+
width
11061112
};
11071113
}
11081114
}

src/cdk/overlay/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ export {ConnectedPositionStrategy} from './position/connected-position-strategy'
2727
export {
2828
ConnectedPosition,
2929
FlexibleConnectedPositionStrategy,
30+
FlexibleConnectedPositionStrategyOrigin,
3031
} from './position/flexible-connected-position-strategy';
3132
export {VIEWPORT_RULER_PROVIDER} from '@angular/cdk/scrolling';

tools/public_api_guard/cdk/overlay.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ export declare class FlexibleConnectedPositionStrategy implements PositionStrate
128128
withViewportMargin(margin: number): this;
129129
}
130130

131+
export declare type FlexibleConnectedPositionStrategyOrigin = ElementRef | HTMLElement | Point & {
132+
width?: number;
133+
height?: number;
134+
};
135+
131136
export declare class FullscreenOverlayContainer extends OverlayContainer implements OnDestroy {
132137
constructor(_document: any);
133138
protected _createContainer(): void;

0 commit comments

Comments
 (0)