Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbo Native Modules - Documentation ported from reactwg #4283

Merged
merged 26 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7d0be8a
[NA] Turbo Native Modules: add docs
blakef Oct 15, 2024
5444a75
feat: Add New Architecture Appendix
blakef Oct 15, 2024
30d1374
fix: update codegen docs with current CLI
blakef Oct 15, 2024
a55c67c
docs: improve turbo native modules file layout
blakef Oct 15, 2024
cfc7bd1
docs: Add syntax highlighing comments for diffs
blakef Oct 15, 2024
892edfc
docs: Add Fabric Native Modules placeholders
blakef Oct 15, 2024
883a758
TM: Android: Add video of built app
blakef Oct 16, 2024
9cf22c9
TM: Sidebar simpler labels
blakef Oct 16, 2024
62c925f
TM: Fix internal links
blakef Oct 16, 2024
7af6cd3
TM: fix links + add codegen notes
blakef Oct 16, 2024
d7ec9ab
TM: fix language linter issues
blakef Oct 16, 2024
557b8a5
TM: Add iOS documentation
blakef Oct 17, 2024
b451ee7
Fix: first round of feedback
blakef Oct 17, 2024
dfd38db
fix: lint error
blakef Oct 17, 2024
b3ab2a0
TM: Consolidate appendix into a single table
blakef Oct 17, 2024
7971b83
chore: fix links
blakef Oct 17, 2024
33a3e0b
chore: fixup final round of feedback
blakef Oct 21, 2024
da4725d
TNM: add code example file paths
blakef Oct 21, 2024
416b9ce
Fix empty spaces
cipolleschi Oct 21, 2024
69bb5dc
import TurboModule as type
cipolleschi Oct 21, 2024
c4fc0bf
Fix punctuation
cipolleschi Oct 21, 2024
63ae7c3
Merge branch 'main' into turbo-native-modules-update
cipolleschi Oct 21, 2024
96c749a
Fix linter
cipolleschi Oct 21, 2024
851115a
Fix sidebar and titles
cipolleschi Oct 21, 2024
cb0e720
Split pod install from output
cipolleschi Oct 21, 2024
0313c06
put back extract library
cipolleschi Oct 21, 2024
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
11 changes: 11 additions & 0 deletions docs/_turbo-native-modules-components.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";
import IOSContent from "./turbo-native-modules-ios.md";
import AndroidContent from "./turbo-native-modules-android.md";

export function TurboNativeModulesIOS() {
return <IOSContent />;
}

export function TurboNativeModulesAndroid() {
return <AndroidContent />;
}
36 changes: 36 additions & 0 deletions docs/appendix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Appendix

## I. Terminology

- **Spec** - TypeScript or Flow code that describes the API for a Turbo Native Module or Fabric Native component. Used by **Codegen** to generate boilerplate code.

- **Turbo Native Modules** - Native libraries that have no User Interface (UI) for the user. Examples would be persistent storage, notifications, network events. These are accessible to your JavaScript application code as functions and objects.
- **Fabric Native Component** - Native platform views that are available to your application JavaScript code through React Components.

- **Legacy Native Components** - Components which are running on the old React Native architecture.
- **Legacy Native Modules** - Modules which are running on the old React Native architecture.

## II. Codegen Typings

You may use the following table as a reference for which types are supported and what they map to in each platform:

