Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/docs/migrating-to-v6.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
sidebar_position: 6
title: Migrating to 6.x
sidebar_label: Migrating to 6.x
---

Version 6 contains the following breaking changes.

### New Architecture only

Support for the old architecture (Paper) has been removed. Your app must use the New Architecture and React Native 0.82 or newer. If you still need the old architecture, stay on 5.x.

### ESM only package

The package is now published as ESM only, with an `exports` map.

- Only the documented entry points (`react-native-safe-area-context` and `react-native-safe-area-context/jest/mock`) can be imported. Deep imports into `lib/` or `src/` no longer work.
- Jest doesn't transform `node_modules` by default. Projects using the React Native Jest preset need to add this package to `transformIgnorePatterns`. See [Testing](./testing.mdx#transforming-the-package-with-jest). The `jest-expo` preset already handles this.
- Loading the package with `require()` in Node.js needs Node 20.19+ or 22.12+.
21 changes: 8 additions & 13 deletions docs/docs/testing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,20 @@ export function TestSafeAreaProvider({ children }) {
}
```

### Enabling Babel Parsing for Modules
### Transforming the package with Jest

While trying to use this mock, a frequently encountered error is:
Starting with version 6, this library is published as ESM only. By default, [Jest does not transform files inside `node_modules`](https://jestjs.io/docs/configuration#transformignorepatterns-arraystring), so loading the package or its mock fails with:

```tsx
SyntaxError: Cannot use import statement outside a module.
```
SyntaxError: Cannot use import statement outside a module
```

This issue arises due to the use of the import statement. To resolve it, you need to permit Babel to parse the file.

