refactor(controls): move controls into own package and replace in examples

This commit is contained in:
Christopher Möller
2022-07-21 14:05:56 +02:00
parent 272d0dfcb4
commit 2d2bbcc8df
29 changed files with 102 additions and 61 deletions
+2 -1
View File
@@ -9,4 +9,5 @@ dist
stats.html stats.html
.eslintcache .eslintcache
.idea .idea
.yarn .yarn
.log
+1
View File
@@ -11,6 +11,7 @@
"dependencies": { "dependencies": {
"@preconstruct/next": "^4.0.0", "@preconstruct/next": "^4.0.0",
"@react-flow/background": "11.0.0", "@react-flow/background": "11.0.0",
"@react-flow/controls": "11.0.0",
"@react-flow/core": "11.0.0", "@react-flow/core": "11.0.0",
"@react-flow/minimap": "11.0.0", "@react-flow/minimap": "11.0.0",
"dagre": "^0.8.5", "dagre": "^0.8.5",
+1 -1
View File
@@ -3,7 +3,6 @@ import { ChangeEvent } from 'react';
import ReactFlow, { import ReactFlow, {
addEdge, addEdge,
Controls,
Node, Node,
ReactFlowInstance, ReactFlowInstance,
Position, Position,
@@ -14,6 +13,7 @@ import ReactFlow, {
} from '@react-flow/core'; } from '@react-flow/core';
import MiniMap from '@react-flow/minimap'; import MiniMap from '@react-flow/minimap';
import Controls from '@react-flow/controls';
import ColorSelectorNode from './ColorSelectorNode'; import ColorSelectorNode from './ColorSelectorNode';
+1 -1
View File
@@ -2,7 +2,6 @@ import React, { useState, DragEvent } from 'react';
import ReactFlow, { import ReactFlow, {
ReactFlowProvider, ReactFlowProvider,
addEdge, addEdge,
Controls,
ReactFlowInstance, ReactFlowInstance,
Connection, Connection,
Edge, Edge,
@@ -10,6 +9,7 @@ import ReactFlow, {
useNodesState, useNodesState,
useEdgesState, useEdgesState,
} from '@react-flow/core'; } from '@react-flow/core';
import Controls from '@react-flow/controls';
import Sidebar from './Sidebar'; import Sidebar from './Sidebar';
+1 -2
View File
@@ -5,7 +5,6 @@ import React from 'react';
import ReactFlow, { import ReactFlow, {
addEdge, addEdge,
Controls,
ReactFlowInstance, ReactFlowInstance,
Connection, Connection,
Edge, Edge,
@@ -14,7 +13,7 @@ import ReactFlow, {
} from '@react-flow/core'; } from '@react-flow/core';
import Background from '@react-flow/background'; import Background from '@react-flow/background';
import Controls from '@react-flow/controls';
import MiniMap from '@react-flow/minimap'; import MiniMap from '@react-flow/minimap';
import { getElements } from './utils'; import { getElements } from './utils';
+1 -2
View File
@@ -2,7 +2,6 @@ import React, { MouseEvent } from 'react';
import ReactFlow, { import ReactFlow, {
addEdge, addEdge,
Connection, Connection,
Controls,
Edge, Edge,
EdgeTypes, EdgeTypes,
MarkerType, MarkerType,
@@ -12,8 +11,8 @@ import ReactFlow, {
useNodesState, useNodesState,
} from '@react-flow/core'; } from '@react-flow/core';
import Controls from '@react-flow/controls';
import Background from '@react-flow/background'; import Background from '@react-flow/background';
import MiniMap from '@react-flow/minimap'; import MiniMap from '@react-flow/minimap';
import CustomEdge from './CustomEdge'; import CustomEdge from './CustomEdge';
+1 -1
View File
@@ -2,7 +2,6 @@ import React, { MouseEvent, CSSProperties } from 'react';
import ReactFlow, { import ReactFlow, {
addEdge, addEdge,
Controls,
Node, Node,
Connection, Connection,
Edge, Edge,
@@ -11,6 +10,7 @@ import ReactFlow, {
ReactFlowInstance, ReactFlowInstance,
} from '@react-flow/core'; } from '@react-flow/core';
import Controls from '@react-flow/controls';
import Background, { BackgroundVariant } from '@react-flow/background'; import Background, { BackgroundVariant } from '@react-flow/background';
const onInit = (reactFlowInstance: ReactFlowInstance) => const onInit = (reactFlowInstance: ReactFlowInstance) =>
+1 -1
View File
@@ -2,7 +2,6 @@ import React, { useState, useEffect, useCallback } from 'react';
import ReactFlow, { import ReactFlow, {
addEdge, addEdge,
Controls,
Connection, Connection,
Edge, Edge,
Node, Node,
@@ -10,6 +9,7 @@ import ReactFlow, {
useEdgesState, useEdgesState,
} from '@react-flow/core'; } from '@react-flow/core';
import Controls from '@react-flow/controls';
import MiniMap from '@react-flow/minimap'; import MiniMap from '@react-flow/minimap';
const initialNodes: Node[] = [ const initialNodes: Node[] = [
+1 -1
View File
@@ -5,7 +5,6 @@ import React, {
} from 'react'; } from 'react';
import ReactFlow, { import ReactFlow, {
addEdge, addEdge,
Controls,
Node, Node,
Connection, Connection,
Edge, Edge,
@@ -15,6 +14,7 @@ import ReactFlow, {
useEdgesState, useEdgesState,
} from '@react-flow/core'; } from '@react-flow/core';
import Controls from '@react-flow/controls';
import MiniMap from '@react-flow/minimap'; import MiniMap from '@react-flow/minimap';
const initialNodes: Node[] = [ const initialNodes: Node[] = [
+2 -1
View File
@@ -2,7 +2,6 @@ import React, { useCallback } from 'react';
import ReactFlow, { import ReactFlow, {
ReactFlowProvider, ReactFlowProvider,
addEdge, addEdge,
Controls,
Connection, Connection,
CoordinateExtent, CoordinateExtent,
Position, Position,
@@ -11,6 +10,8 @@ import ReactFlow, {
MarkerType, MarkerType,
EdgeMarker, EdgeMarker,
} from '@react-flow/core'; } from '@react-flow/core';
import Controls from '@react-flow/controls';
import dagre from 'dagre'; import dagre from 'dagre';
import initialItems from './initial-elements'; import initialItems from './initial-elements';
+1 -1
View File
@@ -4,13 +4,13 @@ import ReactFlow, {
addEdge, addEdge,
useNodesState, useNodesState,
useEdgesState, useEdgesState,
Controls,
Node, Node,
Edge, Edge,
ReactFlowInstance, ReactFlowInstance,
Connection, Connection,
} from '@react-flow/core'; } from '@react-flow/core';
import Controls from '@react-flow/controls';
import MiniMap from '@react-flow/minimap'; import MiniMap from '@react-flow/minimap';
import Background from '@react-flow/background'; import Background from '@react-flow/background';
+1 -1
View File
@@ -6,7 +6,6 @@ import React, {
import ReactFlow, { import ReactFlow, {
addEdge, addEdge,
Controls,
Node, Node,
Viewport, Viewport,
SnapGrid, SnapGrid,
@@ -18,6 +17,7 @@ import ReactFlow, {
OnSelectionChangeParams, OnSelectionChangeParams,
} from '@react-flow/core'; } from '@react-flow/core';
import Controls from '@react-flow/controls';
import Background from '@react-flow/background'; import Background from '@react-flow/background';
import MiniMap from '@react-flow/minimap'; import MiniMap from '@react-flow/minimap';
+1 -1
View File
@@ -3,7 +3,6 @@ import ReactFlow, {
ReactFlowProvider, ReactFlowProvider,
addEdge, addEdge,
Node, Node,
Controls,
Connection, Connection,
Edge, Edge,
ConnectionMode, ConnectionMode,
@@ -11,6 +10,7 @@ import ReactFlow, {
useEdgesState, useEdgesState,
ReactFlowInstance, ReactFlowInstance,
} from '@react-flow/core'; } from '@react-flow/core';
import Controls from '@react-flow/controls';
import Sidebar from './Sidebar'; import Sidebar from './Sidebar';
+1 -1
View File
@@ -1,6 +1,5 @@
import React, { useState, CSSProperties, useCallback } from 'react'; import React, { useState, CSSProperties, useCallback } from 'react';
import ReactFlow, { import ReactFlow, {
Controls,
ReactFlowInstance, ReactFlowInstance,
Edge, Edge,
Node, Node,
@@ -12,6 +11,7 @@ import ReactFlow, {
EdgeChange, EdgeChange,
} from '@react-flow/core'; } from '@react-flow/core';
import Controls from '@react-flow/controls';
import Background from '@react-flow/background'; import Background from '@react-flow/background';
import MiniMap from '@react-flow/minimap'; import MiniMap from '@react-flow/minimap';
+1 -1
View File
@@ -2,7 +2,6 @@ import React, { useState, MouseEvent, useCallback } from 'react';
import ReactFlow, { import ReactFlow, {
addEdge, addEdge,
Controls,
Node, Node,
Edge, Edge,
ReactFlowInstance, ReactFlowInstance,
@@ -12,6 +11,7 @@ import ReactFlow, {
useEdgesState, useEdgesState,
} from '@react-flow/core'; } from '@react-flow/core';
import Controls from '@react-flow/controls';
import Background from '@react-flow/background'; import Background from '@react-flow/background';
import MiniMap from '@react-flow/minimap'; import MiniMap from '@react-flow/minimap';
+20 -7
View File
@@ -1,6 +1,5 @@
import React, { useState, useCallback } from 'react'; import React, { useState, useCallback } from 'react';
import ReactFlow, { import ReactFlow, {
Controls,
updateEdge, updateEdge,
addEdge, addEdge,
applyNodeChanges, applyNodeChanges,
@@ -14,6 +13,8 @@ import ReactFlow, {
HandleType, HandleType,
} from '@react-flow/core'; } from '@react-flow/core';
import Controls from '@react-flow/controls';
const initialNodes: Node[] = [ const initialNodes: Node[] = [
{ {
id: '1', id: '1',
@@ -48,15 +49,26 @@ const initialNodes: Node[] = [
), ),
}, },
position: { x: 400, y: 100 }, position: { x: 400, y: 100 },
style: { background: '#D6D5E6', color: '#333', border: '1px solid #222138', width: 180 }, style: {
background: '#D6D5E6',
color: '#333',
border: '1px solid #222138',
width: 180,
},
}, },
]; ];
const initialEdges = [{ id: 'e1-2', source: '1', target: '2', label: 'This is a draggable edge' }]; const initialEdges = [
{ id: 'e1-2', source: '1', target: '2', label: 'This is a draggable edge' },
];
const onInit = (reactFlowInstance: ReactFlowInstance) => reactFlowInstance.fitView(); const onInit = (reactFlowInstance: ReactFlowInstance) =>
const onEdgeUpdateStart = (_: React.MouseEvent, edge: Edge, handleType: HandleType) => reactFlowInstance.fitView();
console.log(`start update ${handleType} handle`, edge); const onEdgeUpdateStart = (
_: React.MouseEvent,
edge: Edge,
handleType: HandleType
) => console.log(`start update ${handleType} handle`, edge);
const onEdgeUpdateEnd = (_: MouseEvent, edge: Edge, handleType: HandleType) => const onEdgeUpdateEnd = (_: MouseEvent, edge: Edge, handleType: HandleType) =>
console.log(`end update ${handleType} handle`, edge); console.log(`end update ${handleType} handle`, edge);
@@ -65,7 +77,8 @@ const UpdatableEdge = () => {
const [edges, setEdges] = useState<Edge[]>(initialEdges); const [edges, setEdges] = useState<Edge[]>(initialEdges);
const onEdgeUpdate = (oldEdge: Edge, newConnection: Connection) => const onEdgeUpdate = (oldEdge: Edge, newConnection: Connection) =>
setEdges((els) => updateEdge(oldEdge, newConnection, els)); setEdges((els) => updateEdge(oldEdge, newConnection, els));
const onConnect = (connection: Connection) => setEdges((els) => addEdge(connection, els)); const onConnect = (connection: Connection) =>
setEdges((els) => addEdge(connection, els));
const onNodesChange = useCallback((changes: NodeChange[]) => { const onNodesChange = useCallback((changes: NodeChange[]) => {
console.log(changes); console.log(changes);
+15
View File
@@ -0,0 +1,15 @@
{
"name": "@react-flow/controls",
"version": "11.0.0",
"description": "A component that controls the viewport of React Flow",
"main": "dist/react-flow-controls.cjs.js",
"module": "dist/react-flow-controls.esm.js",
"license": "MIT",
"dependencies": {
"@react-flow/core": "11.0.0"
},
"peerDependencies": {
"classcat": "^5.0.3",
"react": "^18.2.0"
}
}
@@ -1,8 +1,11 @@
import React, { memo, FC, useEffect, useState, PropsWithChildren } from 'react'; import React, { memo, FC, useEffect, useState, PropsWithChildren } from 'react';
import cc from 'classcat'; import cc from 'classcat';
import {
import { useStore, useStoreApi } from '../../store'; useStore,
import useReactFlow from '../../hooks/useReactFlow'; useStoreApi,
useReactFlow,
ReactFlowState,
} from '@react-flow/core';
import PlusIcon from './Icons/Plus'; import PlusIcon from './Icons/Plus';
import MinusIcon from './Icons/Minus'; import MinusIcon from './Icons/Minus';
@@ -10,15 +13,24 @@ import FitviewIcon from './Icons/FitView';
import LockIcon from './Icons/Lock'; import LockIcon from './Icons/Lock';
import UnlockIcon from './Icons/Unlock'; import UnlockIcon from './Icons/Unlock';
import { ControlProps, ControlButtonProps, ReactFlowState } from '../../types'; import { ControlProps, ControlButtonProps } from './types';
export const ControlButton: FC<PropsWithChildren<ControlButtonProps>> = ({ children, className, ...rest }) => ( export const ControlButton: FC<PropsWithChildren<ControlButtonProps>> = ({
<button type="button" className={cc(['react-flow__controls-button', className])} {...rest}> children,
className,
...rest
}) => (
<button
type='button'
className={cc(['react-flow__controls-button', className])}
{...rest}
>
{children} {children}
</button> </button>
); );
const isInteractiveSelector = (s: ReactFlowState) => s.nodesDraggable && s.nodesConnectable && s.elementsSelectable; const isInteractiveSelector = (s: ReactFlowState) =>
s.nodesDraggable && s.nodesConnectable && s.elementsSelectable;
const Controls: FC<PropsWithChildren<ControlProps>> = ({ const Controls: FC<PropsWithChildren<ControlProps>> = ({
style, style,
@@ -77,17 +89,17 @@ const Controls: FC<PropsWithChildren<ControlProps>> = ({
<> <>
<ControlButton <ControlButton
onClick={onZoomInHandler} onClick={onZoomInHandler}
className="react-flow__controls-zoomin" className='react-flow__controls-zoomin'
title="zoom in" title='zoom in'
aria-label="zoom in" aria-label='zoom in'
> >
<PlusIcon /> <PlusIcon />
</ControlButton> </ControlButton>
<ControlButton <ControlButton
onClick={onZoomOutHandler} onClick={onZoomOutHandler}
className="react-flow__controls-zoomout" className='react-flow__controls-zoomout'
title="zoom out" title='zoom out'
aria-label="zoom out" aria-label='zoom out'
> >
<MinusIcon /> <MinusIcon />
</ControlButton> </ControlButton>
@@ -95,20 +107,20 @@ const Controls: FC<PropsWithChildren<ControlProps>> = ({
)} )}
{showFitView && ( {showFitView && (
<ControlButton <ControlButton
className="react-flow__controls-fitview" className='react-flow__controls-fitview'
onClick={onFitViewHandler} onClick={onFitViewHandler}
title="fit view" title='fit view'
aria-label="fit view" aria-label='fit view'
> >
<FitviewIcon /> <FitviewIcon />
</ControlButton> </ControlButton>
)} )}
{showInteractive && ( {showInteractive && (
<ControlButton <ControlButton
className="react-flow__controls-interactive" className='react-flow__controls-interactive'
onClick={onToggleInteractivity} onClick={onToggleInteractivity}
title="toggle interactivity" title='toggle interactivity'
aria-label="toggle interactivity" aria-label='toggle interactivity'
> >
{isInteractive ? <UnlockIcon /> : <LockIcon />} {isInteractive ? <UnlockIcon /> : <LockIcon />}
</ControlButton> </ControlButton>
+2
View File
@@ -0,0 +1,2 @@
export { default as default } from './Controls';
export * from './types';
+16
View File
@@ -0,0 +1,16 @@
import { ButtonHTMLAttributes, HTMLAttributes } from 'react';
import { FitViewOptions } from '@react-flow/core';
export interface ControlProps extends HTMLAttributes<HTMLDivElement> {
showZoom?: boolean;
showFitView?: boolean;
showInteractive?: boolean;
fitViewOptions?: FitViewOptions;
onZoomIn?: () => void;
onZoomOut?: () => void;
onFitView?: () => void;
onInteractiveChange?: (interactiveStatus: boolean) => void;
}
export interface ControlButtonProps
extends ButtonHTMLAttributes<HTMLButtonElement> {}
@@ -1,5 +0,0 @@
// These components are not used by React Flow directly
// They can be added as children of a React Flow component
export { default as Controls, ControlButton } from './Controls';
export { default as ReactFlowProvider } from './ReactFlowProvider';
+1 -1
View File
@@ -17,7 +17,6 @@ export {
getSimpleBezierCenter as getSimpleBezierEdgeCenter, getSimpleBezierCenter as getSimpleBezierEdgeCenter,
} from './components/Edges/SimpleBezierEdge'; } from './components/Edges/SimpleBezierEdge';
export { default as SmoothStepEdge, getSmoothStepPath } from './components/Edges/SmoothStepEdge'; export { default as SmoothStepEdge, getSmoothStepPath } from './components/Edges/SmoothStepEdge';
export * from './additional-components';
export { internalsSymbol } from './utils'; export { internalsSymbol } from './utils';
export { export {
@@ -33,6 +32,7 @@ export {
} from './utils/graph'; } from './utils/graph';
export { applyNodeChanges, applyEdgeChanges } from './utils/changes'; export { applyNodeChanges, applyEdgeChanges } from './utils/changes';
export { getMarkerEnd, getCenter as getEdgeCenter } from './components/Edges/utils'; export { getMarkerEnd, getCenter as getEdgeCenter } from './components/Edges/utils';
export { default as ReactFlowProvider } from './components/ReactFlowProvider';
export { default as useReactFlow } from './hooks/useReactFlow'; export { default as useReactFlow } from './hooks/useReactFlow';
export { default as useUpdateNodeInternals } from './hooks/useUpdateNodeInternals'; export { default as useUpdateNodeInternals } from './hooks/useUpdateNodeInternals';
@@ -134,16 +134,3 @@ export interface ReactFlowProps extends HTMLAttributes<HTMLDivElement> {
} }
export type ReactFlowRefType = HTMLDivElement; export type ReactFlowRefType = HTMLDivElement;
export interface ControlProps extends HTMLAttributes<HTMLDivElement> {
showZoom?: boolean;
showFitView?: boolean;
showInteractive?: boolean;
fitViewOptions?: FitViewOptions;
onZoomIn?: () => void;
onZoomOut?: () => void;
onFitView?: () => void;
onInteractiveChange?: (interactiveStatus: boolean) => void;
}
export interface ControlButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {}