Skip to content

Instantly share code, notes, and snippets.

@mozinator
Last active June 6, 2017 17:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mozinator/cb7076521f14cbbef861491fb74f92e2 to your computer and use it in GitHub Desktop.
Save mozinator/cb7076521f14cbbef861491fb74f92e2 to your computer and use it in GitHub Desktop.
(def react-navigation (js/require "react-navigation"))
; Core
(def createNavigationContainer (.-createNavigationContainer react-navigation))
(def StateUtils (.-StateUtils react-navigation))
(def PropTypes (.-PropTypes react-navigation))
(def addNavigationHelpers (.-addNavigationHelpers react-navigation))
(def NavigationActions (.-NavigationActions react-navigation))
; Navigators
(def createNavigator (.-createNavigator react-navigation))
(def StackNavigator (.-StackNavigator react-navigation))
(def TabNavigator (.-TabNavigator react-navigation))
(def DrawerNavigator (.-DrawerNavigator react-navigation))
; Routers
(def StackRouter (.-StackRouter react-navigation))
(def TabRouter (.-TabRouter react-navigation))
; Views
(def Transitioner (.-Transitioner react-navigation))
(def CardStack (.-CardStack react-navigation))
(def DrawerView (.-DrawerView react-navigation))
(def TabView (.-TabView react-navigation))
(def NavigationActionsBACK (aget NavigationActions "BACK"))
(def NavigationActionsINIT (aget NavigationActions "INIT"))
(def NavigationActionsNAVIGATE (aget NavigationActions "NAVIGATE"))
(def NavigationActionsRESET (aget NavigationActions "RESET"))
(def NavigationActionsSET_PARAMS (aget NavigationActions "SET_PARAMS"))
(def NavigationActionsURI (aget NavigationActions "URI"))
(def back (aget NavigationActions "back"))
(def init (aget NavigationActions "init"))
(def navigate (aget NavigationActions "navigate"))
(def reset (aget NavigationActions "reset"))
(def setParams (aget NavigationActions "setParams"))
(def uri (aget NavigationActions "uri"))
(defn getStateForAction [router action]
(.getStateForAction router action))
(defn getActionForPathAndParams [router path params]
(.getActionForPathAndParams router path params))
(defn getPathAndParamsForState [router state]
(.getPathAndParamsForState router state))
(defn getComponentForState [router state]
(.getComponentForState router state))
(defn getComponentForRouteName [router route-name]
(.getComponentForRouteName router route-name))
(defn getRouter [navigator]
(.-router navigator))
(assert react-navigation)
(assert createNavigationContainer)
(assert StateUtils)
(assert PropTypes)
(assert addNavigationHelpers)
(assert NavigationActions)
(assert createNavigator)
(assert StackNavigator)
(assert TabNavigator)
(assert DrawerNavigator)
(assert StackRouter)
(assert TabRouter)
(assert Transitioner)
(assert CardStack)
(assert DrawerView)
(assert TabView)
(assert NavigationActionsBACK)
(assert NavigationActionsINIT)
(assert NavigationActionsNAVIGATE)
(assert NavigationActionsRESET)
(assert NavigationActionsSET_PARAMS)
(assert NavigationActionsURI)
(assert back)
(assert init)
(assert navigate)
(assert reset)
(assert setParams)
(assert uri)
(defonce drawer-navigator-instance (r/atom nil))
(defonce stack-navigator-instance (r/atom nil))
(do
(re/reg-fx
:nav-drawer-dispatch
(fn nav-drawer-dispatch-fx [{:keys [action]}]
(if @drawer-navigator-instance
(.dispatch @drawer-navigator-instance (clj->js action)))))
(re/reg-fx
:nav-stack-dispatch
(fn nav-stack-dispatch-fx [{:keys [action]}]
(if @stack-navigator-instance
(.dispatch @stack-navigator-instance (clj->js action))))))
(defn wrap-navigation [component {:keys [title
left
right]
:or {title (fn wrap-navigation-title-fn [a b] (str "title" a))
left nil
right nil}}]
(let [c (r/reactify-component
(fn wrap-navigation-r [{:keys [navigation]}]
[component (navigation->state navigation)]))]
(aset c "navigationOptions"
(clj->js {:title (fn wrap-navigation-title-fn [navigation]
(let [state (navigation->state navigation)
{:keys [params routeName]} state]
(if c/debug?
(str routeName " " params)
(if title (title state) nil))))
:header (fn wrap-navigation-header-fn [navigation]
(clj->js
(merge
{:titleStyle (merge {:color "#FFF"}
(if c/debug? {:fontSize 10} {}))
:style {:backgroundColor c/color-light-blue}}
(if left {:left (left (navigation->state navigation))} {})
(if right {:right (right (navigation->state navigation))} {}))))}))
c))
(defn account-profile [props]
(let [user (re/subscribe [:user])
{:keys [first_name last_name email]} @user]
(fn account-profile-r [props]
[rn/view {:style {:flex-direction "column"}}
[ui/label {:title first_name}]
(def account-profile-screen
(wrap-navigation
account-profile
{:title #(t :profile)})))
(def stack-router {:account-profile {:screen account-profile-screen}
(def navigator-config {:initialRouteName "goal-list"
:headerMode "screen"})
(def sn (rn/make-stack-navigator stack-router navigator-config))
(defn index-comp [props]
(fn index-comp-r [props]
[sn {:ref (fn sn-ref [r] (reset! stack-navigator-instance r))}]))
(defonce index-screen
(wrap-navigation
index-comp
{:drawer (fn index-screen-drawer-fn []
(clj->js {:label "Index"}))}))
(def drawer-router
{:Index {:screen index-screen}})
(def drawer-navigator-config
{:drawerWidth 300
:drawerPosition "left"
:initialRouteName "Index"
:contentComponent (fn content-component-fn [navigation]
(r/as-element [drawer-content]))})
(defn drawer-nav-inst []
(rn/make-drawer-navigator drawer-router drawer-navigator-config))
(defonce dn
(let [dni (drawer-nav-inst)]
(aset dni "router" "getStateForAction"
(fn dn-get-state-for-action [a]
(let [new-state (rn/navigator-state-for-action (drawer-nav-inst) a)]
(re/dispatch [:nav/get-state-for-action new-state])
new-state)))
(aset dni "router" "getActionForPathAndParams"
(fn dn-get-action-for-path-and-params [path params]
(let [new-action (rn/navigator-action-for-path-and-params (drawer-nav-inst) path params)]
(re/dispatch [:nav/get-action-for-path-and-params new-action])
new-action)))
(aset dni "router" "getPathAndParamsForState"
(fn dn-get-path-and-params-for-state [state]
(let [new-path-and-params (rn/navigator-path-and-params-for-state (drawer-nav-inst) state)]
(re/dispatch [:nav/get-path-and-params-for-state new-path-and-params])
new-path-and-params)))
(aset dni "router" "getComponentForState"
(fn dn-get-component-for-state [state]
(let [new-component (rn/navigator-component-for-state (drawer-nav-inst) state)]
(re/dispatch [:nav/get-component-for-state new-component])
new-component)))
(aset dni "router" "getComponentForRouteName"
(fn dn-get-component-for-route-name [route-name]
(let [new-component (rn/navigator-component-for-route-name (drawer-nav-inst) route-name)]
(re/dispatch [:nav/get-component-for-route-name new-component])
new-component)))
(r/adapt-react-class dni)))
(defn app-root []
(fn app-root-r []
[rn/view {:style {:flex 1}}
[dn {:ref (fn dn-ref [r] (reset! drawer-navigator-instance r))}]]
@seantempesta
Copy link

This is pretty cool. Is there another file this is referring to? I keep seeing (rn/ references.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment