Merge pull request #5428 from Karl255/#5273-handle-click-target-fix

Allow detached handles
This commit is contained in:
Moritz Klack
2025-08-12 14:18:20 +02:00
committed by GitHub
13 changed files with 183 additions and 16 deletions

View File

@@ -58,6 +58,7 @@ import AddNodeOnEdgeDrop from '../examples/AddNodeOnEdgeDrop';
import DevTools from '../examples/DevTools';
import Redux from '../examples/Redux';
import MovingHandles from '../examples/MovingHandles';
import DetachedHandle from '../examples/DetachedHandle';
export interface IRoute {
name: string;
@@ -146,6 +147,11 @@ const routes: IRoute[] = [
path: 'default-nodes',
component: DefaultNodes,
},
{
name: 'DetachedHandle',
path: 'detached-handle',
component: DetachedHandle,
},
{
name: 'DevTools',
path: 'devtools',

View File

@@ -0,0 +1,62 @@
import {
ReactFlow,
Node,
ReactFlowProvider,
Background,
BackgroundVariant,
NodeProps,
Handle,
Position,
} from '@xyflow/react';
import './style.css';
const initialNodes: Node[] = [
{
id: '1',
data: { label: 'Node 1' },
position: { x: 250, y: 5 },
},
{
id: '2',
data: { label: 'Node 2' },
position: { x: 50, y: 100 },
},
{
id: '3',
data: { label: 'Node 3' },
position: { x: 450, y: 100 },
},
];
const CustomNode = (_: NodeProps) => {
return (
<>
<Handle type="target" position={Position.Left} />
<div>Custom node</div>
<Handle type="source" position={Position.Right}>
<button className="detached-handle"></button>
</Handle>
</>
);
};
const nodeTypes = {
default: CustomNode,
};
const DetachedHandle = () => {
return (
<ReactFlow defaultNodes={initialNodes} defaultEdges={[]} connectionRadius={10} nodeTypes={nodeTypes} fitView>
<Background variant={BackgroundVariant.Lines} />
</ReactFlow>
);
};
export default function App() {
return (
<ReactFlowProvider>
<DetachedHandle />
</ReactFlowProvider>
);
}

View File

@@ -0,0 +1,10 @@
.detached-handle {
position: absolute;
top: 50%;
left: 1rem;
transform: translateY(-50%);
width: 2rem;
height: 2rem;
border: none;
border-radius: 50%;
}

View File

@@ -1,6 +1,6 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import { page } from '$app/state';
const routes = [
'a11y',
@@ -9,6 +9,7 @@
'custom-connection-line',
'customnode',
'dagre',
'detached-handle',
'drag-n-drop',
'edges',
'figma',
@@ -36,7 +37,7 @@
<header>
<div class="logo">Svelte Flow</div>
<select onchange={onChange} value={$page.route.id}>
<select onchange={onChange} value={page.route.id}>
{#each routes as route}
<option value={`/examples/${route}`}>{route}</option>
{/each}

View File

@@ -0,0 +1,41 @@
<script lang="ts">
import {
SvelteFlow,
Background,
MiniMap,
Position,
type Node,
type NodeTypes,
type Edge
} from '@xyflow/svelte';
import CustomNode from './CustomNode.svelte';
import '@xyflow/svelte/dist/style.css';
const nodeTypes: NodeTypes = {
custom: CustomNode
};
let nodes = $state.raw<Node[]>([
{
id: '1',
type: 'custom',
data: { label: 'node 1' },
position: { x: 0, y: 0 },
sourcePosition: Position.Right
},
{
id: '2',
type: 'custom',
data: { label: 'node 2' },
position: { x: 250, y: 250 }
}
]);
let edges = $state.raw<Edge[]>([]);
</script>
<SvelteFlow bind:nodes bind:edges {nodeTypes} fitView>
<Background />
<MiniMap />
</SvelteFlow>

View File

@@ -0,0 +1,36 @@
<script lang="ts">
import { Handle, Position, type NodeProps, type Node } from '@xyflow/svelte';
let { data }: NodeProps<Node> = $props();
</script>
<div class="custom">
<div>
{data.label}
</div>
<Handle type="target" position={Position.Left} />
<Handle type="source" position={Position.Right}>
<button class="detached-handle">➡️</button>
</Handle>
</div>
<style>
.custom {
background-color: white;
padding: 10px;
border: 1px solid #777;
border-radius: 20px;
}
.detached-handle {
position: absolute;
top: 50%;
left: 1rem;
transform: translateY(-50%);
width: 2rem;
height: 2rem;
border: none;
border-radius: 50%;
}
</style>