diff --git a/apps/example-apps/react/examples/edges/markers/App.jsx b/apps/example-apps/react/examples/edges/markers/App.tsx similarity index 90% rename from apps/example-apps/react/examples/edges/markers/App.jsx rename to apps/example-apps/react/examples/edges/markers/App.tsx index 97ae1dfe4..2c8673d2c 100644 --- a/apps/example-apps/react/examples/edges/markers/App.jsx +++ b/apps/example-apps/react/examples/edges/markers/App.tsx @@ -1,7 +1,12 @@ -import React from 'react'; -import { ReactFlow, Background, MarkerType } from '@xyflow/react'; +import { ReactFlow, Background, MarkerType, BezierEdge } from '@xyflow/react'; import '@xyflow/react/dist/style.css'; +import CustomEdge from './CustomEdge'; + +const edgeTypes = { + default: BezierEdge, + custom: CustomEdge, +}; const defaultNodes = [ { @@ -39,6 +44,11 @@ const defaultNodes = [ position: { x: 20, y: 450 }, data: { label: 'G' }, }, + { + id: 'H', + position: { x: 500, y: 450 }, + data: { label: 'H' }, + }, ]; const defaultEdges = [ @@ -80,6 +90,18 @@ const defaultEdges = [ markerEnd: 'logo', label: 'custom marker', }, + { + id: 'E->H', + source: 'E', + target: 'H', + type: 'custom', + markerEnd: { + type: MarkerType.ArrowClosed, + width: 20, + height: 20, + }, + label: 'change color on selection', + }, { id: 'B->G', source: 'B', @@ -172,11 +194,7 @@ export default function MarkersExample() { fill="#1A192B" /> - + diff --git a/apps/example-apps/react/examples/edges/markers/CustomEdge.tsx b/apps/example-apps/react/examples/edges/markers/CustomEdge.tsx new file mode 100644 index 000000000..5808273aa --- /dev/null +++ b/apps/example-apps/react/examples/edges/markers/CustomEdge.tsx @@ -0,0 +1,80 @@ +import { + BaseEdge, + EdgeLabelRenderer, + EdgeText, + getBezierPath, + type EdgeProps, +} from '@xyflow/react'; + +export default function CustomEdge({ + id, + sourceX, + sourceY, + targetX, + label, + targetY, + sourcePosition, + targetPosition, + style = {}, + markerEnd, + selected, +}: EdgeProps) { + const [edgePath, labelX, labelY] = getBezierPath({ + sourceX, + sourceY, + sourcePosition, + targetX, + targetY, + targetPosition, + }); + + const activeMarkerEnd = selected ? 'url(#selected-marker)' : markerEnd; + + const color = selected ? '#FFCC00' : style.stroke; + + return ( + <> + {/* You can also define a custom marker in your own Custom Edge component. + This might be useful if you want to have a different marker, depending on the state of the edge. + Ideally, you would store the marker definition in a separate component, so you can reuse it in multiple edges. + If you defined it here, in your custom edge component, it will be re-rendered for every edge instance. + */} + + + + + + + + + + + + ); +} diff --git a/apps/example-apps/react/examples/edges/markers/README.mdx b/apps/example-apps/react/examples/edges/markers/README.mdx index 0394ac8d0..88fa00256 100644 --- a/apps/example-apps/react/examples/edges/markers/README.mdx +++ b/apps/example-apps/react/examples/edges/markers/README.mdx @@ -3,6 +3,20 @@ title: Edge Markers description: Make your edges into arrows, add custom icons, or SVGs --- -React Flow has built-in support for different marker types for your edges. It's possible to add your own [SVG markers](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/marker), too. +React Flow has built-in support for different marker types for your edges. It's +possible to add your own [SVG +markers](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/marker), too. + +The markers are defined as SVG elements, so you can use any SVG you want. +The React Flow edge renderer will expect the marker definition to be in a `` element. + +If, for example, your custom edge is wrapping a built-in `` component, +you can pass the `markerStart` and `markerEnd` props to the `` component. +The built-in React Flow edge components +will expect the `markerStart` and `markerEnd` props to be in the format +`"url(#markerId)"`, where `markerId` is the ID of your marker definition. + +This allows you to customize the marker for a specific edge, depending on the state of the edge. + diff --git a/apps/example-apps/react/examples/edges/markers/index.jsx b/apps/example-apps/react/examples/edges/markers/index.tsx similarity index 64% rename from apps/example-apps/react/examples/edges/markers/index.jsx rename to apps/example-apps/react/examples/edges/markers/index.tsx index 5def1cb16..6c1fba60d 100644 --- a/apps/example-apps/react/examples/edges/markers/index.jsx +++ b/apps/example-apps/react/examples/edges/markers/index.tsx @@ -4,6 +4,7 @@ import App from './App'; import './index.css'; const container = document.querySelector('#app'); -const root = createRoot(container); - -root.render(); +if (container) { + const root = createRoot(container); + root.render(); +}