Skip to content

Commit 9188eec

Browse files
docs: add website
1 parent 1855378 commit 9188eec

36 files changed

+9896
-352
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,3 @@
11
# Contributing
22

3-
## Development workflow
4-
5-
### Install dependencies
6-
7-
Use yarn to install development dependencies.
8-
9-
```sh
10-
yarn install
11-
```
12-
13-
If you don't have bundler installed:
14-
15-
```
16-
gem install bundler
17-
```
18-
19-
Move to the `example` directory and install dependencies there too.
20-
21-
```sh
22-
cd example
23-
yarn install
24-
bundle install
25-
```
26-
27-
```sh
28-
cd ios && bundle exec pod install
29-
```
30-
31-
### Example app
32-
33-
Start the example app to test your changes. You can use one of the following commands from the repo root, depending on the platform you want to use.
34-
35-
From the `example` directory:
36-
37-
#### iOS
38-
39-
```sh
40-
yarn ios
41-
```
42-
43-
I also recommend opening `example/ios/SafeAreaViewExample.xcworkspace` in Xcode if you need to make changes to native code.
44-
45-
#### Android
46-
47-
```sh
48-
yarn android
49-
```
50-
51-
I also recommend opening `example/android` in Android Studio if you need to make changes to native code.
52-
53-
Use `ctrl+cmd+z` on iOS or `ctrl+m` on Android to open the dev menu and choose an example.
54-
55-
### Run tests
56-
57-
```sh
58-
yarn test
59-
```
60-
61-
### Open a pull request!
3+
See https://docs.appandflow.com/react-native-safe-area-context/docs/contributing

README.md

Lines changed: 0 additions & 292 deletions
Original file line numberDiff line numberDiff line change
@@ -36,298 +36,6 @@ You then need to link the native parts of the library for the platforms you are
3636

3737
This library currently has experimental support for the new react-native architecture. Note that there will be breaking changes and only the latest version of react-native will be supported.
3838

