import * as React from 'react'; import { BoxProps, Box } from './preshape/box/box.js'; import { useResizeObserver } from './preshape/hooks/use-resize-observer.js'; import { AntwerpData } from '../../src/types/antwerp-data.js'; import { toShapes } from '../../src/to-shapes/to-shapes.js'; import { ColorScale, getColorScale } from './utils/get-color-scale.js'; import { drawShapes } from './antwerp-drawer/antwerp-drawer.js'; interface Props extends BoxProps { readonly colorMethod: 'placement' | 'transform'; readonly colorScale?: ColorScale; readonly configuration: string; readonly repeatCount?: number | undefined; readonly inRadius?: number | undefined; readonly shapeSize: number; readonly showTransforms: boolean; readonly showVertices: boolean; } function Antwerp(props: Props) { const { colorMethod, colorScale, configuration, repeatCount, inRadius, shapeSize, showTransforms, showVertices, ...rest } = props; const [{height,width}, refSize, sizeNode] = useResizeObserver(); const [data, setData] = React.useState(); const svgRef = React.useRef(null); React.useEffect(() => { if (height && width) { setData(toShapes(configuration, repeatCount, shapeSize, inRadius)); } }, [configuration, repeatCount, inRadius, shapeSize, height, width]); React.useEffect( () => { if (!data) { return; } const svg = svgRef.current!; svg.setAttributeNS(null, 'viewBox', `${-width/2} ${-height/2} ${width} ${height}`) drawShapes( svg, data, { colorMethod, colorScale: getColorScale(colorScale) }, showTransforms, showVertices, shapeSize ); }, [colorMethod, colorScale, data, height, width] ); return ( ); } export { Antwerp }