From 8261de4a0228ca62d4e51d0da3022c4ec018ddc1 Mon Sep 17 00:00:00 2001 From: louis-19041904 Date: Mon, 17 Aug 2026 21:44:55 +0200 Subject: [PATCH] fix(tooltip): prevent tooltips from clipping off-screen near edges --- .../directives/component-tooltip.directive.ts | 186 ++++++++++++++---- webapp/src/styles.scss | 5 + 2 files changed, 154 insertions(+), 37 deletions(-) diff --git a/webapp/src/app/directives/component-tooltip.directive.ts b/webapp/src/app/directives/component-tooltip.directive.ts index 7fc7d482..8bd50ceb 100644 --- a/webapp/src/app/directives/component-tooltip.directive.ts +++ b/webapp/src/app/directives/component-tooltip.directive.ts @@ -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 = { [K in keyof T]: T[K] extends InputSignal ? V : T[K]; }; @@ -47,6 +53,7 @@ export class ComponentTooltipDirective implements OnDestroy { private overlayRef: OverlayRef | null = null; private componentRef: ComponentRef | null = null; + private computedMaxWidthPx = MIN_PANEL_WIDTH_PX; private showTimeout?: ReturnType; private hideTimeout?: ReturnType; private dismissTimeout?: ReturnType; @@ -134,52 +141,143 @@ export class ComponentTooltipDirective 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 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 => { diff --git a/webapp/src/styles.scss b/webapp/src/styles.scss index 14690546..86fde541 100644 --- a/webapp/src/styles.scss +++ b/webapp/src/styles.scss @@ -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;