Skip to content

Commit

Permalink
feat(stacking): simplify zIndex and treeLevel behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
chrtze committed Nov 4, 2021
1 parent 79e46d9 commit 1b390e1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 27 deletions.
12 changes: 5 additions & 7 deletions example/src/Basic/index.tsx
Expand Up @@ -28,7 +28,7 @@ const initialNodes: Node[] = [
data: { label: 'Node 4' },
position: { x: 100, y: 200 },
className: 'light',
style: { backgroundColor: 'rgba(255, 0, 0, 1)' },
style: { backgroundColor: 'rgba(255, 0, 0, 0.8)' },
width: 600,
height: 300,
},
Expand All @@ -42,12 +42,12 @@ const initialNodes: Node[] = [
{
id: '4b',
data: { label: 'Node 4b' },
position: { x: 80, y: 80 },
position: { x: 150, y: 50 },
className: 'light',
style: { backgroundColor: 'rgba(255, 255, 0, 0.2)' },
style: { backgroundColor: 'rgba(255, 0, 255, 0.8)' },
height: 300,
width: 300,
parentNode: '4',
height: 200,
width: 350,
},
{
id: '4b1',
Expand All @@ -68,10 +68,8 @@ const initialNodes: Node[] = [
const initialEdges: Edge[] = [
{ id: 'e1-2', source: '1', target: '2', animated: true },
{ id: 'e1-3', source: '1', target: '3' },
{ id: 'e2-4a', source: '2', target: '4a', animated: true },
{ id: 'e3-4', source: '3', target: '4' },
{ id: 'e3-4b', source: '3', target: '4b' },
{ id: 'e3-4b2', source: '3', target: '4b2' },
{ id: 'e4a-4b1', source: '4a', target: '4b1' },
{ id: 'e4a-4b2', source: '4a', target: '4b2' },
{ id: 'e4b1-4b2', source: '4b1', target: '4b2' },
Expand Down
20 changes: 9 additions & 11 deletions src/components/ConnectionLine/index.tsx
@@ -1,19 +1,19 @@
import React, { useEffect, useState, CSSProperties } from 'react';

import { useStore } from '../../store';
import { getBezierPath } from '../Edges/BezierEdge';
import { getSmoothStepPath } from '../Edges/SmoothStepEdge';
import {
ElementId,
Node,
NodeLookupItem,
Transform,
HandleElement,
Position,
ConnectionLineType,
ConnectionLineComponent,
HandleType,
ReactFlowState,
Node,
} from '../../types';
import useNodeLookup from '../../hooks/useNodeLookup';
interface ConnectionLineProps {
connectionNodeId: ElementId;
connectionHandleId: ElementId | null;
Expand All @@ -27,8 +27,6 @@ interface ConnectionLineProps {
CustomConnectionLineComponent?: ConnectionLineComponent;
}

const nodesSelector = (s: ReactFlowState) => s.nodes;

export default ({
connectionNodeId,
connectionHandleId,
Expand All @@ -41,13 +39,13 @@ export default ({
isConnectable,
CustomConnectionLineComponent,
}: ConnectionLineProps) => {
const nodes = useStore(nodesSelector);
const [sourceNode, setSourceNode] = useState<Node | null>(null);
const nodeLookup = useNodeLookup();
const [sourceNode, setSourceNode] = useState<NodeLookupItem | null>(null);
const nodeId = connectionNodeId;
const handleId = connectionHandleId;

useEffect(() => {
const nextSourceNode = nodes.find((n) => n.id === nodeId) || null;
const nextSourceNode = nodeLookup.get(nodeId) || null;
setSourceNode(nextSourceNode);
}, []);

Expand All @@ -60,8 +58,8 @@ export default ({
: sourceNode.handleBounds[connectionHandleType]![0];
const sourceHandleX = sourceHandle ? sourceHandle.x + sourceHandle.width / 2 : sourceNode.width! / 2;
const sourceHandleY = sourceHandle ? sourceHandle.y + sourceHandle.height / 2 : sourceNode.height!;
const sourceX = sourceNode.position.x + sourceHandleX;
const sourceY = sourceNode.position.y + sourceHandleY;
const sourceX = sourceNode.position!.x + sourceHandleX;
const sourceY = sourceNode.position!.y + sourceHandleY;

const targetX = (connectionPositionX - transform[0]) / transform[2];
const targetY = (connectionPositionY - transform[1]) / transform[2];
Expand All @@ -81,7 +79,7 @@ export default ({
targetPosition={targetPosition}
connectionLineType={connectionLineType}
connectionLineStyle={connectionLineStyle}
sourceNode={sourceNode}
sourceNode={sourceNode as Node}
sourceHandle={sourceHandle}
/>
</g>
Expand Down
2 changes: 1 addition & 1 deletion src/container/EdgeRenderer/index.tsx
Expand Up @@ -247,7 +247,7 @@ const EdgeRenderer = (props: EdgeRendererProps) => {
return (
<>
{edgeTree.map(({ level, edges, isMaxLevel }) => (
<svg key={level} style={{ zIndex: 6 + level }} width={width} height={height} className="react-flow__edges">
<svg key={level} style={{ zIndex: level }} width={width} height={height} className="react-flow__edges">
{isMaxLevel && <MarkerDefinitions defaultColor={defaultMarkerColor} />}
<g>
{edges.map((edge: Edge) => {
Expand Down
2 changes: 1 addition & 1 deletion src/container/NodeRenderer/index.tsx
Expand Up @@ -116,7 +116,7 @@ function Node({
isConnectable={isConnectable}
resizeObserver={resizeObserver}
dragHandle={node.dragHandle}
zIndex={6 + treeLevel}
zIndex={treeLevel}
isParentNode={isParentNode}
/>
);
Expand Down
12 changes: 7 additions & 5 deletions src/store/index.ts
Expand Up @@ -136,7 +136,7 @@ const createStore = () =>
height: node.height || null,
position: node.position,
positionAbsolute: node.position,
treeLevel: 0,
treeLevel: node.zIndex || 0,
};
if (node.parentNode) {
lookupNode.parentNode = node.parentNode;
Expand All @@ -149,19 +149,21 @@ const createStore = () =>
.forEach((node) => {
const positionAbsoluteAndTreeLevel = getAbsolutePositionAndTreeLevel(node, nodeLookup, {
...node.position,
treeLevel: 0,
treeLevel: node.zIndex || 0,
});

nodeLookup.set(node.parentNode!, { ...nodeLookup.get(node.parentNode!), isParentNode: true });

if (positionAbsoluteAndTreeLevel) {
const { treeLevel, x, y } = positionAbsoluteAndTreeLevel;

nodeLookup.set(node.id, {
...nodeLookup.get(node.id),
positionAbsolute: {
x: positionAbsoluteAndTreeLevel.x,
y: positionAbsoluteAndTreeLevel.y,
x,
y,
},
treeLevel: positionAbsoluteAndTreeLevel.treeLevel,
treeLevel,
});
}
});
Expand Down
3 changes: 1 addition & 2 deletions src/types/index.ts
Expand Up @@ -82,9 +82,8 @@ export interface Node<T = any> {
dragHandle?: string;
width?: number | null;
height?: number | null;
handleBounds?: NodeHandleBounds;
parentNode?: ElementId;
childNodes?: Node[];
zIndex?: number;
}

export enum ArrowHeadType {
Expand Down

0 comments on commit 1b390e1

Please sign in to comment.