A reactive login form built with WebSharper.UI
and styled with Bulma. It shows how a small amount of F#
drives a real, validating form: Vars track the inputs and reactive attributes
add and remove CSS classes as the user types.
src/wwwroot/index.html is the whole page template — the layout, the inline
SVG icons, the Bulma markup and the hole attributes all live there. Client.fs
loads that same file with Template<"wwwroot/index.html", ClientLoad.FromDocument>
and binds it with .Bind(), so there is no separate mount point.
The reactive part is in src/Client.fs:
[<SPAEntryPoint>]
let Main () =
let email = Var.Create ""
let password = Var.Create ""
// Warnings appear after the first sign-in attempt, then update live.
let submitted = Var.Create false
let emailOk = email.View |> View.Map (isBlank >> not)
let passwordOk = password.View |> View.Map (isBlank >> not)
let flagged = View.Map2 (fun sent ok -> sent && not ok)
IndexTemplate()
.Email(email)
.Password(password)
.AttrEmail([
Attr.DynamicClassPred "is-danger" (flagged submitted.View emailOk)
Attr.DynamicClassPred "is-success" (View.Map2 (fun s ok -> s && ok) submitted.View emailOk)
])
.AttrEmailMessage(Attr.DynamicClassPred "is-visible" (flagged submitted.View emailOk))
// ... same for the password ...
.Login(fun e ->
e.Event.PreventDefault()
submitted.Value <- true
if not (isBlank email.Value) && not (isBlank password.Value) then
JS.Alert (sprintf "Signed in as %s" email.Value))
.Bind()Key points:
.Bind()on the whole page instead of aDoc.RunById "main"mount point — the served HTML is the template, and.Bind()fills its holes in place.- Reactive class toggling with
Attr.DynamicClassPred(aView<bool>), notAttr.ClassPred(a fixedbool). The latter is evaluated once at start-up, which is why an earlier version of this sample never showed the warnings. ws-attrholes accept a list of attributes, so an input can carry both anis-dangerand anis-successstate.- The form uses
ws-onsubmit, so pressing Enter and clicking Sign in both run the same handler, andPreventDefault()keeps the page from reloading.
LoginWithBulma/
src/
Client.fs # template, Vars, reactive validation, [<SPAEntryPoint>]
Startup.fs # ASP.NET Core host (static files from wwwroot)
LoginWithBulma.fsproj
wsconfig.json # WebSharper SPA output settings
package.json # esbuild
esbuild.config.mjs # bundles/minifies the Release output
Properties/
launchSettings.json # dev launch profile (http://localhost:5102)
wwwroot/
index.html # the whole page template
css/login.css # modern styling on top of Bulma
.github/workflows/
deploy.yml # build Release, publish wwwroot to gh-pages
docs/
screenshot.png
README.md
dotnet run --project src
Then open the URL printed by dotnet (http://localhost:5102).
The sample deliberately runs on port 5102, not the ASP.NET Core default 5000. On macOS, AirPlay Receiver listens on port 5000 and answers browser requests with
403 Forbidden(identified by theServer: AirTunesheader), which makes the app look like it is failing to start. If you prefer another port, override it withASPNETCORE_URLS, e.g.ASPNETCORE_URLS=http://localhost:5050 dotnet run --project src.
dotnet build src/LoginWithBulma.fsproj -c Release
WebSharper compiles the client and esbuild bundles
and minifies it into src/wwwroot/Scripts.
Every push to master triggers .github/workflows/deploy.yml: it builds in
Release and, if that succeeds, publishes src/wwwroot to the gh-pages branch.
