Hooks

ChartKit exports several utility hooks for building responsive and animated charts.

useAutoTheme

Automatically switches chart themes based on your app's dark/light mode. Works with next-themes, Tailwind dark mode, or system preferences.

tsx
import { LineChart, useAutoTheme } from '@derpdaderp/chartkit';

function Chart({ data, series }) {
  // Automatically switches between themes based on dark mode
  const theme = useAutoTheme({ light: 'sunset', dark: 'neon' });
  
  return <LineChart data={data} series={series} theme={theme} />;
}

// Or use system preference instead of DOM class
const theme = useAutoTheme({ 
  light: 'sunset', 
  dark: 'midnight',
  useSystemPreference: true 
});

Options

PropTypeDefaultDescription
light*ThemeName-Theme for light mode
dark*ThemeName-Theme for dark mode
selectorstring"html"DOM selector to observe for class changes
darkClassstring"dark"Class that indicates dark mode
useSystemPreferencebooleanfalseUse system preference instead of DOM class

useContainerWidth

A hook for making charts responsive by tracking their container width.

tsx
import { LineChart, useContainerWidth } from '@derpdaderp/chartkit';

function ResponsiveChart({ data, series }) {
  const { ref, width } = useContainerWidth<HTMLDivElement>();
  
  return (
    <div ref={ref} style={{ width: '100%' }}>
      {width > 0 && (
        <LineChart
          data={data}
          series={series}
          width={width}
          theme="midnight"
        />
      )}
    </div>
  );
}

useResizeObserver

A more complete hook that tracks both width and height of an element.

tsx
import { useResizeObserver } from '@derpdaderp/chartkit';

function AdaptiveChart() {
  const { ref, size, ready } = useResizeObserver<HTMLDivElement>();
  
  return (
    <div ref={ref} style={{ width: '100%', height: '400px' }}>
      {ready && (
        <MyChart width={size.width} height={size.height} />
      )}
    </div>
  );
}

Return Value

PropTypeDefaultDescription
refRefObject<T>-Ref to attach to the container element
size{ width: number; height: number }-Current size of the element
readyboolean-Whether the element has been measured

useAnimatedMount

A hook for animating chart elements on mount with various animation styles.

tsx
import { useAnimatedMount, estimatePathLength } from '@derpdaderp/chartkit';

function AnimatedPath({ pathD }) {
  const { getPathStyle, fadeStyle } = useAnimatedMount({
    duration: 800,
    delay: 200,
  });
  
  const length = estimatePathLength(pathD);
  
  return (
    <svg style={fadeStyle}>
      <path
        d={pathD}
        style={getPathStyle(length)}
        fill="none"
        stroke="currentColor"
      />
    </svg>
  );
}

Options

PropTypeDefaultDescription
durationnumber600Animation duration in ms
delaynumber0Animation delay in ms
easingstring"ease-out"CSS easing function

Return Value

PropTypeDefaultDescription
isAnimatedboolean-Whether animation has completed
progressnumber-Progress from 0 to 1
fadeStyleCSSProperties-Styles for fade-in animation
scaleStyleCSSProperties-Styles for scale animation
getPathStyle(pathLength: number) => CSSProperties-Returns styles for path draw animation

useUniqueId

Generates unique IDs for SVG elements like filters and gradients.

tsx
import { useUniqueId } from '@derpdaderp/chartkit';

function ChartWithGlow() {
  const glowId = useUniqueId('glow');
  
  return (
    <svg>
      <defs>
        <filter id={glowId}>
          <feGaussianBlur stdDeviation="2" />
        </filter>
      </defs>
      <path filter={`url(#${glowId})`} />
    </svg>
  );
}

useMouseTracking

Tracks mouse position over an SVG chart for tooltips and crosshairs.

tsx
import { useMouseTracking } from '@derpdaderp/chartkit';

function InteractiveChart() {
  const { svgRef, mouse, handleMouseMove, handleMouseLeave } = useMouseTracking({
    marginLeft: 60,
    chartWidth: 500,
  });
  
  return (
    <svg
      ref={svgRef}
      onMouseMove={handleMouseMove}
      onMouseLeave={handleMouseLeave}
    >
      {mouse.x !== null && (
        <line x1={mouse.x} y1={0} x2={mouse.x} y2={300} />
      )}
    </svg>
  );
}