Skip to content

Commit f483419

Browse files
authored
Merge pull request #57 from retejs/fix-minimap-size
fix: minimap size
2 parents 70a39e2 + 52ed15f commit f483419

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

package-lock.json

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@
4949
},
5050
"dependencies": {
5151
"@babel/runtime": "^7.21.0",
52-
"usehooks-ts": "^2.9.1"
52+
"usehooks-ts": "^3.1.0"
5353
}
5454
}

src/presets/minimap/components/Minimap.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react'
22
import { useCallback, useRef } from 'react'
33
import styled from 'styled-components'
4-
import { useElementSize } from 'usehooks-ts'
4+
import { useResizeObserver } from 'usehooks-ts'
55

66
import { Rect, Transform, Translate } from '../types'
77
import { px } from '../utils'
@@ -31,13 +31,12 @@ type Props = {
3131
}
3232

3333
export 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

Comments
 (0)