chore(react): cleanup exports

This commit is contained in:
moklick
2024-01-08 11:59:01 +01:00
parent 1efd3ee3d5
commit 30c975e39f
26 changed files with 64 additions and 72 deletions
+7
View File
@@ -1,5 +1,12 @@
# @xyflow/react # @xyflow/react
## 12.0.0-next.4
### Minor changes
- fix applyChanges: handle empty flows + addNodes/addEdges closes
- cleanup hook exports
## 12.0.0-next.3 ## 12.0.0-next.3
### Minor changes ### Minor changes
@@ -3,7 +3,7 @@ import cc from 'classcat';
import { shallow } from 'zustand/shallow'; import { shallow } from 'zustand/shallow';
import { useStore, useStoreApi } from '../../hooks/useStore'; import { useStore, useStoreApi } from '../../hooks/useStore';
import useReactFlow from '../../hooks/useReactFlow'; import { useReactFlow } from '../../hooks/useReactFlow';
import Panel from '../../components/Panel'; import Panel from '../../components/Panel';
import { type ReactFlowState } from '../../types'; import { type ReactFlowState } from '../../types';
@@ -1,5 +1,6 @@
import { useEffect, useRef, memo, type MouseEvent, type KeyboardEvent } from 'react'; import { useEffect, useRef, memo, type MouseEvent, type KeyboardEvent } from 'react';
import cc from 'classcat'; import cc from 'classcat';
import { shallow } from 'zustand/shallow';
import { import {
clampPosition, clampPosition,
elementSelectionKeys, elementSelectionKeys,
@@ -12,12 +13,11 @@ import {
import { useStore, useStoreApi } from '../../hooks/useStore'; import { useStore, useStoreApi } from '../../hooks/useStore';
import { Provider } from '../../contexts/NodeIdContext'; import { Provider } from '../../contexts/NodeIdContext';
import { ARIA_NODE_DESC_KEY } from '../A11yDescriptions'; import { ARIA_NODE_DESC_KEY } from '../A11yDescriptions';
import useDrag from '../../hooks/useDrag'; import { useDrag } from '../../hooks/useDrag';
import useUpdateNodePositions from '../../hooks/useUpdateNodePositions'; import { useUpdateNodePositions } from '../../hooks/useUpdateNodePositions';
import { handleNodeClick } from '../Nodes/utils'; import { handleNodeClick } from '../Nodes/utils';
import type { NodeWrapperProps } from '../../types';
import { arrowKeyDiffs, builtinNodeTypes } from './utils'; import { arrowKeyDiffs, builtinNodeTypes } from './utils';
import { shallow } from 'zustand/shallow'; import type { NodeWrapperProps } from '../../types';
const NodeWrapper = ({ const NodeWrapper = ({
id, id,
@@ -9,10 +9,10 @@ import { shallow } from 'zustand/shallow';
import { getNodesBounds } from '@xyflow/system'; import { getNodesBounds } from '@xyflow/system';
import { useStore, useStoreApi } from '../../hooks/useStore'; import { useStore, useStoreApi } from '../../hooks/useStore';
import useDrag from '../../hooks/useDrag'; import { useDrag } from '../../hooks/useDrag';
import useUpdateNodePositions from '../../hooks/useUpdateNodePositions'; import { useUpdateNodePositions } from '../../hooks/useUpdateNodePositions';
import type { Node, ReactFlowState } from '../../types';
import { arrowKeyDiffs } from '../NodeWrapper/utils'; import { arrowKeyDiffs } from '../NodeWrapper/utils';
import type { Node, ReactFlowState } from '../../types';
export type NodesSelectionProps = { export type NodesSelectionProps = {
onSelectionContextMenu?: (event: MouseEvent, nodes: Node[]) => void; onSelectionContextMenu?: (event: MouseEvent, nodes: Node[]) => void;
@@ -1,8 +1,8 @@
import { memo, type ReactNode } from 'react'; import { memo, type ReactNode } from 'react';
import { useStore } from '../../hooks/useStore'; import { useStore } from '../../hooks/useStore';
import useGlobalKeyHandler from '../../hooks/useGlobalKeyHandler'; import { useGlobalKeyHandler } from '../../hooks/useGlobalKeyHandler';
import useKeyPress from '../../hooks/useKeyPress'; import { useKeyPress } from '../../hooks/useKeyPress';
import { GraphViewProps } from '../GraphView'; import { GraphViewProps } from '../GraphView';
import ZoomPane from '../ZoomPane'; import ZoomPane from '../ZoomPane';
import Pane from '../Pane'; import Pane from '../Pane';
@@ -4,11 +4,11 @@ import FlowRenderer from '../FlowRenderer';
import NodeRenderer from '../NodeRenderer'; import NodeRenderer from '../NodeRenderer';
import EdgeRenderer from '../EdgeRenderer'; import EdgeRenderer from '../EdgeRenderer';
import ViewportWrapper from '../Viewport'; import ViewportWrapper from '../Viewport';
import useOnInitHandler from '../../hooks/useOnInitHandler'; import { useOnInitHandler } from '../../hooks/useOnInitHandler';
import useViewportSync from '../../hooks/useViewportSync'; import { useViewportSync } from '../../hooks/useViewportSync';
import ConnectionLine from '../../components/ConnectionLine'; import ConnectionLine from '../../components/ConnectionLine';
import type { ReactFlowProps } from '../../types'; import type { ReactFlowProps } from '../../types';
import useNodeOrEdgeTypesWarning from './useNodeOrEdgeTypesWarning'; import { useNodeOrEdgeTypesWarning } from './useNodeOrEdgeTypesWarning';
export type GraphViewProps = Omit< export type GraphViewProps = Omit<
ReactFlowProps, ReactFlowProps,
@@ -12,7 +12,7 @@ const emptyTypes = {};
export function useNodeOrEdgeTypesWarning(nodeOrEdgeTypes?: NodeTypes): void; export function useNodeOrEdgeTypesWarning(nodeOrEdgeTypes?: NodeTypes): void;
export function useNodeOrEdgeTypesWarning(nodeOrEdgeTypes?: EdgeTypes): void; export function useNodeOrEdgeTypesWarning(nodeOrEdgeTypes?: EdgeTypes): void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function useNodeOrEdgeTypesWarning(nodeOrEdgeTypes: any = emptyTypes): any { export function useNodeOrEdgeTypesWarning(nodeOrEdgeTypes: any = emptyTypes): any {
const updateCount = useRef(0); const updateCount = useRef(0);
const store = useStoreApi(); const store = useStoreApi();
@@ -15,10 +15,10 @@ import Attribution from '../../components/Attribution';
import SelectionListener from '../../components/SelectionListener'; import SelectionListener from '../../components/SelectionListener';
import StoreUpdater from '../../components/StoreUpdater'; import StoreUpdater from '../../components/StoreUpdater';
import A11yDescriptions from '../../components/A11yDescriptions'; import A11yDescriptions from '../../components/A11yDescriptions';
import { useColorModeClass } from '../../hooks/useColorModeClass';
import GraphView from '../GraphView'; import GraphView from '../GraphView';
import Wrapper from './Wrapper'; import Wrapper from './Wrapper';
import type { ReactFlowProps, ReactFlowRefType } from '../../types'; import type { ReactFlowProps, ReactFlowRefType } from '../../types';
import useColorModeClass from '../../hooks/useColorModeClass';
export const initNodeOrigin: NodeOrigin = [0, 0]; export const initNodeOrigin: NodeOrigin = [0, 0];
const initDefaultViewport: Viewport = { x: 0, y: 0, zoom: 1 }; const initDefaultViewport: Viewport = { x: 0, y: 0, zoom: 1 };
@@ -3,8 +3,8 @@ import { useEffect, useRef } from 'react';
import { shallow } from 'zustand/shallow'; import { shallow } from 'zustand/shallow';
import { XYPanZoom, PanOnScrollMode, type Transform, type PanZoomInstance } from '@xyflow/system'; import { XYPanZoom, PanOnScrollMode, type Transform, type PanZoomInstance } from '@xyflow/system';
import useKeyPress from '../../hooks/useKeyPress'; import { useKeyPress } from '../../hooks/useKeyPress';
import useResizeHandler from '../../hooks/useResizeHandler'; import { useResizeHandler } from '../../hooks/useResizeHandler';
import { useStore, useStoreApi } from '../../hooks/useStore'; import { useStore, useStoreApi } from '../../hooks/useStore';
import { containerStyle } from '../../styles/utils'; import { containerStyle } from '../../styles/utils';
import type { FlowRendererProps } from '../FlowRenderer'; import type { FlowRendererProps } from '../FlowRenderer';
@@ -15,7 +15,7 @@ function getMediaQuery() {
* @internal * @internal
* @param colorMode - The color mode to use ('dark', 'light' or 'system') * @param colorMode - The color mode to use ('dark', 'light' or 'system')
*/ */
export default function useColorModeClass(colorMode: ColorMode): ColorModeClass { export function useColorModeClass(colorMode: ColorMode): ColorModeClass {
const [colorModeClass, setColorModeClass] = useState<ColorModeClass | null>( const [colorModeClass, setColorModeClass] = useState<ColorModeClass | null>(
colorMode === 'system' ? null : colorMode colorMode === 'system' ? null : colorMode
); );
+8 -3
View File
@@ -18,7 +18,14 @@ type UseDragParams = {
* *
* @internal * @internal
*/ */
function useDrag({ nodeRef, disabled = false, noDragClassName, handleSelector, nodeId, isSelectable }: UseDragParams) { export function useDrag({
nodeRef,
disabled = false,
noDragClassName,
handleSelector,
nodeId,
isSelectable,
}: UseDragParams) {
const store = useStoreApi(); const store = useStoreApi();
const [dragging, setDragging] = useState<boolean>(false); const [dragging, setDragging] = useState<boolean>(false);
const xyDrag = useRef<XYDragInstance>(); const xyDrag = useRef<XYDragInstance>();
@@ -64,5 +71,3 @@ function useDrag({ nodeRef, disabled = false, noDragClassName, handleSelector, n
return dragging; return dragging;
} }
export default useDrag;
+1 -3
View File
@@ -11,10 +11,8 @@ const edgesSelector = (state: ReactFlowState) => state.edges;
* @public * @public
* @returns An array of edges * @returns An array of edges
*/ */
function useEdges<EdgeData>(): Edge<EdgeData>[] { export function useEdges<EdgeData>(): Edge<EdgeData>[] {
const edges = useStore(edgesSelector, shallow); const edges = useStore(edgesSelector, shallow);
return edges; return edges;
} }
export default useEdges;
@@ -2,8 +2,8 @@ import { useEffect } from 'react';
import type { KeyCode } from '@xyflow/system'; import type { KeyCode } from '@xyflow/system';
import { useStoreApi } from '../hooks/useStore'; import { useStoreApi } from '../hooks/useStore';
import useKeyPress, { UseKeyPressOptions } from './useKeyPress'; import { useKeyPress, UseKeyPressOptions } from './useKeyPress';
import useReactFlow from './useReactFlow'; import { useReactFlow } from './useReactFlow';
import { Edge, Node } from '../types'; import { Edge, Node } from '../types';
const selected = (item: Node | Edge) => item.selected; const selected = (item: Node | Edge) => item.selected;
@@ -15,13 +15,13 @@ const deleteKeyOptions: UseKeyPressOptions = { actInsideInputWithModifier: false
* *
* @internal * @internal
*/ */
export default ({ export function useGlobalKeyHandler({
deleteKeyCode, deleteKeyCode,
multiSelectionKeyCode, multiSelectionKeyCode,
}: { }: {
deleteKeyCode: KeyCode | null; deleteKeyCode: KeyCode | null;
multiSelectionKeyCode: KeyCode | null; multiSelectionKeyCode: KeyCode | null;
}): void => { }): void {
const store = useStoreApi(); const store = useStoreApi();
const { deleteElements } = useReactFlow(); const { deleteElements } = useReactFlow();
@@ -39,4 +39,4 @@ export default ({
useEffect(() => { useEffect(() => {
store.setState({ multiSelectionActive: multiSelectionKeyPressed }); store.setState({ multiSelectionActive: multiSelectionKeyPressed });
}, [multiSelectionKeyPressed]); }, [multiSelectionKeyPressed]);
}; }
+3 -3
View File
@@ -20,14 +20,14 @@ const defaultDoc = typeof document !== 'undefined' ? document : null;
* @param param.options - Options * @param param.options - Options
* @returns boolean * @returns boolean
*/ */
export default ( export function useKeyPress(
// the keycode can be a string 'a' or an array of strings ['a', 'a+d'] // the keycode can be a string 'a' or an array of strings ['a', 'a+d']
// a string means a single key 'a' or a combination when '+' is used 'a+d' // a string means a single key 'a' or a combination when '+' is used 'a+d'
// an array means different possibilites. Explainer: ['a', 'd+s'] here the // an array means different possibilites. Explainer: ['a', 'd+s'] here the
// user can use the single key 'a' or the combination 'd' + 's' // user can use the single key 'a' or the combination 'd' + 's'
keyCode: KeyCode | null = null, keyCode: KeyCode | null = null,
options: UseKeyPressOptions = { target: defaultDoc, actInsideInputWithModifier: true } options: UseKeyPressOptions = { target: defaultDoc, actInsideInputWithModifier: true }
): boolean => { ): boolean {
const [keyPressed, setKeyPressed] = useState(false); const [keyPressed, setKeyPressed] = useState(false);
// we need to remember if a modifier key is pressed in order to track it // we need to remember if a modifier key is pressed in order to track it
@@ -113,7 +113,7 @@ export default (
}, [keyCode, setKeyPressed]); }, [keyCode, setKeyPressed]);
return keyPressed; return keyPressed;
}; }
// utils // utils
+1 -3
View File
@@ -11,10 +11,8 @@ const nodesSelector = (state: ReactFlowState) => state.nodes;
* @public * @public
* @returns An array of nodes * @returns An array of nodes
*/ */
function useNodes<NodeType extends Node = Node>(): NodeType[] { export function useNodes<NodeType extends Node = Node>(): NodeType[] {
const nodes = useStore(nodesSelector, shallow) as NodeType[]; const nodes = useStore(nodesSelector, shallow) as NodeType[];
return nodes; return nodes;
} }
export default useNodes;
@@ -28,10 +28,8 @@ const defaultOptions = {
* @param options.includeHiddenNodes - defaults to false * @param options.includeHiddenNodes - defaults to false
* @returns boolean indicating whether all nodes are initialized * @returns boolean indicating whether all nodes are initialized
*/ */
function useNodesInitialized(options: UseNodesInitializedOptions = defaultOptions): boolean { export function useNodesInitialized(options: UseNodesInitializedOptions = defaultOptions): boolean {
const initialized = useStore(selector(options)); const initialized = useStore(selector(options));
return initialized; return initialized;
} }
export default useNodesInitialized;
+2 -4
View File
@@ -1,6 +1,6 @@
import { useEffect, useRef } from 'react'; import { useEffect, useRef } from 'react';
import useReactFlow from './useReactFlow'; import { useReactFlow } from './useReactFlow';
import type { OnInit } from '../types'; import type { OnInit } from '../types';
/** /**
@@ -8,7 +8,7 @@ import type { OnInit } from '../types';
* *
* @internal * @internal
*/ */
function useOnInitHandler(onInit: OnInit | undefined) { export function useOnInitHandler(onInit: OnInit | undefined) {
const rfInstance = useReactFlow(); const rfInstance = useReactFlow();
const isInitialized = useRef<boolean>(false); const isInitialized = useRef<boolean>(false);
@@ -19,5 +19,3 @@ function useOnInitHandler(onInit: OnInit | undefined) {
} }
}, [onInit, rfInstance.viewportInitialized]); }, [onInit, rfInstance.viewportInitialized]);
} }
export default useOnInitHandler;
@@ -13,7 +13,7 @@ export type UseOnSelectionChangeOptions = {
* @public * @public
* @params params.onChange - The handler to register * @params params.onChange - The handler to register
*/ */
function useOnSelectionChange({ onChange }: UseOnSelectionChangeOptions) { export function useOnSelectionChange({ onChange }: UseOnSelectionChangeOptions) {
const store = useStoreApi(); const store = useStoreApi();
useEffect(() => { useEffect(() => {
@@ -26,5 +26,3 @@ function useOnSelectionChange({ onChange }: UseOnSelectionChangeOptions) {
}; };
}, [onChange]); }, [onChange]);
} }
export default useOnSelectionChange;
@@ -17,7 +17,7 @@ export type UseOnViewportChangeOptions = {
* @param params.onChange - gets called when the viewport changes * @param params.onChange - gets called when the viewport changes
* @param params.onEnd - gets called when the viewport stops changing * @param params.onEnd - gets called when the viewport stops changing
*/ */
function useOnViewportChange({ onStart, onChange, onEnd }: UseOnViewportChangeOptions) { export function useOnViewportChange({ onStart, onChange, onEnd }: UseOnViewportChangeOptions) {
const store = useStoreApi(); const store = useStoreApi();
useEffect(() => { useEffect(() => {
@@ -32,5 +32,3 @@ function useOnViewportChange({ onStart, onChange, onEnd }: UseOnViewportChangeOp
store.setState({ onViewportChangeEnd: onEnd }); store.setState({ onViewportChangeEnd: onEnd });
}, [onEnd]); }, [onEnd]);
} }
export default useOnViewportChange;
+1 -1
View File
@@ -23,7 +23,7 @@ import { isNode } from '../utils';
* @public * @public
* @returns ReactFlowInstance * @returns ReactFlowInstance
*/ */
export default function useReactFlow<NodeType extends Node = Node, EdgeType extends Edge = Edge>(): ReactFlowInstance< export function useReactFlow<NodeType extends Node = Node, EdgeType extends Edge = Edge>(): ReactFlowInstance<
NodeType, NodeType,
EdgeType EdgeType
> { > {
+1 -3
View File
@@ -8,7 +8,7 @@ import { useStoreApi } from '../hooks/useStore';
* *
* @internal * @internal
*/ */
function useResizeHandler(domNode: MutableRefObject<HTMLDivElement | null>): void { export function useResizeHandler(domNode: MutableRefObject<HTMLDivElement | null>): void {
const store = useStoreApi(); const store = useStoreApi();
useEffect(() => { useEffect(() => {
@@ -42,5 +42,3 @@ function useResizeHandler(domNode: MutableRefObject<HTMLDivElement | null>): voi
} }
}, []); }, []);
} }
export default useResizeHandler;
@@ -9,7 +9,7 @@ import { useStoreApi } from '../hooks/useStore';
* @public * @public
* @returns function for updating node internals * @returns function for updating node internals
*/ */
function useUpdateNodeInternals(): UpdateNodeInternals { export function useUpdateNodeInternals(): UpdateNodeInternals {
const store = useStoreApi(); const store = useStoreApi();
return useCallback<UpdateNodeInternals>((id: string | string[]) => { return useCallback<UpdateNodeInternals>((id: string | string[]) => {
@@ -28,5 +28,3 @@ function useUpdateNodeInternals(): UpdateNodeInternals {
requestAnimationFrame(() => updateNodeDimensions(updates)); requestAnimationFrame(() => updateNodeDimensions(updates));
}, []); }, []);
} }
export default useUpdateNodeInternals;
@@ -13,7 +13,7 @@ const selectedAndDraggable = (nodesDraggable: boolean) => (n: Node) =>
* @internal * @internal
* @returns function for updating node positions * @returns function for updating node positions
*/ */
function useUpdateNodePositions() { export function useUpdateNodePositions() {
const store = useStoreApi(); const store = useStoreApi();
const updatePositions = useCallback((params: { x: number; y: number; isShiftPressed: boolean }) => { const updatePositions = useCallback((params: { x: number; y: number; isShiftPressed: boolean }) => {
@@ -63,5 +63,3 @@ function useUpdateNodePositions() {
return updatePositions; return updatePositions;
} }
export default useUpdateNodePositions;
+1 -3
View File
@@ -16,10 +16,8 @@ const viewportSelector = (state: ReactFlowState) => ({
* @public * @public
* @returns The current viewport * @returns The current viewport
*/ */
function useViewport(): Viewport { export function useViewport(): Viewport {
const viewport = useStore(viewportSelector, shallow); const viewport = useStore(viewportSelector, shallow);
return viewport; return viewport;
} }
export default useViewport;
+1 -1
View File
@@ -12,7 +12,7 @@ const selector = (state: ReactFlowState) => state.panZoom?.syncViewport;
* @internal * @internal
* @param viewport * @param viewport
*/ */
export default function useViewportSync(viewport?: Viewport) { export function useViewportSync(viewport?: Viewport) {
const syncViewport = useStore(selector); const syncViewport = useStore(selector);
const store = useStoreApi(); const store = useStoreApi();
+10 -10
View File
@@ -12,17 +12,17 @@ export { default as Panel, type PanelProps } from './components/Panel';
export { default as EdgeLabelRenderer } from './components/EdgeLabelRenderer'; export { default as EdgeLabelRenderer } from './components/EdgeLabelRenderer';
export { default as ViewportPortal } from './components/ViewportPortal'; export { default as ViewportPortal } from './components/ViewportPortal';
export { default as useReactFlow } from './hooks/useReactFlow'; export { useReactFlow } from './hooks/useReactFlow';
export { default as useUpdateNodeInternals } from './hooks/useUpdateNodeInternals'; export { useUpdateNodeInternals } from './hooks/useUpdateNodeInternals';
export { default as useNodes } from './hooks/useNodes'; export { useNodes } from './hooks/useNodes';
export { default as useEdges } from './hooks/useEdges'; export { useEdges } from './hooks/useEdges';
export { default as useViewport } from './hooks/useViewport'; export { useViewport } from './hooks/useViewport';
export { default as useKeyPress } from './hooks/useKeyPress'; export { useKeyPress } from './hooks/useKeyPress';
export * from './hooks/useNodesEdgesState'; export { useNodesState, useEdgesState } from './hooks/useNodesEdgesState';
export { useStore, useStoreApi } from './hooks/useStore'; export { useStore, useStoreApi } from './hooks/useStore';
export { default as useOnViewportChange, type UseOnViewportChangeOptions } from './hooks/useOnViewportChange'; export { useOnViewportChange, type UseOnViewportChangeOptions } from './hooks/useOnViewportChange';
export { default as useOnSelectionChange, type UseOnSelectionChangeOptions } from './hooks/useOnSelectionChange'; export { useOnSelectionChange, type UseOnSelectionChangeOptions } from './hooks/useOnSelectionChange';
export { default as useNodesInitialized, type UseNodesInitializedOptions } from './hooks/useNodesInitialized'; export { useNodesInitialized, type UseNodesInitializedOptions } from './hooks/useNodesInitialized';
export { useHandleConnections } from './hooks/useHandleConnections'; export { useHandleConnections } from './hooks/useHandleConnections';
export { useNodesData } from './hooks/useNodesData'; export { useNodesData } from './hooks/useNodesData';
export { useConnection } from './hooks/useConnection'; export { useConnection } from './hooks/useConnection';