fix(tooltip): prevent tooltips from clipping off-screen near edges

This commit is contained in:
2026-08-17 21:44:55 +02:00
parent f69b3cf871
commit 8261de4a02
2 changed files with 154 additions and 37 deletions
@@ -18,6 +18,12 @@ import {
} from '@angular/cdk/overlay';
import { ComponentPortal, ComponentType } from '@angular/cdk/portal';
const STICKY_TOOLBAR_HEIGHT_PX = 76;
const EDGE_MARGIN_PX = 8;
const TOP_MARGIN_PX = STICKY_TOOLBAR_HEIGHT_PX + EDGE_MARGIN_PX;
const MIN_PANEL_WIDTH_PX = 240;
const MIN_PANEL_HEIGHT_PX = 150;
type UnwrapInputSignals<T> = {
[K in keyof T]: T[K] extends InputSignal<infer V> ? V : T[K];
};
@@ -47,6 +53,7 @@ export class ComponentTooltipDirective<T extends object> implements OnDestroy {
private overlayRef: OverlayRef | null = null;
private componentRef: ComponentRef<T> | null = null;
private computedMaxWidthPx = MIN_PANEL_WIDTH_PX;
private showTimeout?: ReturnType<typeof setTimeout>;
private hideTimeout?: ReturnType<typeof setTimeout>;
private dismissTimeout?: ReturnType<typeof setTimeout>;
@@ -134,52 +141,143 @@ export class ComponentTooltipDirective<T extends object> implements OnDestroy {
return false;
}
private clampedOffsetX(
alignToStart: boolean,
triggerRect: DOMRect,
actualWidthPx: number,
viewportWidth: number,
): number {
const desiredLeft = alignToStart
? triggerRect.left
: triggerRect.right - actualWidthPx;
const minLeft = EDGE_MARGIN_PX;
const maxLeft = Math.max(
EDGE_MARGIN_PX,
viewportWidth - actualWidthPx - EDGE_MARGIN_PX,
);
const clampedLeft = Math.min(Math.max(desiredLeft, minLeft), maxLeft);
const anchor = alignToStart ? triggerRect.left : triggerRect.right;
return clampedLeft - anchor + (alignToStart ? 0 : actualWidthPx);
}
private computePositioning(): {
positions: ConnectedPosition[];
maxWidthPx: number;
maxHeightPx: number;
} {
const triggerRect = this.elementRef.nativeElement.getBoundingClientRect();
const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight;
const spaceAbove = triggerRect.top - TOP_MARGIN_PX;
const spaceBelow = viewportHeight - triggerRect.bottom - EDGE_MARGIN_PX;
const spaceLeft = triggerRect.left - EDGE_MARGIN_PX;
const spaceRight = viewportWidth - triggerRect.right - EDGE_MARGIN_PX;
const preferBelow = spaceBelow >= spaceAbove;
const preferStartAligned = spaceRight >= spaceLeft;
const bestVerticalSpace = Math.max(spaceAbove, spaceBelow);
const bestHorizontalSpace = Math.max(spaceLeft, spaceRight);
const maxHeightPx = Math.max(bestVerticalSpace, MIN_PANEL_HEIGHT_PX);
const maxWidthPx = Math.max(bestHorizontalSpace, MIN_PANEL_WIDTH_PX);
const actualWidthPx = Math.min(
maxWidthPx,
this.resolveConfiguredWidthPx(this.tooltipMaxWidth()) ?? maxWidthPx,
);
const primaryOffsetX = this.clampedOffsetX(
preferStartAligned,
triggerRect,
actualWidthPx,
viewportWidth,
);
const secondaryOffsetX = this.clampedOffsetX(
!preferStartAligned,
triggerRect,
actualWidthPx,
viewportWidth,
);
const verticalPrimary: ConnectedPosition = preferBelow
? {
originX: preferStartAligned ? 'start' : 'end',
originY: 'bottom',
overlayX: preferStartAligned ? 'start' : 'end',
overlayY: 'top',
offsetX: primaryOffsetX,
offsetY: EDGE_MARGIN_PX,
}
: {
originX: preferStartAligned ? 'start' : 'end',
originY: 'top',
overlayX: preferStartAligned ? 'start' : 'end',
overlayY: 'bottom',
offsetX: primaryOffsetX,
offsetY: -EDGE_MARGIN_PX,
};
const verticalSecondary: ConnectedPosition = preferBelow
? {
originX: preferStartAligned ? 'end' : 'start',
originY: 'top',
overlayX: preferStartAligned ? 'end' : 'start',
overlayY: 'bottom',
offsetX: secondaryOffsetX,
offsetY: -EDGE_MARGIN_PX,
}
: {
originX: preferStartAligned ? 'end' : 'start',
originY: 'bottom',
overlayX: preferStartAligned ? 'end' : 'start',
overlayY: 'top',
offsetX: secondaryOffsetX,
offsetY: EDGE_MARGIN_PX,
};
const sidePrimary: ConnectedPosition = preferStartAligned
? {
originX: 'end',
originY: 'center',
overlayX: 'start',
overlayY: 'center',
offsetX: EDGE_MARGIN_PX,
}
: {
originX: 'start',
originY: 'center',
overlayX: 'end',
overlayY: 'center',
offsetX: -EDGE_MARGIN_PX,
};
const positions: ConnectedPosition[] = [
verticalPrimary,
verticalSecondary,
sidePrimary,
];
return { positions, maxWidthPx, maxHeightPx };
}
private show(): void {
if (this.overlayRef?.hasAttached()) return;
const positions: ConnectedPosition[] = [
{
originX: 'center',
originY: 'top',
overlayX: 'center',
overlayY: 'bottom',
offsetY: -8,
},
{
originX: 'center',
originY: 'bottom',
overlayX: 'center',
overlayY: 'top',
offsetY: 8,
},
{
originX: 'end',
originY: 'center',
overlayX: 'start',
overlayY: 'center',
offsetX: 8,
},
{
originX: 'start',
originY: 'center',
overlayX: 'end',
overlayY: 'center',
offsetX: -8,
},
];
const { positions, maxWidthPx, maxHeightPx } = this.computePositioning();
this.computedMaxWidthPx = maxWidthPx;
const positionStrategy = this.overlayPositionBuilder
.flexibleConnectedTo(this.elementRef)
.withPositions(positions)
.withFlexibleDimensions(false)
.withViewportMargin(12)
.withViewportMargin(EDGE_MARGIN_PX)
.withPush(true);
this.overlayRef = this.overlay.create({
positionStrategy,
scrollStrategy: this.overlay.scrollStrategies.reposition(),
panelClass: 'app-tooltip-panel',
maxWidth: this.tooltipMaxWidth() ?? 'calc(100vw - 24px)',
maxWidth: this.resolveMaxWidth(this.tooltipMaxWidth()),
maxHeight: `${maxHeightPx}px`,
});
const portal = new ComponentPortal(this.component(), null, this.injector);
@@ -206,12 +304,26 @@ export class ComponentTooltipDirective<T extends object> implements OnDestroy {
element: HTMLElement,
maxWidth: string | number | null,
): void {
if (maxWidth === null) {
element.style.maxWidth = '';
return;
element.style.maxWidth = this.resolveMaxWidth(maxWidth);
}
private resolveMaxWidth(maxWidth: string | number | null): string {
const configuredPx = this.resolveConfiguredWidthPx(maxWidth);
if (configuredPx === null) {
return typeof maxWidth === 'number'
? `${maxWidth}px`
: (maxWidth ?? `${this.computedMaxWidthPx}px`);
}
element.style.maxWidth =
typeof maxWidth === 'number' ? `${maxWidth}px` : maxWidth;
return `${Math.min(configuredPx, this.computedMaxWidthPx)}px`;
}
private resolveConfiguredWidthPx(
maxWidth: string | number | null,
): number | null {
if (maxWidth === null) return null;
if (typeof maxWidth === 'number') return maxWidth;
const trimmed = maxWidth.trim();
return trimmed.endsWith('px') ? parseFloat(trimmed) : null;
}
private readonly onOverlayEnter = (): void => {
+5
View File
@@ -582,6 +582,11 @@ html.dark-theme {
line-height: 1.4;
}
.cdk-overlay-pane.app-tooltip-panel {
max-height: 70vh;
overflow: auto;
}
.cdk-overlay-pane .mat-mdc-option {
padding: 0 18px;
font-size: 13px;