Package

purescript-pha

Repository
gbagan/purescript-pha
License
MIT
Uploaded by
pacchettibotti
Published on
2023-11-04T10:33:58Z

a simple and fast Elm-like library also inspired by Halogen.

Documentation

Documentation is published on Pursuit

Minimal example

module Example.Counter where
import Prelude
import Effect (Effect)
import Pha.App (sandbox)
import Pha.Html (Html)
import Pha.Html as H
import Pha.Html.Attributes as P
import Pha.Html.Events as E

type Model = Int
data Msg = Increment | Decrement

init  Model
init = 0

update  Msg  Model  Model
update Increment n = n + 1
update Decrement n = n - 1

view  Model  Html Msg
view counter = 
  H.div []
    [ H.button [E.onClick \_ → Decrement] [H.text "-"]
    , H.span [] [H.text $ show counter]
    , H.button [E.onClick \_ → Increment] [H.text "+"]
    ]

main  Effect Unit
main = sandbox {init, update, view, selector: "#root"}

Example with side effects

module Example.Counter where
import Prelude
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Random (randomInt)
import Pha.App (app)
import Pha.Html (Html)
import Pha.Html as H
import Pha.Html.Attributes as P
import Pha.Html.Events as E
import Pha.Update (Update, liftEffect, put)

type Model = Int
data Msg = RollDice

update  Msg  Update Model Msg Aff Unit
update RollDice = put =<< liftEffect (randomInt 1 6)

view  State  Html Msg
view dice = 
  H.div []
    [ H.button [E.onClick \_ → RollDice] [H.text "Roll dice"]
    , H.span [] [H.text $ show dice]
    ]

main  Effect Unit
main =
  app
    { init: { model: 0, msg: Just RollDice }
    , view
    , update
    , selector: "#root"
    }

More examples

https://github.com/gbagan/purescript-pha-examples/tree/master/src

Some projects using purescript-pha