File tree Expand file tree Collapse file tree 4 files changed +35
-2
lines changed
components/workspace/elements Expand file tree Collapse file tree 4 files changed +35
-2
lines changed Original file line number Diff line number Diff line change 11import { forwardRef } from "react" ;
22import { twMerge } from "tailwind-merge" ;
33import { dataAttributes } from "@/constants" ;
4+ import { useRotationAttributes } from "@/hooks/rotation" ;
45import { ISTKProps , IText } from "@/types" ;
56
67export 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
Original file line number Diff line number Diff line change 1+ export const isSafari = / ^ ( (? ! c h r o m e | a n d r o i d ) .) * s a f a r i / i. test ( navigator . userAgent ) ;
2+ export const isMobileSafari = / i P ( a d | h o n e | o d ) .+ V e r s i o n \/ [ \d . ] + .* S a f a r i / i. test ( navigator . userAgent ) ;
Original file line number Diff line number Diff line change 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 ;
You can’t perform that action at this time.
0 commit comments