| Flow | TypeScript | Flow Nullable Support | TypeScript Nullable Support | Android (Java) | iOS (ObjC) |
| -------------------------------------------------------------------------- | --------------------------------------------------- | ------------------------------------------------------- | ---------------------------------------------------- | ------------------------------------ | -------------------------------------------------------------- |
| `string` | `string` | `?string` | <code>string &#124; null</code> | `string` | `NSString` |
| `boolean` | `boolean` | `?boolean` | <code>boolean &#124; null</code> | `Boolean` | `NSNumber` |
| Object Literal<br /><code>&#123;&#124; foo: string, ...&#124;&#125;</code> | <code>&#123; foo: string, ...&#125; as const</code> | <code>?&#123;&#124; foo: string, ...&#124;&#125;</code> | <code>?&#123; foo: string, ...&#125; as const</code> | \- | \- |
| Object [[1](#notes)] | Object [[1](#notes)] | `?Object` | <code>Object &#124; null</code> | `ReadableMap` | `@` (untyped dictionary) |
| <code>Array&lt;T&gt;</code> | <code>Array&lt;T&gt;</code> | <code>?Array&lt;T&gt;</code> | <code>Array&lt;T&gt; &#124; null</code> | `ReadableArray` | `NSArray` (or `RCTConvertVecToArray` when used inside objects) |
| `Function` | `Function` | `?Function` | <code>Function &#124; null</code> | \- | \- |
| <code>Promise&lt;T&gt;</code> | <code>Promise&lt;T&gt;</code> | <code>?Promise&lt;T&gt;</code> | <code>Promise&lt;T&gt; &#124; null</code> | `com.facebook.react.bridge.Promise` | `RCTPromiseResolve` and `RCTPromiseRejectBlock` |
| Type Unions<br /><code>'SUCCESS'&#124;'FAIL'</code> | Type Unions<br /><code>'SUCCESS'&#124;'FAIL'</code> | Only as callbacks | | \- | \- |
| Callbacks<br />`() =>` | Callbacks<br />`() =>` | Yes | | `com.facebook.react.bridge.Callback` | `RCTResponseSenderBlock` |
| `number` | `number` | No | | `double` | `NSNumber` |

### Notes:

<b>[1]</b> We strongly recommend using Object literals intead of Objects.

:::info
You may also find it useful to refer to the JavaScript specifications for the core modules in React Native. These are located inside the `Libraries/` directory in the React Native repository.
:::
10 changes: 10 additions & 0 deletions docs/fabric-native-modules-android.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
id: fabric-native-modules-android
title: 'Fabric Native Modules: Android'
---

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';

:::caution
This is work-in-progress... 👷
blakef marked this conversation as resolved.
Show resolved Hide resolved
:::
10 changes: 10 additions & 0 deletions docs/fabric-native-modules-ios.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
id: fabric-native-modules-ios
title: 'Fabric Native Modules: iOS'
---

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';

:::caution
This is work-in-progress... 👷
blakef marked this conversation as resolved.
Show resolved Hide resolved
:::
10 changes: 10 additions & 0 deletions docs/fabric-native-modules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
id: fabric-native-modules-introduction
title: 'Fabric Native Modules'
---

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';

:::caution
This is work-in-progress... 👷
:::
36 changes: 36 additions & 0 deletions docs/native-platforms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
id: native-platform
title: Native Platform
---

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';

Your application may need access to platform features that aren’t directly available from React Native or one of the hundreds of [third-party libraries](https://reactnative.directory/) maintained by the community. Maybe you want to reuse some existing Objective-C, Swift, Java, Kotlin or C++ code from the JavaScript runtime. Whatever your reason, React Native exposes a powerful set of API to connect your native code to your JavaScript application code.

This guide introduces:

- **Turbo Native Modules:** native libraries that have no User Interface (UI) for the user. Examples would be persistent storage, notifications, network events. These are accessible to your user as JavaScript functions and objects.
- **Fabric Native Component:** native platform views, widgets and controllers that are available to your application's JavaScript code through React Components.

:::note
You might have previously been familiar with:

- [Legacy Native Modules](./legacy/native-modules-intro);
- [Legacy Native Components](./legacy/native-components-android);

These are our deprecated native module and component API. You can still use many of these legacy libraries with the New Architecture thanks to our interop layers. You should consider:

- using alternative libraries,
- upgrading to newer library versions that have first-class support for the New Architecture, or
- port these libraries yourself to Turbo Native Modules or Fabric Native Components.

:::

1. Turbo Native Modules
- [Introduction](turbo-native-modules.md)
- [Android](turbo-native-modules-android.md)
- [iOS](turbo-native-modules-ios.md)
2. Fabric Native Components
- [Introduction](fabric-native-modules.md)
- [Android](fabric-native-modules-android.md)
- [iOS](fabric-native-modules-ios.md)
28 changes: 15 additions & 13 deletions docs/the-new-architecture/codegen-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,38 @@

Calling Gradle or manually calling a script might be hard to remember and it requires a lot of cerimony.

To simplify it, we created a CLI tool that can help you running those tasks: the **Codegen** cli.

```shell
npx react-native codegen [--path path] [--platform string] [--outputPath path]
To simplify it, we created a CLI tool that can help you running those tasks: the **Codegen** cli. This command runs [react-native-codegen](https://www.npmjs.com/package/react-native-codegen) for your project. The following options are available:

```sh
npx @react-native-community/cli codegen --help
Usage: rnc-cli codegen [options]

Options:
--verbose Increase logging verbosity
--path <path> Path to the React Native project root. (default: "/Users/MyUsername/projects/my-app")
--platform <string> Target platform. Supported values: "android", "ios", "all". (default: "all")
--outputPath <path> Path where generated artifacts will be output to.
-h, --help display help for command
```

This command runs [react-native-codegen](https://www.npmjs.com/package/react-native-codegen) for your project. The following options are available:

- `--path` - Path to `package.json`. The default path is the current working directory.
- `--platform` - Target platform. Supported values: `android`, `ios`, `all`. The default value is `all`.
- `--outputPath` - Output path. The default value is the value defined in `codegenConfig.outputDir`.

## Examples

- Read `package.json` from the current working directory, generate code based on its codegenConfig.

```shell
npx react-native codegen
npx @react-native-codegen/cli codegen
```

- Read `package.json` from the current working directory, generate iOS code in the location defined in the codegenConfig.

```shell
npx react-native codegen --platform ios
npx @react-native-codegen/cli codegen --platform ios
```

- Read `package.json` from `third-party/some-library`, generate Android code in `third-party/some-library/android/generated`.

```shell
npx react-native codegen \
npx @react-native-codegen/cli codegen \
--path third-party/some-library \
--platform android \
--outputPath third-party/some-library/android/generated
Expand Down
19 changes: 13 additions & 6 deletions docs/the-new-architecture/using-codegen.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,20 @@ This structure has been generated by using the value `all` for the `codegenConfi

**Codegen** for iOS relies on some Node scripts that are invoked during the build process. The scripts are located in the `SampleApp/node_modules/react-native/scripts/` folder.

The main script is the `generate-Codegen-artifacts.js` script. To invoke the script, you can run this command from the root folder of your app:
The main script is the `generate-codegen-artifacts.js` script. To invoke the script, you can run this command from the root folder of your app:

```bash
node node_modules/react-native/scripts/generate-Codegen-artifacts.js \
blakef marked this conversation as resolved.
Show resolved Hide resolved
--path <path/to/your/app> \
--outputPath <an/output/path> \
--targetPlatform <android | ios>
node node_modules/react-native/scripts/generate-codegen-artifacts.js

Usage: generate-codegen-artifacts.js -p [path to app] -t [target platform] -o [output path]

Options:
--help Show help [boolean]
--version Show version number [boolean]
-p, --path Path to the React Native project root. [required]
blakef marked this conversation as resolved.
Show resolved Hide resolved
-t, --targetPlatform Target platform. Supported values: "android", "ios",
"all". [required]
-o, --outputPath Path where generated artifacts will be output to.
```

where:
Expand All @@ -152,7 +159,7 @@ where:
Running the script with these arguments:

```shell
node node_modules/react-native/scripts/generate-Codegen-artifacts.js \
blakef marked this conversation as resolved.
Show resolved Hide resolved
node node_modules/react-native/scripts/generate-codegen-artifacts.js \
--path . \
--outputPath ios/ \
--targetPlatform ios
Expand Down
Loading