I have a page (in a Browser.application
) where the navigation inside the page is done with fragment links. Is it possible to restore the browser default behavior without ports?
here is some sample code that shows this problem:
module Main exposing (Model, Msg(..), init, main, subscriptions, update, view, viewLink)
import Browser exposing (Document)
import Browser.Navigation as Nav
import Html exposing (..)
import Html.Attributes exposing (..)
import Lorem
import Url
-- MAIN
main : Program () Model Msg
main =
Browser.application
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
, onUrlChange = UrlChanged
, onUrlRequest = LinkClicked
}
-- MODEL
type alias Model =
{ key : Nav.Key
, url : Url.Url
}
init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flags url key =
( Model key url, Cmd.none )
-- UPDATE
type Msg
= LinkClicked Browser.UrlRequest
| UrlChanged Url.Url
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
LinkClicked urlRequest ->
case urlRequest of
Browser.Internal url ->
( model, Nav.pushUrl model.key (Url.toString url) )
Browser.External href ->
( model, Nav.load href )
UrlChanged url ->
( { model | url = url }
, Cmd.none
)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions _ =
Sub.none
-- VIEW
type Section
= Home
| About
| Services
| ContactUs
sections : List Section
sections =
[ Home, About, Services, ContactUs ]
sectionToString : Section -> String
sectionToString section =
case section of
Home ->
"Home"
About ->
"About"
Services ->
"Services"
ContactUs ->
"Contact Us"
view : Model -> Document Msg
view model =
{ title = "Title"
, body = viewLinks :: List.map viewSection sections
}
viewLinks : Html msg
viewLinks =
ul [] <|
List.map viewLink sections
viewLink : Section -> Html msg
viewLink section =
let
name =
sectionToString section
in
li []
[ a [ href ("#" ++ Url.percentEncode name) ]
[ text name ]
]
viewSection : Section -> Html msg
viewSection section =
let
name =
sectionToString section
in
div [ id (Url.percentEncode name) ] <|
h1 [] [ text name ]
:: (Lorem.paragraphs 10 |> List.map (p [] << List.singleton << text))
and the elm.json
file:
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.0",
"dependencies": {
"direct": {
"elm/browser": "1.0.1",
"elm/core": "1.0.2",
"elm/html": "1.0.0",
"elm/url": "1.0.0",
"ohanhi/lorem": "1.0.2"
},
"indirect": {
"elm/json": "1.1.3",
"elm/time": "1.0.0",
"elm/virtual-dom": "1.0.2"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}