Skip to content

Commit a215265

Browse files
committed
refactor: update background component
1 parent 9fb5c82 commit a215265

File tree

4 files changed

+38
-46
lines changed

4 files changed

+38
-46
lines changed

src/components/graph/plugins/background/Background.tsx

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { type JSX, mergeProps, Show } from "solid-js";
33

44
import { 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";
88
import type { BackgroundVariant } from "./types";
99

1010
const DEFAULT_SIZE: Record<BackgroundVariant, number> = {
@@ -14,64 +14,58 @@ const DEFAULT_SIZE: Record<BackgroundVariant, number> = {
1414
};
1515

1616
type 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;

src/components/graph/plugins/background/DotPattern.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ import clsx from "clsx";
22
import { mergeProps } from "solid-js";
33

44
type DotPatternProps = {
5-
readonly radius: number;
6-
readonly class: string;
5+
readonly class?: string;
6+
readonly radius?: number;
77
};
88

9-
const DotPattern = (props: Partial<DotPatternProps>) => {
10-
const merged = mergeProps({ radius: 5 }, props);
9+
export const DotPattern = (props: DotPatternProps) => {
10+
const _props = mergeProps({ radius: 5 }, props);
1111

1212
return (
1313
<circle
14-
class={clsx("solid-flow__background-pattern", "dots", merged.class)}
15-
cx={merged.radius}
16-
cy={merged.radius}
17-
r={merged.radius}
14+
class={clsx("solid-flow__background-pattern", "dots", _props.class)}
15+
cx={_props.radius}
16+
cy={_props.radius}
17+
r={_props.radius}
1818
/>
1919
);
2020
};
21-
22-
export default DotPattern;

src/components/graph/plugins/background/LinePattern.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import { clsx } from "clsx";
22
import { mergeProps } from "solid-js";
33

4+
import type { BackgroundVariant } from ".";
5+
46
type LinePatternProps = {
7+
readonly class?: string;
58
readonly dimensions: [number, number];
69
readonly lineWidth?: number;
7-
readonly class?: string;
10+
readonly variant: BackgroundVariant;
811
};
912

10-
const LinePattern = (props: LinePatternProps) => {
11-
const merged = mergeProps({ lineWidth: 1 }, props);
13+
export const LinePattern = (props: LinePatternProps) => {
14+
const _props = mergeProps({ lineWidth: 1 }, props);
1215

1316
return (
1417
<path
15-
class={clsx("solid-flow__background-pattern", "lines", merged.class)}
16-
stroke-width={merged.lineWidth}
17-
d={`M${merged.dimensions[0] / 2} 0 V${merged.dimensions[1]} M0 ${merged.dimensions[1] / 2} H${merged.dimensions[0]}`}
18+
stroke-width={_props.lineWidth}
19+
d={`M${_props.dimensions[0] / 2} 0 V${_props.dimensions[1]} M0 ${_props.dimensions[1] / 2} H${_props.dimensions[0]}`}
20+
class={clsx("solid-flow__background-pattern", _props.variant, _props.class)}
1821
/>
1922
);
2023
};
21-
22-
export default LinePattern;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { default as Background } from "./Background";
2-
export type { BackgroundVariant as BackgroundVariant } from "./types";
1+
export { Background } from "./Background";
2+
export type { BackgroundVariant } from "./types";

0 commit comments

Comments
 (0)