Add Route.link and Route.toLink helpers.

This commit is contained in:
Dillon Kearns 2021-05-15 10:36:34 -07:00
parent 075a886adf
commit 33a10ede33
2 changed files with 27 additions and 12 deletions

View File

@ -195,17 +195,13 @@ head staticPayload =
link : Route.Route -> List (Attribute msg) -> List (Html msg) -> Html msg
link route attrs children =
a
(Attr.href
("/"
++ (Route.routeToPath (Just route)
|> String.join "/"
)
)
:: Attr.attribute "elm-pages:prefetch" ""
:: attrs
Route.toLink
(\anchorAttrs ->
a
(List.map Attr.fromUnstyled anchorAttrs ++ attrs)
children
)
children
route
blogCard : ( Route, Article.ArticleMetadata ) -> Html msg

View File

@ -494,8 +494,9 @@ mapBoth : (a -> b) -> (c -> d) -> ( a, c, e ) -> ( b, d, e )
mapBoth fnA fnB ( a, b, c ) =
( fnA a, fnB b, c )
`,
routesModule: `module Route exposing (..)
routesModule: `module Route exposing (Route(..), link, matchers, routeToPath, toLink, urlToRoute)
import Html exposing (Attribute, Html)
import Html.Attributes as Attr
import Router
@ -555,6 +556,24 @@ routeToPath maybeRoute =
})} ]`
)
.join("\n ")}
toLink : (List (Attribute msg) -> tag) -> Route -> tag
toLink toAnchorTag route =
toAnchorTag
[ Attr.href ("/" ++ (routeToPath (Just route) |> String.join "/"))
, Attr.attribute "elm-pages:prefetch" ""
]
link : Route -> List (Attribute msg) -> List (Html msg) -> Html msg
link route attributes children =
toLink
(\\anchorAttrs ->
Html.a
(anchorAttrs ++ attributes)
children
)
route
`,
};
}