Skip to content
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

Feature/iframe tooltip render #1848

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 13 additions & 4 deletions packages/visx-event/src/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@ export function isMouseEvent(event?: EventType): event is MouseEvent {
return !!event && 'clientX' in event;
}

// functional definition of event
export function isEvent(event?: EventType | Element): event is EventType {
function isNativeEvent(event: any): event is Event {
return event && typeof event === 'object' && 'target' in event && 'currentTarget' in event;
}

function isReactSyntheticEvent(event: any): event is React.SyntheticEvent {
return (
!!event &&
(event instanceof Event || ('nativeEvent' in event && event.nativeEvent instanceof Event))
event &&
typeof event === 'object' &&
'nativeEvent' in event &&
isNativeEvent((event as React.SyntheticEvent).nativeEvent)
);
}

export function isEvent(event?: EventType | Element): event is EventType {
return !!event && (isNativeEvent(event) || isReactSyntheticEvent(event));
}
56 changes: 51 additions & 5 deletions packages/visx-xychart/src/components/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ function defaultRenderGlyph<Datum extends object>(props: RenderTooltipGlyphProps
return <DefaultGlyph {...props} />;
}

function getIframeOffsets(target) {
let offsetX = 0;
let offsetY = 0;
let currentWindow = target.ownerDocument.defaultView;

while (currentWindow && currentWindow.frameElement) {
const frameElement = currentWindow.frameElement;
const frameRect = frameElement.getBoundingClientRect();
offsetX += frameRect.left;
offsetY += frameRect.top;
currentWindow = currentWindow.parent;
}

return { x: offsetX, y: offsetY };
}

function TooltipInner<Datum extends object>({
debounce,
detectBounds,
Expand Down Expand Up @@ -132,15 +148,46 @@ function TooltipInner<Datum extends object>({
zIndex,
});

function getIframeOffsets(target) {
let offsetX = 0;
let offsetY = 0;
let currentWindow = target.ownerDocument.defaultView;

while (currentWindow && currentWindow.frameElement) {
const frameElement = currentWindow.frameElement;
const frameRect = frameElement.getBoundingClientRect();
offsetX += frameRect.left;
offsetY += frameRect.top;
currentWindow = currentWindow.parent;
}

return { x: offsetX, y: offsetY };
}

// To correctly position itself in a Portal, the tooltip must know its container bounds
// this is done by rendering an invisible node whose ref can be used to find its parentElement
const setContainerRef = useCallback(
(ownRef: HTMLElement | SVGElement | null) => {
containerRef(ownRef?.parentElement ?? null);
if (ownRef && ownRef.parentElement instanceof Element) {
const iframeOffsets = getIframeOffsets(ownRef);
const parentRect = ownRef.parentElement.getBoundingClientRect();
const adjustedContainer = {
...ownRef.parentElement,
getBoundingClientRect: () => ({
...parentRect,
x: parentRect.x + iframeOffsets.x,
y: parentRect.y + iframeOffsets.y,
left: parentRect.left + iframeOffsets.x,
top: parentRect.top + iframeOffsets.y,
}),
};
containerRef(adjustedContainer);
} else {
containerRef(null);
}
},
[containerRef],
);

const tooltipContent = tooltipContext?.tooltipOpen
? renderTooltip({ ...tooltipContext, colorScale })
: null;
Expand Down Expand Up @@ -301,10 +348,9 @@ function TooltipInner<Datum extends object>({
</svg>
</TooltipInPortal>
)}
{glyphProps.map(({ x, y, ...props }, i) => (
// We render glyps in a portal so that they can overflow the container if necessary
{glyphProps.map(({ key, x, y, ...props }) => (
<TooltipInPortal
key={i}
key={key}
className="visx-tooltip-glyph"
left={x}
top={y}
Expand Down
Loading