elm-lang/navigation/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Navigation

This is a library for managing browser navigation yourself.

The core functionality is the ability to “navigate” to new URLs, changing the address bar of the broswer without the browser kicking off a request to your servers. Instead, you manage the changes yourself in Elm.

Change the URL

newUrl : String -> Cmd msg

Step to a new URL. This will add a new entry to the browser history.

Note: If the user has gone back a few pages, there will be “future pages” that the user can go forward to. Adding a new URL in that scenario will clear out any future pages. It is like going back in time and making a different choice.

modifyUrl : String -> Cmd msg

Modify the current URL. This will not add a new entry to the browser history. It just changes the one you are on right now.

Navigation

back : Int -> Cmd msg

Go back some number of pages. So back 1 goes back one page, and back 2 goes back two pages.

Note: You only manage the browser history that you created. Think of this library as letting you have access to a small part of the overall history. So if you go back farther than the history you own, you will just go back to some other website!

forward : Int -> Cmd msg

Go forward some number of pages. So forward 1 goes forward one page, and forward 2 goes forward two pages. If there are no more pages in the future, this will do nothing.

Note: You only manage the browser history that you created. Think of this library as letting you have access to a small part of the overall history. So if you go forward farther than the history you own, the user will end up on whatever website they visited next!

Start your Program

Parser data
-> { init : data -> ( model, Cmd msg )
, update : msg -> model -> ( model, Cmd msg )
, urlUpdate : data -> model -> ( model, Cmd msg )
, view : model -> Html msg
, subscriptions : model -> Sub msg
}
-> Program Never

This function augments Html.App.program. The new things include:

  • Parser — Whenever this library changes the URL, the parser you provide will run. This turns the raw URL string into useful data.

  • urlUpdate — Whenever the Parser produces new data, we need to update our model in some way to react to the change. The urlUpdate function handles this case. (It works exactly like the normal update function. Take in a message, update the model.)

Note: The urlUpdate function is called every time the URL changes. This includes things exposed by this library, like back and newUrl, as well as whenever the user clicks the back or forward buttons of the browsers. If the address changes, you should hear about it.

Parser data
-> { init : flags -> data -> ( model, Cmd msg )
, update : msg -> model -> ( model, Cmd msg )
, urlUpdate : data -> model -> ( model, Cmd msg )
, view : model -> Html msg
, subscriptions : model -> Sub msg
}
-> Program flags

Works the same as the program function, but can handle flags. See Html.App.programWithFlags for more information.

type Parser a

This library is primarily about treating the address bar as an input to your program. A Parser helps you turn the string in the address bar into data that is easier for your app to handle.

makeParser : (Location -> a) -> Parser a

The makeParser function lets you parse the navigation state any way you want.

Note: Check out the examples associated with this GitHub repo to see a simple usage. See evancz/url-parser for a more complex example of URL parsing. The approach used there makes it pretty easy to turn strings into structured data, and I hope it will serve as a baseline for other URL parsing libraries that folks make.

type alias Location =
{ href : String
, host : String
, hostname : String
, protocol : String
, origin : String
, port_ : String
, pathname : String
, search : String
, hash : String
, username : String
, password : String
}

A bunch of information about the address bar.

Note: These fields correspond exactly with the fields of document.location as described here. Good luck with that.

Note 2: You should be using a library like evancz/url-parser to deal with all this stuff, so generally speaking, you should not have to deal with locations directly.