Search code examples
f#websharper

F# 3 support for WebSharper


I'm defining a DU for the various classes I use within HTML:

type Class =
    | Logo
    | Screen
    | Continue

Now unfortunately FSharpValue.GetUnionFields doesn't translate to JS (I wouldn't expect it to) and overriding ToString on the DU such that it translates to JS doesn't seem to work either. As such, I defined the following operators to deal with adding/removing classes from Dom elements:

[<JavaScript>]
let getClassString = function
    | Logo -> "logo"
    | Screen -> "screen"
    | Continue -> "continue"

[<JavaScript>]
let inline (|+>) (x : Element) (c : Class) = x.AddClass (getClassString c); x
[<JavaScript>]
let inline (|->) (x : Element) (c : Class) = x.RemoveClass (getClassString c)

Which really makes it a bliss:

[<JavaScript>]
let mainMenu =
    Div [
        H2 [Text "Jewel"; Br [] :> _; Text "Warrior"] |+> Logo
        [
            "Play", gameScreen
            "HighScore", highScores
            "About", notReady
            "Exit", notReady
        ] |> Seq.map (fun (text, screen) -> LI [Button [Text text] |>! OnClick (fun _ _ -> show screen)])
        |> UL |+> Menu
    ] |+> MainMenu |+> Screen

Could someone confirm that F# type providers would be a good fit for generating my Class DU directly from the CSS?

Can we expect WebSharper to be compatible with VS11 beta or only when it's released?

Thanks!


Solution

  • Working on it: https://bitbucket.org/IntelliFactory/websharper/issue/32/make-projects-net-45-compatible