Skip to content
clj-commonsPublic

About

Clojurescript library for quick and easy HTML5 pushState

Resources

Stars

29 stars

Watchers

3 watching

Forks

 
 

Repository files navigation

pushy

Clojars Project cljdoc badge CI

A Clojurescript library for quick and easy HTML5 pushState. Fork of the original which is no longer maintained.

Setup

You can construct a new instance by calling the pushy function.

pushy takes in two arguments:

  • dispatch fn: gets called when there is a match
  • match fn: checks if the path matches any routes defined.

Optionally, you can specify an :identity-fn which parses and returns the route based on the result of the match fn.

Event listeners

You can start the event listeners with the start! method.

This adds an event listener to all click events, which dispatches on all matched routes. Bypasses on Alt, Shift, Meta, Ctrl keys and middle clicks.

The stop! method will tear down all event listeners of the pushy instance.

Routing libraries

pushy should work with any routing library:

Secretary

(ns foo.core
  (:require [secretary.core :as secretary :include-macros true :refer-macros [defroute]]
            [pushy.core :as pushy]))

(secretary/set-config! :prefix "/")

(defroute index "/foo" []
  (.log js/console "Hi"))

(def history (pushy/pushy secretary/dispatch!
                          (fn [x] (when (secretary/locate-route x) x))))

;; Start event listeners
(pushy/start! history)

Bidi

(ns foo.core
  (:require [bidi.bidi :as bidi]
            [pushy.core :as pushy]))

(def state (atom {}))

(def app-routes
  ["/" {"foo" :foo}])

(defn set-page! [match]
  (swap! state assoc :page match))

(def history
  (pushy/pushy set-page! (partial bidi/match-route app-routes)))

(pushy/start! history)

Silk

(ns foo.core
  (:require [domkm.silk :as silk]
            [pushy.core :as pushy]))

(def state (atom {}))

(def app-routes
  (silk/routes [[:foo [["/foo"]]]]))

(defn set-page! [match]
  (swap! state assoc :page match))

(def history
  (pushy/pushy set-page! (partial silk/arrive app-routes)))

(pushy/start! history)

Router

(ns foo.core
  (:require [pushy.core :as pushy]
            [darkleaf.router :as r]))

(r/defcontroller root-controller
  (show [_req]
    :root))

(r/defcontroller pages-controller
  (index [_req]
    :pages-index))

(def routing
  (r/group
    (r/resource :root root-controller, :segment false)
    (r/resources :pages :page pages-controller)))

(def handler (r/make-handler routing))

(def history (pushy/pushy (fn [response] (prn response))
                          (fn [uri] (handler {:uri uri, :request-method :get}))))

(pushy/start! history)

#_(pushy/set-token! history "/")
#_(pushy/set-token! history "/pages")

Sibiro

(ns foo.core
  (:require [sibiro.core :as sibiro]
            [pushy.core :as pushy]))

(def state (atom {}))

(defn set-page! [match]
  (assoc state :page match))

(def routes
  {[:get "/home" :home]})

(defn match-uri [uri]
  (:route-handler (sibiro/match-uri (sibiro/compile-routes routes) uri :get)))

(def history (pushy/pushy set-page! match-uri))

(pushy/start! history)

Reitit

(ns foo.core
  (:require [reitit.core :as r]
            [pushy.core :as pushy]))

(def routes
  [["/" {:page :index
         :name :index}]])

(def router (r/router routes))

(def history
  (pushy/pushy
    (fn [match] (prn match))
    (fn [uri] (r/match-by-path router uri))))

(pushy/start! history)

URL handling

By default pushy will dispatch on all relative URLs and absolute URLs that match the window's origin. This means that all external links will be bypassed.

It is possible to specify which URLs are processable to pushy by specifying a custom predicate function :processable-url? in the constructor. This function is passed a single argument which is an instance of goog.URI.

URL fragments

The token passed to match includes the fragment, for example "/docs#install". If your routing library does not expect one, strip it before matching, or such URLs will not match.

When pushy handles a click, the browser does not scroll to the element the fragment names, even for links within the current page such as <a href="#install">. Either scroll in your dispatch fn, or add a data-pushy-ignore attribute to the link to leave it to the browser.

Hash-based routing, where the route itself lives in the fragment (/#/docs), is not supported.

Clicks on links to the current URL

By default, clicking a link that points at the current URL does nothing: the URL has not changed, so dispatch is not called.

To react to those clicks (to close a menu or scroll to the top, for example), pass an :on-same-token-click function to the constructor. It is called with the same value dispatch would receive. No history entry is added.

(def history
  (pushy/pushy dispatch match
               :on-same-token-click (fn [matched-route] (close-menu!))))

To dispatch again instead, pass your dispatch function: :on-same-token-click dispatch.

goog.history.HTML5History methods

You can set the history state manually by calling the set-token! method. This will call the dispatch fn on a successfully matched path.

Example:

(set-token! history "/foo")

(get-token history)
;; => /foo

Likewise, you can call replace-token! which will also call the dispatch fn and replace the current history state without affecting the rest of the history stack.

Calling set-token! with the current token does nothing, so dispatch is not called. To run dispatch again for the current URL, call replace-token! with the current token.

License

Copyright © 2017

Distributed under the Eclipse Public License either version 1.0

About

Clojurescript library for quick and easy HTML5 pushState

Resources

Stars

29 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages