diff --git a/.gitignore b/.gitignore index a99b9057..d663fc2b 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ dist stats.html .eslintcache .idea -.yarn \ No newline at end of file +.yarn +.log \ No newline at end of file diff --git a/examples/nextjs/package.json b/examples/nextjs/package.json index 04ae4d32..e9019d5b 100644 --- a/examples/nextjs/package.json +++ b/examples/nextjs/package.json @@ -11,6 +11,7 @@ "dependencies": { "@preconstruct/next": "^4.0.0", "@react-flow/background": "11.0.0", + "@react-flow/controls": "11.0.0", "@react-flow/core": "11.0.0", "@react-flow/minimap": "11.0.0", "dagre": "^0.8.5", diff --git a/examples/nextjs/pages/CustomNode/index.tsx b/examples/nextjs/pages/CustomNode/index.tsx index 5f6f3d67..acc1b90c 100644 --- a/examples/nextjs/pages/CustomNode/index.tsx +++ b/examples/nextjs/pages/CustomNode/index.tsx @@ -3,7 +3,6 @@ import { ChangeEvent } from 'react'; import ReactFlow, { addEdge, - Controls, Node, ReactFlowInstance, Position, @@ -14,6 +13,7 @@ import ReactFlow, { } from '@react-flow/core'; import MiniMap from '@react-flow/minimap'; +import Controls from '@react-flow/controls'; import ColorSelectorNode from './ColorSelectorNode'; diff --git a/examples/nextjs/pages/DragNDrop/index.tsx b/examples/nextjs/pages/DragNDrop/index.tsx index bff84177..884eb23f 100644 --- a/examples/nextjs/pages/DragNDrop/index.tsx +++ b/examples/nextjs/pages/DragNDrop/index.tsx @@ -2,7 +2,6 @@ import React, { useState, DragEvent } from 'react'; import ReactFlow, { ReactFlowProvider, addEdge, - Controls, ReactFlowInstance, Connection, Edge, @@ -10,6 +9,7 @@ import ReactFlow, { useNodesState, useEdgesState, } from '@react-flow/core'; +import Controls from '@react-flow/controls'; import Sidebar from './Sidebar'; diff --git a/examples/nextjs/pages/EdgeTypes/index.tsx b/examples/nextjs/pages/EdgeTypes/index.tsx index 82b008e2..28f6b173 100644 --- a/examples/nextjs/pages/EdgeTypes/index.tsx +++ b/examples/nextjs/pages/EdgeTypes/index.tsx @@ -5,7 +5,6 @@ import React from 'react'; import ReactFlow, { addEdge, - Controls, ReactFlowInstance, Connection, Edge, @@ -14,7 +13,7 @@ import ReactFlow, { } from '@react-flow/core'; import Background from '@react-flow/background'; - +import Controls from '@react-flow/controls'; import MiniMap from '@react-flow/minimap'; import { getElements } from './utils'; diff --git a/examples/nextjs/pages/Edges/index.tsx b/examples/nextjs/pages/Edges/index.tsx index b1628f8e..e1420b47 100644 --- a/examples/nextjs/pages/Edges/index.tsx +++ b/examples/nextjs/pages/Edges/index.tsx @@ -2,7 +2,6 @@ import React, { MouseEvent } from 'react'; import ReactFlow, { addEdge, Connection, - Controls, Edge, EdgeTypes, MarkerType, @@ -12,8 +11,8 @@ import ReactFlow, { useNodesState, } from '@react-flow/core'; +import Controls from '@react-flow/controls'; import Background from '@react-flow/background'; - import MiniMap from '@react-flow/minimap'; import CustomEdge from './CustomEdge'; diff --git a/examples/nextjs/pages/Empty/index.tsx b/examples/nextjs/pages/Empty/index.tsx index 09df059e..5e36266c 100644 --- a/examples/nextjs/pages/Empty/index.tsx +++ b/examples/nextjs/pages/Empty/index.tsx @@ -2,7 +2,6 @@ import React, { MouseEvent, CSSProperties } from 'react'; import ReactFlow, { addEdge, - Controls, Node, Connection, Edge, @@ -11,6 +10,7 @@ import ReactFlow, { ReactFlowInstance, } from '@react-flow/core'; +import Controls from '@react-flow/controls'; import Background, { BackgroundVariant } from '@react-flow/background'; const onInit = (reactFlowInstance: ReactFlowInstance) => diff --git a/examples/nextjs/pages/Hidden/index.tsx b/examples/nextjs/pages/Hidden/index.tsx index 84b530d1..e7bab0c4 100644 --- a/examples/nextjs/pages/Hidden/index.tsx +++ b/examples/nextjs/pages/Hidden/index.tsx @@ -2,7 +2,6 @@ import React, { useState, useEffect, useCallback } from 'react'; import ReactFlow, { addEdge, - Controls, Connection, Edge, Node, @@ -10,6 +9,7 @@ import ReactFlow, { useEdgesState, } from '@react-flow/core'; +import Controls from '@react-flow/controls'; import MiniMap from '@react-flow/minimap'; const initialNodes: Node[] = [ diff --git a/examples/nextjs/pages/Interaction/index.tsx b/examples/nextjs/pages/Interaction/index.tsx index 523c6950..24bc910b 100644 --- a/examples/nextjs/pages/Interaction/index.tsx +++ b/examples/nextjs/pages/Interaction/index.tsx @@ -5,7 +5,6 @@ import React, { } from 'react'; import ReactFlow, { addEdge, - Controls, Node, Connection, Edge, @@ -15,6 +14,7 @@ import ReactFlow, { useEdgesState, } from '@react-flow/core'; +import Controls from '@react-flow/controls'; import MiniMap from '@react-flow/minimap'; const initialNodes: Node[] = [ diff --git a/examples/nextjs/pages/Layouting/index.tsx b/examples/nextjs/pages/Layouting/index.tsx index 83d7f512..d99f49cf 100644 --- a/examples/nextjs/pages/Layouting/index.tsx +++ b/examples/nextjs/pages/Layouting/index.tsx @@ -2,7 +2,6 @@ import React, { useCallback } from 'react'; import ReactFlow, { ReactFlowProvider, addEdge, - Controls, Connection, CoordinateExtent, Position, @@ -11,6 +10,8 @@ import ReactFlow, { MarkerType, EdgeMarker, } from '@react-flow/core'; + +import Controls from '@react-flow/controls'; import dagre from 'dagre'; import initialItems from './initial-elements'; diff --git a/examples/nextjs/pages/NestedNodes/index.tsx b/examples/nextjs/pages/NestedNodes/index.tsx index e4859fa5..b5489bc9 100644 --- a/examples/nextjs/pages/NestedNodes/index.tsx +++ b/examples/nextjs/pages/NestedNodes/index.tsx @@ -4,13 +4,13 @@ import ReactFlow, { addEdge, useNodesState, useEdgesState, - Controls, Node, Edge, ReactFlowInstance, Connection, } from '@react-flow/core'; +import Controls from '@react-flow/controls'; import MiniMap from '@react-flow/minimap'; import Background from '@react-flow/background'; diff --git a/examples/nextjs/pages/Overview/index.tsx b/examples/nextjs/pages/Overview/index.tsx index d4c2d880..f380741e 100644 --- a/examples/nextjs/pages/Overview/index.tsx +++ b/examples/nextjs/pages/Overview/index.tsx @@ -6,7 +6,6 @@ import React, { import ReactFlow, { addEdge, - Controls, Node, Viewport, SnapGrid, @@ -18,6 +17,7 @@ import ReactFlow, { OnSelectionChangeParams, } from '@react-flow/core'; +import Controls from '@react-flow/controls'; import Background from '@react-flow/background'; import MiniMap from '@react-flow/minimap'; diff --git a/examples/nextjs/pages/Provider/index.tsx b/examples/nextjs/pages/Provider/index.tsx index 35b6cc2f..add633fe 100644 --- a/examples/nextjs/pages/Provider/index.tsx +++ b/examples/nextjs/pages/Provider/index.tsx @@ -3,7 +3,6 @@ import ReactFlow, { ReactFlowProvider, addEdge, Node, - Controls, Connection, Edge, ConnectionMode, @@ -11,6 +10,7 @@ import ReactFlow, { useEdgesState, ReactFlowInstance, } from '@react-flow/core'; +import Controls from '@react-flow/controls'; import Sidebar from './Sidebar'; diff --git a/examples/nextjs/pages/Stress/index.tsx b/examples/nextjs/pages/Stress/index.tsx index 59bda3df..6832e98b 100644 --- a/examples/nextjs/pages/Stress/index.tsx +++ b/examples/nextjs/pages/Stress/index.tsx @@ -1,6 +1,5 @@ import React, { useState, CSSProperties, useCallback } from 'react'; import ReactFlow, { - Controls, ReactFlowInstance, Edge, Node, @@ -12,6 +11,7 @@ import ReactFlow, { EdgeChange, } from '@react-flow/core'; +import Controls from '@react-flow/controls'; import Background from '@react-flow/background'; import MiniMap from '@react-flow/minimap'; diff --git a/examples/nextjs/pages/Subflow/index.tsx b/examples/nextjs/pages/Subflow/index.tsx index a026b935..9a6083fb 100644 --- a/examples/nextjs/pages/Subflow/index.tsx +++ b/examples/nextjs/pages/Subflow/index.tsx @@ -2,7 +2,6 @@ import React, { useState, MouseEvent, useCallback } from 'react'; import ReactFlow, { addEdge, - Controls, Node, Edge, ReactFlowInstance, @@ -12,6 +11,7 @@ import ReactFlow, { useEdgesState, } from '@react-flow/core'; +import Controls from '@react-flow/controls'; import Background from '@react-flow/background'; import MiniMap from '@react-flow/minimap'; diff --git a/examples/nextjs/pages/UpdatableEdge/index.tsx b/examples/nextjs/pages/UpdatableEdge/index.tsx index a3df5460..88fee9ce 100644 --- a/examples/nextjs/pages/UpdatableEdge/index.tsx +++ b/examples/nextjs/pages/UpdatableEdge/index.tsx @@ -1,6 +1,5 @@ import React, { useState, useCallback } from 'react'; import ReactFlow, { - Controls, updateEdge, addEdge, applyNodeChanges, @@ -14,6 +13,8 @@ import ReactFlow, { HandleType, } from '@react-flow/core'; +import Controls from '@react-flow/controls'; + const initialNodes: Node[] = [ { id: '1', @@ -48,15 +49,26 @@ const initialNodes: Node[] = [ ), }, 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 onEdgeUpdateStart = (_: React.MouseEvent, edge: Edge, handleType: HandleType) => - console.log(`start update ${handleType} handle`, edge); +const onInit = (reactFlowInstance: ReactFlowInstance) => + reactFlowInstance.fitView(); +const onEdgeUpdateStart = ( + _: React.MouseEvent, + edge: Edge, + handleType: HandleType +) => console.log(`start update ${handleType} handle`, edge); const onEdgeUpdateEnd = (_: MouseEvent, edge: Edge, handleType: HandleType) => console.log(`end update ${handleType} handle`, edge); @@ -65,7 +77,8 @@ const UpdatableEdge = () => { const [edges, setEdges] = useState(initialEdges); const onEdgeUpdate = (oldEdge: Edge, newConnection: Connection) => 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[]) => { console.log(changes); diff --git a/packages/controls/package.json b/packages/controls/package.json new file mode 100644 index 00000000..97124e89 --- /dev/null +++ b/packages/controls/package.json @@ -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" + } +} diff --git a/packages/core/src/additional-components/Controls/index.tsx b/packages/controls/src/Controls.tsx similarity index 71% rename from packages/core/src/additional-components/Controls/index.tsx rename to packages/controls/src/Controls.tsx index 26bf351b..24023644 100644 --- a/packages/core/src/additional-components/Controls/index.tsx +++ b/packages/controls/src/Controls.tsx @@ -1,8 +1,11 @@ import React, { memo, FC, useEffect, useState, PropsWithChildren } from 'react'; import cc from 'classcat'; - -import { useStore, useStoreApi } from '../../store'; -import useReactFlow from '../../hooks/useReactFlow'; +import { + useStore, + useStoreApi, + useReactFlow, + ReactFlowState, +} from '@react-flow/core'; import PlusIcon from './Icons/Plus'; import MinusIcon from './Icons/Minus'; @@ -10,15 +13,24 @@ import FitviewIcon from './Icons/FitView'; import LockIcon from './Icons/Lock'; import UnlockIcon from './Icons/Unlock'; -import { ControlProps, ControlButtonProps, ReactFlowState } from '../../types'; +import { ControlProps, ControlButtonProps } from './types'; -export const ControlButton: FC> = ({ children, className, ...rest }) => ( - ); -const isInteractiveSelector = (s: ReactFlowState) => s.nodesDraggable && s.nodesConnectable && s.elementsSelectable; +const isInteractiveSelector = (s: ReactFlowState) => + s.nodesDraggable && s.nodesConnectable && s.elementsSelectable; const Controls: FC> = ({ style, @@ -77,17 +89,17 @@ const Controls: FC> = ({ <> @@ -95,20 +107,20 @@ const Controls: FC> = ({ )} {showFitView && ( )} {showInteractive && ( {isInteractive ? : } diff --git a/packages/core/src/additional-components/Controls/Icons/FitView.tsx b/packages/controls/src/Icons/FitView.tsx similarity index 100% rename from packages/core/src/additional-components/Controls/Icons/FitView.tsx rename to packages/controls/src/Icons/FitView.tsx diff --git a/packages/core/src/additional-components/Controls/Icons/Lock.tsx b/packages/controls/src/Icons/Lock.tsx similarity index 100% rename from packages/core/src/additional-components/Controls/Icons/Lock.tsx rename to packages/controls/src/Icons/Lock.tsx diff --git a/packages/core/src/additional-components/Controls/Icons/Minus.tsx b/packages/controls/src/Icons/Minus.tsx similarity index 100% rename from packages/core/src/additional-components/Controls/Icons/Minus.tsx rename to packages/controls/src/Icons/Minus.tsx diff --git a/packages/core/src/additional-components/Controls/Icons/Plus.tsx b/packages/controls/src/Icons/Plus.tsx similarity index 100% rename from packages/core/src/additional-components/Controls/Icons/Plus.tsx rename to packages/controls/src/Icons/Plus.tsx diff --git a/packages/core/src/additional-components/Controls/Icons/Unlock.tsx b/packages/controls/src/Icons/Unlock.tsx similarity index 100% rename from packages/core/src/additional-components/Controls/Icons/Unlock.tsx rename to packages/controls/src/Icons/Unlock.tsx diff --git a/packages/controls/src/index.tsx b/packages/controls/src/index.tsx new file mode 100644 index 00000000..fef34d91 --- /dev/null +++ b/packages/controls/src/index.tsx @@ -0,0 +1,2 @@ +export { default as default } from './Controls'; +export * from './types'; diff --git a/packages/controls/src/types.ts b/packages/controls/src/types.ts new file mode 100644 index 00000000..84c8e8c8 --- /dev/null +++ b/packages/controls/src/types.ts @@ -0,0 +1,16 @@ +import { ButtonHTMLAttributes, HTMLAttributes } from 'react'; +import { FitViewOptions } from '@react-flow/core'; + +export interface ControlProps extends HTMLAttributes { + showZoom?: boolean; + showFitView?: boolean; + showInteractive?: boolean; + fitViewOptions?: FitViewOptions; + onZoomIn?: () => void; + onZoomOut?: () => void; + onFitView?: () => void; + onInteractiveChange?: (interactiveStatus: boolean) => void; +} + +export interface ControlButtonProps + extends ButtonHTMLAttributes {} diff --git a/packages/core/src/additional-components/index.ts b/packages/core/src/additional-components/index.ts deleted file mode 100644 index 4d69af86..00000000 --- a/packages/core/src/additional-components/index.ts +++ /dev/null @@ -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'; diff --git a/packages/core/src/additional-components/ReactFlowProvider/index.tsx b/packages/core/src/components/ReactFlowProvider/index.tsx similarity index 100% rename from packages/core/src/additional-components/ReactFlowProvider/index.tsx rename to packages/core/src/components/ReactFlowProvider/index.tsx diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 8ad297aa..48178bb7 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -17,7 +17,6 @@ export { getSimpleBezierCenter as getSimpleBezierEdgeCenter, } from './components/Edges/SimpleBezierEdge'; export { default as SmoothStepEdge, getSmoothStepPath } from './components/Edges/SmoothStepEdge'; -export * from './additional-components'; export { internalsSymbol } from './utils'; export { @@ -33,6 +32,7 @@ export { } from './utils/graph'; export { applyNodeChanges, applyEdgeChanges } from './utils/changes'; 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 useUpdateNodeInternals } from './hooks/useUpdateNodeInternals'; diff --git a/packages/core/src/types/component-props.ts b/packages/core/src/types/component-props.ts index 2ee5f045..425cbba8 100644 --- a/packages/core/src/types/component-props.ts +++ b/packages/core/src/types/component-props.ts @@ -134,16 +134,3 @@ export interface ReactFlowProps extends HTMLAttributes { } export type ReactFlowRefType = HTMLDivElement; - -export interface ControlProps extends HTMLAttributes { - showZoom?: boolean; - showFitView?: boolean; - showInteractive?: boolean; - fitViewOptions?: FitViewOptions; - onZoomIn?: () => void; - onZoomOut?: () => void; - onFitView?: () => void; - onInteractiveChange?: (interactiveStatus: boolean) => void; -} - -export interface ControlButtonProps extends ButtonHTMLAttributes {}