import { JSX, Show, createEffect, mergeProps, splitProps } from 'solid-js'; import cc from 'classcat'; import { useStore } from '../../hooks/useStore'; import { DotPattern, LinePattern } from './Patterns'; import { containerStyle } from '../../styles/utils'; import { type BackgroundProps, BackgroundVariant } from './types'; import { type SolidFlowState } from '../../types'; import { useRef } from '../../utils/hooks'; const defaultSize = { [BackgroundVariant.Dots]: 1, [BackgroundVariant.Lines]: 1, [BackgroundVariant.Cross]: 6, }; const selector = (s: SolidFlowState) => ({ transform: s.transform, patternId: `pattern-${s.rfId.get()}` }); function BackgroundComponent(_p: BackgroundProps) { const p = mergeProps({ gap: 20, lineWidth: 1, offset: 2, variant: BackgroundVariant.Dots, }, _p); const ref = useRef(null); const { transform, patternId } = useStore(selector); const patternSize = () => p.size || defaultSize[p.variant]; const isDots = () => p.variant === BackgroundVariant.Dots; const isCross = () => p.variant === BackgroundVariant.Cross; const gapXY = () => { const gap = p.gap; if (Array.isArray(gap)) { return gap; } else { return [gap, gap]; } }; const scaledGap: () => [number, number] = () => [gapXY()[0] * transform.get()[2] || 1, gapXY()[1] * transform.get()[2] || 1]; const scaledSize = () => patternSize() * transform.get()[2]; const patternDimensions: () => [number, number] = () => (isCross() ? [scaledSize(), scaledSize()] : scaledGap()); const patternOffset = () => { if (isDots()) { return [scaledSize() / p.offset, scaledSize() / p.offset]; } else { return [patternDimensions()[0] / p.offset, patternDimensions()[1] / p.offset]; } }; const _patternId = () => `${patternId}${p.id ? p.id : ''}`; const style = (): JSX.CSSProperties => { return { ...p.style, ...containerStyle, '--xy-background-color-props': p.bgColor, '--xy-background-pattern-color-props': p.color, } as JSX.CSSProperties; }; return ( (ref.current = el)} data-testid="rf__background" > } > ); } BackgroundComponent.displayName = 'Background'; export const Background = BackgroundComponent;