11import * as React from 'react'
22import { useCallback , useRef } from 'react'
33import styled from 'styled-components'
4- import { useElementSize } from 'usehooks-ts'
4+ import { useResizeObserver } from 'usehooks-ts'
55
66import { Rect , Transform , Translate } from '../types'
77import { px } from '../utils'
@@ -31,13 +31,12 @@ type Props = {
3131}
3232
3333export function Minimap ( props : Props ) {
34- const container = useRef < HTMLElement | null > ( )
35- const [ containerRef , { width : containerWidth } ] = useElementSize ( )
36- const scale = ( v : number ) => v * containerWidth
37- const ref = useCallback ( ( node : HTMLDivElement | null ) => {
38- container . current = node
39- containerRef ( node )
40- } , [ containerRef ] )
34+ const ref = useRef < HTMLDivElement > ( null )
35+ const { width = 0 } = useResizeObserver ( {
36+ ref
37+ } )
38+ const containerWidth = ref . current ?. clientWidth || width
39+ const scale = useCallback ( ( v : number ) => v * containerWidth , [ containerWidth ] )
4140
4241 return < Styles
4342 size = { props . size }
@@ -52,8 +51,8 @@ export function Minimap(props: Props) {
5251 onDoubleClick = { e => {
5352 e . stopPropagation ( )
5453 e . preventDefault ( )
55- if ( ! container . current ) return
56- const box = container . current . getBoundingClientRect ( )
54+ if ( ! ref . current ) return
55+ const box = ref . current . getBoundingClientRect ( )
5756 const x = ( e . clientX - box . left ) / ( props . size * props . ratio )
5857 const y = ( e . clientY - box . top ) / ( props . size * props . ratio )
5958
@@ -62,15 +61,15 @@ export function Minimap(props: Props) {
6261 ref = { ref }
6362 data-testid = "minimap"
6463 >
65- { containerWidth && props . nodes . map ( ( node , i ) => (
64+ { containerWidth ? props . nodes . map ( ( node , i ) => (
6665 < MiniNode
6766 key = { i }
6867 left = { scale ( node . left ) }
6968 top = { scale ( node . top ) }
7069 width = { scale ( node . width ) }
7170 height = { scale ( node . height ) }
7271 />
73- ) ) }
72+ ) ) : null }
7473 < MiniViewport
7574 { ...props . viewport }
7675 start = { props . start }
0 commit comments