Skip to content

Commit 1b58541

Browse files
committed
fix: support individual transform #969
1 parent 710f019 commit 1b58541

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

packages/react-moveable/src/utils/getMatrixStackInfo.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getSVGMatrix, getBodyOffset, getAbsoluteMatrix,
1313
} from "../utils";
1414
import { getDocumentBody, getDocumentElement } from "@daybrush/utils";
15+
import { parseMat } from "css-to-mat";
1516

1617

1718
export function getShadowRoot(parentElement: HTMLElement | SVGElement) {
@@ -60,6 +61,8 @@ export function getMatrixStackInfo(
6061
const getStyle = getCachedStyle(el);
6162
const position = getStyle("position");
6263
const scale = getStyle("scale") as string;
64+
const rotate = getStyle("rotate") as string;
65+
const translate = getStyle("translate") as string;
6366
const transform = getElementTransform(el);
6467
const isFixed = position === "fixed";
6568
let matrix: number[] = convertCSStoMatrix(getTransformMatrix(transform));
@@ -203,13 +206,18 @@ export function getMatrixStackInfo(
203206
matrix: getAbsoluteMatrix(matrix, n, origin),
204207
});
205208

206-
if (scale && scale !== "1" && scale !== "none") {
207-
const [
208-
scaleX,
209-
scaleY = scaleX,
210-
] = scale.split(" ").map(scale => parseFloat(scale)) as number[];
211-
const scaleMatrix = createScaleMatrix([scaleX, scaleY], n);
209+
const individualTransforms: string[] = [];
212210

211+
if (translate && translate !== "0px" && translate !== "none") {
212+
individualTransforms.push(`translate(${translate.split(/\s+/).join(",")})`);
213+
}
214+
if (rotate && rotate !== "1" && rotate !== "none") {
215+
individualTransforms.push(`rotate(${rotate})`);
216+
}
217+
if (scale && scale !== "1" && scale !== "none") {
218+
individualTransforms.push(`scale(${scale.split(/\s+/).join(",")})`);
219+
}
220+
if (individualTransforms.length) {
213221
matrixes.push({
214222
type: "offset",
215223
target: el,
@@ -219,7 +227,7 @@ export function getMatrixStackInfo(
219227
matrixes.push({
220228
type: "target",
221229
target: el,
222-
matrix: getAbsoluteMatrix(scaleMatrix, n, origin),
230+
matrix: getAbsoluteMatrix(parseMat(individualTransforms), n, origin),
223231
});
224232
}
225233
if (hasOffset) {

packages/react-moveable/stories/99-Tests/Deafult.stories.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ export const TestsZoomedTarget = add("Test css zoomed target", {
121121
});
122122

123123

124-
export const TestsScaleTarget = add("Test css scale target", {
125-
app: require("./ReactScaleTargetApp").default,
126-
path: require.resolve("./ReactScaleTargetApp"),
124+
export const TestsTRSTarget = add("Test css translate & rotate & scale target", {
125+
app: require("./ReactTRSTargetApp").default,
126+
path: require.resolve("./ReactTRSTargetApp"),
127127
play: async ({ canvasElement }) => {
128128
await wait();
129129
const target = canvasElement.querySelector<HTMLElement>(".target")!;

packages/react-moveable/stories/99-Tests/ReactScaleTargetApp.tsx renamed to packages/react-moveable/stories/99-Tests/ReactTRSTargetApp.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ export default function App(props: Record<string, any>) {
1212
transform: "translate3d(0px, 0px, 100px)",
1313
}}>
1414
<div className="target" style={{
15+
translate: "100px 10px",
16+
rotate: "30deg",
1517
scale: "1 2",
16-
transform: "translate(0px, 0px)",
18+
transform: "translate(100px, 0px)",
1719
}}>Target</div>
1820
</div>
1921
<Moveable

0 commit comments

Comments
 (0)