expo-module-template 10.5.0 → 10.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/$package.json CHANGED
@@ -13,7 +13,12 @@
13
13
  "prepublishOnly": "expo-module prepublishOnly",
14
14
  "expo-module": "expo-module"
15
15
  },
16
- "keywords": ["react-native", "expo", "<%- project.slug %>", "<%- project.name %>"],
16
+ "keywords": [
17
+ "react-native",
18
+ "expo",
19
+ "<%- project.slug %>",
20
+ "<%- project.name %>"
21
+ ],
17
22
  "repository": "<%- repo %>",
18
23
  "bugs": {
19
24
  "url": "<%- repo %>/issues"
@@ -24,9 +29,11 @@
24
29
  "dependencies": {},
25
30
  "devDependencies": {
26
31
  "expo-module-scripts": "^2.0.0",
27
- "expo-modules-core": "^0.9.0"
32
+ "expo-modules-core": "^0.11.3"
28
33
  },
29
34
  "peerDependencies": {
30
- "expo": "*"
35
+ "expo": "*",
36
+ "react": "*",
37
+ "react-native": "*"
31
38
  }
32
39
  }
@@ -4,18 +4,47 @@ import expo.modules.kotlin.modules.Module
4
4
  import expo.modules.kotlin.modules.ModuleDefinition
5
5
 
