-
-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathkeep-scale.tsx
More file actions
34 lines (28 loc) · 936 Bytes
/
keep-scale.tsx
File metadata and controls
34 lines (28 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import React, { useContext, useEffect, useRef } from "react";
import { mergeRefs } from "utils/ref.utils";
import { Context } from "../transform-wrapper/transform-wrapper";
export const KeepScale = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>((props, ref) => {
const localRef = useRef<HTMLDivElement>(null);
const instance = useContext(Context);
useEffect(() => {
const applyScale = (scale: number) => {
if (localRef.current) {
const positionX = 0;
const positionY = 0;
localRef.current.style.transform = instance.handleTransformStyles(
positionX,
positionY,
1 / scale,
);
}
};
applyScale(instance.transformState.scale);
return instance.onChange((ctx) => {
applyScale(ctx.instance.transformState.scale);
});
}, [instance]);
return <div {...props} ref={mergeRefs([localRef, ref])} />;
});