refactor(markers): cleanup

This commit is contained in:
moklick
2023-12-18 11:43:33 +01:00
parent 5b455a0b2d
commit 2a51d804b6
2 changed files with 27 additions and 24 deletions
@@ -51,17 +51,22 @@ const markerSelector =
return markers; return markers;
}; };
const markersEqual = (a: MarkerProps[], b: MarkerProps[]) =>
// the id includes all marker options, so we just need to look at that part of the marker
!(a.length !== b.length || a.some((m, i) => m.id !== b[i].id));
// when you have multiple flows on a page and you hide the first one, the other ones have no markers anymore // when you have multiple flows on a page and you hide the first one, the other ones have no markers anymore
// when they do have markers with the same ids. To prevent this the user can pass a unique id to the react flow wrapper // when they do have markers with the same ids. To prevent this the user can pass a unique id to the react flow wrapper
// that we can then use for creating our unique marker ids // that we can then use for creating our unique marker ids
const MarkerDefinitions = ({ defaultColor, rfId }: MarkerDefinitionsProps) => { const MarkerDefinitions = ({ defaultColor, rfId }: MarkerDefinitionsProps) => {
const markers = useStore( const markers = useStore(useCallback(markerSelector({ defaultColor, rfId }), [defaultColor, rfId]), markersEqual);
useCallback(markerSelector({ defaultColor, rfId }), [defaultColor, rfId]),
// the id includes all marker options, so we just need to look at that part of the marker if (!markers.length) {
(a, b) => !(a.length !== b.length || a.some((m, i) => m.id !== b[i].id)) return null;
); }
return ( return (
<svg className="react-flow__marker">
<defs> <defs>
{markers.map((marker: MarkerProps) => ( {markers.map((marker: MarkerProps) => (
<Marker <Marker
@@ -77,6 +82,7 @@ const MarkerDefinitions = ({ defaultColor, rfId }: MarkerDefinitionsProps) => {
/> />
))} ))}
</defs> </defs>
</svg>
); );
}; };
@@ -28,7 +28,6 @@ type EdgeRendererProps = Pick<
| 'disableKeyboardA11y' | 'disableKeyboardA11y'
| 'edgeTypes' | 'edgeTypes'
> & { > & {
elevateEdgesOnSelect: boolean;
children: ReactNode; children: ReactNode;
}; };
@@ -66,9 +65,7 @@ const EdgeRenderer = ({
return ( return (
<div className="react-flow__edges"> <div className="react-flow__edges">
<svg className="react-flow__marker">
<MarkerDefinitions defaultColor={defaultMarkerColor} rfId={rfId} /> <MarkerDefinitions defaultColor={defaultMarkerColor} rfId={rfId} />
</svg>
{edgeIds.map((id) => { {edgeIds.map((id) => {
return ( return (