-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Arrow for Diagram Component #6863
base: master
Are you sure you want to change the base?
Changes from 5 commits
9f64ad0
fd2b6c5
3c624d7
6e30aca
f871fda
85f1c68
4337791
2bd7bee
0bd64e0
8974d44
5d4129d
dfbf756
f86aa56
54ffa1d
50fc9da
717a177
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import React, { | ||
Fragment, | ||
forwardRef, | ||
useCallback, | ||
useContext, | ||
|
@@ -66,6 +67,19 @@ const findTarget = (target) => { | |
return target; | ||
}; | ||
|
||
const openArrow = (color, index, name = 'openArrowEnd', orient = 'auto') => ( | ||
<marker | ||
id={`${name}-${index}`} | ||
markerWidth="7" | ||
markerHeight="9" | ||
refX="2" | ||
refY="6" | ||
orient={orient} | ||
> | ||
<path d="M1,4 L3,6 L1,8" stroke={color} fill="none" /> | ||
</marker> | ||
); | ||
|
||
const Diagram = forwardRef(({ connections, ...rest }, ref) => { | ||
const theme = useContext(ThemeContext) || defaultProps.theme; | ||
const [dimensions, setDimensions] = useState({ width: 0, height: 0 }); | ||
|
@@ -117,7 +131,7 @@ const Diagram = forwardRef(({ connections, ...rest }, ref) => { | |
const placeConnections = useCallback(() => { | ||
const containerRect = svgRef.current.getBoundingClientRect(); | ||
const updatedConnectionPoints = connections.map( | ||
({ anchor, fromTarget, toTarget }) => { | ||
({ anchor, fromTarget, toTarget, endpoint }) => { | ||
let points; | ||
const fromElement = findTarget(fromTarget); | ||
const toElement = findTarget(toTarget); | ||
|
@@ -145,16 +159,52 @@ const Diagram = forwardRef(({ connections, ...rest }, ref) => { | |
toPoint[0] += toRect.width / 2; | ||
if (fromRect.top < toRect.top) { | ||
fromPoint[1] += fromRect.height; | ||
|
||
if (typeof endpoint === 'object' && endpoint?.from) { | ||
fromPoint[1] += 10; | ||
} else if (typeof endpoint === 'object' && endpoint?.to) { | ||
toPoint[1] -= 10; | ||
} else if (typeof endpoint === 'string') { | ||
fromPoint[1] += 10; | ||
toPoint[1] -= 10; | ||
} | ||
} else { | ||
toPoint[1] += toRect.height; | ||
|
||
if (typeof endpoint === 'object' && endpoint?.from) { | ||
fromPoint[1] -= 10; | ||
} else if (typeof endpoint === 'object' && endpoint?.to) { | ||
toPoint[1] += 10; | ||
} else if (typeof endpoint === 'string') { | ||
fromPoint[1] -= 10; | ||
toPoint[1] += 10; | ||
} | ||
} | ||
} else if (anchor === 'horizontal') { | ||
fromPoint[1] += fromRect.height / 2; | ||
toPoint[1] += toRect.height / 2; | ||
if (fromRect.left < toRect.left) { | ||
fromPoint[0] += fromRect.width; | ||
|
||
if (typeof endpoint === 'object' && endpoint?.from) { | ||
fromPoint[0] += 10; | ||
} else if (typeof endpoint === 'object' && endpoint?.to) { | ||
toPoint[0] -= 10; | ||
} else if (typeof endpoint === 'string') { | ||
fromPoint[0] += 10; | ||
toPoint[0] -= 10; | ||
} | ||
} else { | ||
toPoint[0] += toRect.width; | ||
|
||
if (typeof endpoint === 'object' && endpoint?.from) { | ||
fromPoint[0] -= 10; | ||
} else if (typeof endpoint === 'object' && endpoint?.to) { | ||
toPoint[0] += 10; | ||
} else if (typeof endpoint === 'string') { | ||
fromPoint[0] -= 10; | ||
toPoint[0] += 10; | ||
} | ||
} | ||
} else { | ||
// center | ||
|
@@ -190,6 +240,7 @@ const Diagram = forwardRef(({ connections, ...rest }, ref) => { | |
round, | ||
thickness, | ||
type, | ||
endpoint, | ||
...connectionRest | ||
}, | ||
index, | ||
|
@@ -222,20 +273,49 @@ const Diagram = forwardRef(({ connections, ...rest }, ref) => { | |
colorName = colors[index % colors.length]; | ||
} | ||
|
||
let arrowMarker = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a smaller comment but to be more flexible for future, should we name this variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since, |
||
|
||
if (typeof endpoint === 'object' && endpoint?.from === 'arrow') { | ||
arrowMarker = openArrow( | ||
normalizeColor(colorName, theme), | ||
index, | ||
'openArrowStart', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also to be more flexible for future, should we name this |
||
'auto-start-reverse', | ||
); | ||
} else if (typeof endpoint === 'object' && endpoint?.to === 'arrow') { | ||
arrowMarker = openArrow(normalizeColor(colorName, theme), index); | ||
} else if (typeof endpoint === 'string' && endpoint === 'arrow') { | ||
arrowMarker = ( | ||
<> | ||
{openArrow( | ||
normalizeColor(colorName, theme), | ||
index, | ||
'openArrowStart', | ||
'auto-start-reverse', | ||
)} | ||
{openArrow(normalizeColor(colorName, theme), index)} | ||
</> | ||
); | ||
} | ||
|
||
path = ( | ||
<path | ||
// eslint-disable-next-line react/no-array-index-key | ||
key={index} | ||
// eslint-disable-next-line react/no-unknown-property | ||
animation={animation} | ||
{...cleanedRest} | ||
stroke={normalizeColor(colorName, theme)} | ||
strokeWidth={strokeWidth} | ||
strokeLinecap={round ? 'round' : 'butt'} | ||
strokeLinejoin={round ? 'round' : 'miter'} | ||
fill="none" | ||
d={d} | ||
/> | ||
// eslint-disable-next-line react/no-array-index-key | ||
<Fragment key={index}> | ||
<path | ||
// eslint-disable-next-line react/no-unknown-property | ||
animation={animation} | ||
{...cleanedRest} | ||
stroke={normalizeColor(colorName, theme)} | ||
strokeWidth={strokeWidth} | ||
strokeLinecap={round ? 'round' : 'butt'} | ||
strokeLinejoin={round ? 'round' : 'miter'} | ||
fill="none" | ||
d={d} | ||
markerStart={`url("#openArrowStart-${index}")`} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any downside to having the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, if multiple diagrams are rendered on a page, will this index id approach work still? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, there is, I will fix that. |
||
markerEnd={`url("#openArrowEnd-${index}")`} | ||
/> | ||
{endpoint && arrowMarker && <defs>{arrowMarker}</defs>} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we wrap in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main use case for |
||
</Fragment> | ||
); | ||
} | ||
return path; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How was
10
determined? It might be helpful to add a code comment for future.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't used a definitive approach to calculate the 10. I just check the
Animated
story and10
seems good which isn't closely aligned with the start and end of the main diamond diagram