dotnet-websharper / ui

A reactive UI library for WebSharper.
https://websharper-samples.github.io/ui/
Apache License 2.0
77 stars 22 forks source link

Add `Router.LinkHash` #285

Open granicz opened 4 months ago

granicz commented 4 months ago

Currently, the Router type from WebSharper.Sitelets is available for client-side use through a macro. This enables creating routers for endpoint types on the client, which is often used in SPAs in combination of hashed URLs:

open WebSharper.Sitelets
open WebSharper.UI

// SPA endpoints
type EndPoint =
    | [<EndPoint "/">] Home
    | [<EndPoint "/counter">] Counter

// Creating a client-side router
let router = Router.Infer<EndPoint>()
// Installing it and tracking the current page via an RV
let currentPage = Router.InstallHash EndPoint.Home router

Given this, it would be desirable to be able to compute hash-based URLs, instead of manually accounting for the hash - which looks something like:

IndexTemplate()
    .Url_Home("/#" + router.Link EndPoint.Home)

This ticket would add this as an extension member to Router<'T>, when WebSharper.UI is opened.

type Router<'T when 'T: equality> with
    member this.LinkHash (ep: 'T) = "/#" + this.Link ep