Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(elements): render only visible elements
- Loading branch information
Showing
21 changed files
with
376 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React, { memo, FC, CSSProperties } from 'react'; | ||
|
||
import { Handle, Position, NodeProps, Connection, Edge } from 'react-flow-renderer'; | ||
|
||
const targetHandleStyle: CSSProperties = { background: '#555' }; | ||
const sourceHandleStyleA: CSSProperties = { ...targetHandleStyle, top: 10 }; | ||
const sourceHandleStyleB: CSSProperties = { ...targetHandleStyle, bottom: 10, top: 'auto' }; | ||
|
||
const onConnect = (params: Connection | Edge) => console.log('handle onConnect', params); | ||
|
||
const ColorSelectorNode: FC<NodeProps> = ({ data, isConnectable }) => { | ||
return ( | ||
<> | ||
<Handle type="target" position={Position.Left} style={targetHandleStyle} onConnect={onConnect} /> | ||
<div> | ||
Custom Color Picker Node: <strong>{data.color}</strong> | ||
</div> | ||
<input className="nodrag" type="color" onChange={data.onChange} defaultValue={data.color} /> | ||
<Handle type="source" position={Position.Right} id="a" style={sourceHandleStyleA} isConnectable={isConnectable} /> | ||
<Handle type="source" position={Position.Right} id="b" style={sourceHandleStyleB} isConnectable={isConnectable} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default memo(ColorSelectorNode); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { useState, useEffect, MouseEvent, useCallback } from 'react'; | ||
import { ChangeEvent } from 'react'; | ||
|
||
import ReactFlow, { | ||
addEdge, | ||
MiniMap, | ||
Controls, | ||
Node, | ||
OnLoadParams, | ||
Position, | ||
SnapGrid, | ||
Connection, | ||
Edge, | ||
NodeChange, | ||
applyNodeChanges, | ||
applyEdgeChanges, | ||
EdgeChange, | ||
} from 'react-flow-renderer'; | ||
|
||
import ColorSelectorNode from './ColorSelectorNode'; | ||
|
||
const onLoad = (reactFlowInstance: OnLoadParams) => console.log('flow loaded:', reactFlowInstance); | ||
const onNodeDragStop = (_: MouseEvent, node: Node) => console.log('drag stop', node); | ||
const onNodeClick = (_: MouseEvent, node: Node) => console.log('click', node); | ||
|
||
const initBgColor = '#1A192B'; | ||
|
||
const connectionLineStyle = { stroke: '#fff' }; | ||
const snapGrid: SnapGrid = [16, 16]; | ||
const nodeTypes = { | ||
selectorNode: ColorSelectorNode, | ||
}; | ||
|
||
const CustomNodeFlow = () => { | ||
const [nodes, setNodes] = useState<Node[]>([]); | ||
const [edges, setEdges] = useState<Edge[]>([]); | ||
const [bgColor, setBgColor] = useState<string>(initBgColor); | ||
|
||
useEffect(() => { | ||
const onChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
setNodes((nds) => | ||
nds.map((node) => { | ||
if (node.id !== '2') { | ||
return node; | ||
} | ||
|
||
const color = event.target.value; | ||
|
||
setBgColor(color); | ||
|
||
return { | ||
...node, | ||
data: { | ||
...node.data, | ||
color, | ||
}, | ||
}; | ||
}) | ||
); | ||
}; | ||
|
||
setNodes([ | ||
{ | ||
id: '1', | ||
type: 'input', | ||
data: { label: 'An input node' }, | ||
position: { x: 0, y: 50 }, | ||
sourcePosition: Position.Right, | ||
}, | ||
{ | ||
id: '2', | ||
type: 'selectorNode', | ||
data: { onChange: onChange, color: initBgColor }, | ||
style: { border: '1px solid #777', padding: 10 }, | ||
position: { x: 250, y: 50 }, | ||
}, | ||
{ | ||
id: '3', | ||
type: 'output', | ||
data: { label: 'Output A' }, | ||
position: { x: 550, y: 25 }, | ||
targetPosition: Position.Left, | ||
}, | ||
{ | ||
id: '4', | ||
type: 'output', | ||
data: { label: 'Output B' }, | ||
position: { x: 550, y: 100 }, | ||
targetPosition: Position.Left, | ||
}, | ||
]); | ||
|
||
setEdges([ | ||
{ id: 'e1-2', source: '1', target: '2', animated: true, style: { stroke: '#fff' } }, | ||
{ id: 'e2a-3', source: '2', sourceHandle: 'a', target: '3', animated: true, style: { stroke: '#fff' } }, | ||
{ id: 'e2b-4', source: '2', sourceHandle: 'b', target: '4', animated: true, style: { stroke: '#fff' } }, | ||
]); | ||
}, []); | ||
|
||
const onConnect = (params: Connection | Edge) => | ||
setEdges((eds) => addEdge({ ...params, animated: true, style: { stroke: '#fff' } }, eds)); | ||
|
||
const onNodesChange = useCallback((changes: NodeChange[]) => { | ||
setNodes((ns) => applyNodeChanges(changes, ns)); | ||
}, []); | ||
|
||
const onEdgesChange = useCallback((changes: EdgeChange[]) => { | ||
setEdges((es) => applyEdgeChanges(changes, es)); | ||
}, []); | ||
|
||
return ( | ||
<ReactFlow | ||
nodes={nodes} | ||
edges={edges} | ||
onNodesChange={onNodesChange} | ||
onEdgesChange={onEdgesChange} | ||
onNodeClick={onNodeClick} | ||
onConnect={onConnect} | ||
onNodeDragStop={onNodeDragStop} | ||
style={{ background: bgColor }} | ||
onLoad={onLoad} | ||
nodeTypes={nodeTypes} | ||
connectionLineStyle={connectionLineStyle} | ||
snapToGrid={true} | ||
snapGrid={snapGrid} | ||
defaultZoom={1.5} | ||
> | ||
<MiniMap | ||
nodeStrokeColor={(n: Node): string => { | ||
if (n.type === 'input') return '#0041d0'; | ||
if (n.type === 'selectorNode') return bgColor; | ||
if (n.type === 'output') return '#ff0072'; | ||
|
||
return '#eee'; | ||
}} | ||
nodeColor={(n: Node): string => { | ||
if (n.type === 'selectorNode') return bgColor; | ||
|
||
return '#fff'; | ||
}} | ||
/> | ||
<Controls /> | ||
</ReactFlow> | ||
); | ||
}; | ||
|
||
export default CustomNodeFlow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.