-
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 9 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, id, orient = 'auto') => ( | ||
<marker | ||
id={id} | ||
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,55 @@ const Diagram = forwardRef(({ connections, ...rest }, ref) => { | |
toPoint[0] += toRect.width / 2; | ||
if (fromRect.top < toRect.top) { | ||
fromPoint[1] += fromRect.height; | ||
|
||
// Here, we are +10 or -10 the length of the line based | ||
// on the anchor - vertical or horizontal, so that it | ||
// doesn't touch the start or end of the main digram closely. | ||
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 | ||
|
@@ -179,7 +232,18 @@ const Diagram = forwardRef(({ connections, ...rest }, ref) => { | |
}, [connectionPoints, placeConnections]); | ||
|
||
let paths; | ||
const markerElements = []; | ||
if (connectionPoints) { | ||
// addedMarkerElmentIds - To track the marker elements are added | ||
// with their id's based on open and closed arrows and their | ||
// color. For eg: | ||
// There can be a open arrow with blue color - | ||
// __grommet__openArrowStart__blue | ||
// And there can be a closed arrow with pink color - | ||
// __grommet__openArrowEnd__pink | ||
// So, instead of creating multiple arrows of the same color, we | ||
// will be leveraging one arrow based on open, close and it's color. | ||
const addedMarkerElmentIds = {}; | ||
paths = connections.map( | ||
( | ||
{ | ||
|
@@ -190,6 +254,7 @@ const Diagram = forwardRef(({ connections, ...rest }, ref) => { | |
round, | ||
thickness, | ||
type, | ||
endpoint, | ||
...connectionRest | ||
}, | ||
index, | ||
|
@@ -222,6 +287,105 @@ const Diagram = forwardRef(({ connections, ...rest }, ref) => { | |
colorName = colors[index % colors.length]; | ||
} | ||
|
||
if (typeof endpoint === 'object' && endpoint?.from === 'arrow') { | ||
// eslint-disable-next-line max-len | ||
cleanedRest.markerStart = `url("#__grommet__openArrowStart__${normalizeColor( | ||
colorName, | ||
theme, | ||
)}")`; | ||
|
||
if ( | ||
!addedMarkerElmentIds[ | ||
`__grommet__openArrowStart__${normalizeColor(colorName, theme)}` | ||
] | ||
) { | ||
addedMarkerElmentIds[ | ||
`__grommet__openArrowStart__${normalizeColor(colorName, theme)}` | ||
] = true; | ||
markerElements.push( | ||
openArrow( | ||
normalizeColor(colorName, theme), | ||
`__grommet__openArrowStart__${normalizeColor( | ||
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 value to building this Still wondering if this will cause issues if multiple diagrams are presented on the same screen where we might have clashing |
||
colorName, | ||
theme, | ||
)}`, | ||
'auto-start-reverse', | ||
), | ||
); | ||
} | ||
} else if (typeof endpoint === 'object' && endpoint?.to === 'arrow') { | ||
// eslint-disable-next-line max-len | ||
cleanedRest.markerEnd = `url("#__grommet__openArrowEnd__${normalizeColor( | ||
colorName, | ||
theme, | ||
)}")`; | ||
|
||
if ( | ||
!addedMarkerElmentIds[ | ||
`__grommet__openArrowEnd__${normalizeColor(colorName, theme)}` | ||
] | ||
) { | ||
addedMarkerElmentIds[ | ||
`__grommet__openArrowEnd__${normalizeColor(colorName, theme)}` | ||
] = true; | ||
markerElements.push( | ||
openArrow( | ||
normalizeColor(colorName, theme), | ||
`__grommet__openArrowEnd__${normalizeColor( | ||
colorName, | ||
theme, | ||
)}`, | ||
), | ||
); | ||
} | ||
} else if (typeof endpoint === 'string' && endpoint === 'arrow') { | ||
// eslint-disable-next-line max-len | ||
cleanedRest.markerStart = `url("#__grommet__openArrowStart__${normalizeColor( | ||
colorName, | ||
theme, | ||
)}")`; | ||
// eslint-disable-next-line max-len | ||
cleanedRest.markerEnd = `url("#__grommet__openArrowEnd__${normalizeColor( | ||
colorName, | ||
theme, | ||
)}")`; | ||
|
||
if ( | ||
!addedMarkerElmentIds[ | ||
`__grommet__openArrowStart__${normalizeColor(colorName, theme)}` | ||
] && | ||
!addedMarkerElmentIds[ | ||
`__grommet__openArrowEnd__${normalizeColor(colorName, theme)}` | ||
] | ||
) { | ||
addedMarkerElmentIds[ | ||
`__grommet__openArrowStart__${normalizeColor(colorName, theme)}` | ||
] = true; | ||
addedMarkerElmentIds[ | ||
`__grommet__openArrowEnd__${normalizeColor(colorName, theme)}` | ||
] = true; | ||
markerElements.push( | ||
<> | ||
{openArrow( | ||
normalizeColor(colorName, theme), | ||
`__grommet__openArrowStart__${normalizeColor( | ||
colorName, | ||
theme, | ||
)}`, | ||
'auto-start-reverse', | ||
)} | ||
{openArrow( | ||
normalizeColor(colorName, theme), | ||
`__grommet__openArrowEnd__${normalizeColor( | ||
colorName, | ||
theme, | ||
)}`, | ||
)} | ||
</>, | ||
); | ||
} | ||
} | ||
|
||
path = ( | ||
<path | ||
// eslint-disable-next-line react/no-array-index-key | ||
|
@@ -251,6 +415,14 @@ const Diagram = forwardRef(({ connections, ...rest }, ref) => { | |
connections={paths} | ||
{...rest} | ||
> | ||
{markerElements.length > 0 && ( | ||
<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. Do we need |
||
{markerElements.map((marker, index) => ( | ||
// eslint-disable-next-line react/no-array-index-key | ||
<Fragment key={index}>{marker}</Fragment> | ||
))} | ||
</defs> | ||
)} | ||
<g>{paths}</g> | ||
</StyledDiagram> | ||
); | ||
|
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