Merge pull request #5544 from xyflow/edge-toolbar

Add EdgeToolbar Component
This commit is contained in:
Moritz Klack
2025-10-20 13:02:57 +02:00
committed by GitHub
18 changed files with 417 additions and 0 deletions
@@ -0,0 +1,79 @@
import { useCallback } from 'react';
import cc from 'classcat';
import { shallow } from 'zustand/shallow';
import { getEdgeToolbarTransform } from '@xyflow/system';
import { EdgeLabelRenderer } from '../../components/EdgeLabelRenderer';
import { useStore } from '../../hooks/useStore';
import { Edge, ReactFlowState } from '../../types';
import type { EdgeToolbarProps } from './types';
const zoomSelector = (state: ReactFlowState) => state.transform[2];
/**
* This component can render a toolbar or tooltip to one side of a custom edge. This
* toolbar doesn't scale with the viewport so that the content stays the same size.
*
* @public
* @example
* ```jsx
* import { EdgeToolbar, BaseEdge, getBezierPath, type EdgeProps } from "@xyflow/react";
*
* export function CustomEdge({ id, data, ...props }: EdgeProps) {
* const [edgePath, centerX, centerY] = getBezierPath(props);
*
* return (
* <>
* <BaseEdge id={id} path={edgePath} />
* <EdgeToolbar edgeId={id} x={centerX} y={centerY} isVisible>
* <button onClick={() => console.log('edge', id, 'click')}}>Click me</button>
* </EdgeToolbar>
* </>
* );
* }
* ```
*/
export function EdgeToolbar({
edgeId,
x,
y,
children,
className,
style,
isVisible,
alignX = 'center',
alignY = 'center',
...rest
}: EdgeToolbarProps) {
const edgeSelector = useCallback((state: ReactFlowState): Edge | undefined => state.edgeLookup.get(edgeId), [edgeId]);
const edge = useStore(edgeSelector, shallow);
const isActive = typeof isVisible === 'boolean' ? isVisible : edge?.selected;
const zoom = useStore(zoomSelector);
if (!isActive) {
return null;
}
const zIndex = (edge?.zIndex ?? 0) + 1;
const transform = getEdgeToolbarTransform(x, y, zoom, alignX, alignY);
return (
<EdgeLabelRenderer>
<div
style={{
position: 'absolute',
transform,
zIndex,
pointerEvents: 'all',
transformOrigin: '0 0',
...style,
}}
className={cc(['react-flow__edge-toolbar', className])}
data-id={edge?.id ?? ''}
{...rest}
>
{children}
</div>
</EdgeLabelRenderer>
);
}
@@ -0,0 +1,2 @@
export { EdgeToolbar } from './EdgeToolbar';
export type { EdgeToolbarProps } from './types';
@@ -0,0 +1,10 @@
import type { HTMLAttributes, ReactNode } from 'react';
import type { EdgeToolbarBaseProps } from '@xyflow/system';
/**
* @inline
*/
export type EdgeToolbarProps = EdgeToolbarBaseProps &
HTMLAttributes<HTMLDivElement> & {
children?: ReactNode;
};
@@ -3,3 +3,4 @@ export * from './Controls';
export * from './MiniMap';
export * from './NodeResizer';
export * from './NodeToolbar';
export * from './EdgeToolbar';
+1
View File
@@ -22,6 +22,7 @@ export * from '$lib/plugins/Controls';
export * from '$lib/plugins/Background';
export * from '$lib/plugins/Minimap';
export * from '$lib/plugins/NodeToolbar';
export * from '$lib/plugins/EdgeToolbar';
export * from '$lib/plugins/NodeResizer';
// store
@@ -0,0 +1,40 @@
<script lang="ts">
import { getEdgeToolbarTransform } from '@xyflow/system';
import { useStore } from '$lib/store';
import { EdgeLabel } from '$lib/components/EdgeLabel';
import type { EdgeToolbarProps } from './types';
let {
edgeId,
x,
y,
alignX = 'center',
alignY = 'center',
isVisible,
children,
...rest
}: EdgeToolbarProps = $props();
const store = useStore();
const edge = $derived(store.edgeLookup.get(edgeId));
const isActive = $derived(typeof isVisible === 'boolean' ? isVisible : edge?.selected);
const transform = $derived(getEdgeToolbarTransform(x, y, store.viewport.zoom, alignX, alignY));
const zIndex = $derived((edge?.zIndex ?? 0) + 1);
</script>
{#if store.domNode && isActive}
<EdgeLabel>
<div
style:position="absolute"
style:transform
style:z-index={zIndex}
style:transform-origin="0 0"
{...rest}
class="svelte-flow__edge-toolbar"
data-id={edgeId ?? ''}
>
{@render children?.()}
</div>
</EdgeLabel>
{/if}
@@ -0,0 +1,2 @@
export { default as EdgeToolbar } from './EdgeToolbar.svelte';
export * from './types';
@@ -0,0 +1,7 @@
import type { EdgeToolbarBaseProps } from '@xyflow/system';
import type { Snippet } from 'svelte';
import type { HTMLAttributes } from 'svelte/elements';
export type EdgeToolbarProps = EdgeToolbarBaseProps & {
children?: Snippet;
} & HTMLAttributes<HTMLDivElement>;
+31
View File
@@ -129,3 +129,34 @@ export type EdgePosition = {
};
export type EdgeLookup<EdgeType extends EdgeBase = EdgeBase> = Map<string, EdgeType>;
export type EdgeToolbarBaseProps = {
/**
* An edge toolbar must be attached to an edge.
*/
edgeId: string;
/**
* The `x` position of the edge label.
*/
x: number;
/**
* The `y` position of the edge label.
*/
y: number;
/** If `true`, edge toolbar is visible even if edge is not selected.
* @default false
*/
isVisible?: boolean;
/**
* Align the vertical toolbar position relative to the passed x position.
* @default "center"
* @example 'left', 'center', 'right'
*/
alignX?: 'left' | 'center' | 'right';
/**
* Align the horizontal toolbar position relative to the passed y position.
* @default "center"
* @example 'top', 'center', 'bottom'
*/
alignY?: 'top' | 'center' | 'bottom';
};
+23
View File
@@ -0,0 +1,23 @@
const alignXToPercent: Record<'left' | 'center' | 'right', number> = {
left: 0,
center: 50,
right: 100,
};
const alignYToPercent: Record<'top' | 'center' | 'bottom', number> = {
top: 0,
center: 50,
bottom: 100,
};
export function getEdgeToolbarTransform(
x: number,
y: number,
zoom: number,
alignX: 'left' | 'center' | 'right' = 'center',
alignY: 'top' | 'center' | 'bottom' = 'center'
): string {
return `translate(${x}px, ${y}px) scale(${1 / zoom}) translate(${-(alignXToPercent[alignX] ?? 50)}%, ${-(
alignYToPercent[alignY] ?? 50
)}%)`;
}
+1
View File
@@ -5,6 +5,7 @@ export * from './graph';
export * from './general';
export * from './marker';
export * from './node-toolbar';
export * from './edge-toolbar';
export * from './store';
export * from './types';
export * from './shallow-node-data';