From d4ed5a898da14634b25d68258df7fa159fad3952 Mon Sep 17 00:00:00 2001 From: Freddy Harris Date: Thu, 12 Dec 2024 16:20:58 +0100 Subject: [PATCH] Linking (#71) * linking config for NavigationContainer * add example linking config --- src/Example.bs.js | 43 +++++++++++++++++++++++++++++++++++++++++++ src/Example.res | 33 ++++++++++++++++++++++++++++++++- src/Native.bs.js | 3 +++ src/Native.res | 17 +++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) diff --git a/src/Example.bs.js b/src/Example.bs.js index c0119c5..1c517ab 100644 --- a/src/Example.bs.js +++ b/src/Example.bs.js @@ -1,6 +1,7 @@ 'use strict'; var React = require("react"); +var Js_dict = require("rescript/lib/js/js_dict.js"); var Caml_option = require("rescript/lib/js/caml_option.js"); var ReactNative = require("react-native"); var Stack$ReactNavigation = require("./Stack.bs.js"); @@ -71,8 +72,49 @@ var $$Navigator$1 = include$1.$$Navigator; var $$Screen$1 = include$1.$$Screen; +var linking_prefixes = ["https://www.example.com"]; + +var linking_config = { + initialRouteName: "app", + screens: Js_dict.fromArray([[ + "app", + { + screens: Js_dict.fromArray([ + [ + "tab1", + { + screens: Js_dict.fromArray([[ + "home", + { + path: "" + } + ]]) + } + ], + [ + "tab2", + { + screens: Js_dict.fromArray([[ + "config", + { + path: "/config" + } + ]]) + } + ] + ]) + } + ]]) +}; + +var linking = { + prefixes: linking_prefixes, + config: linking_config +}; + function Example$RootStackScreen(props) { return React.createElement(Native.NavigationContainer, { + linking: linking, children: React.createElement($$Navigator$1.make, { screenOptions: (function (param) { return { @@ -101,6 +143,7 @@ var RootStackScreen = { $$Navigator: $$Navigator$1, $$Screen: $$Screen$1, Group: RootStackScreen_Group, + linking: linking, make: Example$RootStackScreen }; diff --git a/src/Example.res b/src/Example.res index fac9e3e..1a95089 100644 --- a/src/Example.res +++ b/src/Example.res @@ -42,9 +42,40 @@ module MainStackScreen = { module RootStackScreen = { include Stack.Make() + let linking = { + open Native.Linking + { + prefixes: ["https://www.example.com"], + config: { + initialRouteName: "app", + screens: [ + ( + "app", + { + screens: [ + ( + "tab1", + { + screens: [("home", {path: ""})]->Js.Dict.fromArray, + }, + ), + ( + "tab2", + { + screens: [("config", {path: "/config"})]->Js.Dict.fromArray, + }, + ), + ]->Js.Dict.fromArray, + }, + ), + ]->Js.Dict.fromArray, + }, + } + } + @react.component let make = () => - + {presentation: #modal}}> diff --git a/src/Native.bs.js b/src/Native.bs.js index c2f2abe..be7fc2e 100644 --- a/src/Native.bs.js +++ b/src/Native.bs.js @@ -1,6 +1,8 @@ 'use strict'; +var Linking = {}; + var NavigationContainer = {}; var ServerContainer = {}; @@ -9,6 +11,7 @@ var CommonActions = {}; var Link = {}; +exports.Linking = Linking; exports.NavigationContainer = NavigationContainer; exports.ServerContainer = ServerContainer; exports.CommonActions = CommonActions; diff --git a/src/Native.res b/src/Native.res index 3363a0b..da332ee 100644 --- a/src/Native.res +++ b/src/Native.res @@ -19,6 +19,22 @@ external darkTheme: theme = "DarkTheme" @module("@react-navigation/native") external useTheme: unit => theme = "useTheme" +module Linking = { + type rec config = { + path?: string, + exact?: bool, + initialRouteName?: string, + screens?: screens, + } + and screens = dict + + type t = { + enabled?: bool, + prefixes: array, + config?: config, + } +} + module NavigationContainer = { type state = Js.Json.t type navigationState = state => unit @@ -30,6 +46,7 @@ module NavigationContainer = { ~onStateChange: navigationState=?, ~onReady: unit => unit=?, ~theme: theme=?, + ~linking: Linking.t=?, ~children: React.element, ~independent: bool=?, ) => React.element = "NavigationContainer"