From 6cc29104775cbdb847aa47e30c4144e25d23bd5b Mon Sep 17 00:00:00 2001 From: moklick Date: Fri, 26 Mar 2021 22:43:19 +0100 Subject: [PATCH] fix(background): multiple backgrounds, separate pattern ids closes #1037 --- example/src/MultiFlows/index.tsx | 45 +++++++++++++++++++ example/src/MultiFlows/multiflows.css | 13 ++++++ example/src/index.tsx | 5 +++ .../Background/index.tsx | 20 ++++++--- 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 example/src/MultiFlows/index.tsx create mode 100644 example/src/MultiFlows/multiflows.css diff --git a/example/src/MultiFlows/index.tsx b/example/src/MultiFlows/index.tsx new file mode 100644 index 00000000..8e185992 --- /dev/null +++ b/example/src/MultiFlows/index.tsx @@ -0,0 +1,45 @@ +import React, { useState, FC } from 'react'; + +import ReactFlow, { + removeElements, + addEdge, + Background, + Elements, + Edge, + Connection, + ReactFlowProvider, +} from 'react-flow-renderer'; + +import './multiflows.css'; + +const initialElements: Elements = [ + { id: '1', type: 'input', data: { label: 'Node 1' }, position: { x: 250, y: 5 }, className: 'light' }, + { id: '2', data: { label: 'Node 2' }, position: { x: 100, y: 100 }, className: 'light' }, + { id: '3', data: { label: 'Node 3' }, position: { x: 400, y: 100 }, className: 'light' }, + { id: '4', data: { label: 'Node 4' }, position: { x: 400, y: 200 }, className: 'light' }, + { id: 'e1-2', source: '1', target: '2', animated: true }, + { id: 'e1-3', source: '1', target: '3' }, +]; + +const Flow: FC = () => { + const [elements, setElements] = useState(initialElements); + const onElementsRemove = (elementsToRemove: Elements) => setElements((els) => removeElements(elementsToRemove, els)); + const onConnect = (params: Edge | Connection) => setElements((els) => addEdge(params, els)); + + return ( + + + + + + ); +}; + +const MultiFlows: FC = () => ( +
+ + +
+); + +export default MultiFlows; diff --git a/example/src/MultiFlows/multiflows.css b/example/src/MultiFlows/multiflows.css new file mode 100644 index 00000000..b7017390 --- /dev/null +++ b/example/src/MultiFlows/multiflows.css @@ -0,0 +1,13 @@ +.react-flow__example-multiflows { + display: flex; + height: 100%; +} + +.react-flow__example-multiflows .react-flow { + width: 100%; + height: 100%; +} + +.react-flow__example-multiflows .react-flow:first-child { + border-right: 2px solid #333; +} diff --git a/example/src/index.tsx b/example/src/index.tsx index 70c617ce..5ac43c39 100644 --- a/example/src/index.tsx +++ b/example/src/index.tsx @@ -25,6 +25,7 @@ import SwitchFlows from './Switch'; import UseZoomPanHelper from './UseZoomPanHelper'; import UseUpdateNodeInternals from './UseUpdateNodeInternals'; import Undirectional from './Undirectional'; +import MultiFlows from './MultiFlows'; import './index.css'; @@ -121,6 +122,10 @@ const routes = [ path: '/undirectional', component: Undirectional, }, + { + path: '/multiflows', + component: MultiFlows, + }, ]; const Header = withRouter(({ history, location }) => { diff --git a/src/additional-components/Background/index.tsx b/src/additional-components/Background/index.tsx index 3fdf4e72..9e8661da 100644 --- a/src/additional-components/Background/index.tsx +++ b/src/additional-components/Background/index.tsx @@ -1,4 +1,4 @@ -import React, { memo, HTMLAttributes } from 'react'; +import React, { memo, useMemo, FC, HTMLAttributes } from 'react'; import cc from 'classcat'; import { useStoreState } from '../../store/hooks'; @@ -17,15 +17,17 @@ const defaultColors = { [BackgroundVariant.Lines]: '#eee', }; -const Background = ({ +const Background: FC = ({ variant = BackgroundVariant.Dots, gap = 15, size = 0.5, color, style, className, -}: BackgroundProps) => { +}) => { const [x, y, scale] = useStoreState((s) => s.transform); + // when there are multiple flows on a page we need to make sure that every background gets its own pattern. + const patternId = useMemo(() => `pattern-${Math.floor(Math.random() * 100000)}`, []); const bgClasses = cc(['react-flow__background', className]); const scaledGap = gap * scale; @@ -45,11 +47,17 @@ const Background = ({ height: '100%', }} > - + {path} - - + ); };