on master help sign in
Add `dynRouteLink`
commit469b4d33f2e1465f5cdf9866b344e1ce3e00faf8 authorJohn Ericson <John.Ericson@Obsidian.Systems> dateTue 14 May 2019, 13:36 (7 years ago) parent851967e
1 file changed+24
lib/route/src/Obelisk/Route/Frontend.hs+24
lib/route/src/Obelisk/Route/Frontend.hs+24
@@ -47,6 +47,7 @@ module Obelisk.Route.Frontend
4747 , runRouteToUrlT
4848 , mapRouteToUrlT
4949 , routeLink
50+ , dynRouteLink
5051 ) where
5152
5253import Prelude hiding ((.), id)
@@ -470,6 +471,29 @@ routeLink r w = do
470471 setRoute $ r <$ domEvent Click e
471472 return a
472473
474+-- | A link widget that, when clicked, sets the route to current value of the
475+-- provided dynamic route. In non-javascript contexts the value of the dynamic post
476+-- build is used so the link still works like 'routeLink'.
477+dynRouteLink
478+ :: forall t m a route.
479+ ( DomBuilder t m
480+ , PostBuild t m
481+ , RouteToUrl (R route) m
482+ , SetRoute t (R route) m
483+ )
484+ => Dynamic t (R route) -- ^ Target route
485+ -> m a -- ^ Child widget
486+ -> m a
487+dynRouteLink dr w = do
488+ enc <- askRouteToUrl
489+ er <- dynamicAttributesToModifyAttributes $ ("href" =:) . enc <$> dr
490+ let cfg = (def :: ElementConfig EventResult t (DomBuilderSpace m))
491+ & elementConfig_eventSpec %~ addEventSpecFlags (Proxy :: Proxy (DomBuilderSpace m)) Click (\_ -> preventDefault)
492+ & elementConfig_modifyAttributes .~ er
493+ (e, a) <- element "a" cfg w
494+ setRoute $ tag (current dr) $ domEvent Click e
495+ return a
496+
473497-- On ios due to sandboxing when loading the page from a file adapt the
474498-- path to be based on the hash.
475499