Skip to Content

2024-08-14

新的边缘示例

Hayleigh Thompson
Software Engineer

这里有一组关于新边的示例,包括如何沿边路径动画节点,如何创建临时边,以及每个连接事件的演示。我们希望这组新的示例能帮助你提升流程操作水平!

🌐 Here’s a care drop of new edge examples including how to animate nodes along an edge path, how to create temporary edges, and a demo of every connection event. We hope this new bundle of examples will help you level up your flow game!

import { useEffect, useMemo } from 'react'; import { BaseEdge, getBezierPath, useReactFlow, type Edge, type EdgeProps, } from '@xyflow/react'; export type AnimatedNodeEdge = Edge<{ node: string }, 'animatedNode'>; export function AnimatedNodeEdge({ id, data = { node: '' }, sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, }: EdgeProps<AnimatedNodeEdge>) { const { getNode, updateNode } = useReactFlow(); const [edgePath] = getBezierPath({ sourceX, sourceY, sourcePosition, targetX, targetY, targetPosition, }); const selector = useMemo( () => `.react-flow__node[data-id="${data.node}"]`, [data.node], ); useEffect(() => { const node = document.querySelector(selector) as HTMLElement; if (!node) return; node.style.offsetPath = `path('${edgePath}')`; node.style.offsetRotate = '0deg'; // This property is fairly new and not all versions of TypeScript have it // in the lib.dom.d.ts file. If you get an error here, you can either // ignore it or add the property to the CSSStyleDeclaration interface // yourself. // // @ts-expect-error node.style.offsetAnchor = 'center'; let wasDraggable = getNode(data.node).draggable; updateNode(data.node, { draggable: false }); return () => { node.style.offsetPath = 'none'; updateNode(data.node, { draggable: wasDraggable }); }; }, [selector, edgePath]); useEffect(() => { const node = document.querySelector(selector) as HTMLElement; if (!node) return; const keyframes = [{ offsetDistance: '0%' }, { offsetDistance: '100%' }]; const animation = node.animate(keyframes, { duration: 2000, direction: 'alternate', iterations: Infinity, }); return () => { animation.cancel(); }; }, [selector]); return <BaseEdge id={id} path={edgePath} />; }

这是新增内容的详细分解:

🌐 This is a breakdown of what’s been added:

  • 两个示例合一,展示如何沿边路径为 SVG 元素和其他节点制作动画。
  • 一个新的示例,展示如何创建一个只附加到一个句柄的临时边。这些边可以在以后重新拾取并连接。
  • 我们有很多与连接相关的事件,所以我们创建了一个 小演示,展示每个连接和事件以及它们触发的时间。

Get Pro examples, prioritized bug reports, 1:1 support from the maintainers, and more with React Flow Pro

Last updated on