@@ -51,6 +51,7 @@ module Obelisk.Route.Frontend
5151 , runRouteToUrlT
5252 , mapRouteToUrlT
5353 , routeLink
54+ , dynRouteLink
5455 ) where
5556
5657import Prelude hiding ((.), id)
@@ -503,6 +504,29 @@ routeLink r w = do
503504 setRoute $ r <$ domEvent Click e
504505 return a
505506
507+-- | A link widget that, when clicked, sets the route to current value of the
508+-- provided dynamic route. In non-javascript contexts the value of the dynamic post
509+-- build is used so the link still works like 'routeLink'.
510+dynRouteLink
511+ :: forall t m a route.
512+ ( DomBuilder t m
513+ , PostBuild t m
514+ , RouteToUrl (R route) m
515+ , SetRoute t (R route) m
516+ )
517+ => Dynamic t (R route) -- ^ Target route
518+ -> m a -- ^ Child widget
519+ -> m a
520+dynRouteLink dr w = do
521+ enc <- askRouteToUrl
522+ er <- dynamicAttributesToModifyAttributes $ ("href" =:) . enc <$> dr
523+ let cfg = (def :: ElementConfig EventResult t (DomBuilderSpace m))
524+ & elementConfig_eventSpec %~ addEventSpecFlags (Proxy :: Proxy (DomBuilderSpace m)) Click (\_ -> preventDefault)
525+ & elementConfig_modifyAttributes .~ er
526+ (e, a) <- element "a" cfg w
527+ setRoute $ tag (current dr) $ domEvent Click e
528+ return a
529+
506530-- On ios due to sandboxing when loading the page from a file adapt the
507531-- path to be based on the hash.
508532