@@ -3,8 +3,8 @@ import { type JSX, mergeProps, Show } from "solid-js";
33
44import { useInternalSolidFlow } from "@/components/contexts" ;
55
6- import DotPattern from "./DotPattern" ;
7- import LinePattern from "./LinePattern" ;
6+ import { DotPattern } from "./DotPattern" ;
7+ import { LinePattern } from "./LinePattern" ;
88import type { BackgroundVariant } from "./types" ;
99
1010const DEFAULT_SIZE : Record < BackgroundVariant , number > = {
@@ -14,64 +14,58 @@ const DEFAULT_SIZE: Record<BackgroundVariant, number> = {
1414} ;
1515
1616type BackgroundProps = {
17- readonly id : string ;
17+ readonly id ? : string ;
1818 /** Variant of the pattern
1919 * @example 'lines', 'dots', 'cross'
2020 */
21- readonly variant : BackgroundVariant ;
21+ readonly variant ? : BackgroundVariant ;
2222 /** Color of the background */
23- readonly bgColor : string ;
23+ readonly bgColor ? : string ;
2424 /** Color of the pattern */
25- readonly patternColor : string ;
25+ readonly patternColor ? : string ;
2626 /** Class applied to the pattern */
27- readonly patternClass : string ;
27+ readonly patternClass ? : string ;
2828 /** Class applied to the container */
29- readonly class : string ;
29+ readonly class ? : string ;
3030 /** Gap between repetitions of the pattern */
31- readonly gap : number | [ number , number ] ;
31+ readonly gap ? : number | [ number , number ] ;
3232 /** Size of a single pattern element */
33- readonly size : number ;
33+ readonly size ? : number ;
3434 /** Line width of the Line pattern */
35- readonly lineWidth : number ;
35+ readonly lineWidth ? : number ;
3636 /** Style applied to the container */
37- readonly style : JSX . CSSProperties ;
37+ readonly style ? : JSX . CSSProperties ;
3838} ;
3939
40- const Background = ( props : Partial < BackgroundProps > ) => {
40+ export const Background = ( props : BackgroundProps ) => {
4141 const _props = mergeProps (
4242 {
43- id : undefined ,
4443 variant : "dots" as BackgroundVariant ,
4544 gap : 20 ,
4645 size : 1 ,
4746 lineWidth : 1 ,
48- bgColor : undefined ,
49- patternColor : undefined ,
50- patternClass : undefined ,
51- class : "" ,
5247 style : { } as JSX . CSSProperties ,
5348 } ,
5449 props ,
5550 ) ;
5651
5752 const { store } = useInternalSolidFlow ( ) ;
5853
59- const patternSize = ( ) => _props . size || DEFAULT_SIZE [ _props . variant ] ;
6054 const isDots = ( ) => _props . variant === "dots" ;
6155 const isCross = ( ) => _props . variant === "cross" ;
56+
57+ const patternId = ( ) => `background-pattern-${ store . id } -${ _props . id ?? "" } ` ;
58+ const scaledSize = ( ) => ( _props . size ?? DEFAULT_SIZE [ _props . variant ] ) * store . viewport . zoom ;
59+
6260 const gapXY = ( ) =>
6361 Array . isArray ( _props . gap ) ? _props . gap : ( [ _props . gap , _props . gap ] as [ number , number ] ) ;
6462
65- const patternId = ( ) => `background-pattern-${ store . id } -${ _props . id ? _props . id : "" } ` ;
66-
6763 const scaledGap = ( ) =>
6864 [ gapXY ( ) [ 0 ] * store . viewport . zoom || 1 , gapXY ( ) [ 1 ] * store . viewport . zoom || 1 ] as [
6965 number ,
7066 number ,
7167 ] ;
7268
73- const scaledSize = ( ) => patternSize ( ) * store . viewport . zoom ;
74-
7569 const patternDimensions = ( ) =>
7670 isCross ( ) ? ( [ scaledSize ( ) , scaledSize ( ) ] as [ number , number ] ) : scaledGap ( ) ;
7771
@@ -104,15 +98,14 @@ const Background = (props: Partial<BackgroundProps>) => {
10498 fallback = { < DotPattern radius = { scaledSize ( ) / 2 } class = { _props . patternClass } /> }
10599 >
106100 < LinePattern
101+ class = { _props . patternClass }
107102 dimensions = { patternDimensions ( ) }
103+ variant = { _props . variant }
108104 lineWidth = { _props . lineWidth }
109- class = { _props . patternClass }
110105 />
111106 </ Show >
112107 </ pattern >
113108 < rect x = "0" y = "0" width = "100%" height = "100%" fill = { `url(#${ patternId ( ) } )` } />
114109 </ svg >
115110 ) ;
116111} ;
117-
118- export default Background ;
0 commit comments