Reactflow – 背景显示在节点之上

huangapple go评论102阅读模式
英文:

Reactflow - Background displaying over nodes

问题

使用reactflow,在一个简单的图形上,我尝试如下使用<Background/> Reactflow组件:

import React, { useCallback, useState } from 'react';
import ReactFlow, {
    addEdge,
    FitViewOptions,
    applyNodeChanges,
    applyEdgeChanges,
    Node,
    Edge,
    NodeChange,
    EdgeChange,
    Connection,
    Background,
    BackgroundVariant,
} from 'reactflow';

interface CreateGraphProps {

}

const initialNodes: Node[] = [
    { id: '1', data: { label: 'Node 1' }, position: { x: 5, y: 5 } },
    { id: '2', data: { label: 'Node 2' }, position: { x: 5, y: 100 } },
];

const initialEdges: Edge[] = [{ id: 'e1-2', source: '1', target: '2' }];

const fitViewOptions: FitViewOptions = {
    padding: 0.2,
};

export const CreateGraph: React.FC<CreateGraphProps> = ({ }) => {
    const [nodes, setNodes] = useState<Node[]>(initialNodes);
    const [edges, setEdges] = useState<Edge[]>(initialEdges);

    const onNodesChange = useCallback(
        (changes: NodeChange[]) => setNodes((nds) => applyNodeChanges(changes, nds)),
        [setNodes]
    );
    const onEdgesChange = useCallback(
        (changes: EdgeChange[]) => setEdges((eds) => applyEdgeChanges(changes, eds)),
        [setEdges]
    );
    const onConnect = useCallback(
        (connection: Connection) => setEdges((eds) => addEdge(connection, eds)),
        [setEdges]
    );

    return (
        <div>
            <ReactFlow
                nodes={nodes}
                edges={edges}
                onNodesChange={onNodesChange}
                onEdgesChange={onEdgesChange}
                onConnect={onConnect}
                attributionPosition='bottom-right'
                fitView
                fitViewOptions={fitViewOptions}>
                    <Background color='#a6a6a6' variant='dots' />
            </ReactFlow>
        </div>
    );
}

没有背景时,OnConnectonEdgesChangesonNodesChange函数能够正常工作。然而,有了背景后情况就不同了。在Background组件存在时,节点无法移动。

经过进一步调查,似乎background组件位于reactflow画布的上方。是否有什么遗漏的内容,阻止它正常运作?

英文:

Using reactflow, on a simple graph I am trying to use the &lt;Background/&gt; Reactflow component as follows :

import React, { useCallback, useState } from &#39;react&#39;;
import ReactFlow, {
addEdge,
FitViewOptions,
applyNodeChanges,
applyEdgeChanges,
Node,
Edge,
NodeChange,
EdgeChange,
Connection,
Background,
BackgroundVariant,
} from &#39;reactflow&#39;;
interface CreateGraphProps {
}
const initialNodes: Node[] = [
{ id: &#39;1&#39;, data: { label: &#39;Node 1&#39; }, position: { x: 5, y: 5 } },
{ id: &#39;2&#39;, data: { label: &#39;Node 2&#39; }, position: { x: 5, y: 100 } },
];
const initialEdges: Edge[] = [{ id: &#39;e1-2&#39;, source: &#39;1&#39;, target: &#39;2&#39; }];
const fitViewOptions: FitViewOptions = {
padding: 0.2,
};
export const CreateGraph: React.FC&lt;CreateGraphProps&gt; = ({ }) =&gt; {
const [nodes, setNodes] = useState&lt;Node[]&gt;(initialNodes);
const [edges, setEdges] = useState&lt;Edge[]&gt;(initialEdges);
const onNodesChange = useCallback(
(changes: NodeChange[]) =&gt; setNodes((nds) =&gt; applyNodeChanges(changes, nds)),
[setNodes]
);
const onEdgesChange = useCallback(
(changes: EdgeChange[]) =&gt; setEdges((eds) =&gt; applyEdgeChanges(changes, eds)),
[setEdges]
);
const onConnect = useCallback(
(connection: Connection) =&gt; setEdges((eds) =&gt; addEdge(connection, eds)),
[setEdges]
);
return (
&lt;div&gt;
&lt;ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
attributionPosition=&#39;bottom-right&#39;
fitView
fitViewOptions={fitViewOptions}&gt;
&lt;Background color=&#39;#a6a6a6&#39; variant={&#39;dots&#39; as BackgroundVariant}/&gt;
&lt;/ReactFlow&gt;
&lt;/div&gt;
);
}

Without the background, the OnConnect, onEdgesChanges and onNodesChange functions work well. However, with the background it is not the case. The nodes can't be moved when the Background component is present.

Upon further investigation it seems that the background component is placed above the reactflow canvas. Is there somthing missing, prohibiting it from functionning properly?

答案1

得分: 2

我已经找到一个对我有效的答案,要使主题工作,你需要导入:

import 'reactflow/dist/style.css';

这可以用作启动的基础主题。文档在这里

英文:

I have found an answer that works for me, to get theming to work you need to import:

import &#39;reactflow/dist/style.css&#39;;

This can be used as a base theme to get started. Docs are here.

huangapple
  • 本文由 发表于 2023年2月24日 06:02:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定