import { useToken } from '@chakra-ui/react'; import * as d3 from 'd3'; import React from 'react'; interface Props extends Omit, 'scale'> { type: 'vertical' | 'horizontal'; scale: d3.ScaleTime | d3.ScaleLinear; disableAnimation?: boolean; size: number; ticks: number; } const ChartGridLine = ({ type, scale, ticks, size, disableAnimation, ...props }: Props) => { const ref = React.useRef(null); const strokeColor = useToken('colors', 'divider'); React.useEffect(() => { if (!ref.current) { return; } const axisGenerator = type === 'vertical' ? d3.axisBottom : d3.axisLeft; const axis = axisGenerator(scale).ticks(ticks).tickSize(-size); const gridGroup = d3.select(ref.current); if (disableAnimation) { gridGroup.call(axis); } else { gridGroup.transition().duration(750).ease(d3.easeLinear).call(axis); } gridGroup.select('.domain').remove(); gridGroup.selectAll('text').remove(); gridGroup.selectAll('line').attr('stroke', strokeColor); }, [ scale, ticks, size, disableAnimation, type, strokeColor ]); return ; }; export default React.memo(ChartGridLine);