concept ClickedLink in category elm

appears as: ClickedLink
Elm in Action

This is an excerpt from Manning's book Elm in Action.

First we have onUrlRequest = ClickedLink. This says that whenever the user requests a new URL (by clicking a link), Elm’s runtime will send a ClickedLink message to our update function. It’s like a page-wide event handler, except that it goes a step further by overriding the default behavior of all links on the page.

With Browser.application, clicking links only sends this ClickedLink message—it doesn’t automatically load a new page as the browser normally would. This gives us total control over what we want the application to do when the user clicks a link.

Handling ClickedLink

Next, let’s incorporate the ClickedLink message into update like so:

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        ClickedLink urlRequest ->
            case urlRequest of
                Browser.External href ->
                    ( model, Nav.load href )

                Browser.Internal url ->
                    ( model, Nav.pushUrl model.key (Url.toString url) )