-
Notifications
You must be signed in to change notification settings - Fork 637
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
Tap outside to close #11
Comments
Hey! |
😆 pls add this function~~ thanks |
Thank you @mmazzarolo , actually I fixed using exactly same method you mentioned. It works, but since the first image in your preview images (on README.md) does it correctly, I thought it was implemented already, wasn't it? |
@Doko-Demo-Doa according to the source code, haven't implemented yet, there is no close event on backdrop view or you can do sth like this, not so good but should be work.
|
@Doko-Demo-Doa oh wow, honestly I don't remember how I implemented it for that screenshot (I did it in a customized version of this lib 😞 ). @DavidKongDesheng the problem with that solution is that clicking everywhere on the modal will close it.
What do you think guys? Also, thank you @Doko-Demo-Doa and @DavidKongDesheng for the discussion, any feedback is of course warmly welcomed! 😸 @Doko-Demo-Doa could you share with us your solution? |
I played a bit with it. onStartShouldSetResponder={() => {
console.log('You pressed the backdrop!');
return true;
}} to the backdrop view. The problem with this approach is that it won't work correctly if you're centering the modal content using this: content: {
flex: 1,
justifyContent: 'center'
} ...which is the default style applied to the content. I guess we should wait until react-native 0.43 stable lands and try changing that style to the new I'm obviously open to discussions on the matter :) |
@mmazzarolo On my solution I added two touch events, one for Modal itself which will close the modal, the other one is on the Modal content view which will do nothing 😸, so only when you click the backdrop will close the modal. Yup, the right solution should be make the backdrop respond to user touch. |
Oh, nice! @DavidKongDesheng did you test your solution? |
@mmazzarolo Just added touch event to your example code, you can download and try, but this is not the best way to implement. |
It seems that RN's modal component is pretty meh. If there's something like Dialog on Android or UIAlertView on iOS, it would be much better in this case :( |
Hi, I did exactly the same thing as @DavidKongDesheng but still don't working on my side ! <TouchableWithoutFeedback onPress={() => this.setState({ modalVisible: false })}>
<Modal transparent={true} ref="modal" visible={this.state.modalVisible} onRequestClose={this.close.bind(this)} animationType={this.state.animationType}>
{this.renderOptionList()}
</Modal>
</TouchableWithoutFeedback> Or maybe i don't make it well... no ? react-native 0.42.3 |
Hi! For me the solution of @DavidKongDesheng works great 🔥 ` _showModal() { _hideModal() { render() {
Hope to be helpfull! |
@emalorenzo Thanks! But it doesn't work :( I don't know why it doesn't triggered the background :/ |
@warka0 Can't reproduce it according the attached codes, but it looks like you are using Modal from 'react-native' not from 'react-native-modal' Or maybe try
Or more codes attach... |
@emalorenzo Thanks for your solution, this worked for me 👏 👍 |
@DavidKongDesheng Your solution worked for me, thanks! I hope this functionality is added soon to react-native-modal, it's such an important function. |
I'll test it in two days. Cannot do it right now, i'm between two airports. :p |
Hey, I'll try to take a look at it soon (PRs are obviously warmly welcome) :) |
Ok, i've tried and it's working, thanks! |
@warka0, does it also work if you add a button inside the modal? Doesn't the |
@mmazzarolo So the modal is wrapped in |
It was easier than I thought then :) Anyone willing to submit a PR? |
Sure I'll submit an example |
@jm90m thank you for the PRs! |
@mmazzarolo ohh, yeah looks like the main issue is more complex than it seems. I see, if I ever come across a solution, I'll submit a PR if no one has done so yet. |
You guys can take a look at my repo, the idea is surrounding modal with 4 touchables. My initial solution was use a screen size touchable to perform tapToClose, a modal size touchable wrapping the modal to prevent the tap event goes up(no onPress). But then I found I can't use scrollView inside the touchable. In the end I came with up the solution I used in my repo |
Released on npm on |
The release notes of |
I am going to give it a shot shortly. Just wanted to clarify that I use the setup properly. Previously, I was wrapping inside of Modal with |
@alexkuc, yep, just set the visibility of the modal to false in the |
Yesssssss |
It took so long but we did it! 🎈 Feel free to open another issue if needed. |
@mmazzarolo , I mean the bug was literally in compiler (or interpreter in this case): the optimizer of React Native's Java's ShadowNode tree. I think it's fair this one took a while to solve lol 😂 Now just to get react-native to accept their fix so we can remove our hacky fix. If you want to jump on and +1 the fix or add context, that would be much appreciated: facebook/react-native#15529 |
Summary: Currently, having the property `pointerEvents={'box-only'}` marks a view as layoutOnly. For optimization reasons, layoutOnly nodes are [not added to the node hierarchy](https://github.com/facebook/react-native/blob/b103903ec869bc48dfcaf001dc926957d0b5200a/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyOptimizer.java#L99) The problem is that should this box ever need to be [transitioned to not-layout-only](https://github.com/facebook/react-native/blob/b103903ec869bc48dfcaf001dc926957d0b5200a/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyOptimizer.java#L394) (for example, because you add the a CSS transform property), it must be added to the hierarchy. To add it to the hierarchy the [React Styles Diff Map](https://github.com/facebook/react-native/blob/b103903ec869bc48dfcaf001dc926957d0b5200a/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyOptimizer.java#L396) is passed along with the backing shadowNode to create the node. The problem is that at no point were the original `pointerEvents` and so the new node will be created with `pointerEvents = 'auto'`. A more correct fix to this problem might be to have shadowNodes carry around their pointerEvents, although this is likely a greater design decision which is why am I proposing the quick fix now. Will also resolve: react-native-modal/react-native-modal#11 Closes #15529 Reviewed By: AaaChiuuu Differential Revision: D5792012 Pulled By: tomasreimers fbshipit-source-id: 625242c53e16cb293c64811a57f6c3905b3483e0
Hi I'm still having this issue.
Passed a function as prop to a custom component
In
It wont close after tapping on the backdrop. But when i use it in "react-native": "0.44.2", |
Are you using Expo by any mean? |
Nope, i'm not using Expo. Btw, the problem solved. The react-native-modal package didnt update properly. My bad. Thanks for the quick response. |
Hi Guys I tried the above suggestions but they dont work for me When I click outside the modal my _hideModal() is not fired. I am using RN 0.47.2 and react-native-modal 3.0.2 import React, { Component } from 'react' class ConfirmSchedule extends Component { _hideModal() { render () { const styles = StyleSheet.create({ module.exports = ConfirmSchedule; |
That works greats for me, only use one layer behind options. <View>
<Modal
animationType="slide"
transparent={true}
visible={this.state.modalVisible}
onRequestClose={() => {
this.closeModal();
}}>
<TouchableWithoutFeedback //this touchable closes modal
onPress={() => {
this.closeModal();
}}>
<View style={{flex:2,backgroundColor:'#6666669c'}}>//layer behind modal options
{this.renderOptionList()}
</View>
</TouchableWithoutFeedback>
</Modal>
</View> |
Hi guys, I just found this issue and tried hard to read all of your comments and suggestions.
Here is my the screen where I want to show my modal.
Here is modal component : CameraImageSelectModal.js
Thanks!! |
Nevermind. I just updated the react-native-modal to the newer version and it solved my problem. Thanks!! (I was using 2.5.0 version lol) |
@jbaek7023 onBackdropPress works for u? |
What if there is scrollView inside? |
I think this solution would help you. |
I implements using @DavidKongDesheng 's solution. const constainerStyle = StyleSheet.create({
...
})
export function MyModal(props) {
const {modalVisible = false, onClose, children, preset = 'nonClose'} = props;
return preset === 'nonClose' ? (
<Modal animationType="fade" transparent={true} visible={modalVisible}>
<View style={containerStyle.centeredView}>
<View style={containerStyle.modalView}>{children}</View>
</View>
</Modal>
) : (
<Modal animationType="fade" transparent={true} visible={modalVisible}>
<TouchableWithoutFeedback onPress={onClose}> // when trigger click modal outside
<View style={containerStyle.centeredView}>
<TouchableWithoutFeedback onPress={() => {}}> // prevent trigger onClose when click modal inside
<View style={containerStyle.modalView}>{children}</View>
</TouchableWithoutFeedback>
</View>
</TouchableWithoutFeedback>
</Modal>
);
} |
THIS WILL WORK 100% <Modal
<View By clicking anywhere on an empty space modal will dismiss. |
It's working. |
Hi! I've tried most of the aproaches above and any of them seemed to solve the main issue. The last ones works but are half incomplete and difficult to understand, so I leave you here an updated easy to read component that solves the issue. The code 👇👇👇 export const ModalScreen = () => {
const [isVisible, setIsVisible] = useState(false);
const showModal = () => {
setIsVisible(true);
};
const hideModal = () => {
setIsVisible(false);
};
return (
<View style={styles.screenContainer}>
<Button title="Open Modal" onPress={showModal} />
<Modal visible={isVisible} transparent>
<Pressable onPress={hideModal} style={styles.modalContainer}>
<TouchableWithoutFeedback>
<View style={styles.modalContent}>
<Text>Modal Title</Text>
<Button title="Close Modal" onPress={hideModal} />
</View>
</TouchableWithoutFeedback>
</Pressable>
</Modal>
</View>
);
};
const styles = StyleSheet.create({
screenContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
modalContainer: {
flex: 1,
backgroundColor: 'rgba(0,0,0,0.3)',
justifyContent: 'center',
alignItems: 'center',
},
modalContent: {
backgroundColor: 'white',
borderRadius: 10,
padding: 40,
justifyContent: 'center',
alignItems: 'center',
},
}); |
Works great! Thanks! |
Is there a way to implement that function? Right now when I tap on the darkened region, the modal doesn't close.
The text was updated successfully, but these errors were encountered: