@@ -45,9 +45,13 @@ function useDimensions(
4545 const [ dimensions , setDimensions ] =
4646 React . useState < Dimensions > ( null )
4747
48+ // in case the element starts hidden https://github.com/code-hike/codehike/issues/372
49+ const [ visibility , markAsVisible ] = React . useState ( 0 )
50+
4851 const windowWidth = useWindowWidth ( )
4952 const prevLineRef = React . useRef < HTMLDivElement > ( null ! )
5053
54+ // the element to render before dimensions are calculated
5155 const { prevLongestLine, nextLongestLine, element } =
5256 React . useMemo ( ( ) => {
5357 const prevLongestLine = getLongestLine (
@@ -135,6 +139,7 @@ function useDimensions(
135139 prevLongestLine ,
136140 nextLongestLine ,
137141 minColumns ,
142+ visibility ,
138143 ]
139144
140145 useLayoutEffect ( ( ) => {
@@ -143,6 +148,21 @@ function useDimensions(
143148 const contentElement = pll ?. parentElement !
144149 const codeElement = contentElement . parentElement !
145150
151+ const { width } = codeElement . getBoundingClientRect ( )
152+ if ( ! width && visibility === 0 ) {
153+ const resizeObserver = new ResizeObserver (
154+ ( [ entry ] ) => {
155+ const { width } = entry . contentRect
156+ if ( width ) {
157+ resizeObserver . disconnect ( )
158+ markAsVisible ( 1 )
159+ }
160+ }
161+ )
162+ resizeObserver . observe ( codeElement )
163+ return ( ) => resizeObserver . disconnect ( )
164+ }
165+
146166 // TODO is it clientWidth or clientRect?
147167 const lineContentDiv = pll ?. querySelector (
148168 ":scope > div"
@@ -195,6 +215,7 @@ function useDimensions(
195215 }
196216 setDimensions ( d )
197217 }
218+ return ( ) => { }
198219 } , allDeps )
199220
200221 if (
0 commit comments