feat: create enablePanOnFocus prop, update onFocus func

This commit is contained in:
Abbey Yacoe
2025-06-05 12:22:06 +02:00
parent 343ed4364b
commit 6693c7300e
14 changed files with 125 additions and 59 deletions

View File

@@ -1,4 +1,4 @@
import { MouseEvent } from 'react';
import { MouseEvent, useState } from 'react';
import {
ReactFlow,
MiniMap,
@@ -10,13 +10,9 @@ import {
Edge,
OnNodeDrag,
AriaLabelConfig,
Panel,
} from '@xyflow/react';
const onNodeDrag: OnNodeDrag = (_, node: Node, nodes: Node[]) => console.log('drag', node, nodes);
const onNodeDragStart = (_: MouseEvent, node: Node, nodes: Node[]) => console.log('drag start', node, nodes);
const onNodeDragStop = (_: MouseEvent, node: Node, nodes: Node[]) => console.log('drag stop', node, nodes);
const onNodeClick = (_: MouseEvent, node: Node) => console.log('click', node);
const initialNodes: Node[] = [
{
id: '1',
@@ -41,9 +37,16 @@ const initialNodes: Node[] = [
id: '4',
data: { label: 'Node 4' },
position: { x: 300, y: 100 },
className: 'light',
ariaRoleDescription: 'custom node role',
ariaRole: 'button',
},
{
id: '5',
data: { label: 'Node 5' },
position: { x: 400, y: 200 },
},
{
id: '6',
data: { label: 'Node 6' },
position: { x: -1000, y: 200 },
},
];
@@ -51,6 +54,8 @@ const initialEdges: Edge[] = [
{ id: 'e1-2', source: '1', target: '2', animated: true },
{ id: 'e1-3', source: '1', target: '3' },
{ id: 'e1-4', source: '1', target: '4' },
{ id: 'e1-5', source: '4', target: '5' },
{ id: 'e1-6', source: '3', target: '6' },
];
const ariaLabelConfig: Partial<AriaLabelConfig> = {
@@ -68,19 +73,14 @@ const ariaLabelConfig: Partial<AriaLabelConfig> = {
};
const A11y = () => {
const [isFocusPannable, setEnablePanOnFocus] = useState(true);
return (
<ReactFlow
defaultNodes={initialNodes}
defaultEdges={initialEdges}
onNodesChange={console.log}
onNodeClick={onNodeClick}
onNodeDragStop={onNodeDragStop}
onNodeDragStart={onNodeDragStart}
onNodeDrag={onNodeDrag}
className="react-flow-basic-example"
minZoom={2}
maxZoom={4}
// fitView
enablePanOnFocus={isFocusPannable}
selectNodesOnDrag={false}
elevateEdgesOnSelect
elevateNodesOnSelect={false}
@@ -90,6 +90,20 @@ const A11y = () => {
<Background variant={BackgroundVariant.Dots} />
<MiniMap />
<Controls />
<Panel position="top-right">
<div>
<label htmlFor="focusPannable">
<input
id="focusPannable"
type="checkbox"
checked={isFocusPannable}
onChange={(event) => setEnablePanOnFocus(event.target.checked)}
className="xy-theme__checkbox"
/>
enablePanOnFocus
</label>
</div>
</Panel>
</ReactFlow>
);
};

View File

@@ -1,5 +1,5 @@
<script lang="ts">
import { SvelteFlow, Controls, Background, MiniMap } from '@xyflow/svelte';
import { SvelteFlow, Controls, Background, MiniMap, Panel } from '@xyflow/svelte';
import '@xyflow/svelte/dist/style.css';
@@ -10,7 +10,7 @@
data: { label: 'A' }
},
{ id: 'B', position: { x: -100, y: 150 }, data: { label: 'B' } },
{ id: 'C', position: { x: 100, y: 150 }, data: { label: 'C' } },
{ id: 'C', position: { x: 1000, y: 150 }, data: { label: 'C' } },
{ id: 'D', position: { x: 0, y: 260 }, data: { label: 'D' } }
]);
@@ -19,16 +19,12 @@
{ id: 'A-C', source: 'A', target: 'C' },
{ id: 'A-D', source: 'A', target: 'D' }
]);
</script>
<SvelteFlow
bind:nodes
bind:edges
fitView
ariaLabelConfig={{
let isFocusPannable = $state(true);
const ariaLabelConfig = $state(
{
'node.a11yDescription.default': 'Svelte Custom Node Desc.',
'node.a11yDescription.keyboardDisabled': 'Svelte Custom Keyboard Desc.',
'node.a11yDescription.ariaLiveMessage': ({ direction, x, y }) =>
'node.a11yDescription.ariaLiveMessage': ({ direction, x, y }: { direction: string; x: number; y: number }) =>
`Custom Moved selected node ${direction}. New position, x: ${x}, y: ${y}`,
'edge.a11yDescription.default': 'Svelte Custom Edge Desc.',
'controls.ariaLabel': 'Svelte Custom Control Aria Label',
@@ -37,9 +33,30 @@
// 'controls.fitView.ariaLabel': 'Svelte Custom Fit View',
'controls.interactive.ariaLabel': 'Svelte Custom Toggle Interactivity',
'minimap.ariaLabel': 'Svelte Custom Minimap'
}}
}
);
</script>
<SvelteFlow
bind:nodes
bind:edges
enablePanOnFocus={isFocusPannable}
ariaLabelConfig={ariaLabelConfig}
>
<Controls />
<Background />
<MiniMap />
<Panel class="panel top-right">
<div>
<label for="focusPannable">
Enable Pan on Focus
<input
id="focusPannable"
type="checkbox"
bind:checked={isFocusPannable}
class="svelte-flow__zoomonscroll"
/>
</label>
</div>
</Panel>
</SvelteFlow>