@@ -3,8 +3,8 @@ import { type JSX, mergeProps, Show } from "solid-js";
3
3
4
4
import { useInternalSolidFlow } from "@/components/contexts" ;
5
5
6
- import DotPattern from "./DotPattern" ;
7
- import LinePattern from "./LinePattern" ;
6
+ import { DotPattern } from "./DotPattern" ;
7
+ import { LinePattern } from "./LinePattern" ;
8
8
import type { BackgroundVariant } from "./types" ;
9
9
10
10
const DEFAULT_SIZE : Record < BackgroundVariant , number > = {
@@ -14,64 +14,58 @@ const DEFAULT_SIZE: Record<BackgroundVariant, number> = {
14
14
} ;
15
15
16
16
type BackgroundProps = {
17
- readonly id : string ;
17
+ readonly id ? : string ;
18
18
/** Variant of the pattern
19
19
* @example 'lines', 'dots', 'cross'
20
20
*/
21
- readonly variant : BackgroundVariant ;
21
+ readonly variant ? : BackgroundVariant ;
22
22
/** Color of the background */
23
- readonly bgColor : string ;
23
+ readonly bgColor ? : string ;
24
24
/** Color of the pattern */
25
- readonly patternColor : string ;
25
+ readonly patternColor ? : string ;
26
26
/** Class applied to the pattern */
27
- readonly patternClass : string ;
27
+ readonly patternClass ? : string ;
28
28
/** Class applied to the container */
29
- readonly class : string ;
29
+ readonly class ? : string ;
30
30
/** Gap between repetitions of the pattern */
31
- readonly gap : number | [ number , number ] ;
31
+ readonly gap ? : number | [ number , number ] ;
32
32
/** Size of a single pattern element */
33
- readonly size : number ;
33
+ readonly size ? : number ;
34
34
/** Line width of the Line pattern */
35
- readonly lineWidth : number ;
35
+ readonly lineWidth ? : number ;
36
36
/** Style applied to the container */
37
- readonly style : JSX . CSSProperties ;
37
+ readonly style ? : JSX . CSSProperties ;
38
38
} ;
39
39
40
- const Background = ( props : Partial < BackgroundProps > ) => {
40
+ export const Background = ( props : BackgroundProps ) => {
41
41
const _props = mergeProps (
42
42
{
43
- id : undefined ,
44
43
variant : "dots" as BackgroundVariant ,
45
44
gap : 20 ,
46
45
size : 1 ,
47
46
lineWidth : 1 ,
48
- bgColor : undefined ,
49
- patternColor : undefined ,
50
- patternClass : undefined ,
51
- class : "" ,
52
47
style : { } as JSX . CSSProperties ,
53
48
} ,
54
49
props ,
55
50
) ;
56
51
57
52
const { store } = useInternalSolidFlow ( ) ;
58
53
59
- const patternSize = ( ) => _props . size || DEFAULT_SIZE [ _props . variant ] ;
60
54
const isDots = ( ) => _props . variant === "dots" ;
61
55
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
+
62
60
const gapXY = ( ) =>
63
61
Array . isArray ( _props . gap ) ? _props . gap : ( [ _props . gap , _props . gap ] as [ number , number ] ) ;
64
62
65
- const patternId = ( ) => `background-pattern-${ store . id } -${ _props . id ? _props . id : "" } ` ;
66
-
67
63
const scaledGap = ( ) =>
68
64
[ gapXY ( ) [ 0 ] * store . viewport . zoom || 1 , gapXY ( ) [ 1 ] * store . viewport . zoom || 1 ] as [
69
65
number ,
70
66
number ,
71
67
] ;
72
68
73
- const scaledSize = ( ) => patternSize ( ) * store . viewport . zoom ;
74
-
75
69
const patternDimensions = ( ) =>
76
70
isCross ( ) ? ( [ scaledSize ( ) , scaledSize ( ) ] as [ number , number ] ) : scaledGap ( ) ;
77
71
@@ -104,15 +98,14 @@ const Background = (props: Partial<BackgroundProps>) => {
104
98
fallback = { < DotPattern radius = { scaledSize ( ) / 2 } class = { _props . patternClass } /> }
105
99
>
106
100
< LinePattern
101
+ class = { _props . patternClass }
107
102
dimensions = { patternDimensions ( ) }
103
+ variant = { _props . variant }
108
104
lineWidth = { _props . lineWidth }
109
- class = { _props . patternClass }
110
105
/>
111
106
</ Show >
112
107
</ pattern >
113
108
< rect x = "0" y = "0" width = "100%" height = "100%" fill = { `url(#${ patternId ( ) } )` } />
114
109
</ svg >
115
110
) ;
116
111
} ;
117
-
118
- export default Background ;
0 commit comments