A ClojureScript library designed to make it easy to get your Single Page Application to behave more like a ”regular” site would do when it comes to navigating between, and within, pages.
Online demo here: clerk-demo.netlify.com
Clerk takes care of the scroll positioning when:
Clerk does not deal with anything else beside the above. Use it together wih your routing and HTML5 history libararies of choice.
Today's web browsers handles all this automatic scroll positioniing perfectly for regular sites. But the S in SPA really means that everything happens on the same page, even if it looks to the user as if navigatin between pages happens. A new page is just the result of rendering new content. So without managing the scroll positioning we have this Ux problem:
In addition to this:
Let Clerk take care of all this for you!
Add the dependency:
[pez/clerk "1.0.0-SNAPSHOT"]
The examples in this README assumes Clerk is required like so:
(:require
...
[clerk.core :as clerk]
...
Initialize as early as possible when your app is starting:
(clerk/initialize!)
After any routing/navigation dispatch of your app you need to tell Clerk about the new path
(clerk/navigate-page! path)
Then just one more thing. To avoid flicker, Clerk deferrs scroll adjustment until after the page is rendered. You need to tell Clerk when rendering is done:
(clerk/after-render!)
Depending on your project the after render notification will need to be injected in different ways. Here are exemples for two common ClojureScript React frameworks, Rum and Reagent:
Rum has a utility callback :after-render
that can be used in a mixin for this purpose, like so:
(defc page < rum/reactive
{:after-render
(fn [state]
(after-render!)
state)})
...
For Reagent, you can use the reagent/after-render
function, which calls any function you provide to it when rendering is done:
(reagent/after-render clerk/after-render!)
(You can also hook it in to the compenent life cycle, :component-did-mount
and :component-did-update
, if that suits your project and testes better.)
The Leiningen Reagent template's init!
function will look like so with all clerky stuff added:
(defn init! []
(clerk/initialize!)
(accountant/configure-navigation!
{:nav-handler
(fn [path]
(reagent/after-render clerk/after-render!)
(secretary/dispatch! path)
(clerk/navigate-page! path))
:path-exists?
(fn [path]
(secretary/locate-route path))})
(accountant/dispatch-current!)
(mount-root))
(For Rum it will look very similar, except that you need to use a mixin for your page component's :after-render
callback instead of using the reagent/after-render
.)
In Scotland, the term scrow was used from about the 13th to the 17th centuries for scroll, writing, or documents in list or schedule form. There existed an office of Clerk of the Scrow (Rotulorum Clericus) meaning the Clerk of the Rolls or Clerk of the Register. (From WikiPedia.)
Also, it is quite beautiful to imagine that with some projects maybe Secretary, Accountant and Clerk will work together to get the SPA to behave according to the expectations of its users.
Questions, suggestions, PRs. Just throw it at me. File issues at will. You can also most often find me at the Clojurians Slack. Have praise? Tweet it! Tag @pappapez.
Copyright © 2018 Peter Strömberg
Distributed under the Eclipse Public License, either version 1.0 or (at your option) any later version.
I'm pretty confident about that Clerk works in reasonably modern web browsers, much thanks to Browserstack.
Can you improve this documentation? These fine people already did:
Peter Strömberg & ledEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close