Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable the touch of a child element in Touchable Component

I have following code

<BlurView blurType={'light'} blurAmount={10} style={{flex: 1}}>
    <TouchableOpacity style={{flex: 1, justifyContent: 'center', alignItems: 'center'}} onPress={() => this.setState({displayVideoModal: false})}>
        <View style={{width: moderateScale(300), height: moderateScale(300), backgroundColor: '#333333', borderRadius: moderateScale(24)}}>

        </View>
    </TouchableOpacity>
</BlurView>

Which result in following

enter image description here

I have TouchableOpacity covering the BlurView area because I want the full blur view screen to respond to touch event and hide it. except the view in the center. It will contain the video and must not receive the touch event of its parent component and it must have touch event of its own.

How do I do this? Or I am willing to know if there are alternatives to achieve the similar result.

like image 346
Ibrahim Azhar Armar Avatar asked Dec 18 '19 03:12

Ibrahim Azhar Armar


People also ask

How do I disable Touchableopacity?

To simply disable the button, we can use the disabled prop in our button element and set its value to true . If we want to disable our button after clicking on it, We can disable it by using react's state.

How do I turn off View touch in React Native?

React Native recipe: Disable touch/tap events on a transparent view. You might have encountered a use case where you want to hide a view but don't want to remove it from the view hierarchy. You can achieve this simply by making the view transparent by toggling opacity .

What is touchable opacity?

This component fades out when pressed, and fades back in when released. We can style it however we want, just like a View . We can configure the pressed opacity with the activeOpacity prop. This is typically the most common kind of button in a React Native app.

What is touchable opacity in react?

A wrapper for making views respond properly to touches. On press down, the opacity of the wrapped view is decreased, dimming it. Opacity is controlled by wrapping the children in an Animated. View, which is added to the view hierarchy.


3 Answers

Nested Touchable seems to work in my case.

<BlurView blurType={'light'} blurAmount={10} style={{flex: 1}}>
    <TouchableOpacity style={{flex: 1, justifyContent: 'center', alignItems: 'center'}} onPress={() => this.setState({displayVideoModal: false})}>
        <TouchableHighlight style={{width: moderateScale(300), height: moderateScale(300), backgroundColor: '#333333', borderRadius: moderateScale(24)}}>
            <View></View>
        </TouchableHighlight>
    </TouchableOpacity>
</BlurView>

When I click on child TouchableHighlight it seems to receive the child touch event and ignore parent. This seems to work for now, not sure if it is the good option, will be open to hear for better options.

I also found this

How can I propagate touch event in nested Touchable in React Native?

https://facebook.github.io/react-native/docs/gesture-responder-system

React Native onStartShouldSetResponder and onResponderRelease?

like image 56
Ibrahim Azhar Armar Avatar answered Oct 08 '22 03:10

Ibrahim Azhar Armar


You can leverage pointerEvents:

<BlurView blurType={'light'} blurAmount={10} style={{flex: 1}}>
    <TouchableOpacity style={{flex: 1, justifyContent: 'center', alignItems: 'center'}} onPress={() => this.setState({displayVideoModal: false})}>
        <View pointerEvents="none" style={{width: moderateScale(300), height: moderateScale(300), backgroundColor: '#333333', borderRadius: moderateScale(24)}}>
        </View>
    </TouchableOpacity>
</BlurView>
like image 6
Umair Sarfraz Avatar answered Oct 08 '22 04:10

Umair Sarfraz


Nested touchables were not working for me because I also had buttons in the floating view. On the background overlay view (BlurView in this case), add the following props:

onStartShouldSetResponder={(event) => {
    return event.nativeEvent.touches.length == 1;
}}
onResponderRelease={(event) => {
    if (event.target == event.currentTarget) {
        this.props.onCancel()
    }
}}

This works because event.currentTarget is the view that has the prop onResponderRelease and target is the view that the touch was released upon.

React Native Gesture Responder System documentation: https://facebook.github.io/react-native/docs/gesture-responder-system

like image 3
SamB Avatar answered Oct 08 '22 03:10

SamB