From 18e742d092d36ec92d27f8a2b5c82458368136bf Mon Sep 17 00:00:00 2001 From: Oliver Marshall Date: Tue, 24 Jan 2023 09:40:58 +0000 Subject: [PATCH 1/4] Update scroll section in README --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 From 2e763b9ae2145ddb672c68560a202acf80aaee1e Mon Sep 17 00:00:00 2001 From: Oliver Marshall Date: Tue, 24 Jan 2023 10:22:39 +0000 Subject: [PATCH 2/4] Change ::poll to use :kee-frame/route instead of :route-counter :route --- src/kee_frame/scroll.cljs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/kee_frame/scroll.cljs b/src/kee_frame/scroll.cljs index fc7a862..81ce3af 100644 --- a/src/kee_frame/scroll.cljs +++ b/src/kee_frame/scroll.cljs @@ -5,10 +5,8 @@ [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!)) @@ -20,10 +18,10 @@ (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)}))))) (rf/reg-event-fx ::scroll @@ -33,11 +31,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)}))))) From 831f4c3e2ff7b54ada5d6dba34217f281b44724a Mon Sep 17 00:00:00 2001 From: Oliver Marshall Date: Tue, 24 Jan 2023 10:23:48 +0000 Subject: [PATCH 3/4] Only set the ajax interceptor once, move the first ::poll call into the scroll namespace --- src/kee_frame/router.cljc | 8 ++------ src/kee_frame/scroll.clj | 2 +- src/kee_frame/scroll.cljs | 10 ++++++---- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/kee_frame/router.cljc b/src/kee_frame/router.cljc index 293c093..108b374 100644 --- a/src/kee_frame/router.cljc +++ b/src/kee_frame/router.cljc @@ -100,13 +100,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 81ce3af..929adaa 100644 --- a/src/kee_frame/scroll.cljs +++ b/src/kee_frame/scroll.cljs @@ -9,10 +9,7 @@ (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) @@ -24,6 +21,11 @@ (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!) From e277ccb050636235702c0a190c74057628d9f30d Mon Sep 17 00:00:00 2001 From: Ernesto Garcia Date: Wed, 20 Sep 2023 12:51:25 +0200 Subject: [PATCH 4/4] Fix: Default value for `scroll` config option. --- src/kee_frame/router.cljc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/kee_frame/router.cljc b/src/kee_frame/router.cljc index 108b374..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)