Skip to content

Commit 1bf419e

Browse files
committed
fix: 🐛 MiniMap is observing content resizing
1 parent cb7d5ee commit 1bf419e

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

src/components/mini-map/mini-map.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
useTransformEffect,
77
useTransformInit,
88
} from "hooks";
9+
import { useResize } from "./use-resize.hook";
910

1011
export type MiniMapProps = {
1112
width?: number;
@@ -131,6 +132,10 @@ export const MiniMap: React.FC<MiniMapProps> = ({
131132
initialize();
132133
});
133134

135+
useResize(instance.contentComponent, () => {
136+
initialize();
137+
});
138+
134139
const wrapperStyle = useMemo(() => {
135140
return {
136141
...size,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { useEffect, useRef } from "react";
2+
3+
export type NonNullableKeys<T> = {
4+
[P in keyof T]-?: NonNullable<T[P]>;
5+
};
6+
7+
export type Nullable<T> = T | null;
8+
9+
export type ElementRect = Omit<DOMRect, "toJSON">;
10+
11+
const initialElementRect: ElementRect = {
12+
width: 0,
13+
height: 0,
14+
y: 0,
15+
x: 0,
16+
top: 0,
17+
bottom: 0,
18+
left: 0,
19+
right: 0,
20+
};
21+
22+
type ResizeHandler<T extends HTMLElement> = (
23+
elementRect: ElementRect,
24+
element: T,
25+
) => void;
26+
27+
export const useResize = <T extends HTMLElement>(
28+
ref: Nullable<T>,
29+
onResize: ResizeHandler<T>,
30+
) => {
31+
const resizeObserverRef = useRef<ResizeObserver>();
32+
33+
const rectRef = useRef(initialElementRect);
34+
35+
const didUnmount = useRef(false);
36+
37+
useEffect(() => {
38+
didUnmount.current = false;
39+
if (ref) {
40+
resizeObserverRef.current = new ResizeObserver(
41+
(entries: ResizeObserverEntry[]) => {
42+
const newSize = ref.getBoundingClientRect();
43+
if (
44+
!Array.isArray(entries) ||
45+
!entries.length ||
46+
didUnmount.current ||
47+
newSize.width === rectRef.current.width ||
48+
newSize.height === rectRef.current.height
49+
)
50+
return;
51+
52+
onResize(newSize, ref);
53+
rectRef.current = newSize;
54+
},
55+
);
56+
57+
resizeObserverRef.current?.observe(ref);
58+
}
59+
60+
return () => {
61+
didUnmount.current = true;
62+
if (ref) {
63+
resizeObserverRef.current?.unobserve(ref);
64+
}
65+
};
66+
}, [onResize, ref]);
67+
};

src/hooks/use-transform-init.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ export const useTransformInit = (
1313
let unmountCallback: void | (() => void);
1414
let unmount: void | (() => void);
1515
if (libraryContext.contentComponent && libraryContext.wrapperComponent) {
16-
console.warn(333);
1716
unmountCallback = callback(getState(libraryContext));
1817
} else {
19-
console.warn(444);
2018
unmount = libraryContext.onInit((ref) => {
2119
unmountCallback = callback(getState(ref.instance));
2220
});

0 commit comments

Comments
 (0)