From f6f3202b571555cd516704155ea7b7dd86aefa22 Mon Sep 17 00:00:00 2001 From: moklick Date: Sun, 2 Aug 2020 18:34:03 +0200 Subject: [PATCH] refactor(graphview): put resize handling in its own hook --- example/src/index.css | 2 +- src/container/GraphView/index.tsx | 43 ++----------------------------- src/hooks/useResizeHandler.ts | 43 +++++++++++++++++++++++++++++++ src/store/index.ts | 6 +++-- 4 files changed, 50 insertions(+), 44 deletions(-) create mode 100644 src/hooks/useResizeHandler.ts diff --git a/example/src/index.css b/example/src/index.css index 657a6763..c9324205 100644 --- a/example/src/index.css +++ b/example/src/index.css @@ -176,4 +176,4 @@ nav a.active:before { .overview-example__add { display: block; } -} +} \ No newline at end of file diff --git a/src/container/GraphView/index.tsx b/src/container/GraphView/index.tsx index 9ebd9767..02ce5dcb 100644 --- a/src/container/GraphView/index.tsx +++ b/src/container/GraphView/index.tsx @@ -1,5 +1,4 @@ import React, { useEffect, useRef, memo, CSSProperties, MouseEvent } from 'react'; -import { ResizeObserver } from 'resize-observer'; import { useStoreState, useStoreActions } from '../../store/hooks'; import NodeRenderer from '../NodeRenderer'; @@ -10,7 +9,7 @@ import useKeyPress from '../../hooks/useKeyPress'; import useD3Zoom from '../../hooks/useD3Zoom'; import useGlobalKeyHandler from '../../hooks/useGlobalKeyHandler'; import useElementUpdater from '../../hooks/useElementUpdater'; -import { getDimensions } from '../../utils'; +import useResizeHandler from '../../hooks/useResizeHandler'; import { project, getElements } from '../../utils/graph'; import { Elements, @@ -114,7 +113,6 @@ const GraphView = ({ const height = useStoreState((s) => s.height); const d3Initialised = useStoreState((s) => s.d3Initialised); const nodesSelectionActive = useStoreState((s) => s.nodesSelectionActive); - const updateSize = useStoreActions((actions) => actions.updateSize); const setNodesSelection = useStoreActions((actions) => actions.setNodesSelection); const setOnConnect = useStoreActions((a) => a.setOnConnect); const setOnConnectStart = useStoreActions((a) => a.setOnConnectStart); @@ -135,44 +133,7 @@ const GraphView = ({ setNodesSelection({ isActive: false }); }; - const updateDimensions = () => { - if (!rendererNode.current) { - return; - } - - const size = getDimensions(rendererNode.current); - - if (size.height === 0 || size.width === 0) { - throw new Error('The React Flow parent container needs a width and a height to render the graph.'); - } - - updateSize(size); - }; - - useEffect(() => { - let resizeObserver: ResizeObserver; - - updateDimensions(); - window.onresize = updateDimensions; - - if (rendererNode.current) { - resizeObserver = new ResizeObserver((entries) => { - for (let _ of entries) { - updateDimensions(); - } - }); - - resizeObserver.observe(rendererNode.current); - } - - return () => { - window.onresize = null; - - if (resizeObserver && rendererNode.current) { - resizeObserver.unobserve(rendererNode.current!); - } - }; - }, []); + useResizeHandler(rendererNode); useD3Zoom({ zoomPane, diff --git a/src/hooks/useResizeHandler.ts b/src/hooks/useResizeHandler.ts new file mode 100644 index 00000000..93c4ca9e --- /dev/null +++ b/src/hooks/useResizeHandler.ts @@ -0,0 +1,43 @@ +import { useEffect, MutableRefObject } from 'react'; +import { ResizeObserver } from 'resize-observer'; +import { useStoreActions } from '../store/hooks'; + +import { getDimensions } from '../utils'; + +export default (rendererNode: MutableRefObject) => { + const updateSize = useStoreActions((actions) => actions.updateSize); + + useEffect(() => { + let resizeObserver: ResizeObserver; + + const updateDimensions = () => { + if (!rendererNode.current) { + return; + } + + const size = getDimensions(rendererNode.current); + + if (size.height === 0 || size.width === 0) { + console.warn('The React Flow parent container needs a width and a height to render the graph.'); + } + + updateSize(size); + }; + + updateDimensions(); + window.onresize = updateDimensions; + + if (rendererNode.current) { + resizeObserver = new ResizeObserver(() => updateDimensions()); + resizeObserver.observe(rendererNode.current); + } + + return () => { + window.onresize = null; + + if (resizeObserver && rendererNode.current) { + resizeObserver.unobserve(rendererNode.current!); + } + }; + }, []); +}; diff --git a/src/store/index.ts b/src/store/index.ts index be995188..d70802ec 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -365,8 +365,10 @@ export const storeModel: StoreModel = { }), updateSize: action((state, size) => { - state.width = size.width; - state.height = size.height; + // when parent has no size we use these default values + // so that the calculations don't throw any errors + state.width = size.width || 500; + state.height = size.height || 500; }), initD3: action((state, zoomPaneNode) => {