39-
## Usage
40-
41-
This library has 2 important concepts, if you are familiar with React Context this is very similar.
42-
43-
### Providers
44-
45-
The [SafeAreaProvider](#safeareaprovider) component is a `View` from where insets provided by [Consumers](#consumers) are relative to. This means that if this view overlaps with any system elements (status bar, notches, etc.) these values will be provided to descendent consumers. Usually you will have one provider at the top of your app.
46-
47-
### Consumers
48-
49-
Consumers are components and hooks that allow using inset values provided by the nearest parent [Provider](#providers). Values are always relative to a provider and not to these components.
50-
51-
- [SafeAreaView](#safeareaview) is the preferred way to consume insets. This is a regular `View` with insets applied as extra padding or margin. It offers better performance by applying insets natively and avoids flickers that can happen with the other JS based consumers.
52-
53-
- [useSafeAreaInsets](#usesafeareainsets) offers more flexibility, but can cause some layout flicker in certain cases. Use this if you need more control over how insets are applied.
54-
55-
## API
56-
57-
### SafeAreaProvider
58-
59-
You should add `SafeAreaProvider` in your app root component. You may need to add it in other places like the root of modals and routes when using [`react-native-screens`](https://github.com/software-mansion/react-native-screens).
60-
61-
Note that providers should not be inside a `View` that is animated with `Animated` or inside a `ScrollView` since it can cause very frequent updates.
62-
63-
#### Example
64-
65-
```js
66-
import { SafeAreaProvider } from 'react-native-safe-area-context';
67-
68-
function App() {
69-
return <SafeAreaProvider>...</SafeAreaProvider>;
70-
}
71-
```
72-
73-
#### Props
74-
75-
Accepts all [View](https://reactnative.dev/docs/view#props) props. Has a default style of `{flex: 1}`.
76-
77-
##### `initialMetrics`
78-
79-
Optional, defaults to `null`.
80-
81-
Can be used to provide the initial value for frame and insets, this allows rendering immediatly. See [optimization](#optimization) for more information on how to use this prop.
82-
83-
### SafeAreaView
84-
85-
`SafeAreaView` is a regular `View` component with the safe area insets applied as padding or margin.
86-
87-
Padding or margin styles are added to the insets, for example `style={{paddingTop: 10}}` on a `SafeAreaView` that has insets of 20 will result in a top padding of 30.
88-
89-
#### Example
90-
91-
```js
92-
import { SafeAreaView } from 'react-native-safe-area-context';
93-
94-
function SomeComponent() {
95-
return (
96-
<SafeAreaView style={{ flex: 1, backgroundColor: 'red' }}>
97-
<View style={{ flex: 1, backgroundColor: 'blue' }} />
98-
</SafeAreaView>
99-
);
100-
}
101-
```
102-
103-
#### Props
104-
105-
Accepts all [View](https://reactnative.dev/docs/view#props) props.
106-
107-
##### `edges`
108-
109-
Optional, array of `top`, `right`, `bottom`, and `left`. Defaults to all.
110-
111-
Sets the edges to apply the safe area insets to.
112-
113-
For example if you don't want insets to apply to the top edge because the view does not touch the top of the screen you can use:
114-
115-
```js
116-
<SafeAreaView edges={['right', 'bottom', 'left']} />
117-
```
118-
119-
Optionally it can be set to an object `{ top?: EdgeMode, right?: EdgeMode, bottom?: EdgeMode, left?: EdgeMode }` where `EdgeMode = 'off' | 'additive' | 'maximum'`. Additive is a default mode and is the same as passing and edge in the array: `finalPadding = safeArea + padding`. Maximum mode will use safe area inset or padding/margin (depends on `mode`) if safe area is less: `finalPadding = max(safeArea, padding)`. For example if you want a floating UI element that should be at the bottom safe area edge on devices with safe area or 24px from the bottom of the screen on devices without safe area or if safe area is less than 24px:
120-
121-
```js
122-
<SafeAreaView style={{paddingBottom: 24}} edges={{bottom: 'maximum'}} />
123-
```
124-
125-
##### `mode`
126-
127-
Optional, `padding` (default) or `margin`.
128-
129-
Apply the safe area to either the padding or the margin.
130-
131-
This can be useful for example to create a safe area aware separator component:
132-
133-
```js
134-
<SafeAreaView mode="margin" style={{ height: 1, backgroundColor: '#eee' }} />
135-
```
136-
137-
### useSafeAreaInsets
138-
139-
Returns the safe area insets of the nearest provider. This allows manipulating the inset values from JavaScript. Note that insets are not updated synchronously so it might cause a slight delay for example when rotating the screen.
140-
141-
Object with `{ top: number, right: number, bottom: number, left: number }`.
142-
143-
```js
144-
import { useSafeAreaInsets } from 'react-native-safe-area-context';
145-
146-
function HookComponent() {
147-
const insets = useSafeAreaInsets();
148-
149-
return <View style={{ paddingBottom: Math.max(insets.bottom, 16) }} />;
150-
}
151-
```
152-
153-
### useSafeAreaFrame
154-
155-
Returns the frame of the nearest provider. This can be used as an alternative to the `Dimensions` module.
156-
157-
Object with `{ x: number, y: number, width: number, height: number }`
158-
159-
### `SafeAreaInsetsContext`
160-
161-
React Context with the value of the safe area insets.
162-
163-
Can be used with class components:
164-
165-
```js
166-
import { SafeAreaInsetsContext } from 'react-native-safe-area-context';
167-
168-
class ClassComponent extends React.Component {
169-
render() {
170-
return (
171-
<SafeAreaInsetsContext.Consumer>
172-
{(insets) => <View style={{ paddingTop: insets.top }} />}
173-
</SafeAreaInsetsContext.Consumer>
174-
);
175-
}
176-
}
177-
```
178-
179-
### `withSafeAreaInsets`
180-
181-
Higher order component that provides safe area insets as the `insets` prop.
182-
183-
```ts
184-
type Props = WithSafeAreaInsetsProps & {
185-
someProp: number;
186-
};
187-
188-
class ClassComponent extends React.Component<Props> {
189-
render() {
190-
return <View style={{ paddingTop: this.props.insets.top }} />;
191-
}
192-
}
193-
194-
const ClassComponentWithInsets = withSafeAreaInsets(ClassComponent);
195-
196-
<ClassComponentWithInsets someProp={1} />;
197-
```
198-
199-
### `SafeAreaFrameContext`
200-
201-
React Context with the value of the safe area frame.
202-
203-
### `initialWindowMetrics`
204-
205-
Insets and frame of the window on initial render. This can be used with the `initialMetrics` from `SafeAreaProvider`. See [optimization](#optimization) for more information.
206-
207-
Object with:
208-
209-
```ts
210-
{
211-
frame: { x: number, y: number, width: number, height: number },
212-
insets: { top: number, left: number, right: number, bottom: number },
213-
}
214-
```
215-
216-
**NOTE:** This value can be null or out of date as it is computed when the native module is created.
217-
218-
## Deprecated apis
219-
220-
### useSafeArea
221-
222-
Use `useSafeAreaInsets` instead.
223-
224-
### SafeAreaConsumer
225-
226-
Use `SafeAreaInsetsContext.Consumer` instead.
227-
228-
### SafeAreaContext
229-
230-
Use `SafeAreaInsetsContext` instead.
231-
232-
### initialWindowSafeAreaInsets
233-
234-
Use `initialWindowMetrics` instead.
235-
236-
## Web SSR
237-
238-
If you are doing server side rendering on the web you can use `initialMetrics` to inject insets and frame value based on the device the user has, or simply pass zero values. Since insets measurement is async it will break rendering your page content otherwise.
239-
240-
## Optimization
241-
242-
If you can, use `SafeAreaView`. It's implemented natively so when rotating the device, there is no delay from the asynchronous bridge.
243-
244-
To speed up the initial render, you can import `initialWindowMetrics` from this package and set as the `initialMetrics` prop on the provider as described in Web SSR. You cannot do this if your provider remounts, or you are using `react-native-navigation`.
245-
246-
```js
247-
import {
248-
SafeAreaProvider,
249-
initialWindowMetrics,
250-
} from 'react-native-safe-area-context';
251-
252-
function App() {
253-
return (
254-
<SafeAreaProvider initialMetrics={initialWindowMetrics}>
255-
...
256-
</SafeAreaProvider>
257-
);
258-
}
259-
```
260-
261-
## Testing
262-
263-
This library includes a built in mock for Jest. It will use the following metrics by default:
264-
265-
```js
266-
{
267-
frame: {
268-
width: 320,
269-
height: 640,
270-
x: 0,
271-
y: 0,
272-
},
273-
insets: {
274-
left: 0,
275-
right: 0,
276-
bottom: 0,
277-
top: 0,
278-
},
279-
}
280-
```
281-
282-
To use it, add the following code to the jest setup file:
283-
284-
```js
285-
import mockSafeAreaContext from 'react-native-safe-area-context/jest/mock';
286-
287-
jest.mock('react-native-safe-area-context', () => mockSafeAreaContext);
288-
```
289-
290-
To have more control over the test values it is also possible to pass `initialMetrics` to
291-
`SafeAreaProvider` to provide mock data for frame and insets.
292-
293-
```js
294-
export function TestSafeAreaProvider({ children }) {
295-
return (
296-
<SafeAreaProvider
297-
initialMetrics={{
298-
frame: { x: 0, y: 0, width: 0, height: 0 },
299-
insets: { top: 0, left: 0, right: 0, bottom: 0 },
300-
}}
301-
>
302-
{children}
303-
</SafeAreaProvider>
304-
);
305-
}
306-
```
307-
308-
#### Enabling Babel Parsing for Modules
309-
310-
While trying to use this mock, a frequently encountered error is:
311-
312-
```js
313-
SyntaxError: Cannot use import statement outside a module.
314-
```
315-
316-
This issue arises due to the use of the import statement. To resolve it, you need to permit Babel to parse the file.
317-
318-
By default, [Jest does not parse files located within the node_modules folder](<(https://jestjs.io/docs/configuration#transformignorepatterns-arraystring)>).
319-
320-
However, you can modify this behavior as outlined in the Jest documentation on [`transformIgnorePatterns` customization](https://jestjs.io/docs/tutorial-react-native#transformignorepatterns-customization).
321-
If you're using a preset, like the one from [react-native](https://github.com/facebook/react-native/blob/main/packages/react-native/jest-preset.js), you should update your Jest configuration to include `react-native-safe-area-context` as shown below:
322-
323-
```js
324-
transformIgnorePatterns: [
325-
'node_modules/(?!((jest-)?react-native|@react-native(-community)?|react-native-safe-area-context)/)',
326-
];
327-
```
328-
329-
This adjustment ensures Babel correctly parses modules, avoiding the aforementioned syntax error.
330-
33139
## Contributing
33240

33341
See the [Contributing Guide](CONTRIBUTING.md)

docs/.eslintrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
'react/react-in-jsx-scope': 'off',
4+
},
5+
};

docs/.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Dependencies
2+
/node_modules
3+
4+
# Production
5+
/build
6+
7+
# Generated files
8+
.docusaurus
9+
.cache-loader
10+
11+
# Misc
12+
.DS_Store
13+
.env.local
14+
.env.development.local
15+
.env.test.local
16+
.env.production.local
17+
18+
npm-debug.log*
19+
yarn-debug.log*
20+
yarn-error.log*

0 commit comments

Comments
 (0)