Skip to content

Commit 67158ea

Browse files
authored
Merge pull request #260 from dcangulo/chore/minor-changes
Chore/minor changes
2 parents 0d3d28b + 03b60dd commit 67158ea

4 files changed

Lines changed: 47 additions & 19 deletions

File tree

README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,32 @@ yarn add react-native-outside-press
1616
```
1717

1818
## Usage
19+
20+
### EventProvider
1921
Wrap your app with `EventProvider`.
22+
2023
```js
2124
import { EventProvider } from 'react-native-outside-press';
2225

2326
export default function App() {
2427
return (
25-
<EventProvider style={{ flex: 1 }}>
28+
<EventProvider>
2629
<RestOfYourApp />
2730
</EventProvider>
2831
);
2932
}
3033
```
31-
### `EventProvider` Props
32-
| **Name** | **Type** | **Default** |
33-
|----------|----------|-----------------------------------------|
34-
| All of [`ViewProps`](https://reactnative.dev/docs/view#props) |
3534

36-
Then wrap every component you want to detect outside press.
35+
#### Props
36+
37+
| Name | Description | Type | Default | Required? |
38+
|-------------|---------------------|------------------------------------------------------------|---------------|-----------|
39+
| `style` | | [ViewStyle](https://reactnative.dev/docs/view-style-props) | `{ flex: 1 }` | `false` |
40+
| `ViewProps` | Inherits ViewProps. | [ViewProps](https://reactnative.dev/docs/view#props) | | `false` |
41+
42+
### OutsidePressHandler
43+
Wrap every component you want to detect outside press with `OutsidePressHandler`.
44+
3745
```js
3846
import { View } from 'react-native';
3947
import OutsidePressHandler from 'react-native-outside-press';
@@ -50,15 +58,17 @@ export default function MyComponent() {
5058
);
5159
}
5260
```
53-
### `OutsidePressHandler` Props
54-
| **Name** | **Type** | **Default** |
55-
|------------------|----------|---------------------------------|
56-
| `onOutsidePress` | function | `undefined` |
57-
| `disabled` | boolean | `undefined` |
58-
| All of [`ViewProps`](https://reactnative.dev/docs/view#props) |
61+
62+
#### Props
63+
64+
| Name | Description | Type | Default | Required? |
65+
|------------------|------------------------------------------------------|------------------------------------------------------|---------------|-----------|
66+
| `onOutsidePress` | Function to run when pressed outside of component. | function | | `true` |
67+
| `disabled` | Controls whether `onOutsidePress` should run or not. | boolean | `false` | `false` |
68+
| `ViewProps` | Inherits ViewProps. | [ViewProps](https://reactnative.dev/docs/view#props) | | `false` |
5969

6070
## Changelogs
6171
See [CHANGELOGS.md](CHANGELOGS.md)
6272

6373
## License
64-
Copyright © 2022 David Angulo, released under the MIT license, see [LICENSE](LICENSE).
74+
Copyright © 2023 David Angulo, released under the MIT license, see [LICENSE](LICENSE).

example/src/App.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ export default function App() {
66
return (
77
<EventProvider style={styles.container}>
88
<OutsidePressHandler
9-
disabled={false}
109
onOutsidePress={() => {
1110
console.log('Pressed outside the black box!');
1211
}}
1312
>
14-
<View style={styles.blackBox} />
13+
<View style={styles.blackBox}>
14+
<Text
15+
style={styles.text}
16+
onPress={() => console.log('Pressed inside blackbox!')}
17+
>
18+
Press Me
19+
</Text>
20+
</View>
1521
</OutsidePressHandler>
1622
<Text onPress={() => console.log('Pressed!')}>Press Me</Text>
1723
<OutsidePressHandler
18-
disabled={false}
1924
onOutsidePress={() => {
2025
console.log('Pressed outside the red box!');
2126
}}
@@ -38,4 +43,7 @@ const styles = StyleSheet.create({
3843
height: 200,
3944
backgroundColor: 'red',
4045
},
46+
text: {
47+
color: 'white',
48+
},
4149
});

src/components/event-provider.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import React from 'react';
2+
import { StyleSheet } from 'react-native';
23
import type { ViewProps } from 'react-native';
34
import useEventStore from '../hooks/use-event-store';
45
import EventContext from '../event-context';
56
import Container from './container';
67

78
export default function EventProvider(props: ViewProps) {
9+
const { style, ...rest } = props;
810
const eventStore = useEventStore();
911

1012
return (
1113
<EventContext.Provider value={eventStore}>
12-
<Container {...props}>{props.children}</Container>
14+
<Container style={[styles.container, style]} {...rest}>
15+
{props.children}
16+
</Container>
1317
</EventContext.Provider>
1418
);
1519
}
20+
21+
const styles = StyleSheet.create({
22+
container: {
23+
flex: 1,
24+
},
25+
});

src/components/outside-press-handler.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import deepClone from '../utils/deep-clone';
66

77
interface IOutsidePressHandlerProps extends ViewProps {
88
onOutsidePress: () => void;
9-
disabled: boolean;
9+
disabled?: boolean;
1010
}
1111

1212
export default function OutsidePressHandler(props: IOutsidePressHandlerProps) {
13-
const { children, onOutsidePress, disabled } = props;
13+
const { children, onOutsidePress, disabled = false } = props;
1414
const id: string = useRef(Math.random().toString()).current;
1515
const { appendEvent, removeEvent, setSkippedEventId } = useEvent();
1616
const setSkippedEventIdFunc = () => setSkippedEventId(id);

0 commit comments

Comments
 (0)