6
6
  class <%- project.name %>Module : Module() {
7
+ // Each module class must implement the definition function. The definition consists of components
8
+ // that describes the module's functionality and behavior.
9
+ // See https://docs.expo.dev/modules/module-api for more details about available components.
7
10
  override fun definition() = ModuleDefinition {
11
+ // Sets the name of the module that JavaScript code will use to refer to the module. Takes a string as an argument.
12
+ // Can be inferred from module's class name, but it's recommended to set it explicitly for clarity.
13
+ // The module will be accessible from `requireNativeModule('<%- project.name %>')` in JavaScript.
8
14
  Name("<%- project.name %>")
9
15
 
10
- AsyncFunction("helloAsync") { options: Map<String, String> ->
11
- println("Hello 👋")
16
+ // Sets constant properties on the module. Can take a dictionary or a closure that returns a dictionary.
17
+ Constants(
18
+ "PI" to Math.PI
19
+ )
20
+
21
+ // Defines event names that the module can send to JavaScript.
22
+ Events("onChange")
23
+
24
+ // Defines a JavaScript synchronous function that runs the native code on the JavaScript thread.
25
+ Function("hello") {
26
+ "Hello world! 👋"
27
+ }
28
+
29
+ // Defines a JavaScript function that always returns a Promise and whose native code
30
+ // is by default dispatched on the different thread than the JavaScript runtime runs on.
31
+ AsyncFunction("setValueAsync") { value: String ->
32
+ // Send an event to JavaScript.
33
+ sendEvent("onChange", mapOf(
34
+ "value" to value
35
+ ))
12
36
  }
13
37
 
38
+ // Enables the module to be used as a view manager. The view manager definition is built from
39
+ // the definition components used in the closure passed to viewManager.
40
+ // Definition components that are accepted as part of the view manager definition: `View`, `Prop`.
14
41
  ViewManager {
42
+ // Defines the factory creating a native view when the module is used as a view.
15
43
  View { context ->
16
44
  <%- project.name %>View(context)
17
45
  }
18
46
 
47
+ // Defines a setter for the `name` prop.
19
48
  Prop("name") { view: <%- project.name %>View, prop: String ->
20
49
  println(prop)
21
50
  }
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "platforms": ["ios", "android", "web"],
3
3
  "ios": {
4
- "modulesClassNames": ["<%- project.name %>Module"]
4
+ "modules": ["<%- project.name %>Module"]
5
5
  },
6
6
  "android": {
7
- "modulesClassNames": ["<%- project.package %>.<%- project.name %>Module"]
7
+ "modules": ["<%- project.package %>.<%- project.name %>Module"]
8
8
  }
9
9
  }
@@ -1,18 +1,47 @@
1
1
  import ExpoModulesCore
2
2
 
3
3
  public class <%- project.name %>Module: Module {
4
+ // Each module class must implement the definition function. The definition consists of components
5
+ // that describes the module's functionality and behavior.
6
+ // See https://docs.expo.dev/modules/module-api for more details about available components.
4
7
  public func definition() -> ModuleDefinition {
8
+ // Sets the name of the module that JavaScript code will use to refer to the module. Takes a string as an argument.
9
+ // Can be inferred from module's class name, but it's recommended to set it explicitly for clarity.
10
+ // The module will be accessible from `requireNativeModule('<%- project.name %>')` in JavaScript.
5
11
  Name("<%- project.name %>")
6
12
 
7
- AsyncFunction("helloAsync") { (options: [String: String]) in
8
- print("Hello 👋")
13
+ // Sets constant properties on the module. Can take a dictionary or a closure that returns a dictionary.
14
+ Constants([
15
+ "PI": Double.pi
16
+ ])
17
+
18
+ // Defines event names that the module can send to JavaScript.
19
+ Events("onChange")
20
+
21
+ // Defines a JavaScript synchronous function that runs the native code on the JavaScript thread.
22
+ Function("hello") {
23
+ return "Hello world! 👋"
24
+ }
25
+
26
+ // Defines a JavaScript function that always returns a Promise and whose native code
27
+ // is by default dispatched on the different thread than the JavaScript runtime runs on.
28
+ AsyncFunction("setValueAsync") { (value: String) in
29
+ // Send an event to JavaScript.
30
+ self.sendEvent("onChange", [
31
+ "value": value
32
+ ])
9
33
  }
10
34
 
35
+ // Enables the module to be used as a view manager. The view manager definition is built from
36
+ // the definition components used in the closure passed to viewManager.
37
+ // Definition components that are accepted as part of the view manager definition: `View`, `Prop`.
11
38
  ViewManager {
39
+ // Defines the factory creating a native view when the module is used as a view.
12
40
  View {
13
41
  <%- project.name %>View()
14
42
  }
15
43
 
44
+ // Defines a setter for the `name` prop.
16
45
  Prop("name") { (view: <%- project.name %>View, prop: String) in
17
46
  print(prop)
18
47
  }
@@ -1,5 +1,7 @@
1
- import UIKit
1
+ import ExpoModulesCore
2
2
 
3
- class <%- project.name %>View: UIView {
3
+ // This view will be used as a native component. Make sure to inherit from `ExpoView`
4
+ // to apply the proper styling (e.g. border radius and shadows).
5
+ class <%- project.name %>View: ExpoView {
4
6
 
5
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-module-template",
3
- "version": "10.5.0",
3
+ "version": "10.7.1",
4
4
  "description": "ExpoModuleTemplate standalone module",
5
5
  "main": "build/ModuleTemplate.js",
6
6
  "types": "build/ModuleTemplate.d.ts",
@@ -24,5 +24,5 @@
24
24
  "dependencies": {},
25
25
  "devDependencies": {},
26
26
  "peerDependencies": {},
27
- "gitHead": "0df2d4a2b2d494826ebd4355d4246bc66781c5b2"
27
+ "gitHead": "5d900179d047d9f4d89c0b9953b7121a1f1df8a2"
28
28
  }
@@ -1,14 +1,29 @@
1
- import { NativeModulesProxy } from 'expo-modules-core';
1
+ import { NativeModulesProxy, EventEmitter, Subscription } from 'expo-modules-core';
2
2
 
3
- import <%- project.name %>View, { <%- project.name %>ViewProps } from './<%- project.name %>View'
3
+ // Import the native module. On web, it will be resolved to <%- project.name %>.web.ts
4
+ // and on native platforms to <%- project.name %>.ts
5
+ import <%- project.name %> from './<%- project.name %>Module';
6
+ import <%- project.name %>View from './<%- project.name %>View';
7
+ import { ChangeEventPayload, <%- project.name %>ViewProps } from './<%- project.name %>.types';
4
8
 
5
- const { <%- project.name %> } = NativeModulesProxy;
9
+ // Get the native constant value.
10
+ export const PI = <%- project.name %>.PI;
6
11
 
7
- export async function helloAsync(options: Record<string, string>) {
8
- return await <%- project.name %>.helloAsync(options);
12
+ export function hello(): string {
13
+ return <%- project.name %>.hello();
9
14
  }
10
15
 
11
- export {
12
- <%- project.name %>View,
13
- <%- project.name %>ViewProps
14
- };
16
+ export async function setValueAsync(value: string) {
17
+ return await <%- project.name %>.setValueAsync(value);
18
+ }
19
+
20
+ // For now the events are not going through the JSI, so we have to use its bridge equivalent.
21
+ // This will be fixed in the stable release and built into the module object.
22
+ // Note: On web, NativeModulesProxy.<%- project.name %> is undefined, so we fall back to the directly imported implementation
23
+ const emitter = new EventEmitter(NativeModulesProxy.<%- project.name %> ?? <%- project.name %>);
24
+
25
+ export function addChangeListener(listener: (event: ChangeEventPayload) => void): Subscription {
26
+ return emitter.addListener<ChangeEventPayload>('onChange', listener);
27
+ }
28
+
29
+ export { <%- project.name %>View, <%- project.name %>ViewProps, ChangeEventPayload };
@@ -0,0 +1,7 @@
1
+ export type ChangeEventPayload = {
2
+ value: string;
3
+ };
4
+
5
+ export type <%- project.name %>ViewProps = {
6
+ name: string;
7
+ };
@@ -0,0 +1,5 @@
1
+ import { requireNativeModule } from 'expo-modules-core';
2
+
3
+ // It loads the native module object from the JSI or falls back to
4
+ // the bridge module (from NativeModulesProxy) if the remote debugger is on.
5
+ export default requireNativeModule('<%- project.name %>');
@@ -0,0 +1,13 @@
1
+ import { EventEmitter } from 'expo-modules-core';
2
+
3
+ const emitter = new EventEmitter({} as any);
4
+
5
+ export default {
6
+ PI: Math.PI,
7
+ async setValueAsync(value: string): Promise<void> {
8
+ emitter.emit('onChange', { value });
9
+ },
10
+ hello() {
11
+ return 'Hello world! 👋';
12
+ },
13
+ };
@@ -1,17 +1,11 @@
1
1
  import { requireNativeViewManager } from 'expo-modules-core';
2
2
  import * as React from 'react';
3
3
 
4
- export type <%- project.name %>ViewProps = {
5
- name: number;
6
- };
7
-
8
- type <%- project.name %>ViewState = {}
4
+ import { <%- project.name %>ViewProps } from './<%- project.name %>.types';
9
5
 
10
6
  const NativeView: React.ComponentType<<%- project.name %>ViewProps> =
11
7
  requireNativeViewManager('<%- project.name %>');
12
8
 
13
- export default class <%- project.name %>View extends React.Component<<%- project.name %>ViewProps, <%- project.name %>ViewState> {
14
- render() {
15
- return <NativeView name={this.props.name} />;
16
- }
9
+ export default function <%- project.name %>View(props: <%- project.name %>ViewProps) {
10
+ return <NativeView name={props.name} />;
17
11
  }
@@ -0,0 +1,13 @@
1
+ import * as React from 'react';
2
+
3
+ import { <%- project.name %>ViewProps } from './<%- project.name %>.types';
4
+
5
+ function <%- project.name %>WebView(props: <%- project.name %>ViewProps) {
6
+ return (
7
+ <div>
8
+ <span>{props.name}</span>
9
+ </div>
10
+ );
11
+ }
12
+
13
+ export default <%- project.name %>WebView;
package/tsconfig.json CHANGED
@@ -5,5 +5,5 @@
5
5
  "outDir": "./build"
6
6
  },
7
7
  "include": ["./src"],
8
- "exclude": ["**/__mocks__/*", "**/__tests__/*"]
8
+ "exclude": ["**/__mocks__/*", "**/__tests__/*", "**/__stories__/*"]
9
9
  }
package/babel.config.js DELETED
@@ -1,6 +0,0 @@
1
- module.exports = function (api) {
2
- api.cache(true);
3
- return {
4
- presets: ['babel-preset-expo'],
5
- };
6
- };