1717 */
1818
1919import * as React from 'react' ;
20- import { FC } from 'react' ;
20+ import { FC , useEffect , useRef , useState } from 'react' ;
2121import { ActionURL } from '@labkey/api' ;
2222import { RoomItemStringType } from '../../types/typings' ;
2323import { ReactSVG } from 'react-svg' ;
@@ -28,16 +28,49 @@ interface RoomItemTemplateProps {
2828}
2929export const RoomItemTemplate : FC < RoomItemTemplateProps > = ( props ) => {
3030 const { fileName, className} = props ;
31+ const imgRef = useRef ( null ) ;
32+ const [ width , setWidth ] = useState < string > ( "100%" ) ;
33+ const [ height , setHeight ] = useState < string > ( "100%" ) ;
34+
35+ // Effect reloads the svg to change the height and width of the wrapper for the requested svg after it is injected.
36+ // This ensures that it doesn't have a lot of empty space in between the svgs
37+ useEffect ( ( ) => {
38+ if ( ! imgRef . current ) return ;
39+ // Wait briefly for the nested SVG to render (adjust delay if needed)
40+ const timer = setTimeout ( ( ) => {
41+ const nestedSvg = imgRef . current ?. reactWrapper . children [ 0 ] . children [ 0 ] ;
42+ if ( ! nestedSvg ) return ;
43+
44+ // Method 1: Use explicit width/height (if nested SVG has them)
45+ const tempWidth = nestedSvg . getAttribute ( "width" ) ;
46+ const tempHeight = nestedSvg . getAttribute ( "height" ) ;
47+
48+ if ( tempWidth && tempHeight ) {
49+ setWidth ( tempWidth ) ;
50+ setHeight ( tempHeight ) ;
51+ }
52+ // Method 2: Fallback to rendered dimensions
53+ else {
54+ const rect = nestedSvg . getBoundingClientRect ( ) ;
55+ setWidth ( rect . width . toString ( ) ) ;
56+ setHeight ( rect . height . toString ( ) ) ;
57+ }
58+ } , 100 ) ; // Short delay to ensure rendering
59+
60+ return ( ) => clearTimeout ( timer ) ;
61+ } , [ ] ) ; // Empty dependency array = runs once after mount
62+
3163
3264 return (
3365 < div id = { `${ fileName } -template` } >
3466 < ReactSVG
3567 src = { `${ ActionURL . getContextPath ( ) } /cageui/static/${ fileName } .svg` }
3668 id = { `${ fileName } _template_wrapper` }
3769 wrapper = { 'svg' }
38- className = { className }
39- width = { '250' }
40- height = { '250' }
70+ ref = { imgRef }
71+ height = { height }
72+ width = { width }
73+ className = { className + " util-svg-template-wrapper" }
4174 />
4275 </ div >
4376 ) ;
0 commit comments