OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
'use client';
import { localPoint } from '@visx/event';
import { sankey, sankeyCenter, sankeyLinkHorizontal } from '@visx/sankey';
import type { Transition } from 'motion/react';
import {
memo,
type ReactNode,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { ParentSize } from '../chart-parent-size';
import {
type Margin,
type SankeyLinkDatum,
type SankeyNodeDatum,
SankeyProvider,
type SankeyTooltipData,
} from './sankey-context';
import { cn } from '@/lib/utils';
export interface SankeyData {
nodes: SankeyNodeDatum[];
links: SankeyLinkDatum[];
}
export interface SankeyChartProps {
/** Sankey data with nodes and links */
data: SankeyData;
/** Chart margins */
margin?: Partial<Margin>;
/** Animation duration in milliseconds. Default: 1100 */
animationDuration?: number;
/** Motion enter transition (spring or cubic-bezier tween). */
enterTransition?: Transition;
/** Signature of motion URL state — triggers enter replay when it changes. */
revealSignature?: string;
/** Aspect ratio as "width / height". Default: "2 / 1" */
aspectRatio?: string;
/** Node width in pixels. Default: 16 */
nodeWidth?: number;
/** Node padding in pixels. Default: 24 */
nodePadding?: number;
/** Additional class name for the container */
className?: string;
/** Child components (SankeyNode, SankeyLink, SankeyTooltip) */
children: ReactNode;
/** Controlled hovered node index (e.g. from ChartLegend). */
hoveredNodeIndex?: number | null;
/** Called when node hover changes from the chart surface. */
onNodeHoverChange?: (index: number | null) => void;
}
const DEFAULT_MARGIN: Margin = { top: 40, right: 180, bottom: 40, left: 180 };
interface SankeyChartInnerProps {
data: SankeyData;
width: number;
height: number;
margin: Margin;
animationDuration: number;
enterTransition?: Transition;
revealSignature?: string;
nodeWidth: number;
nodePadding: number;
children: ReactNode;
hoveredNodeIndexProp?: number | null;
onNodeHoverChange?: (index: number | null) => void;
}
function SankeyChartInner(props: SankeyChartInnerProps) {
const { width, height } = props;
if (width < 10 || height < 10) {
return null;
}
return <SankeyChartCore {...props} />;
}
const SankeyChartCore = memo(function SankeyChartCore({
data,
width,
height,
margin,
animationDuration,
enterTransition,
revealSignature = '',
nodeWidth,
nodePadding,
children,
hoveredNodeIndexProp,
onNodeHoverChange,
}: SankeyChartInnerProps) {
const containerRef = useRef<HTMLDivElement>(null);
const [isLoaded, setIsLoaded] = useState(false);
const [revealEpoch, setRevealEpoch] = useState(0);
const [internalHoveredNodeIndex, setInternalHoveredNodeIndex] = useState<
number | null
>(null);
const isNodeHoverControlled = hoveredNodeIndexProp !== undefined;
const hoveredNodeIndex = isNodeHoverControlled
? hoveredNodeIndexProp
: internalHoveredNodeIndex;
const setHoveredNodeIndex = useCallback(
(index: number | null) => {
if (isNodeHoverControlled) {
onNodeHoverChange?.(index);
} else {
setInternalHoveredNodeIndex(index);
}
},
[isNodeHoverControlled, onNodeHoverChange]
);
const [hoveredLinkIndex, setHoveredLinkIndex] = useState<number | null>(null);
const [tooltipData, setTooltipData] = useState<SankeyTooltipData | null>(
null
);
const [mousePos, setMousePos] = useState<{ x: number; y: number } | null>(
null
);
const innerWidth = width - margin.left - margin.right;
const innerHeight = height - margin.top - margin.bottom;
// biome-ignore lint/correctness/useExhaustiveDependencies: revealSignature
useEffect(() => {
setRevealEpoch((n) => n + 1);
setIsLoaded(false);
const timeout = setTimeout(() => {
setIsLoaded(true);
}, animationDuration);
return () => clearTimeout(timeout);
}, [animationDuration, revealSignature]);
const sankeyGenerator = useMemo(() => {
return sankey<SankeyNodeDatum, SankeyLinkDatum>()
.nodeWidth(nodeWidth)
.nodePadding(nodePadding)
.nodeAlign(sankeyCenter)
.extent([
[0, 0],
[innerWidth, innerHeight],
]);
}, [innerWidth, innerHeight, nodeWidth, nodePadding]);
const graph = useMemo(() => {
const clonedData = {
nodes: data.nodes.map((node) => ({ ...node })),
links: data.links.map((link) => ({ ...link })),
};
return sankeyGenerator(clonedData);
}, [data, sankeyGenerator]);
const createPath = useCallback(
// biome-ignore lint/suspicious/noExplicitAny: d3-sankey types are complex
(link: any) => {
try {
const pathGenerator = sankeyLinkHorizontal();
return pathGenerator(link) || '';
} catch {
return '';
}
},
[]
);
const handleMouseMove = useCallback((event: React.MouseEvent) => {
const point = localPoint(event);
if (point) {
setMousePos({ x: point.x, y: point.y });
}
}, []);
const handleMouseLeave = useCallback(() => {
setHoveredNodeIndex(null);
setHoveredLinkIndex(null);
setTooltipData(null);
setMousePos(null);
}, [setHoveredNodeIndex]);
const contextValue = {
graph,
nodes: graph.nodes,
links: graph.links,
width,
height,
innerWidth,
innerHeight,
margin,
hoveredNodeIndex,
hoveredLinkIndex,
setHoveredNodeIndex,
setHoveredLinkIndex,
tooltipData,
setTooltipData,
containerRef,
isLoaded,
animationDuration,
enterTransition,
revealEpoch,
mousePos,
createPath,
};
return (
<SankeyProvider value={contextValue}>
<div className="relative h-full w-full" ref={containerRef}>
<svg
aria-hidden="true"
className="overflow-visible"
height={height}
onMouseLeave={handleMouseLeave}
onMouseMove={handleMouseMove}
width={width}
>
<g transform={`translate(${margin.left},${margin.top})`}>
{children}
</g>
</svg>
</div>
</SankeyProvider>
);
});
export function SankeyChart({
data,
margin: marginProp,
animationDuration = 1100,
enterTransition,
revealSignature,
aspectRatio = '2 / 1',
nodeWidth = 16,
nodePadding = 24,
className = '',
children,
hoveredNodeIndex,
onNodeHoverChange,
}: SankeyChartProps) {
const margin = { ...DEFAULT_MARGIN, ...marginProp };
return (
<div className={cn('relative w-full', className)} style={{ aspectRatio }}>
<ParentSize>
{({ width, height }) => (
<SankeyChartInner
animationDuration={animationDuration}
data={data}
enterTransition={enterTransition}
height={height}
hoveredNodeIndexProp={hoveredNodeIndex}
margin={margin}
nodePadding={nodePadding}
nodeWidth={nodeWidth}
onNodeHoverChange={onNodeHoverChange}
revealSignature={revealSignature}
width={width}
>
{children}
</SankeyChartInner>
)}
</ParentSize>
</div>
);
}
SankeyChart.displayName = 'SankeyChart';
export default SankeyChart;