Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Fix live demo shared tooltip positioning

rcourtman4cf99026e16e
diff --git a/docs/release-control/v6/internal/subsystems/frontend-primitives.md b/docs/release-control/v6/internal/subsystems/frontend-primitives.md
index 1b5390a407..80d54fc896 100644
--- a/docs/release-control/v6/internal/subsystems/frontend-primitives.md
+++ b/docs/release-control/v6/internal/subsystems/frontend-primitives.md
@@ -773,6 +773,10 @@ canonical tag-badge primitive. Dashboard workload rows and the unified-resource
detail drawer must import that shared owner instead of keeping a dashboard-local
tag badge variant or importing a feature-local path into infrastructure
surfaces.
+That same owner now also holds the CSP-safe tag-dot rendering contract: tag
+color and active-state emphasis must travel through SVG fill/stroke attributes
+or stable classes, not inline `background-color`, `box-shadow`, or other
+`style=` mutations that break the hosted demo CSP.
`frontend-modern/src/components/Settings/OperationsPanel.tsx` is now also the
canonical shared settings wrapper for operations-style panels such as
diagnostics, reporting, and system logs. Those surfaces must extend that owner
@@ -1016,6 +1020,10 @@ The remaining header, overlay, and tooltip render surfaces now live in
`frontend-modern/src/components/shared/HistoryChartOverlay.tsx`, and
`frontend-modern/src/components/shared/HistoryChartTooltip.tsx` instead of
re-accumulating those sections inline in the shell.
+That tooltip owner now also holds the CSP-safe hover contract: chart tooltips
+must render inside the chart surface with model-owned layout and SVG/attribute
+positioning, not through fixed portals or inline `left`/`top` style attributes
+that violate the public demo CSP.
The shared container update badge now follows that same owner split.
`frontend-modern/src/components/shared/ContainerUpdateBadge.tsx` stays the
render surface for the badge, icon, and update button shells,
@@ -1157,6 +1165,9 @@ sanitization logic back into the shared shell. Shared portal-mounted tooltip
shells such as `frontend-modern/src/components/shared/TooltipPortal.tsx` must
use the same semantic surface tokens as the canonical tooltip instead of
introducing light-mode-inverted palettes.
+That same tooltip owner now also holds the CSP-safe portal contract: shared
+tooltip shells must render through SVG/attribute positioning and viewport-
+clamped layout helpers rather than fixed inline `left`/`top` style attributes.
The shared collapsible search input now follows that same owner split.
`frontend-modern/src/components/shared/CollapsibleSearchInput.tsx` stays the
render shell, `frontend-modern/src/components/shared/useCollapsibleSearchInputState.ts`
diff --git a/frontend-modern/src/components/shared/HistoryChart.tsx b/frontend-modern/src/components/shared/HistoryChart.tsx
index b7fdd0b23f..e9892e601d 100644
--- a/frontend-modern/src/components/shared/HistoryChart.tsx
+++ b/frontend-modern/src/components/shared/HistoryChart.tsx
@@ -33,9 +33,13 @@ export const HistoryChart: Component<HistoryChartProps> = (props) => {
onMouseLeave={chart.handleMouseLeave}
/>
<HistoryChartOverlay chart={chart} hideLock={props.hideLock} />
+ <HistoryChartTooltip
+ hoveredPoint={chart.hoveredPoint()}
+ chartWidth={chart.chartWidth()}
+ chartHeight={chart.chartHeight()}
+ unit={props.unit}
+ />
</div>
-
- <HistoryChartTooltip hoveredPoint={chart.hoveredPoint()} unit={props.unit} />
</div>
);
};
diff --git a/frontend-modern/src/components/shared/HistoryChartTooltip.tsx b/frontend-modern/src/components/shared/HistoryChartTooltip.tsx
index 063777b263..fea3d2a226 100644
--- a/frontend-modern/src/components/shared/HistoryChartTooltip.tsx
+++ b/frontend-modern/src/components/shared/HistoryChartTooltip.tsx
@@ -1,36 +1,56 @@
import { Component, Show } from 'solid-js';
-import { Portal } from 'solid-js/web';
-import { formatHistoryChartTooltipValue, type HistoryChartHoverPoint } from './historyChartModel';
+import {
+ formatHistoryChartTooltipValue,
+ getHistoryChartTooltipLayout,
+ type HistoryChartHoverPoint,
+} from './historyChartModel';
interface HistoryChartTooltipProps {
hoveredPoint: HistoryChartHoverPoint | null;
+ chartWidth: number;
+ chartHeight: number;
unit?: string;
}
export const HistoryChartTooltip: Component<HistoryChartTooltipProps> = (props) => {
return (
- <Portal>
+ <svg
+ class="pointer-events-none absolute inset-0 h-full w-full overflow-visible"
+ viewBox={`0 0 ${props.chartWidth} ${props.chartHeight}`}
+ preserveAspectRatio="none"
+ aria-hidden="true"
+ >
<Show when={props.hoveredPoint}>
- {(point) => (
- <div
- class="fixed pointer-events-none text-xs rounded px-2 py-1 shadow-lg border border-slate-600 z-[9999]"
- style={{
- left: `${point().x}px`,
- top: `${point().y}px`,
- transform: 'translateX(-50%)',
- 'background-color': 'rgb(15, 23, 42)',
- color: 'rgb(248, 250, 252)',
- }}
- >
- <div class="font-medium text-center mb-0.5">
- {new Date(point().timestamp).toLocaleString()}
- </div>
- <div style={{ color: 'rgb(203, 213, 225)' }}>
- {formatHistoryChartTooltipValue(point().value, props.unit)}
- </div>
- </div>
- )}
+ {(point) => {
+ const layout = () =>
+ getHistoryChartTooltipLayout({
+ hoveredPoint: point(),
+ chartWidth: props.chartWidth,
+ chartHeight: props.chartHeight,
+ });
+ return (
+ <foreignObject
+ x={layout().x}
+ y={layout().y}
+ width={layout().width}
+ height={layout().height}
+ overflow="visible"
+ >
+ <div
+ data-history-chart-tooltip="true"
+ class="h-full w-full rounded border border-slate-600 bg-slate-900 px-2 py-1 text-xs text-slate-50 shadow-lg"
+ >
+ <div class="mb-0.5 text-center font-medium">
+ {new Date(point().timestamp).toLocaleString()}
+ </div>
+ <div class="text-slate-300">
+ {formatHistoryChartTooltipValue(point().value, props.unit)}
+ </div>
+ </div>
+ </foreignObject>
+ );
+ }}
</Show>
- </Portal>
+ </svg>
);
};
diff --git a/frontend-modern/src/components/shared/SharedPrimitives.guardrails.test.ts b/frontend-modern/src/components/shared/SharedPrimitives.guardrails.test.ts
index b35587aa47..9b552c2693 100644
--- a/frontend-modern/src/components/shared/SharedPrimitives.guardrails.test.ts
+++ b/frontend-modern/src/components/shared/SharedPrimitives.guardrails.test.ts
@@ -236,7 +236,8 @@ describe('shared primitive guardrails', () => {
});
it('keeps shared tooltip shells on semantic theme tokens', () => {
- expect(tooltipSource).toContain('bg-surface text-base-content border-border');
+ expect(tooltipSource).toContain('border border-border bg-surface');
+ expect(tooltipSource).toContain('text-base-content');
expect(tooltipSource).not.toContain("'background-color': 'rgb(15, 23, 42)'");
expect(tooltipPortalSource).toContain('bg-surface');
expect(tooltipPortalSource).toContain('text-base-content');
@@ -895,6 +896,7 @@ describe('shared primitive guardrails', () => {
expect(historyChartStateSource).toContain('HISTORY_CHART_RANGES');
expect(historyChartModelSource).toContain('formatHistoryChartTooltipValue');
+ expect(historyChartModelSource).toContain('getHistoryChartTooltipLayout');
expect(historyChartModelSource).toContain('HISTORY_CHART_RANGES');
expect(historyChartModelSource).toContain('getHistoryChartScale');
expect(historyChartModelSource).toContain('findHistoryChartClosestPoint');
@@ -909,10 +911,23 @@ describe('shared primitive guardrails', () => {
expect(historyChartOverlaySource).not.toContain('setupCanvasDPR');
expect(historyChartTooltipSource).toContain('formatHistoryChartTooltipValue');
+ expect(historyChartTooltipSource).toContain('getHistoryChartTooltipLayout');
+ expect(historyChartTooltipSource).toContain('foreignObject');
expect(historyChartTooltipSource).toContain('new Date(point().timestamp).toLocaleString()');
+ expect(historyChartTooltipSource).not.toContain('<Portal>');
+ expect(historyChartTooltipSource).not.toContain('style={');
expect(historyChartTooltipSource).not.toContain('ChartsAPI.getMetricsHistory');
});
+ it('keeps tag badges CSP-safe', () => {
+ expect(tagBadgesSource).toContain('data-tag-dot="true"');
+ expect(tagBadgesSource).toContain('data-tag-dot-fill="true"');
+ expect(tagBadgesSource).toContain('data-tag-dot-ring="true"');
+ expect(tagBadgesSource).not.toContain('style={');
+ expect(tagBadgesSource).not.toContain('box-shadow');
+ expect(tagBadgesSource).not.toContain('background-color');
+ });
+
it('keeps container update badge on shell, runtime, and model owners', () => {
expect(containerUpdateBadgeSource).toContain('useContainerUpdateButtonState');
expect(containerUpdateBadgeSource).toContain('getUpdateButtonClass');
@@ -1166,12 +1181,21 @@ describe('shared primitive guardrails', () => {
it('keeps tooltip on shell, runtime, and model owners', () => {
expect(tooltipSource).toContain('useTooltipState');
expect(tooltipSource).toContain('createTooltipSystemState');
+ expect(tooltipSource).toContain('foreignObject');
expect(tooltipSource).not.toContain('createSignal');
expect(tooltipSource).not.toContain('requestAnimationFrame');
expect(tooltipSource).not.toContain('sanitizeTooltipContent');
expect(tooltipSource).not.toContain('resolveTooltipPosition');
+ expect(tooltipSource).not.toContain('style={');
+
+ expect(tooltipPortalSource).toContain('useTooltipPortalState');
+ expect(tooltipPortalSource).toContain('foreignObject');
+ expect(tooltipPortalSource).not.toContain('createSignal');
+ expect(tooltipPortalSource).not.toContain('resolveTooltipPosition');
+ expect(tooltipPortalSource).not.toContain('style={');
expect(tooltipStateSource).toContain('export function useTooltipState');
+ expect(tooltipStateSource).toContain('export function useTooltipPortalState');
expect(tooltipStateSource).toContain('export function createTooltipSystemState');
expect(tooltipStateSource).toContain('createSignal');
expect(tooltipStateSource).toContain('requestAnimationFrame');
diff --git a/frontend-modern/src/components/shared/TagBadges.tsx b/frontend-modern/src/components/shared/TagBadges.tsx
index fdfc45672d..48ae13f390 100644
--- a/frontend-modern/src/components/shared/TagBadges.tsx
+++ b/frontend-modern/src/components/shared/TagBadges.tsx
@@ -23,6 +23,8 @@ export const TagBadges: Component<TagBadgesProps> = (props) => {
const TagDot: Component<{ tag: string }> = (dotProps) => {
const colors = () => getTagColorWithSpecial(dotProps.tag, isDark());
const isActive = () => props.activeSearch?.includes(`tags:${dotProps.tag}`) || false;
+ const ringClass = () =>
+ isActive() ? (isDark() ? 'text-white/90' : 'text-black/80') : 'text-transparent';
return (
<div
@@ -42,17 +44,24 @@ export const TagBadges: Component<TagBadgesProps> = (props) => {
props.onTagClick?.(dotProps.tag);
}}
>
- <div
- class="w-2 h-2 rounded-full hover:scale-150 transition-transform duration-200 ease-out cursor-pointer"
- style={{
- 'background-color': colors().bg,
- 'box-shadow': isActive()
- ? isDark()
- ? `0 0 0 2.5px rgba(255, 255, 255, 0.9)`
- : `0 0 0 2.5px rgba(0, 0, 0, 0.8)`
- : 'none',
- }}
- />
+ <svg
+ data-tag-dot="true"
+ data-active={isActive() ? 'true' : 'false'}
+ viewBox="0 0 10 10"
+ aria-hidden="true"
+ class={`h-2 w-2 overflow-visible hover:scale-150 transition-transform duration-200 ease-out cursor-pointer ${ringClass()}`}
+ >
+ <circle
+ data-tag-dot-ring="true"
+ cx="5"
+ cy="5"
+ r="4"
+ fill="none"
+ stroke="currentColor"
+ stroke-width="2"
+ />
+ <circle data-tag-dot-fill="true" cx="5" cy="5" r="3" fill={colors().bg} />
+ </svg>
</div>
);
};
diff --git a/frontend-modern/src/components/shared/Tooltip.tsx b/frontend-modern/src/components/shared/Tooltip.tsx
index c03c7113e1..21e11bb9d6 100644
--- a/frontend-modern/src/components/shared/Tooltip.tsx
+++ b/frontend-modern/src/components/shared/Tooltip.tsx
@@ -19,18 +19,28 @@ export const Tooltip: Component<TooltipProps> = (props) => {
return (
<Show when={props.visible}>
<Portal mount={document.body}>
- <div
- ref={state.setTooltipRef}
- class="fixed z-[9999] px-3 py-2 text-xs whitespace-pre-line rounded-md border shadow-sm pointer-events-none bg-surface text-base-content border-border leading-tight"
- style={{
- left: `${state.position().left}px`,
- top: `${state.position().top}px`,
- 'max-width': `${props.maxWidth ?? 240}px`,
- opacity: props.visible ? '1' : '0',
- transition: 'opacity 120ms ease-out',
- }}
- textContent={state.sanitizedContent()}
- />
+ <svg
+ class="fixed inset-0 z-[9999] h-screen w-screen overflow-visible pointer-events-none"
+ viewBox={`0 0 ${state.viewport().width} ${state.viewport().height}`}
+ preserveAspectRatio="none"
+ aria-hidden="true"
+ >
+ <foreignObject
+ x={state.position().left}
+ y={state.position().top}
+ width={state.maxWidth()}
+ height={state.viewport().height}
+ overflow="visible"
+ >
+ <div
+ ref={state.setTooltipRef}
+ data-tooltip="true"
+ class="inline-block max-w-full whitespace-pre-line rounded-md border border-border bg-surface px-3 py-2 text-xs leading-tight text-base-content shadow-sm"
+ >
+ {state.sanitizedContent()}
+ </div>
+ </foreignObject>
+ </svg>
</Portal>
</Show>
);
diff --git a/frontend-modern/src/components/shared/TooltipPortal.tsx b/frontend-modern/src/components/shared/TooltipPortal.tsx
index 7382123ee5..9020f85df7 100644
--- a/frontend-modern/src/components/shared/TooltipPortal.tsx
+++ b/frontend-modern/src/components/shared/TooltipPortal.tsx
@@ -1,8 +1,11 @@
import { Show } from 'solid-js';
import { Portal } from 'solid-js/web';
import type { JSX } from 'solid-js';
+import { useTooltipPortalState } from './useTooltipState';
+import type { TooltipOptions } from './tooltipModel';
-interface TooltipPortalProps {
+interface TooltipPortalProps extends TooltipOptions {
+ maxWidth?: number;
when: boolean;
x: number;
y: number;
@@ -14,23 +17,33 @@ interface TooltipPortalProps {
* Replaces 10+ copy-pasted Portal + fixed positioning blocks.
*/
export function TooltipPortal(props: TooltipPortalProps) {
+ const state = useTooltipPortalState(props);
+
return (
<Show when={props.when}>
<Portal mount={document.body}>
- <div
- class="fixed z-[9999] pointer-events-none"
- style={{
- left: `${props.x}px`,
- top: `${props.y - 8}px`,
- transform: 'translate(-50%, -100%)',
- }}
+ <svg
+ class="fixed inset-0 z-[9999] h-screen w-screen overflow-visible pointer-events-none"
+ viewBox={`0 0 ${state.viewport().width} ${state.viewport().height}`}
+ preserveAspectRatio="none"
+ aria-hidden="true"
>
- <div
- class="rounded-md border border-border bg-surface px-2 py-1.5 text-[10px] text-base-content shadow-lg"
+ <foreignObject
+ x={state.position().left}
+ y={state.position().top}
+ width={state.maxWidth()}
+ height={state.viewport().height}
+ overflow="visible"
>
- {props.children}
- </div>
- </div>
+ <div
+ ref={state.setTooltipRef}
+ data-tooltip-portal="true"
+ class="inline-block max-w-full rounded-md border border-border bg-surface px-2 py-1.5 text-[10px] text-base-content shadow-lg"
+ >
+ {props.children}
+ </div>
+ </foreignObject>
+ </svg>
</Portal>
</Show>
);
diff --git a/frontend-modern/src/components/shared/__tests__/HistoryChart.test.tsx b/frontend-modern/src/components/shared/__tests__/HistoryChart.test.tsx
index 100ffb8ce8..6beed26999 100644
--- a/frontend-modern/src/components/shared/__tests__/HistoryChart.test.tsx
+++ b/frontend-modern/src/components/shared/__tests__/HistoryChart.test.tsx
@@ -83,6 +83,7 @@ describe('HistoryChart', () => {
expect(historyChartStateSource).not.toContain('getTrialTryAgainLaterMessage()');
expect(historyChartModelSource).toContain('formatHistoryChartTooltipValue');
+ expect(historyChartModelSource).toContain('getHistoryChartTooltipLayout');
expect(historyChartModelSource).toContain('HISTORY_CHART_RANGES');
expect(historyChartModelSource).toContain('getHistoryChartScale');
expect(historyChartModelSource).toContain('findHistoryChartClosestPoint');
@@ -99,7 +100,11 @@ describe('HistoryChart', () => {
expect(historyChartOverlaySource).not.toContain('setupCanvasDPR');
expect(historyChartTooltipSource).toContain('formatHistoryChartTooltipValue');
+ expect(historyChartTooltipSource).toContain('getHistoryChartTooltipLayout');
+ expect(historyChartTooltipSource).toContain('foreignObject');
expect(historyChartTooltipSource).toContain('new Date(point().timestamp).toLocaleString()');
+ expect(historyChartTooltipSource).not.toContain('<Portal>');
+ expect(historyChartTooltipSource).not.toContain('style={');
expect(historyChartTooltipSource).not.toContain('ChartsAPI.getMetricsHistory');
});
diff --git a/frontend-modern/src/components/shared/__tests__/TagBadges.test.tsx b/frontend-modern/src/components/shared/__tests__/TagBadges.test.tsx
index dd8e0c5eff..8405ee7807 100644
--- a/frontend-modern/src/components/shared/__tests__/TagBadges.test.tsx
+++ b/frontend-modern/src/components/shared/__tests__/TagBadges.test.tsx
@@ -38,7 +38,15 @@ import { TagBadges } from '../TagBadges';
/** Return all tag dot elements (the colored circles) scoped to the test container. */
function getTagDots(container?: HTMLElement) {
- return (container ?? document).querySelectorAll('.rounded-full');
+ return (container ?? document).querySelectorAll('[data-tag-dot="true"]');
+}
+
+function getTagDotFill(dot: Element) {
+ return dot.querySelector('[data-tag-dot-fill="true"]')?.getAttribute('fill');
+}
+
+function getTagDotRing(dot: Element) {
+ return dot.querySelector('[data-tag-dot-ring="true"]');
}
afterEach(() => {
@@ -134,68 +142,69 @@ describe('TagBadges', () => {
darkModeMock.mockReturnValue(false);
render(() => <TagBadges tags={['web']} />);
expect(getTagColorWithSpecialMock).toHaveBeenCalledWith('web', false);
- const dot = getTagDots()[0] as HTMLElement;
- expect(dot.style.backgroundColor).toBe('rgb(200, 200, 200)');
+ const dot = getTagDots()[0];
+ expect(getTagDotFill(dot)).toBe('rgb(200, 200, 200)');
});
it('calls getTagColorWithSpecial with isDark=true when isDarkMode prop is true', () => {
render(() => <TagBadges tags={['web']} isDarkMode={true} />);
expect(getTagColorWithSpecialMock).toHaveBeenCalledWith('web', true);
- const dot = getTagDots()[0] as HTMLElement;
- expect(dot.style.backgroundColor).toBe('rgb(30, 30, 30)');
+ const dot = getTagDots()[0];
+ expect(getTagDotFill(dot)).toBe('rgb(30, 30, 30)');
});
it('uses dark mode signal when isDarkMode prop is not set', () => {
darkModeMock.mockReturnValue(true);
render(() => <TagBadges tags={['db']} />);
expect(getTagColorWithSpecialMock).toHaveBeenCalledWith('db', true);
- const dot = getTagDots()[0] as HTMLElement;
- expect(dot.style.backgroundColor).toBe('rgb(30, 30, 30)');
+ const dot = getTagDots()[0];
+ expect(getTagDotFill(dot)).toBe('rgb(30, 30, 30)');
});
it('isDarkMode=false overrides dark mode signal', () => {
darkModeMock.mockReturnValue(true);
render(() => <TagBadges tags={['web']} isDarkMode={false} />);
expect(getTagColorWithSpecialMock).toHaveBeenCalledWith('web', false);
- const dot = getTagDots()[0] as HTMLElement;
- expect(dot.style.backgroundColor).toBe('rgb(200, 200, 200)');
+ const dot = getTagDots()[0];
+ expect(getTagDotFill(dot)).toBe('rgb(200, 200, 200)');
});
});
describe('activeSearch highlighting', () => {
- it('applies box-shadow when tag is in activeSearch', () => {
+ it('marks the tag as active when it is in activeSearch', () => {
render(() => <TagBadges tags={['web']} activeSearch="tags:web" />);
const dot = getTagDots()[0] as HTMLElement;
- expect(dot.style.boxShadow).not.toBe('none');
+ expect(dot.dataset.active).toBe('true');
+ expect(getTagDotRing(dot)).toBeInTheDocument();
});
- it('does not apply box-shadow when tag is not in activeSearch', () => {
+ it('keeps the tag inactive when it is not in activeSearch', () => {
render(() => <TagBadges tags={['web']} activeSearch="tags:db" />);
const dot = getTagDots()[0] as HTMLElement;
- expect(dot.style.boxShadow).toBe('none');
+ expect(dot.dataset.active).toBe('false');
});
- it('uses light-mode shadow color when not dark', () => {
+ it('uses light-mode ring color when not dark', () => {
darkModeMock.mockReturnValue(false);
render(() => <TagBadges tags={['web']} activeSearch="tags:web" />);
const dot = getTagDots()[0] as HTMLElement;
- expect(dot.style.boxShadow).toContain('rgba(0, 0, 0');
+ expect(dot.getAttribute('class')).toContain('text-black/80');
});
- it('uses dark-mode shadow color when dark', () => {
+ it('uses dark-mode ring color when dark', () => {
darkModeMock.mockReturnValue(true);
render(() => <TagBadges tags={['web']} activeSearch="tags:web" />);
const dot = getTagDots()[0] as HTMLElement;
- expect(dot.style.boxShadow).toContain('rgba(255, 255, 255');
+ expect(dot.getAttribute('class')).toContain('text-white/90');
});
it('substring match: "web" highlights when activeSearch contains "tags:webapp" (documents current behavior)', () => {
// Documents that the component uses .includes() for matching, which
// means substring matches occur. If exact-token matching is added later,
- // update this test to expect boxShadow === 'none'.
+ // update this test to expect data-active === 'false'.
render(() => <TagBadges tags={['web']} activeSearch="tags:webapp" />);
const dot = getTagDots()[0] as HTMLElement;
- expect(dot.style.boxShadow).not.toBe('none');
+ expect(dot.dataset.active).toBe('true');
});
});
diff --git a/frontend-modern/src/components/shared/__tests__/Tooltip.test.tsx b/frontend-modern/src/components/shared/__tests__/Tooltip.test.tsx
index fefd020ecb..0072a76450 100644
--- a/frontend-modern/src/components/shared/__tests__/Tooltip.test.tsx
+++ b/frontend-modern/src/components/shared/__tests__/Tooltip.test.tsx
@@ -7,6 +7,7 @@ import {
hideTooltip,
} from '@/components/shared/Tooltip';
import tooltipSource from '@/components/shared/Tooltip.tsx?raw';
+import tooltipPortalSource from '@/components/shared/TooltipPortal.tsx?raw';
import tooltipModelSource from '@/components/shared/tooltipModel.ts?raw';
import tooltipStateSource from '@/components/shared/useTooltipState.ts?raw';
@@ -19,12 +20,21 @@ describe('Tooltip', () => {
it('keeps tooltip on shell, runtime, and model owners', () => {
expect(tooltipSource).toContain('useTooltipState');
expect(tooltipSource).toContain('createTooltipSystemState');
+ expect(tooltipSource).toContain('foreignObject');
expect(tooltipSource).not.toContain('createSignal');
expect(tooltipSource).not.toContain('requestAnimationFrame');
expect(tooltipSource).not.toContain('sanitizeTooltipContent');
expect(tooltipSource).not.toContain('resolveTooltipPosition');
+ expect(tooltipSource).not.toContain('style={');
+
+ expect(tooltipPortalSource).toContain('useTooltipPortalState');
+ expect(tooltipPortalSource).toContain('foreignObject');
+ expect(tooltipPortalSource).not.toContain('createSignal');
+ expect(tooltipPortalSource).not.toContain('resolveTooltipPosition');
+ expect(tooltipPortalSource).not.toContain('style={');
expect(tooltipStateSource).toContain('export function useTooltipState');
+ expect(tooltipStateSource).toContain('export function useTooltipPortalState');
expect(tooltipStateSource).toContain('export function createTooltipSystemState');
expect(tooltipStateSource).toContain('createSignal');
expect(tooltipStateSource).toContain('requestAnimationFrame');
@@ -41,7 +51,7 @@ describe('Tooltip', () => {
it('sanitizes tooltip content through the model owner', async () => {
render(() => <Tooltip content={`<b>"unsafe"</b> & 'quoted'`} x={24} y={24} visible />);
- const tooltip = document.body.querySelector('div[style*="opacity: 1"]') as HTMLDivElement | null;
+ const tooltip = document.body.querySelector('[data-tooltip="true"]') as HTMLDivElement | null;
expect(tooltip).not.toBeNull();
if (!tooltip) return;
@@ -75,13 +85,15 @@ describe('Tooltip', () => {
render(() => <Tooltip content="CPU" x={2} y={2} visible />);
- const tooltip = document.body.querySelector('div[style*="opacity: 1"]') as HTMLDivElement | null;
+ const tooltip = document.body.querySelector('[data-tooltip="true"]') as HTMLDivElement | null;
expect(tooltip).not.toBeNull();
if (!tooltip) return;
await waitFor(() => {
- expect(Number.parseFloat(tooltip.style.left)).toBeGreaterThanOrEqual(4);
- expect(Number.parseFloat(tooltip.style.top)).toBeGreaterThanOrEqual(4);
+ const portal = tooltip.closest('foreignObject');
+ expect(portal).not.toBeNull();
+ expect(Number.parseFloat(portal?.getAttribute('x') ?? '0')).toBeGreaterThanOrEqual(4);
+ expect(Number.parseFloat(portal?.getAttribute('y') ?? '0')).toBeGreaterThanOrEqual(4);
});
expect(raf).toHaveBeenCalled();
diff --git a/frontend-modern/src/components/shared/__tests__/TooltipPortal.test.tsx b/frontend-modern/src/components/shared/__tests__/TooltipPortal.test.tsx
new file mode 100644
index 0000000000..cf61bbc6b2
--- /dev/null
+++ b/frontend-modern/src/components/shared/__tests__/TooltipPortal.test.tsx
@@ -0,0 +1,99 @@
+import { createSignal } from 'solid-js';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { cleanup, render, waitFor } from '@solidjs/testing-library';
+import { TooltipPortal } from '@/components/shared/TooltipPortal';
+import tooltipPortalSource from '@/components/shared/TooltipPortal.tsx?raw';
+import tooltipStateSource from '@/components/shared/useTooltipState.ts?raw';
+
+describe('TooltipPortal', () => {
+ afterEach(() => {
+ cleanup();
+ vi.restoreAllMocks();
+ });
+
+ it('keeps tooltip portal on shell and runtime owners', () => {
+ expect(tooltipPortalSource).toContain('useTooltipPortalState');
+ expect(tooltipPortalSource).toContain('foreignObject');
+ expect(tooltipPortalSource).not.toContain('createSignal');
+ expect(tooltipPortalSource).not.toContain('resolveTooltipPosition');
+ expect(tooltipPortalSource).not.toContain('style={');
+
+ expect(tooltipStateSource).toContain('export function useTooltipPortalState');
+ expect(tooltipStateSource).toContain('resolveTooltipPosition');
+ });
+
+ it('renders portal content without inline styles', async () => {
+ const raf = vi
+ .spyOn(window, 'requestAnimationFrame')
+ .mockImplementation((callback: FrameRequestCallback) => {
+ callback(0);
+ return 1;
+ });
+
+ render(() => (
+ <TooltipPortal when x={120} y={80}>
+ <span>Memory Composition</span>
+ </TooltipPortal>
+ ));
+
+ await waitFor(() => {
+ expect(document.body.querySelector('[data-tooltip-portal="true"]')).not.toBeNull();
+ });
+
+ const tooltip = document.body.querySelector('[data-tooltip-portal="true"]') as HTMLElement | null;
+ const portal = tooltip?.closest('foreignObject');
+ expect(tooltip?.textContent).toContain('Memory Composition');
+ expect(Number.parseFloat(portal?.getAttribute('x') ?? '0')).toBeGreaterThan(0);
+ expect(Number.parseFloat(portal?.getAttribute('y') ?? '0')).toBeGreaterThan(0);
+ expect(document.body.querySelector('[style]')).toBeNull();
+ expect(raf).toHaveBeenCalled();
+ });
+
+ it('repositions when a hidden portal becomes visible with live coordinates', async () => {
+ const raf = vi
+ .spyOn(window, 'requestAnimationFrame')
+ .mockImplementation((callback: FrameRequestCallback) => {
+ callback(0);
+ return 1;
+ });
+
+ const getBoundingClientRect = vi
+ .spyOn(HTMLDivElement.prototype, 'getBoundingClientRect')
+ .mockReturnValue({
+ width: 180,
+ height: 40,
+ left: 0,
+ top: 0,
+ right: 180,
+ bottom: 40,
+ x: 0,
+ y: 0,
+ toJSON: () => ({}),
+ } as DOMRect);
+
+ const [when, setWhen] = createSignal(false);
+ const [x, setX] = createSignal(0);
+ const [y, setY] = createSignal(0);
+
+ render(() => (
+ <TooltipPortal when={when()} x={x()} y={y()}>
+ <span>Memory Composition</span>
+ </TooltipPortal>
+ ));
+
+ setX(240);
+ setY(180);
+ setWhen(true);
+
+ await waitFor(() => {
+ const tooltip = document.body.querySelector('[data-tooltip-portal="true"]');
+ const portal = tooltip?.closest('foreignObject');
+ expect(tooltip).not.toBeNull();
+ expect(Number.parseFloat(portal?.getAttribute('x') ?? '0')).toBeGreaterThan(0);
+ expect(Number.parseFloat(portal?.getAttribute('y') ?? '0')).toBeGreaterThan(0);
+ });
+
+ expect(raf).toHaveBeenCalled();
+ expect(getBoundingClientRect).toHaveBeenCalled();
+ });
+});
diff --git a/frontend-modern/src/components/shared/historyChartModel.ts b/frontend-modern/src/components/shared/historyChartModel.ts
index c877826fee..612df8b020 100644
--- a/frontend-modern/src/components/shared/historyChartModel.ts
+++ b/frontend-modern/src/components/shared/historyChartModel.ts
@@ -28,6 +28,13 @@ export interface HistoryChartHoverPoint {
y: number;
}
+export interface HistoryChartTooltipLayout {
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+}
+
export const HISTORY_CHART_RANGES: HistoryTimeRange[] = ['24h', '7d', '30d', '90d'];
export function formatHistoryChartTooltipValue(value: number, unit?: string): string {
@@ -172,3 +179,27 @@ export function findHistoryChartClosestPoint(
}
return closest;
}
+
+export function getHistoryChartTooltipLayout({
+ hoveredPoint,
+ chartWidth,
+ chartHeight,
+}: {
+ hoveredPoint: HistoryChartHoverPoint;
+ chartWidth: number;
+ chartHeight: number;
+}): HistoryChartTooltipLayout {
+ const width = 156;
+ const height = 46;
+ const margin = 8;
+ const minX = margin;
+ const maxX = Math.max(minX, chartWidth - width - margin);
+ const x = Math.min(Math.max(hoveredPoint.x - width / 2, minX), maxX);
+ const showBelow = hoveredPoint.y < height + margin;
+ const minY = margin;
+ const maxY = Math.max(minY, chartHeight - height - margin);
+ const y = showBelow
+ ? Math.min(Math.max(hoveredPoint.y + 12, minY), maxY)
+ : Math.min(Math.max(hoveredPoint.y - height - 12, minY), maxY);
+ return { x, y, width, height };
+}
diff --git a/frontend-modern/src/components/shared/useHistoryChartState.ts b/frontend-modern/src/components/shared/useHistoryChartState.ts
index f4bacc295d..115f7b3624 100644
--- a/frontend-modern/src/components/shared/useHistoryChartState.ts
+++ b/frontend-modern/src/components/shared/useHistoryChartState.ts
@@ -52,6 +52,8 @@ export function useHistoryChartState(props: HistoryChartProps, refs: HistoryChar
const [cursorX, setCursorX] = createSignal<number | null>(null);
const [startingTrial, setStartingTrial] = createSignal(false);
const [hoveredPoint, setHoveredPoint] = createSignal<HistoryChartHoverPoint | null>(null);
+ const [chartWidth, setChartWidth] = createSignal(300);
+ const chartHeight = createMemo(() => props.height || 200);
const canStartTrial = createMemo(() => canStartCommercialTrial());
@@ -200,7 +202,7 @@ export function useHistoryChartState(props: HistoryChartProps, refs: HistoryChar
const points = data();
const width = canvas.parentElement?.clientWidth || 300;
- const height = props.height || 200;
+ const height = chartHeight();
setupCanvasDPR(canvas, ctx, width, height);
ctx.clearRect(0, 0, width, height);
@@ -322,6 +324,7 @@ export function useHistoryChartState(props: HistoryChartProps, refs: HistoryChar
const updateMaxPoints = () => {
const width = container.clientWidth || 0;
if (width <= 0) return;
+ setChartWidth(width);
const next = calculateOptimalPoints(width, 'history');
if (next !== maxPoints()) {
setMaxPoints(next);
@@ -347,7 +350,7 @@ export function useHistoryChartState(props: HistoryChartProps, refs: HistoryChar
const width = rect.width;
const geometry = createHistoryChartGeometry({
width,
- height: props.height || 200,
+ height: chartHeight(),
startTime: points[0].timestamp,
endTime: points[points.length - 1].timestamp,
minValue: getHistoryChartScale(points, props.unit).minValue,
@@ -364,12 +367,14 @@ export function useHistoryChartState(props: HistoryChartProps, refs: HistoryChar
const ratio = (x - 40) / (width - 40);
const hoverTimestamp = points[0].timestamp + ratio * geometry.timeSpan;
const closest = findHistoryChartClosestPoint(points, hoverTimestamp);
+ const pointX = geometry.getX(closest.timestamp);
+ const pointY = geometry.getY(closest.value);
setHoveredPoint({
value: closest.value,
timestamp: closest.timestamp,
- x: rect.left + x,
- y: rect.top + 20,
+ x: pointX,
+ y: pointY,
});
};
@@ -388,6 +393,8 @@ export function useHistoryChartState(props: HistoryChartProps, refs: HistoryChar
handleMouseLeave,
handleMouseMove,
handleStartTrial,
+ chartHeight,
+ chartWidth,
hoveredPoint,
isLocked,
loading,
diff --git a/frontend-modern/src/components/shared/useTooltipState.ts b/frontend-modern/src/components/shared/useTooltipState.ts
index 6e921ecc72..eef693adac 100644
--- a/frontend-modern/src/components/shared/useTooltipState.ts
+++ b/frontend-modern/src/components/shared/useTooltipState.ts
@@ -1,10 +1,11 @@
-import { createEffect, createSignal } from 'solid-js';
+import { createEffect, createSignal, onCleanup } from 'solid-js';
import type { Accessor } from 'solid-js';
import {
resolveTooltipPosition,
sanitizeTooltipContent,
type TooltipOptions,
type TooltipPosition,
+ type TooltipViewportRect,
} from './tooltipModel';
interface TooltipStateOptions extends TooltipOptions {
@@ -14,6 +15,22 @@ interface TooltipStateOptions extends TooltipOptions {
y: number;
}
+interface TooltipPortalStateOptions extends TooltipOptions {
+ maxWidth?: number;
+ when: boolean;
+ x: number;
+ y: number;
+}
+
+interface TooltipLayoutStateOptions {
+ align: Accessor<TooltipOptions['align'] | undefined>;
+ direction: Accessor<TooltipOptions['direction'] | undefined>;
+ maxWidth: Accessor<number | undefined>;
+ visible: Accessor<boolean>;
+ x: Accessor<number>;
+ y: Accessor<number>;
+}
+
interface TooltipInstance {
hide: () => void;
show: (content: string, x: number, y: number, options?: TooltipOptions) => void;
@@ -21,47 +38,115 @@ interface TooltipInstance {
let tooltipInstance: TooltipInstance | null = null;
-export function useTooltipState(options: TooltipStateOptions): {
+function useTooltipLayoutState(options: TooltipLayoutStateOptions): {
+ maxWidth: Accessor<number>;
position: Accessor<TooltipPosition>;
- sanitizedContent: Accessor<string>;
setTooltipRef: (el: HTMLDivElement) => void;
+ viewport: Accessor<TooltipViewportRect>;
} {
let tooltipRef: HTMLDivElement | undefined;
const [position, setPosition] = createSignal<TooltipPosition>({ left: 0, top: 0 });
+ const [viewport, setViewport] = createSignal<TooltipViewportRect>({
+ height: typeof window === 'undefined' ? 0 : window.innerHeight,
+ width: typeof window === 'undefined' ? 0 : window.innerWidth,
+ });
+
+ const updateViewport = () => {
+ if (typeof window === 'undefined') return;
+ setViewport({ height: window.innerHeight, width: window.innerWidth });
+ };
+
+ const updatePosition = () => {
+ if (typeof window === 'undefined' || !tooltipRef || !options.visible()) return;
+ updateViewport();
+ const rect = tooltipRef.getBoundingClientRect();
+ setPosition(
+ resolveTooltipPosition({
+ align: options.align(),
+ direction: options.direction(),
+ rect,
+ viewportHeight: window.innerHeight,
+ viewportWidth: window.innerWidth,
+ x: options.x(),
+ y: options.y(),
+ }),
+ );
+ };
createEffect(() => {
- if (!options.visible) {
- setPosition({ left: options.x, top: options.y });
+ if (typeof window === 'undefined') return;
+ updateViewport();
+ const handleResize = () => updateViewport();
+ window.addEventListener('resize', handleResize);
+ onCleanup(() => window.removeEventListener('resize', handleResize));
+ });
+
+ createEffect(() => {
+ if (!options.visible()) {
+ setPosition({ left: options.x(), top: options.y() });
return;
}
- requestAnimationFrame(() => {
- if (!tooltipRef) return;
-
- const rect = tooltipRef.getBoundingClientRect();
- setPosition(
- resolveTooltipPosition({
- align: options.align,
- direction: options.direction,
- rect,
- viewportHeight: window.innerHeight,
- viewportWidth: window.innerWidth,
- x: options.x,
- y: options.y,
- }),
- );
- });
+ requestAnimationFrame(updatePosition);
});
return {
+ maxWidth: () =>
+ Math.max(48, Math.min(options.maxWidth() ?? 240, Math.max(48, viewport().width - 8))),
position,
- sanitizedContent: () => sanitizeTooltipContent(options.content),
setTooltipRef: (el) => {
tooltipRef = el;
+ if (options.visible()) {
+ requestAnimationFrame(updatePosition);
+ }
},
+ viewport,
};
}
+export function useTooltipState(options: TooltipStateOptions): {
+ maxWidth: Accessor<number>;
+ position: Accessor<TooltipPosition>;
+ sanitizedContent: Accessor<string>;
+ setTooltipRef: (el: HTMLDivElement) => void;
+ viewport: Accessor<TooltipViewportRect>;
+} {
+ const layout = useTooltipLayoutState({
+ align: () => options.align,
+ direction: () => options.direction,
+ maxWidth: () => options.maxWidth,
+ visible: () => options.visible,
+ x: () => options.x,
+ y: () => options.y,
+ });
+
+ return {
+ maxWidth: layout.maxWidth,
+ position: layout.position,
+ sanitizedContent: () => sanitizeTooltipContent(options.content),
+ setTooltipRef: layout.setTooltipRef,
+ viewport: layout.viewport,
+ };
+}
+
+export function useTooltipPortalState(
+ options: TooltipPortalStateOptions,
+): {
+ maxWidth: Accessor<number>;
+ position: Accessor<TooltipPosition>;
+ setTooltipRef: (el: HTMLDivElement) => void;
+ viewport: Accessor<TooltipViewportRect>;
+} {
+ return useTooltipLayoutState({
+ align: () => options.align,
+ direction: () => options.direction,
+ maxWidth: () => options.maxWidth ?? 320,
+ visible: () => options.when,
+ x: () => options.x,
+ y: () => options.y,
+ });
+}
+
export function createTooltipSystemState() {
const [visible, setVisible] = createSignal(false);
const [content, setContent] = createSignal('');
diff --git a/frontend-modern/src/hooks/__tests__/useTooltip.test.ts b/frontend-modern/src/hooks/__tests__/useTooltip.test.ts
new file mode 100644
index 0000000000..adfefb136b
--- /dev/null
+++ b/frontend-modern/src/hooks/__tests__/useTooltip.test.ts
@@ -0,0 +1,39 @@
+import { createRoot } from 'solid-js';
+import { describe, expect, it } from 'vitest';
+import { useTooltip } from '@/hooks/useTooltip';
+
+describe('useTooltip', () => {
+ it('prefers pointer coordinates when they are available', () => {
+ createRoot((dispose) => {
+ const tip = useTooltip();
+ tip.onMouseEnter({
+ clientX: 120,
+ clientY: 80,
+ currentTarget: {
+ getBoundingClientRect: () => ({ left: 10, top: 20, width: 40, height: 16 }),
+ },
+ } as unknown as MouseEvent);
+
+ expect(tip.pos()).toEqual({ x: 120, y: 80 });
+ expect(tip.show()).toBe(true);
+ dispose();
+ });
+ });
+
+ it('falls back to trigger geometry when pointer coordinates are missing', () => {
+ createRoot((dispose) => {
+ const tip = useTooltip();
+ tip.onMouseEnter({
+ clientX: Number.NaN,
+ clientY: Number.NaN,
+ currentTarget: {
+ getBoundingClientRect: () => ({ left: 10, top: 20, width: 40, height: 16 }),
+ },
+ } as unknown as MouseEvent);
+
+ expect(tip.pos()).toEqual({ x: 30, y: 20 });
+ expect(tip.show()).toBe(true);
+ dispose();
+ });
+ });
+});
diff --git a/frontend-modern/src/hooks/useTooltip.ts b/frontend-modern/src/hooks/useTooltip.ts
index 37b6928e35..ced9656390 100644
--- a/frontend-modern/src/hooks/useTooltip.ts
+++ b/frontend-modern/src/hooks/useTooltip.ts
@@ -25,7 +25,11 @@ export function useTooltip() {
const onMouseEnter = (e: MouseEvent) => {
const rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
- setPos({ x: rect.left + rect.width / 2, y: rect.top });
+ const hasPointerPosition = Number.isFinite(e.clientX) && Number.isFinite(e.clientY);
+ setPos({
+ x: hasPointerPosition ? e.clientX : rect.left + rect.width / 2,
+ y: hasPointerPosition ? e.clientY : rect.top,
+ });
setShow(true);
};