import { memo, useRef } from 'react'; import cc from 'classcat'; import { useStore, ReactFlowState } from '@reactflow/core'; import shallow from 'zustand/shallow'; import { BackgroundProps, BackgroundVariant } from './types'; import { DotPattern, LinePattern } from './Patterns'; const defaultColor = { [BackgroundVariant.Dots]: '#91919a', [BackgroundVariant.Lines]: '#eee', [BackgroundVariant.Cross]: '#e2e2e2', }; const defaultSize = { [BackgroundVariant.Dots]: 1, [BackgroundVariant.Lines]: 1, [BackgroundVariant.Cross]: 6, }; const selector = (s: ReactFlowState) => ({ transform: s.transform, patternId: `pattern-${s.rfId}` }); function Background({ variant = BackgroundVariant.Dots, gap = 20, // only used for dots and cross size, // only used for lines and cross lineWidth = 1, color, style, className, }: BackgroundProps) { const ref = useRef(null); const { transform, patternId } = useStore(selector, shallow); const patternColor = color || defaultColor[variant]; const patternSize = size || defaultSize[variant]; const isDots = variant === BackgroundVariant.Dots; const isCross = variant === BackgroundVariant.Cross; const gapXY: [number, number] = Array.isArray(gap) ? gap : [gap, gap]; const scaledGap: [number, number] = [gapXY[0] * transform[2] || 1, gapXY[1] * transform[2] || 1]; const scaledSize = patternSize * transform[2]; const patternDimensions: [number, number] = isCross ? [scaledSize, scaledSize] : scaledGap; const patternOffset = isDots ? [scaledSize / 2, scaledSize / 2] : [patternDimensions[0] / 2, patternDimensions[1] / 2]; return ( {isDots ? ( ) : ( )} ); } Background.displayName = 'Background'; export default memo(Background);