# nano-ui-sdl-web
Browser backend for [nano-ui](https://github.com/goolord/nano-ui), targeting GHC's WebAssembly backend. Text shaping uses the same SDL3_ttf engine as [nano-ui-sdl](https://hackage.haskell.org/package/nano-ui-sdl) (HarfBuzz + FreeType into a texture atlas) and frames draw with WebGL2.
```haskell
{-# LANGUAGE CPP, OverloadedStrings #-}
import NanoUI
import NanoUI.Backend.SdlWeb (defaultSdlWebOptions, runSdlWebApp)
main :: IO ()
main = runSdlWebApp defaultSdlWebOptions (label "Hello")
#if defined(wasm32_HOST_ARCH)
foreign export javascript "nanoMain" main :: IO ()
#endif
```
`runSdlWebAppReduce` pairs `model -> NanoUIE msg ()` with an update function just like `runSdlAppReduce`. Configure page title (`webWindowSettings`), themes or `prefers-color-scheme` tracking (`webThemeFor`), fonts, base size, and extra UI scale through `SdlWebOptions`.
## Building for the browser
Building for WebAssembly requires Linux or macOS with [ghc-wasm-meta](https://gitlab.haskell.org/haskell-wasm/ghc-wasm-meta) (GHC 9.14, Cabal 3.14, and `node` on `PATH` for Template Haskell font embedding).
### 1. One-time WASI C libraries
Upstream SDL3 does not publish prebuilt `wasm32-wasi` static archives, so cross-compile FreeType, HarfBuzz, SDL3, and SDL3_ttf into `~/.nano-ui-wasi` once per machine using `scripts/build-wasi-deps.sh` (shipped in `nano-ui-sdl-web`'s `data-files` and source tarball):
```bash
source ~/.ghc-wasm/env
cabal unpack nano-ui-sdl-web
nano-ui-sdl-web-0.2.0.0/scripts/build-wasi-deps.sh
```
### 2. Project setup
Start from [`templates/web`](https://github.com/goolord/nano-ui/tree/main/templates/web) (`nix flake init -t github:goolord/nano-ui#web`), or configure an executable as a Wasm reactor exporting `nanoMain`:
```cabal
executable my-webapp
main-is: Main.hs
default-language: GHC2024
default-extensions: OverloadedStrings
build-depends:
base >=4.20 && <4.23
, nano-ui ^>=0.2
, nano-ui-sdl-web ^>=0.2
, text ^>=2.1
if arch(wasm32)
default-extensions: JavaScriptFFI
ghc-options:
-no-hs-main
-optl-mexec-model=reactor
"-optl-Wl,--export=hs_init,--export=nanoMain"
```
Add `split-sections: True` in `cabal.project` so the linker strips unreachable code:
```cabal
package *
split-sections: True
```
### 3. Standard GHC Wasm pipeline
You can use Vite, esbuild, or your own HTML page directly. Build the `.wasm` binary and run GHC's `post-link.mjs` before `wasm-opt` strips the JSFFI custom section:
```bash
source ~/.ghc-wasm/env
export PKG_CONFIG_PATH=~/.nano-ui-wasi/lib/pkgconfig
wasm32-wasi-cabal build -j1 my-webapp
wasm="$(wasm32-wasi-cabal list-bin my-webapp)"
"$(wasm32-wasi-ghc --print-libdir)/post-link.mjs" -i "$wasm" -o dist-web/ghc_wasm_jsffi.js
wasm-opt --strip-debug -O2 "$wasm" -o dist-web/main.wasm
```
Serve three browser assets alongside `main.wasm` and `ghc_wasm_jsffi.js`:
- `web/nano-ui-sdl-web.js` (loaded before starting Wasm) and `web/main.js` (ES module loader), both installed in `nano-ui-sdl-web`'s Cabal `data-files` directory (`share/web/`).
- `@bjorn3/browser_wasi_shim` from npm.
- `fonts/inter-extended.ttf` (from `web/fonts/`), unless you set `webFallbackFonts = []` or supply your own font URLs.
If you prefer a self-contained static directory with no JS bundler, `scripts/build-web.sh [out-dir [cabal-target]]` runs the steps above and copies the assets (`CABAL_PROJECT` and `WASM_OPT_FLAGS` override defaults):
```bash
PKG_CONFIG_PATH=~/.nano-ui-wasi/lib/pkgconfig \
bash path/to/nano-ui-sdl-web/scripts/build-web.sh dist-web my-webapp
python3 -m http.server -d dist-web 8000
```
## Browser-specific behavior
- **Fonts:** Embedded Inter covers printable ASCII to keep `.wasm` small; extended Latin, Greek, Cyrillic, and symbols come from `webFallbackFonts` (`fonts/inter-extended.ttf` by default). `WebFontSearch` and `webFontFamilies` use Chromium's Local Font Access API (prompting lazily on the first canvas click/key, or explicitly via `listWebFontFamilies`) and an optional Fontsource CDN catalog (`webFontService = Just Fontsource`, off by default so apps make no third-party requests unless you opt in). Explicit fonts also load via `WebFontUrl` (requires CORS if cross-origin) or `WebFontBytes`.
- **File dialogs:** `openWebFiles`, `openWebFolder`, and `saveWebFile` work on filenames and byte contents (`showSaveFilePicker` in Chromium, standard download fallback in Firefox and Safari). Launch them from a `useTask` started inside a click handler so the browser allows the picker gesture.
- **Canvas and windowing:** Give `<canvas>` an explicit CSS size; the backend matches the WebGL drawing buffer to that box multiplied by `devicePixelRatio` and tracks resizes. `setWindowTitle` updates `document.title`, and clipboard reads see text copied or pasted within the page.
## Documentation
- [The nano-ui Book](https://goolord.github.io/nano-ui/book/): Tutorials, widget gallery, and backend overview.
## License
MIT. The bundled Inter font is under the SIL Open Font License 1.1 (`LICENSE-Inter`); include `LICENSE-Inter` when distributing builds.