Skip to content

Commit 92c97f5

Browse files
Merge pull request #24 from mezh-hq/fix/text-rotate-platform-issues
Fix: text rotation issue on ios devices
2 parents f5334bd + 5643a84 commit 92c97f5

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

bun.lockb

-8 Bytes
Binary file not shown.

src/components/workspace/elements/text.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { forwardRef } from "react";
22
import { twMerge } from "tailwind-merge";
33
import { dataAttributes } from "@/constants";
4+
import { useRotationAttributes } from "@/hooks/rotation";
45
import { ISTKProps, IText } from "@/types";
56

67
export const textFontSize = 35;
@@ -36,6 +37,7 @@ const Text: React.FC<ITextProps> = forwardRef(
3637
},
3738
ref: any
3839
) => {
40+
const rotationAttrs = useRotationAttributes(ref, rotation);
3941
return (
4042
<text
4143
ref={ref}
@@ -52,9 +54,9 @@ const Text: React.FC<ITextProps> = forwardRef(
5254
consumer.styles?.elements?.text?.base?.className,
5355
consumer.mode === "user" && "!pointer-events-none"
5456
)}
57+
{...rotationAttrs.svg}
5558
style={{
56-
transform: `rotate(${rotation ?? 0}deg)`,
57-
transformOrigin: "center",
59+
...rotationAttrs.css,
5860
...consumer.styles?.elements?.text?.base?.properties,
5961
stroke: color,
6062
color

src/constants/user-agent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
2+
export const isMobileSafari = /iP(ad|hone|od).+Version\/[\d.]+.*Safari/i.test(navigator.userAgent);

src/hooks/rotation.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useLayoutEffect, useState } from "react";
2+
import { isMobileSafari, isSafari } from "@/constants/user-agent";
3+
4+
/** Constructs dynamic svg rotation attributes to overcome partial CSS support in Safari */
5+
export const useRotationAttributes = (ref: React.RefObject<SVGGraphicsElement>, rotation?: number) => {
6+
const [center, setCenter] = useState({ x: 0, y: 0 });
7+
8+
useLayoutEffect(() => {
9+
if (ref.current) {
10+
const bbox = ref.current.getBBox();
11+
setCenter({
12+
x: bbox.x + (isMobileSafari ? bbox.width / 2 : 0),
13+
y: bbox.y + bbox.height / 2
14+
});
15+
}
16+
}, [ref]);
17+
18+
return {
19+
svg: {
20+
transform: isSafari ? `rotate(${rotation ?? 0}, ${center.x}, ${center.y})` : undefined
21+
},
22+
css: {
23+
transform: isSafari ? undefined : `rotate(${rotation ?? 0}deg)`,
24+
transformOrigin: "center"
25+
}
26+
};
27+
};
28+
29+
export default useRotationAttributes;

0 commit comments

Comments
 (0)