Merge pull request #2943 from wbkd/feat/multiple-backgrounds
Feat/multiple backgrounds
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'@reactflow/background': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
add id and offset prop
|
||||||
@@ -19,13 +19,15 @@ const initialNodes: Node[] = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const Flow: FC<{ id: string; bgProps: BackgroundProps }> = ({ id, bgProps }) => {
|
const Flow: FC<{ id: string; bgProps: BackgroundProps[] }> = ({ id, bgProps }) => {
|
||||||
const [nodes, , onNodesChange] = useNodesState(initialNodes);
|
const [nodes, , onNodesChange] = useNodesState(initialNodes);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ReactFlowProvider>
|
<ReactFlowProvider>
|
||||||
<ReactFlow nodes={nodes} onNodesChange={onNodesChange} id={id}>
|
<ReactFlow nodes={nodes} onNodesChange={onNodesChange} id={id}>
|
||||||
<Background {...bgProps} />
|
{bgProps.map((props, idx) => (
|
||||||
|
<Background key={idx} id={idx.toString()} {...props} />
|
||||||
|
))}
|
||||||
</ReactFlow>
|
</ReactFlow>
|
||||||
</ReactFlowProvider>
|
</ReactFlowProvider>
|
||||||
);
|
);
|
||||||
@@ -33,9 +35,16 @@ const Flow: FC<{ id: string; bgProps: BackgroundProps }> = ({ id, bgProps }) =>
|
|||||||
|
|
||||||
const Backgrounds: FC = () => (
|
const Backgrounds: FC = () => (
|
||||||
<div className={styles.wrapper}>
|
<div className={styles.wrapper}>
|
||||||
<Flow id="flow-a" bgProps={{ variant: BackgroundVariant.Dots }} />
|
<Flow id="flow-a" bgProps={[{ variant: BackgroundVariant.Dots }]} />
|
||||||
<Flow id="flow-b" bgProps={{ variant: BackgroundVariant.Lines, gap: [50, 50] }} />
|
<Flow id="flow-b" bgProps={[{ variant: BackgroundVariant.Lines, gap: [50, 50] }]} />
|
||||||
<Flow id="flow-c" bgProps={{ variant: BackgroundVariant.Cross, gap: [100, 50] }} />
|
<Flow id="flow-c" bgProps={[{ variant: BackgroundVariant.Cross, gap: [100, 50] }]} />
|
||||||
|
<Flow
|
||||||
|
id="flow-d"
|
||||||
|
bgProps={[
|
||||||
|
{ variant: BackgroundVariant.Lines, gap: 10 },
|
||||||
|
{ variant: BackgroundVariant.Lines, gap: 100, offset: 2, color: '#ccc' },
|
||||||
|
]}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -21,12 +21,14 @@ const defaultSize = {
|
|||||||
const selector = (s: ReactFlowState) => ({ transform: s.transform, patternId: `pattern-${s.rfId}` });
|
const selector = (s: ReactFlowState) => ({ transform: s.transform, patternId: `pattern-${s.rfId}` });
|
||||||
|
|
||||||
function Background({
|
function Background({
|
||||||
|
id,
|
||||||
variant = BackgroundVariant.Dots,
|
variant = BackgroundVariant.Dots,
|
||||||
gap = 20,
|
|
||||||
// only used for dots and cross
|
// only used for dots and cross
|
||||||
size,
|
gap = 20,
|
||||||
// only used for lines and cross
|
// only used for lines and cross
|
||||||
|
size,
|
||||||
lineWidth = 1,
|
lineWidth = 1,
|
||||||
|
offset = 2,
|
||||||
color,
|
color,
|
||||||
style,
|
style,
|
||||||
className,
|
className,
|
||||||
@@ -44,8 +46,8 @@ function Background({
|
|||||||
const patternDimensions: [number, number] = isCross ? [scaledSize, scaledSize] : scaledGap;
|
const patternDimensions: [number, number] = isCross ? [scaledSize, scaledSize] : scaledGap;
|
||||||
|
|
||||||
const patternOffset = isDots
|
const patternOffset = isDots
|
||||||
? [scaledSize / 2, scaledSize / 2]
|
? [scaledSize / offset, scaledSize / offset]
|
||||||
: [patternDimensions[0] / 2, patternDimensions[1] / 2];
|
: [patternDimensions[0] / offset, patternDimensions[1] / offset];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<svg
|
<svg
|
||||||
@@ -62,7 +64,7 @@ function Background({
|
|||||||
data-testid="rf__background"
|
data-testid="rf__background"
|
||||||
>
|
>
|
||||||
<pattern
|
<pattern
|
||||||
id={patternId}
|
id={patternId+id}
|
||||||
x={transform[0] % scaledGap[0]}
|
x={transform[0] % scaledGap[0]}
|
||||||
y={transform[1] % scaledGap[1]}
|
y={transform[1] % scaledGap[1]}
|
||||||
width={scaledGap[0]}
|
width={scaledGap[0]}
|
||||||
@@ -71,12 +73,12 @@ function Background({
|
|||||||
patternTransform={`translate(-${patternOffset[0]},-${patternOffset[1]})`}
|
patternTransform={`translate(-${patternOffset[0]},-${patternOffset[1]})`}
|
||||||
>
|
>
|
||||||
{isDots ? (
|
{isDots ? (
|
||||||
<DotPattern color={patternColor} radius={scaledSize / 2} />
|
<DotPattern color={patternColor} radius={scaledSize / offset} />
|
||||||
) : (
|
) : (
|
||||||
<LinePattern dimensions={patternDimensions} color={patternColor} lineWidth={lineWidth} />
|
<LinePattern dimensions={patternDimensions} color={patternColor} lineWidth={lineWidth} />
|
||||||
)}
|
)}
|
||||||
</pattern>
|
</pattern>
|
||||||
<rect x="0" y="0" width="100%" height="100%" fill={`url(#${patternId})`} />
|
<rect x="0" y="0" width="100%" height="100%" fill={`url(#${patternId+id})`} />
|
||||||
</svg>
|
</svg>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,12 @@ export enum BackgroundVariant {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type BackgroundProps = {
|
export type BackgroundProps = {
|
||||||
|
id?: string
|
||||||
color?: string;
|
color?: string;
|
||||||
className?: string;
|
className?: string;
|
||||||
gap?: number | [number, number];
|
gap?: number | [number, number];
|
||||||
size?: number;
|
size?: number;
|
||||||
|
offset?: number;
|
||||||
lineWidth?: number;
|
lineWidth?: number;
|
||||||
variant?: BackgroundVariant;
|
variant?: BackgroundVariant;
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
|
|||||||
Reference in New Issue
Block a user