Fun.Blazor [](https://www.nuget.org/packages/Fun.Blazor)

November 16, 2024 ยท View on GitHub

image

This is a project to make F# developer to write blazor easier.

  1. Use F# โค๏ธ๐Ÿ˜Š for blazor
  2. Computation expression (CE) style DSL for internal and third party blazor libraries
  3. Dependency injection (html.inject)
  4. Adaptive model (adaptiview/AdaptivieForm) (recommend), elmish model (html.elmish)
  5. Giraffe style routing (html.route/blazor official style)
  6. Type safe css style (Fun.Css)
  7. Convert html to CE style by Fun.Dev.Tools

Check the WASM Docs for more ๐Ÿš€

Donation

If you find my projects helpful and would like to support my work, consider making a donation via PayPal. Your support is greatly appreciated!

Start to use

dotnet new install Fun.Blazor.Templates::4.1.1
dotnet new fun-blazor -o FunBlazorDemo1

The template is upgraded to dotnet 9, but you can downgrade it according to your needs.

Code samples

// Functional style
let count = cval 0
let counter (str: string) = section {
    h2 { "Counter: "; str }
    adapt {
        let! count, setCount = count.WithSetter()
        button {
            onclick (fun _ -> setCount (count + 1))
            "Increase "; count
        }
    }
}

// Class style
type CountPage() =
    inherit FunComponent()

    let mutable count = 0

    override _.Render() = main {
        h1 { "Counter Page" }
        p { "hi here" }
        button {
            onclick (fun _ -> count <- count + 1)
            "Increase "; count
        }
        counter "functional style"
    }

Local development

You can run dotnet fsi build.fsx -- -h to check what is available to help you get started.

Benchmark

BenchmarkDotNet v0.14.0, Windows 11 (10.0.22631.4317/23H2/2023Update/SunValley3) 12th Gen Intel Core i7-12700H, 1 CPU, 20 logical and 14 physical cores .NET SDK 9.0.100 [Host] : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2 DEBUG DefaultJob : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2

MethodMeanErrorStdDevRatioRatioSDGen0AllocatedAlloc Ratio
RenderWithRazorCSharp247.7 ns3.80 ns3.37 ns1.000.020.0291368 B1.00
RenderWithFunBlazorInlineCE374.1 ns5.53 ns4.91 ns1.510.030.0439552 B1.50
RenderWithFunBlazorSSRTemplate475.6 ns5.78 ns5.40 ns1.920.030.0420528 B1.43
RenderWithBolero497.9 ns8.46 ns10.07 ns2.010.050.11921496 B4.07
RenderWithFunBlazorArray525.2 ns10.44 ns11.17 ns2.120.050.11441440 B3.91
RenderWithFunBlazorTemplate785.9 ns7.95 ns7.44 ns3.170.050.12401560 B4.24