import type { CSSProperties } from 'react'; import { useState } from 'react'; import type { AnnotationLineProps } from '../components/Annotations/Line.js'; import { Line } from '../components/Annotations/Line.js'; import type { AnnotationTextProps } from '../components/Annotations/Text.js'; import { Text } from '../components/Annotations/Text.js'; import { usePlotEvents } from '../contexts/plotController/plotControllerContext.js'; import type { ControllerHookOptions, DualAxisOptions } from './types.js'; export interface UseCrossHairOptions extends ControllerHookOptions, DualAxisOptions { /** * Cross hair line color. * @defaultValue "black" * */ color?: CSSProperties['stroke']; /** * Additional style to apply to the crosshair's lines. * */ lineStyle?: CSSProperties; /** * Cross hair text annotation style. * */ textStyle?: CSSProperties; /** * Transformation to apply to the crosshair's text. * * */ textTransform?: string; } type Positions = Record; export function useCrossHair(options: UseCrossHairOptions = {}) { const [hover, setHover] = useState(null); const { color = 'black', horizontalAxisId = 'x', verticalAxisId = 'y', lineStyle, textStyle, textTransform = '', } = options; usePlotEvents( { onPointerMove({ coordinates }) { setHover(coordinates); }, onPointerLeave() { setHover(null); }, }, options, ); const lineProps: Partial = { color, strokeWidth: '2', style: lineStyle, }; const textProps: Partial = { transform: `translate(-3 -2) ${textTransform}`, style: textStyle, }; if (!hover) return { annotations: null }; const annotations = ( <> {hover[horizontalAxisId]?.toFixed(2)} , {hover[verticalAxisId]?.toFixed(2)} ); return { annotations }; }