Skip to content

Commit

Permalink
Add Route.link and Route.toLink helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonkearns committed May 15, 2021
1 parent 075a886 commit 33a10ed
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
16 changes: 6 additions & 10 deletions examples/docs/src/Page/Blog.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions generator/src/generate-template-module-connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
`,
};
}
Expand Down

0 comments on commit 33a10ed

Please sign in to comment.