diff --git a/.changeset/public-sites-admire.md b/.changeset/public-sites-admire.md new file mode 100644 index 0000000000..f5f3304f9f --- /dev/null +++ b/.changeset/public-sites-admire.md @@ -0,0 +1,6 @@ +--- +'@backstage/core-components': patch +'@backstage/plugin-catalog-graph': patch +--- + +Fixed DependencyGraph `svg` size not adapting to the container size diff --git a/packages/core-components/src/components/DependencyGraph/DependencyGraph.tsx b/packages/core-components/src/components/DependencyGraph/DependencyGraph.tsx index 411a0e2c57..df26bb86fd 100644 --- a/packages/core-components/src/components/DependencyGraph/DependencyGraph.tsx +++ b/packages/core-components/src/components/DependencyGraph/DependencyGraph.tsx @@ -22,6 +22,9 @@ import { useRef, useState, } from 'react'; +import useMeasure from 'react-use/esm/useMeasure'; +import classNames from 'classnames'; +import { once } from 'lodash'; import * as d3Zoom from 'd3-zoom'; import * as d3Selection from 'd3-selection'; import useTheme from '@material-ui/core/styles/useTheme'; @@ -41,11 +44,18 @@ import { useTranslationRef } from '@backstage/core-plugin-api/alpha'; import { coreComponentsTranslationRef } from '../../translation'; const useStyles = makeStyles((theme: Theme) => ({ + fullscreenButton: { + position: 'absolute', + right: 0, + }, root: { overflow: 'hidden', minHeight: '100%', minWidth: '100%', }, + fixedHeight: { + height: '100%', + }, fullscreen: { backgroundColor: theme.palette.background.paper, }, @@ -268,9 +278,11 @@ export function DependencyGraph( const maxWidth = Math.max(graphWidth, containerWidth); const maxHeight = Math.max(graphHeight, containerHeight); - const minHeight = Math.min(graphHeight, containerHeight); - const scalableHeight = fit === 'grow' ? maxHeight : minHeight; + const [_measureRef] = useMeasure(); + const measureRef = once(_measureRef); + + const scalableHeight = fit === 'grow' ? maxHeight : containerHeight; const containerRef = useMemo( () => @@ -278,6 +290,8 @@ export function DependencyGraph( if (!root) { return; } + measureRef(root); + // Set up zooming + panning const node: SVGSVGElement = root.querySelector( `svg#${DEPENDENCY_GRAPH_SVG}`, @@ -328,7 +342,7 @@ export function DependencyGraph( setContainerHeight(newContainerHeight); } }, 100), - [containerHeight, containerWidth, maxWidth, maxHeight, zoom], + [measureRef, containerHeight, containerWidth, maxWidth, maxHeight, zoom], ); const setNodesAndEdges = useCallback(() => { @@ -431,28 +445,43 @@ export function DependencyGraph( updateGraph, ]); - function setNode(id: string, node: Types.DependencyNode) { - graph.current.setNode(id, node); - updateGraph(); - return graph.current; - } + const setNode = useCallback( + (id: string, node: Types.DependencyNode) => { + graph.current.setNode(id, node); + updateGraph(); + return graph.current; + }, + [updateGraph], + ); - function setEdge(id: dagre.Edge, edge: Types.DependencyEdge) { - graph.current.setEdge(id, edge); - updateGraph(); - return graph.current; - } + const setEdge = useCallback( + (id: dagre.Edge, edge: Types.DependencyEdge) => { + graph.current.setEdge(id, edge); + updateGraph(); + return graph.current; + }, + [updateGraph], + ); return ( -
+
{allowFullscreen && ( { renderLabel={renderLabel || DefaultRenderLabel} direction={direction} className={classes.graph} + fit="contain" paddingX={theme.spacing(4)} paddingY={theme.spacing(4)} labelPosition={DependencyGraphTypes.LabelPosition.RIGHT}