Skip to content

Commit 8d42c32

Browse files
committed
feat: clamp floating map's size inside window
1 parent 801da18 commit 8d42c32

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/components/editor/floatingMap/FloatingMap.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { Button, Card, NonIdealState, Spinner } from '@blueprintjs/core'
22

33
import clsx from 'clsx'
4+
import { clamp } from 'lodash-es'
45
import { useCallback, useEffect, useState } from 'react'
56
import { createPortal } from 'react-dom'
67
import { Rnd, RndResizeCallback } from 'react-rnd'
8+
import { useWindowSize } from 'react-use'
79

810
import { sendMessage, useMessage } from '../../../utils/messenger'
911
import { useLazyStorage } from '../../../utils/useLazyStorage'
@@ -57,15 +59,15 @@ export function FloatingMap() {
5759
(savedValue, defaultValue) => ({ ...defaultValue, ...savedValue }),
5860
)
5961

62+
const { width: windowWidth, height: windowHeight } = useWindowSize()
63+
6064
useEffect(() => {
61-
if (config.show) {
62-
setConfig({
63-
...config,
64-
x: Math.max(0, Math.min(config.x, window.innerWidth - config.width)),
65-
y: Math.max(0, Math.min(config.y, window.innerHeight - config.height)),
66-
})
67-
}
68-
}, [config.show])
65+
setConfig((cfg) => ({
66+
...cfg,
67+
x: clamp(cfg.x, 0, windowWidth - cfg.width),
68+
y: clamp(cfg.y, 0, windowHeight - cfg.height),
69+
}))
70+
}, [windowWidth, windowHeight])
6971

7072
const [iframeWindow, setIframeWindow] = useState<Window | null | undefined>()
7173
const [mapStatus, setMapStatus] = useState(MapStatus.Loading)
@@ -162,6 +164,7 @@ export function FloatingMap() {
162164
lockAspectRatio={ASPECT_RATIO}
163165
lockAspectRatioExtraHeight={HEADER_HEIGHT}
164166
default={config}
167+
position={config}
165168
onDragStart={onDragStartHandler}
166169
onDragStop={onDragStopHandler}
167170
onResizeStart={onResizeStartHandler}

0 commit comments

Comments
 (0)