react-native-svg icon indicating copy to clipboard operation
react-native-svg copied to clipboard

ForeignObject onPress() event not firing

Open JoBerkner opened this issue 5 years ago • 13 comments

Bug

I created a line chart with each line being stored in a <G> container together with a label inside a <ForeignObject> (due to styling options). The code for one such plotted line looks like this:

<G>
<Path
    d={*linePath*}
    fill="transparent"
    stroke={"black"}
    strokeWidth={1}
/>
<ForeignObject
    x={*pathEndPosition*}
    y={*pathEndPosition*}
>
    <TouchableOpacity
    onPress={() => console.log("hello")}
    style={{
        position: "absolute",
        backgroundColor: "red",
        borderRadius: 3,
    }}
    >
    <Text
        style={{
        color: "black",
        fontSize: 10,
        }}
    >
        {*data.myLabel*}
    </Text>
    </TouchableOpacity>
</ForeignObject>
</G>

The label should be interactive, as it should open a <Modal> when pressed. Therefore, I wrapped the <Text> with a TouchableOpacity> component and added an onPress() event to it.

However, this event is not triggered inside the <ForeignObject> (also when adding the event to the <ForeignObject> itself). But it does fire if I add the onPress() event to the <G> or <Path> component. I am wondering what may cause this.

Expectation:

onPress() event should be triggered in a <ForeignObject> or nested <TouchableOpacity>

Environment info

 [email protected]
[email protected]

Related Issue

#580

JoBerkner avatar May 26 '20 05:05 JoBerkner

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and I will leave this open.

stale[bot] avatar Jul 25 '20 05:07 stale[bot]

Hey, could anyone take a look at this issue?

egorshulga avatar Jul 31 '20 12:07 egorshulga

I think it is a bug I have the same issue

mortezacruise avatar Aug 15 '20 19:08 mortezacruise

I also have this issue - any updates or workarounds?

alicema15 avatar Sep 01 '20 20:09 alicema15

Any Updates?

aditya1711 avatar Sep 22 '20 10:09 aditya1711

Same problem here, any updates?

quintenbox avatar Nov 11 '20 06:11 quintenbox

Same issue here.

sdandois avatar Feb 10 '21 14:02 sdandois

Add a dummy <Rect/> inside of your <ForeignObject/> in order to fire onPress event :

<ForeignObject
    onPress={() => console.log("hello")}
    x={*pathEndPosition*}
    y={*pathEndPosition*}
>
    <View
    style={{
        position: "absolute",
        backgroundColor: "red",
        borderRadius: 3,
    }}
    >
    <Text
        style={{
        color: "black",
        fontSize: 10,
        }}
    >
        {*data.myLabel*}
    </Text>
    </View>
    <Rect
	x={*pathEndPosition*}
        y={*pathEndPosition*}
	width={"width"}
        height={"height"}
	fill={'transparent'}
     />
</ForeignObject>

(you must specify a width & a height for your Rect)

Gwynevere avatar Apr 01 '21 09:04 Gwynevere

Add a dummy Rect not working on IOS

pkvov avatar Jul 12 '21 13:07 pkvov

Same issue

Stevemoretz avatar Aug 23 '21 11:08 Stevemoretz

Add a dummy <Rect/> inside of your <ForeignObject/> in order to fire onPress event :

Part of this idea was a workaround for me. I put an onPressIn in the transparent Rect that was OUTSIDE of the ForeignObject component. It acted like an invisible touch zone on the thing I wanted to display in the ForeignObject.

          <Rect 
            x={xDelete} 
            y={yDelete} 
            width={24} 
            height={24} 
            fill={'transparent'} 
            onPressIn={()=>{
              console.log('It Works!');
            }} 
          />
          <ForeignObject x={xDelete} y={yDelete}>
            <IconDeleteObject />
          </ForeignObject>

pbaker0804 avatar Jan 21 '22 18:01 pbaker0804

Add a dummy <Rect/> inside of your <ForeignObject/> in order to fire onPress event :

Part of this idea was a workaround for me. I put an onPressIn in the transparent Rect that was OUTSIDE of the ForeignObject component. It acted like an invisible touch zone on the thing I wanted to display in the ForeignObject.

          <Rect 
            x={xDelete} 
            y={yDelete} 
            width={24} 
            height={24} 
            fill={'transparent'} 
            onPressIn={()=>{
              console.log('It Works!');
            }} 
          />
          <ForeignObject x={xDelete} y={yDelete}>
            <IconDeleteObject />
          </ForeignObject>

it works for me!

cuantmac avatar Jul 28 '22 09:07 cuantmac

Add a dummy <Rect/> inside of your <ForeignObject/> in order to fire onPress event :

Part of this idea was a workaround for me. I put an onPressIn in the transparent Rect that was OUTSIDE of the ForeignObject component. It acted like an invisible touch zone on the thing I wanted to display in the ForeignObject.

          <Rect 
            x={xDelete} 
            y={yDelete} 
            width={24} 
            height={24} 
            fill={'transparent'} 
            onPressIn={()=>{
              console.log('It Works!');
            }} 
          />
          <ForeignObject x={xDelete} y={yDelete}>
            <IconDeleteObject />
          </ForeignObject>

This worked for me too. Keeping the rect outside the foreingObject did the job, I was keeping it inside and it was breaking in iOS.

bhushan-patil-official avatar Mar 09 '23 19:03 bhushan-patil-official