By default, [Jest does not parse files located within the node_modules folder](https://jestjs.io/configuration#transformignorepatterns-arraystring).

However, you can modify this behavior as outlined in the Jest documentation on [`transformIgnorePatterns` customization](https://jestjs.io/tutorial-react-native#transformignorepatterns-customization).
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:
If you use the React Native Jest preset (`react-native` or `@react-native/jest-preset`), add `react-native-safe-area-context` to `transformIgnorePatterns` in your Jest config:

```tsx
```js
transformIgnorePatterns: [
'node_modules/(?!((jest-)?react-native|@react-native(-community)?|react-native-safe-area-context)/)',
];
],
```

This adjustment ensures Babel correctly parses modules, avoiding the aforementioned syntax error.
If you already customize `transformIgnorePatterns`, add `react-native-safe-area-context` to your existing pattern instead. The `jest-expo` preset already transforms this package, so Expo projects don't need any change.
4 changes: 4 additions & 0 deletions example/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const config = {
// We need to make sure that only one version is loaded for peerDependencies
// So we block them at the root, and alias them to the versions in example's node_modules
resolver: {
unstable_conditionNames: [
...(defaultConfig.resolver.unstable_conditionNames ?? []),
'react-native-safe-area-context-source',
],
blockList: [
...modules.map(
(m) =>
Expand Down
1 change: 0 additions & 1 deletion jest/mock.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion jest/mock.js

This file was deleted.

51 changes: 41 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,52 @@
"name": "react-native-safe-area-context",
"version": "5.11.0",
"description": "A flexible way to handle safe area, also works on Android and web.",
"main": "lib/commonjs/index.js",
"module": "lib/module/index.js",
"react-native": "src/index.tsx",
"types": "lib/typescript/src/index.d.ts",
"source": "src/index.tsx",
"main": "./lib/module/index.js",
"types": "./lib/typescript/src/index.d.ts",
"exports": {
".": {
"react-native-safe-area-context-source": "./src/index.tsx",
"types": "./lib/typescript/src/index.d.ts",
"default": "./lib/module/index.js"
},
"./jest/mock": {
"react-native-safe-area-context-source": "./src/jest/mock.tsx",
"types": "./lib/typescript/src/jest/mock.d.ts",
"default": "./lib/module/jest/mock.js"
},
"./package.json": "./package.json"
},
"sideEffects": false,
"files": [
"src",
"lib",
"android",
"ios",
"common",
"jest",
"*.podspec",
"Package.swift",
"react-native.config.js",
"!lib/commonjs",
"!ios/build",
"!android/build",
"!android/gradle",
"!android/gradlew",
"!android/gradlew.bat",
"!android/local.properties",
"!**/__tests__",
"!**/__fixtures__",
"!**/__mocks__",
"!**/.*",
"!example"
],
"author": "Janic Duplessis <janicduplessis@gmail.com>",
"contributors": [
"Evan Bacon <bacon@expo.io> (https://github.com/evanbacon)"
],
"homepage": "https://github.com/AppAndFlow/react-native-safe-area-context#readme",
"bugs": {
"url": "https://github.com/AppAndFlow/react-native-safe-area-context/issues"
},
"license": "MIT",
"scripts": {
"start": "react-native start",
Expand Down Expand Up @@ -86,7 +108,7 @@
"react": "19.2.8",
"react-dom": "19.2.8",
"react-native": "^0.87.0",
"react-native-builder-bob": "^0.23.2",
"react-native-builder-bob": "^0.43.0",
"react-test-renderer": "19.2.8",
"release-it": "^17.4.0",
"typescript": "^7.0.2"
Expand Down Expand Up @@ -129,9 +151,18 @@
"source": "src",
"output": "lib",
"targets": [
"commonjs",
"module",
"typescript"
[
"module",
{
"esm": true
}
],
[
"typescript",
{
"project": "tsconfig.build.json"
}
]
]
},
"swiftpmConfig": {
Expand Down
24 changes: 19 additions & 5 deletions scripts/check-package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ for (const required of [
'README.md',
'LICENSE',
'src/index.tsx',
'src/specs/NativeSafeAreaProvider.ts',
'src/specs/NativeSafeAreaView.ts',
'lib/commonjs/index.js',
'src/specs/SafeAreaProviderNativeComponent.ts',
'src/specs/SafeAreaViewNativeComponent.ts',
'lib/module/index.js',
'lib/typescript/src/index.d.ts',
'react-native-safe-area-context.podspec',
Expand All @@ -40,20 +39,35 @@ for (const required of [
'ios/RNCSafeAreaContext.mm',
'android/build.gradle',
'common/cpp/react/renderer/components/safeareacontext/RNCSafeAreaViewShadowNode.cpp',
'jest/mock.js',
'lib/module/jest/mock.js',
'lib/typescript/src/jest/mock.d.ts',
]) {
if (!entries.includes(`package/${required}`)) {
throw new Error(`Missing package file: ${required}`);
}
}

for (const field of ['main', 'module', 'types', 'react-native', 'source']) {
for (const field of ['main', 'types']) {
const target = manifest[field];
if (!target || !entries.includes(`package/${target.replace(/^\.\//, '')}`)) {
throw new Error(`Missing ${field} target: ${target ?? '<unset>'}`);
}
}

function checkExportTargets(value) {
if (typeof value === 'string') {
if (!entries.includes(`package/${value.replace(/^\.\//, '')}`)) {
throw new Error(`Missing export target: ${value}`);
}
} else if (value && typeof value === 'object') {
for (const target of Object.values(value)) {
checkExportTargets(target);
}
}
}

checkExportTargets(manifest.exports);

for (const entry of entries) {
if (/^package\/(?:android|ios)\/(?:.*\/)?build\//.test(entry)) {
throw new Error(`Unexpected native build output: ${entry}`);
Expand Down
2 changes: 1 addition & 1 deletion src/NativeSafeAreaProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import NativeSafeAreaProvider from './specs/NativeSafeAreaProvider';
import NativeSafeAreaProvider from './specs/SafeAreaProviderNativeComponent';

export { NativeSafeAreaProvider };
2 changes: 1 addition & 1 deletion src/SafeAreaView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
NativeSafeAreaViewInstance,
NativeSafeAreaViewProps,
} from './SafeArea.types';
import NativeSafeAreaView from './specs/NativeSafeAreaView';
import NativeSafeAreaView from './specs/SafeAreaViewNativeComponent';
import { useMemo } from 'react';

const defaultEdges: Record<Edge, EdgeMode> = {
Expand Down
5 changes: 5 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["src"],
"exclude": ["src/__tests__", "lib", "example", "docs"]
}
Loading
Loading