Skip to content

Instantly share code, notes, and snippets.

@nicolas-amabile
Created August 18, 2020 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolas-amabile/3b9fd47c896d8f807bd39b02bba8c159 to your computer and use it in GitHub Desktop.
Save nicolas-amabile/3b9fd47c896d8f807bd39b02bba8c159 to your computer and use it in GitHub Desktop.
A simple React Native component that detects outside tap events
import PropTypes from 'prop-types';
import React from 'react';
import styled from 'styled-components';
const Modal = styled.Modal.attrs(() => ({
transparent: true,
}))``;
const Wrapper = styled.TouchableOpacity`
flex: 1;
`;
const WatchOutsideTap = ({ children, onTapOutside }) => (
<Modal>
<Wrapper onPress={onTapOutside}>
{children}
</Wrapper>
</Modal>
);
WatchOutsideTap.propTypes = {
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
]).isRequired,
onTapOutside: PropTypes.func.isRequired,
};
export default WatchOutsideTap;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment