From 33a10ede33e69efc0b28b74e1611c7658eab6e49 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Sat, 15 May 2021 10:36:34 -0700 Subject: [PATCH] Add Route.link and Route.toLink helpers. --- examples/docs/src/Page/Blog.elm | 16 +++++-------- .../src/generate-template-module-connector.js | 23 +++++++++++++++++-- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/examples/docs/src/Page/Blog.elm b/examples/docs/src/Page/Blog.elm index 04dc7f2e..4d3c3a34 100644 --- a/examples/docs/src/Page/Blog.elm +++ b/examples/docs/src/Page/Blog.elm @@ -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 diff --git a/generator/src/generate-template-module-connector.js b/generator/src/generate-template-module-connector.js index f1786a45..60105dcd 100644 --- a/generator/src/generate-template-module-connector.js +++ b/generator/src/generate-template-module-connector.js @@ -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 `, }; }