diff --git a/README.md b/README.md index f4b8776..55bfdcd 100644 --- a/README.md +++ b/README.md @@ -435,11 +435,13 @@ You can optionally include your own error-handling component function as the fir ## Scroll behavior on navigation In a traditional static website, the browser handles the scrolling for you nicely. Meaning that when you navigate back and forward, the browser "remembers" how far down you scrolled on the last visit. This is convenient for many websites, -so Kee-frame utilizes a third-party JS lib to get this behavior for a SPA. The only thing you need to do is this in -your main namespace: +so Kee-frame utilizes a third-party JS lib to get this behavior for a SPA. This functionality is enabled by default, +but if you want to disable it just pass the following to `kf/start!`: ```clojure -(:require [kee-frame.scroll]) +(k/start! {:scroll false + ;; Other settings here + }) ``` ## Credits diff --git a/src/kee_frame/router.cljc b/src/kee_frame/router.cljc index 293c093..b45754b 100644 --- a/src/kee_frame/router.cljc +++ b/src/kee_frame/router.cljc @@ -81,7 +81,8 @@ (some->> not-found (match-url routes)) (route-match-not-found routes url)))) -(defn bootstrap-routes [{:keys [routes router hash-routing? scroll route-change-event not-found]}] +(defn bootstrap-routes [{:keys [routes router hash-routing? scroll route-change-event not-found] + :or {scroll true}}] (let [initialized? (boolean @state/navigator) router (or router (->ReititRouter (reitit/router routes) hash-routing? not-found))] (reset! state/router router) @@ -100,13 +101,9 @@ (rf/reg-event-fx ::route-changed [event-logger/interceptor] (fn [{:keys [db] :as ctx} [_ route]] - (when scroll - (scroll/monitor-requests! route)) (let [{:keys [update-controllers dispatch-n]} (controller/controller-effects @state/controllers ctx route)] - (cond-> {:db (assoc db :kee-frame/route route) - :dispatch-later [(when scroll - {:ms 50 - :dispatch [::scroll/poll route 0]})]} + (cond-> {:db (assoc db :kee-frame/route route)} + scroll (scroll/monitor-requests! route) dispatch-n (assoc :dispatch-n dispatch-n) update-controllers (assoc :update-controllers update-controllers)))))) diff --git a/src/kee_frame/scroll.clj b/src/kee_frame/scroll.clj index 774d002..e990b2b 100644 --- a/src/kee_frame/scroll.clj +++ b/src/kee_frame/scroll.clj @@ -2,4 +2,4 @@ (defn start! []) -(defn monitor-requests! [_]) \ No newline at end of file +(defn monitor-requests! [& _]) diff --git a/src/kee_frame/scroll.cljs b/src/kee_frame/scroll.cljs index fc7a862..929adaa 100644 --- a/src/kee_frame/scroll.cljs +++ b/src/kee_frame/scroll.cljs @@ -5,27 +5,27 @@ [clerk.core :as clerk])) (rf/reg-event-db ::connection-balance - (fn [db [_ route inc-or-dec]] - (-> db - (assoc-in [:route-counter :route] route) - (update-in [:route-counter :balance] inc-or-dec)))) + (fn [db [_ inc-or-dec]] + (update db ::route-counter inc-or-dec))) (defn start! [] - (clerk/initialize!)) - -(defn monitor-requests! [route] - (clerk/navigate-page! (:path route)) + (clerk/initialize!) (swap! ajax/default-interceptors (fn [interceptors] (conj (filter #(not= "route-interceptor" (:name %)) interceptors) (ajax/to-interceptor {:name "route-interceptor" :request (fn [request] - (rf/dispatch [::connection-balance route inc]) + (rf/dispatch [::connection-balance inc]) request) :response (fn [response] - (rf/dispatch [::connection-balance route dec]) + (rf/dispatch [::connection-balance dec]) response)}))))) +(defn monitor-requests! [fx route] + (clerk/navigate-page! (:path route)) + (assoc fx :dispatch-later [{:ms 50 + :dispatch [::poll route 0]}])) + (rf/reg-event-fx ::scroll (fn [_ _] (r/after-render clerk/after-render!) @@ -33,11 +33,12 @@ (rf/reg-event-fx ::poll (fn [{:keys [db]} [_ active-route counter]] - (let [{:keys [route balance]} (:route-counter db)] + (let [route (:kee-frame/route db) + balance (::route-counter db)] (when (= route active-route) (cond (not (pos? balance)) {:dispatch [::scroll]} (pos? balance) {:dispatch-later [{:ms 50 :dispatch [::poll active-route (inc counter)]}]} - (< 20 counter) {:db (assoc db :route-counter nil)}))))) + (< 20 counter) {:db (assoc db ::route-counter nil)})))))