Skip to content

Instantly share code, notes, and snippets.

@mununki
Created February 6, 2023 08:42
Show Gist options
  • Save mununki/5ae11a1221666d75962e791a5c30cf9f to your computer and use it in GitHub Desktop.
Save mununki/5ae11a1221666d75962e791a5c30cf9f to your computer and use it in GitHub Desktop.
type rec navigation
and route = {@as("key") routeKey: string, name: string, params: params, state: state}
and params = {url?: string}
and state = {
@as("type") _type?: string,
@as("key") stateKey: string,
routeNames: array<string>,
routes: array<route>,
index: int,
stale: bool,
}
module CommonActions: {
type t
let navigate: (~name: Screens.t, ~params: params=?, unit) => t
let reset: state => t
let goBack: unit => t
} = {
type t
type commonActions
@module("@react-navigation/native")
external make: commonActions = "CommonActions"
@send
external _navigate: (commonActions, ~name: Screens.t, ~params: params=?, unit) => t = "navigate"
let navigate = make->_navigate
@send external _reset: (commonActions, state) => t = "reset"
let reset = make->_reset
@send external _goBack: (commonActions, unit) => t = "goBack"
let goBack = make->_goBack
}
module StackActions: {
type t
let replace: (~name: Screens.t, ~params: params=?, unit) => t
let push: (~name: Screens.t, ~params: params=?, unit) => t
let pop: int => t
let popToTop: unit => t
} = {
type t
type stackActions
@module("@react-navigation/native")
external make: stackActions = "StackActions"
@send
external _replace: (stackActions, ~name: Screens.t, ~params: params=?, unit) => t = "replace"
let replace = make->_replace
@send
external _push: (stackActions, ~name: Screens.t, ~params: params=?, unit) => t = "push"
let push = make->_push
@send external _pop: (stackActions, int) => t = "pop"
let pop = make->_pop
@send external _popToTop: (stackActions, unit) => t = "popToTop"
let popToTop = make->_popToTop
}
module TabActions: {
type t
let jumpTo: (~name: Screens.t, ~params: params=?, unit) => t
} = {
type t
type tabActions
@module("@react-navigation/native")
external make: tabActions = "TabActions"
@send
external _jumpTo: (tabActions, ~name: Screens.t, ~params: params=?, unit) => t = "jumpTo"
let jumpTo = make->_jumpTo
}
module NavigationContainer = {
@module("@react-navigation/native") @react.component
external make: (~children: React.element) => React.element = "NavigationContainer"
}
module Stack = {
type rec t
and navigatorProps = {
initialRouteName?: Screens.t,
children: React.element,
screenOptions?: screenOptions,
}
and screenOptions = {headerShown?: bool}
and screenProps = {name: string, component: React.component<componentProps>}
and componentProps = {navigation: navigation, route: route}
@module("@react-navigation/native-stack")
external make: unit => {
"Navigator": React.component<navigatorProps>,
"Screen": React.component<screenProps>,
} = "createNativeStackNavigator"
module Navigator = {
let make = make()["Navigator"]
}
module Screen = {
let make = make()["Screen"]
}
module Navigation = {
@send
external navigate: (navigation, ~name: Screens.t, ~params: params=?, unit) => unit = "navigate"
@send
external goBack: navigation => unit = "goBack"
@send
external replace: (navigation, ~name: Screens.t, ~params: params=?, unit) => unit = "replace"
@send external push: (navigation, ~name: Screens.t, ~params: params=?, unit) => unit = "push"
@send external pop: (navigation, int) => unit = "pop"
@send external popToTop: navigation => unit = "popToTop"
@send external dispatchCommonAction: (navigation, CommonActions.t) => unit = "dispatch"
}
}
module Tab = {
type rec t
and navigatorProps = {
initialRouteName?: Screens.t,
children: React.element,
screenOptions?: screenOptions,
}
and screenOptions = {headerShown?: bool}
and screenProps = {name: string, component: React.component<componentProps>}
and componentProps
@module("@react-navigation/bottom-tabs")
external make: unit => {
"Navigator": React.component<navigatorProps>,
"Screen": React.component<screenProps>,
} = "createBottomTabNavigator"
module Navigator = {
let make = make()["Navigator"]
}
module Screen = {
let make = make()["Screen"]
}
module Navigation = {
@send
external navigate: (navigation, ~name: Screens.t, ~params: params=?, unit) => unit = "navigate"
@send
external goBack: navigation => unit = "goBack"
@send
external jumpTo: (navigation, ~name: Screens.t, ~params: params=?, unit) => unit = "jumpTo"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment