haskell-halogen-material wraps Google's Material Components as Halogen children. This form uses a text field, checkbox and button, each mounted with HH.slot as in lesson 4. The library also provides radio buttons, lists and tabs.
The slots
Slots declares the children's queries, outputs and indices:
TextField.textField emits TextField.InputChanged with the current text on each edit. The parent converts it to Name t. Its Int index allows multiple fields under one label.
Checkbox.checkbox emits Checkbox.CheckboxChange with the checked state.
Button.button emits Button.ButtonClicked, converted here to Send.
Each component takes a specification as input. Start with a default such as TextField.emptyTextFieldSpec and update fields, for example TextField.label = Just "Name". The parent stores child outputs in State and reads them when handling Send.
The monad
Material's JavaScript adds behaviours such as floating labels and ripples after elements mount. The components require MonadMaterial m for those browser effects. Text fields and checkboxes also require MonadUUID m to generate IDs connecting labels to inputs.
Your component declares these capabilities while remaining polymorphic in m. The lesson wrapper chooses BrowserDOM, which provides both instances. A custom application monad can provide the same instances. Native BrowserDOM bindings allow type checking but do not run Material in a browser.
The layer includes material.js and material.css. The tutorial loads both before mounting your component, just as an application would include them in its page.
Exercise
Add an email field and show both submitted values:
Add email to State and Email Text to the action type.
Mount another TextField.textField with HH.slot "field" 1, label it "Email", and convert its InputChanged output to Email. The index distinguishes it from the existing field.
Update Send to include st.email alongside st.name in sent.
Lesson.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
module Lesson (component) where
import Control.Monad.State (modify)
import Control.Monad.UUID (MonadUUID)
import Data.Row (type (.+), type (.==))
import Data.Text (Text)
import Halogen qualified as H
import Halogen.HTML qualified as HH
import Halogen.Material.Button qualified as Button
import Halogen.Material.Checkbox qualified as Checkbox
import Halogen.Material.Monad (MonadMaterial)
import Halogen.Material.TextField qualified as TextField
type Slots =
("field" .== H.Slot TextField.TextFieldQuery TextField.TextFieldOutput Int)
.+ ("news" .== H.Slot Checkbox.CheckboxQuery Checkbox.CheckboxChange ())
.+ ("send" .== H.Slot H.VoidF Button.ButtonClicked ())
data State = State {name :: Text, news :: Bool, sent :: Text}
data Action = Name Text | News Bool | Send
-- The program around this module adds Material's script and stylesheet to
-- the page, then mounts this with m = BrowserDOM.
component :: forall q o m. (MonadMaterial m, MonadUUID m) => H.Component q () o m
component =
H.component initialState render [H.onAction handleAction]
where
initialState _ = pure State {name = "", news = False, sent = "Nothing sent yet."}
render :: State -> H.ComponentHTML Action Slots m
render st =
HH.div_
[ HH.slot "field" 0 TextField.textField TextField.emptyTextFieldSpec {TextField.label = Just "Name"} (\(TextField.InputChanged t) -> Name t)
, HH.slot "news" () Checkbox.checkbox Checkbox.emptyCheckboxSpec {Checkbox.label = "Send me news"} (\(Checkbox.CheckboxChange b) -> News b)
, HH.slot "send" () Button.button Button.emptyButtonSpec {Button.label = "Send", Button.style = Just Button.Raised} (\_ -> Send)
, HH.p_ [HH.text st.sent]
]
handleAction action = case action of
Name t -> modify (\st -> st {name = t})
News b -> modify (\st -> st {news = b})
Send -> modify (\st -> st {sent = "Sent: " <> st.name <> (if st.news then ", with news" else "")})
The compiler loads once the page has started.
Your component will appear here after compilation.