Git Product home page Git Product logo

hop's Introduction

Hop: A router for Elm SPAs

Codeship Status for sporto/hop

alt Hop

How this works

Matching routes

 ,-------.            ,---.                ,---.
 |History|            |Hop|                |App|
 `---+---'            `-+-'                `-+-'
     |1 location change |                    |  
     |----------------->|                    |  
     |                  |                    |  
     |                  |2 (Route, Location) |  
     |                  |------------------->|  
 ,---+---.            ,-+-.                ,-+-.
 |History|            |Hop|                |App|
 `-------'            `---'                `---'
  • Routes are defined as union types e.g. User Int
  • You define path matchers e.g. match2 User "/users/ int. This matches a path like /users/1.
  • (1) When the browser location changes, Hop will match the path e.g. /users/1 -> User 1.
  • (2) Hop provides a signal that you application consumes, this signal carries the matched routes and information about the current location.

Navigation

 ,---.          ,---.              ,---.          ,-------.
 |Elm|          |App|              |Hop|          |History|
 `-+-'          `-+-'              `-+-'          `---+---'
   |              |1 navigateTo path |                |    
   |              |----------------->|                |    
   |              |                  |                |    
   |              |                  | 2 change path  |    
   |              |                  |--------------->|    
   |              |                  |                |    
   |              |                  |   3 Task       |    
   |              |                  |<- - - - - - - -|    
   |              |                  |                |    
   |              |    4 Effects     |                |    
   |              |<- - - - - - - - -|                |    
   |              |                  |                |    
   |   5 Task     |                  |                |    
   |<-------------|                  |                |    
 ,-+-.          ,-+-.              ,-+-.          ,---+---.
 |Elm|          |App|              |Hop|          |History|
 `---'          `---'              `---'          `-------'
  • (1-2) To change the browser location you call Hop.Navigate.navigateTo.
  • (3-5) This will return an effect that your application must run via ports.
  • When the task is run by Elm the browser location changes.

Hop works with StartApp out of the box.

Hash routing

At the moment only hash routes are supported i.e. #/users/1.

Although a proper url should have the query before the hash e.g. ?keyword=Ja#/users/1, in Hop query parameters are appended after the hash path e.g. #/users/1?keyword=Ja. This is done for aesthetics and so the router is fully controlled by the hash fragment.

Docs

Examples

See examples. To run the example apps:

  • Clone this repo
  • Go to example folder (e.g. examples/basic or examples/full)
  • Follow the readme in that folder

Testing

cd ./test
elm package install -y
npm i
npm test

TODO:

  • Navigate needs functions to:
    • Change path only (without changing the query)
  • Navigate without adding to history
  • Push state - Support routes without hashes
  • Redirects e.g. "/" -> "/dashboard"

Possible improvements

  • In order to match the initial route we need to manually send tasks to a port. Done via route.run. This is one more thing for the user to do. Is this really necessary, can this be removed? Maybe in Elm 0.17.

  • Remove the need to pass the current url to query methods. At the moment we need to send setQuery location dict because Hop cannot figure out the current query by itself. This project could be the solution.

Acknowledgements

Thanks to @etaque and @Bogdanp for the inspiration to make this better.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.