Skip to content
This repository was archived by the owner on Jan 30, 2021. It is now read-only.

Commit 25d7ab1

Browse files
authored
Merge pull request #34 from charpeni/patch-1
Implement withSafeArea HOC
2 parents b95ef75 + a21186c commit 25d7ab1

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This is a JS-only version of SafeAreaView that will be available in React Native
44

55
## Usage
66

7-
Wrap components that touch any edge of the screen with SafeAreaView.
7+
Wrap components that touch any edge of the screen with SafeAreaView.
88

99
```jsx
1010
<SafeAreaView>
@@ -16,7 +16,7 @@ Wrap components that touch any edge of the screen with SafeAreaView.
1616
1717
### forceInset
1818
19-
Sometimes you will observe unexpected behavior and jank because SafeAreaView uses `onLayout` then calls `measureInWindow` on the view. If you know your view will touch certain edges, use `forceInset` to force it to apply the inset padding on the view.
19+
Sometimes you will observe unexpected behavior and jank because SafeAreaView uses `onLayout` then calls `measureInWindow` on the view. If you know your view will touch certain edges, use `forceInset` to force it to apply the inset padding on the view.
2020
2121
```jsx
2222
<SafeAreaView forceInset={{ top: 'always' }}>
@@ -27,3 +27,14 @@ Sometimes you will observe unexpected behavior and jank because SafeAreaView use
2727
```
2828

2929
`forceInset` takes an object with the keys `top | bottom | left | right | vertical | horizontal` and the values `'always' | 'never'`. Or you can override the padding altogether by passing an integer.
30+
31+
### With HOC
32+
33+
Sometimes you would prefer to use a higher-order component to wrap components.
34+
35+
```js
36+
withSafeArea()(Component);
37+
38+
// Or with forceInset props
39+
withSafeArea({ top: 'always' })(Component);
40+
```

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
Animated,
1010
} from 'react-native';
1111
import withOrientation from './withOrientation';
12+
import withSafeArea from './withSafeArea';
13+
export { withSafeArea };
1214

1315
// See https://mydevice.io/devices/ for device dimensions
1416
const X_WIDTH = 375;

withSafeArea.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React, { Component } from 'react';
2+
import hoistStatics from 'hoist-non-react-statics';
3+
4+
import SafeAreaView from './';
5+
6+
export default function (forceInset = {}) {
7+
return (WrappedComponent) => {
8+
class withSafeArea extends Component {
9+
render() {
10+
return (
11+
<SafeAreaView style={{ flex: 1 }} forceInset={forceInset}>
12+
<WrappedComponent {...this.props} />;
13+
</SafeAreaView>
14+
);
15+
}
16+
}
17+
18+
return hoistStatics(withSafeArea, WrappedComponent);
19+
};
20+
}

0 commit comments

Comments
 (0)