question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ForeignObject onPress() event not firing

See original GitHub issue

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

 react-native@0.61.4
react-native-svg@12.1.0

Related Issue

#580

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:12

github_iconTop GitHub Comments

5reactions
Gwyneverecommented, Apr 1, 2021

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)

2reactions
pbaker0804commented, Jan 21, 2022

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>
Read more comments on GitHub >

github_iconTop Results From Across the Web

ForeignObject onPress() event not firing · Issue #1389 - GitHub
Bug I created a line chart with each line being stored in a container together with a label inside a (due to styling...
Read more >
ForeignObject onPress() event not firing - - Bountysource
Bug. I created a line chart with each line being stored in a <G> container together with a label inside a <ForeignObject> (due...
Read more >
Events not triggering in foreignObject for Firefox - Stack Overflow
When the my sub-meny items extended outside of my foreignObject's container size my mouseleave event triggered. I made a simpler example to show...
Read more >
react-native-svg onPress not working since expo v39 - Snack
Try this project on your phone! Use Expo's online editor to make changes and save your own copy.
Read more >
react-native-svg - npm
Touch Events; Serialize; Run example; TODO; Known issues ... Add RNSVG to your Podfile (with RN 0.60+ autolinking, this is not needed).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found