Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 4 additions & 7 deletions src/kee_frame/router.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))))))

Expand Down
2 changes: 1 addition & 1 deletion src/kee_frame/scroll.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

(defn start! [])

(defn monitor-requests! [_])
(defn monitor-requests! [& _])
25 changes: 13 additions & 12 deletions src/kee_frame/scroll.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,40 @@
[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!)
nil))

(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)})))))