Skip to content

[Web] Handle SVG wrapped with createAnimatedComponent #3379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/handlers/gestures/GestureDetector/Wrap.web.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import React, { forwardRef } from 'react';
import type { LegacyRef, PropsWithChildren } from 'react';
import { tagMessage } from '../../../utils';
import { isRNSVGNode } from '../../../web/utils';

export const Wrap = forwardRef<HTMLDivElement, PropsWithChildren<{}>>(
({ children }, ref) => {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const child: any = React.Children.only(children);

const isRNSVGNode =
Object.getPrototypeOf(child?.type)?.name === 'WebShape';

if (isRNSVGNode) {
if (isRNSVGNode(child)) {
const clone = React.cloneElement(
child,
{ ref },
Expand Down
18 changes: 15 additions & 3 deletions src/web/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function spherical2tilt(altitudeAngle: number, azimuthAngle: number) {
return { tiltX, tiltY };
}

const RNSVGElements = [
export const RNSVGElements = new Set([
'Circle',
'ClipPath',
'Ellipse',
Expand All @@ -254,7 +254,7 @@ const RNSVGElements = [
'Text',
'TextPath',
'Use',
];
]);

// This function helps us determine whether given node is SVGElement or not. In our implementation of
// findNodeHandle, we can encounter such element in 2 forms - SVG tag or ref to SVG Element. Since Gesture Handler
Expand All @@ -269,7 +269,19 @@ export function isRNSVGElement(viewRef: SVGRef | GestureHandlerRef) {
const componentClassName = Object.getPrototypeOf(viewRef).constructor.name;

return (
RNSVGElements.indexOf(componentClassName) >= 0 &&
RNSVGElements.has(componentClassName) &&
Object.hasOwn(viewRef, 'elementRef')
);
}

// This function checks if given node is SVGElement. Unlike the function above, this one
// operates on React Nodes, not DOM nodes.
//
// Second condition was introduced to handle case where SVG element was wrapped with
// `createAnimatedComponent` from Reanimated.
export function isRNSVGNode(node: any) {
return (
Object.getPrototypeOf(node?.type)?.name === 'WebShape' ||
RNSVGElements.has(node?.type?.displayName)
);
}
Loading