λpurs-solid

PureScript bindings for Solid 2

Fine-grained reactivity, checked by the compiler.

Signals, stores, async data, server rendering and hydration, a typed router and start mode, through Solid's public API. The types reject code that would misbehave at runtime.

import Data.Tuple.Nested ((/\))
import Solid.Component as Component
import Solid.DOM.HTML as H
import Solid.Reactivity (createMemo)
import Solid.Signal (createSignal, modify_)

counter :: Component.Component {}
counter = Component.component \_ -> do
  count /\ setCount <- createSignal 0
  doubled <- createMemo ((_ * 2) <$> count)
  pure $ H.div { class: "counter" }
    [ H.button
        { onClick: \_ -> modify_ setCount (_ + 1)
        , class: { big: (_ > 3) <$> count }
        }
        "+1"
    , H.span {} (show <$> count)
    , H.small {} ((\n -> "doubled: " <> show n) <$> doubled)
    ]

Rendered at build time, hydrated in your browser:

0doubled: 0

What the compiler catches

Props checked per element

Each field is checked against the element's attributes and events. Every attribute takes a value or an accessor.

link :: Accessor Boolean -> Accessor String -> JSX
link isActive label =
  -- no: a div has no href
  --   H.div { href: "/docs" } []
  -- no: disabled is a Boolean
  --   H.button { disabled: "yes" } []
  H.a { href: "/docs", class: { active: isActive } } label

Setup can't write

Components run in Setup, which creates signals, memos and effects. Writes happen in handlers, in Effect.

counter :: Component.Component {}
counter = Component.component \_ -> do
  count /\ setCount <- createSignal 0
  doubled <- createMemo ((_ * 2) <$> count)
  -- no: setup can't write signals
  --   set setCount 1
  pure $ H.button
    { onClick: \_ -> modify_ setCount (_ + 1) }
    (show <$> doubled)

Async can't be read early

createAsync gives an Async with no get: render it, derive from it or await it, and loading shows a fallback.

user :: Component.Component { id :: Accessor Int }
user = Component.component \{ id } -> do
  found /\ _ <- createAsync (fetchUser <$> id)
  -- no: it may not have loaded
  --   name <- get found
  pure $ Control.loading (H.p {} "Loading…")
    (H.h1 {} (_.name <$> found))

Routes typed by their path

A route's params come from its pattern, :id<int> arrives as an Int, and href builds links from the same pattern.

routes :: Array Router.Route
routes =
  -- no: id is an Int
  --   href @"/users/:id<int>" { id: "7" }
  [ Router.route @"/users/:id<int>" \{ params } ->
      pure (H.h1 {} (show <<< _.id <$> params))
  , Router.route @"/" \_ -> pure $
      H.a { href: href @"/users/:id<int>" { id: 7 } } "7"
  ]

ARIA from the spec

aria-* attributes follow WAI-ARIA 1.2: booleans, numbers, and a type for each set of keywords.

menu :: Accessor Boolean -> JSX
menu isOpen =
  -- no: it's a Boolean
  --   "aria-expanded": "true"
  -- no: it's a HasPopup, with no "menus"
  --   "aria-haspopup": "menus"
  H.div { role: "toolbar" }
    [ H.button { "aria-expanded": isOpen } "Details"
    , H.button { "aria-haspopup": Aria.Menu } "Menu"
    , H.p { "aria-live": Aria.Polite } "Saved"
    ]

Server code stays on the server

In start mode a server module's functions never reach the browser, and what crosses the wire must be Serializable.

module App.Api
  ( module Solid.Start.UseServer
  , greet
  ) where

import Prelude

import Solid.Start.ServerFunction as Server
import Solid.Start.UseServer (useServer)

greet :: Server.ServerFunction String String
greet = Server.serverFunction \name ->
  pure ("Hello, " <> name)

Solid underneath

The Vite wrapper compiles eligible views into the same templates as Solid JSX. The runtime renderer clones templates and applies props when it creates the view.

Get started