diff --git a/packages/create-react-native-library/templates/native-library-legacy/src/index.tsx b/packages/create-react-native-library/templates/native-library-legacy/src/index.tsx new file mode 100644 index 000000000..651971c33 --- /dev/null +++ b/packages/create-react-native-library/templates/native-library-legacy/src/index.tsx @@ -0,0 +1,22 @@ +import { NativeModules, Platform } from 'react-native'; + +const LINKING_ERROR = + `The package '<%- project.slug -%>' doesn't seem to be linked. Make sure: \n\n` + + Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + + '- You rebuilt the app after installing the package\n' + + '- You are not using Expo Go\n'; + +const <%- project.name -%> = NativeModules.<%- project.name %> + ? NativeModules.<%- project.name %> + : new Proxy( + {}, + { + get() { + throw new Error(LINKING_ERROR); + }, + } + ); + +export function multiply(a: number, b: number): Promise { + return <%- project.name -%>.multiply(a, b); +} diff --git a/packages/create-react-native-library/templates/native-library-mixed/src/Native{%- project.name %}.ts b/packages/create-react-native-library/templates/native-library-mixed/src/Native{%- project.name %}.ts new file mode 100644 index 000000000..166b44ed7 --- /dev/null +++ b/packages/create-react-native-library/templates/native-library-mixed/src/Native{%- project.name %}.ts @@ -0,0 +1,8 @@ +import type { TurboModule } from 'react-native'; +import { TurboModuleRegistry } from 'react-native'; + +export interface Spec extends TurboModule { + multiply(a: number, b: number): Promise; +} + +export default TurboModuleRegistry.getEnforcing('<%- project.name -%>'); diff --git a/packages/create-react-native-library/templates/native-library-mixed/src/index.tsx b/packages/create-react-native-library/templates/native-library-mixed/src/index.tsx new file mode 100644 index 000000000..c583391a5 --- /dev/null +++ b/packages/create-react-native-library/templates/native-library-mixed/src/index.tsx @@ -0,0 +1,29 @@ +import { NativeModules, Platform } from 'react-native'; + +const LINKING_ERROR = + `The package '<%- project.slug -%>' doesn't seem to be linked. Make sure: \n\n` + + Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + + '- You rebuilt the app after installing the package\n' + + '- You are not using Expo Go\n'; + +// @ts-expect-error +const isTurboModuleEnabled = global.__turboModuleProxy != null; + +const <%- project.name -%>Module = isTurboModuleEnabled + ? require('./Native<%- project.name -%>').default + : NativeModules.<%- project.name -%>; + +const <%- project.name -%> = <%- project.name -%>Module + ? <%- project.name -%>Module + : new Proxy( + {}, + { + get() { + throw new Error(LINKING_ERROR); + }, + } + ); + +export function multiply(a: number, b: number): Promise { + return <%- project.name -%>.multiply(a, b); +} diff --git a/packages/create-react-native-library/templates/native-library-new/src/Native{%- project.name %}.ts b/packages/create-react-native-library/templates/native-library-new/src/Native{%- project.name %}.ts new file mode 100644 index 000000000..b6f84feb8 --- /dev/null +++ b/packages/create-react-native-library/templates/native-library-new/src/Native{%- project.name %}.ts @@ -0,0 +1,8 @@ +import type { TurboModule } from 'react-native'; +import { TurboModuleRegistry } from 'react-native'; + +export interface Spec extends TurboModule { + multiply(a: number, b: number): number; +} + +export default TurboModuleRegistry.getEnforcing('<%- project.name -%>'); diff --git a/packages/create-react-native-library/templates/native-library-new/src/index.tsx b/packages/create-react-native-library/templates/native-library-new/src/index.tsx new file mode 100644 index 000000000..adc8b3076 --- /dev/null +++ b/packages/create-react-native-library/templates/native-library-new/src/index.tsx @@ -0,0 +1,5 @@ +const <%- project.name -%> = require('./Native<%- project.name -%>').default; + +export function multiply(a: number, b: number): number { + return <%- project.name -%>.multiply(a, b); +} diff --git a/packages/create-react-native-library/templates/native-view-legacy/src/index.tsx b/packages/create-react-native-library/templates/native-view-legacy/src/index.tsx new file mode 100644 index 000000000..055016821 --- /dev/null +++ b/packages/create-react-native-library/templates/native-view-legacy/src/index.tsx @@ -0,0 +1,26 @@ +import { + requireNativeComponent, + UIManager, + Platform, + type ViewStyle, +} from 'react-native'; + +const LINKING_ERROR = + `The package '<%- project.slug -%>' doesn't seem to be linked. Make sure: \n\n` + + Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + + '- You rebuilt the app after installing the package\n' + + '- You are not using Expo Go\n'; + +type <%- project.name -%>Props = { + color: string; + style: ViewStyle; +}; + +const ComponentName = '<%- project.name -%>View'; + +export const <%- project.name -%>View = + UIManager.getViewManagerConfig(ComponentName) != null + ? requireNativeComponent<<%- project.name -%>Props>(ComponentName) + : () => { + throw new Error(LINKING_ERROR); + };