Skip to content

Commit

Permalink
Merge pull request #57 from retejs/fix-minimap-size
Browse files Browse the repository at this point in the history
fix: minimap size
  • Loading branch information
Ni55aN authored Aug 25, 2024
2 parents 70a39e2 + 52ed15f commit f483419
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@
},
"dependencies": {
"@babel/runtime": "^7.21.0",
"usehooks-ts": "^2.9.1"
"usehooks-ts": "^3.1.0"
}
}
23 changes: 11 additions & 12 deletions src/presets/minimap/components/Minimap.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import { useCallback, useRef } from 'react'
import styled from 'styled-components'
import { useElementSize } from 'usehooks-ts'
import { useResizeObserver } from 'usehooks-ts'

import { Rect, Transform, Translate } from '../types'
import { px } from '../utils'
Expand Down Expand Up @@ -31,13 +31,12 @@ type Props = {
}

export function Minimap(props: Props) {
const container = useRef<HTMLElement | null>()
const [containerRef, { width: containerWidth }] = useElementSize()
const scale = (v: number) => v * containerWidth
const ref = useCallback((node: HTMLDivElement | null) => {
container.current = node
containerRef(node)
}, [containerRef])
const ref = useRef<HTMLDivElement>(null)
const { width = 0 } = useResizeObserver({
ref
})
const containerWidth = ref.current?.clientWidth || width
const scale = useCallback((v: number) => v * containerWidth, [containerWidth])

return <Styles
size={props.size}
Expand All @@ -52,8 +51,8 @@ export function Minimap(props: Props) {
onDoubleClick={e => {
e.stopPropagation()
e.preventDefault()
if (!container.current) return
const box = container.current.getBoundingClientRect()
if (!ref.current) return
const box = ref.current.getBoundingClientRect()
const x = (e.clientX - box.left) / (props.size * props.ratio)
const y = (e.clientY - box.top) / (props.size * props.ratio)

Expand All @@ -62,15 +61,15 @@ export function Minimap(props: Props) {
ref={ref}
data-testid="minimap"
>
{containerWidth && props.nodes.map((node, i) => (
{containerWidth ? props.nodes.map((node, i) => (
<MiniNode
key={i}
left={scale(node.left)}
top={scale(node.top)}
width={scale(node.width)}
height={scale(node.height)}
/>
))}
)) : null}
<MiniViewport
{...props.viewport}
start={props.start}
Expand Down

0 comments on commit f483419

Please sign in to comment.