@@ -6,46 +6,46 @@ import Data.Array.NonEmpty (cons')
66import Data.Maybe (Maybe (..))
77import Effect (Effect )
88import Effect.Exception (throw )
9- import React.Basic.DOM (css , render )
9+ import React.Basic.DOM (css )
1010import React.Basic.DOM as R
11+ import React.Basic.DOM.Client (createRoot , renderRoot )
1112import React.Basic.Events (handler_ )
1213import React.Basic.Hooks (Component , component , useState , (/\))
1314import React.Basic.Hooks as React
1415import Test.QuickCheck (mkSeed )
1516import Test.QuickCheck.Gen (Gen , elements , runGen )
17+ import Web.DOM.NonElementParentNode (getElementById )
1618import Web.HTML (window )
17- import Web.HTML.HTMLDocument (body )
18- import Web.HTML.HTMLElement (toElement )
19+ import Web.HTML.HTMLDocument (toNonElementParentNode )
1920import Web.HTML.Window (document )
2021
2122main :: Effect Unit
2223main = do
23- body <- body =<< document =<< window
24- case body of
25- Nothing -> throw " Could not find body."
26- Just b -> do
27- cardsComponent <- mkCardsComponent
28- render (cardsComponent {}) (toElement b)
24+ doc <- document =<< window
25+ root <- getElementById " root" $ toNonElementParentNode doc
26+ case root of
27+ Nothing -> throw " Could not find root."
28+ Just container -> do
29+ reactRoot <- createRoot container
30+ app <- mkApp
31+ renderRoot reactRoot (app {})
2932
30- mkCardsComponent :: Component { }
31- mkCardsComponent = do
32- let
33- initialGenState = { newSeed: mkSeed 3 , size: 1 }
33+ mkApp :: Component { }
34+ mkApp = do
35+ let initialGenState = { newSeed: mkSeed 3 , size: 1 }
3436 component " Cards" \_ -> React .do
3537 -- "Card state" is a tuple of the card and generator state.
3638 -- We don't need the actual generator state for rendering, so we're ignoring it with `_`.
3739 (card /\ _) /\ setCardState <- useState (runGen cardGenerator initialGenState)
3840 let
39- onClick =
40- handler_ do
41- -- Modify the card generator state by re-running the generator.
42- -- We don't need the card value for this update function, so it is ignored with `_`.
43- setCardState \(_ /\ genState) -> runGen cardGenerator genState
44- pure
45- $ R .div_
46- [ R .button { onClick, children: [ R .text " Draw" ] }
47- , R .div { style: css { fontSize: " 12em" }, children: [ R .text (viewCard card) ] }
48- ]
41+ onClick = handler_ do
42+ -- Modify the card generator state by re-running the generator.
43+ -- We don't need the card value for this update function, so it is ignored with `_`.
44+ setCardState \(_ /\ genState) -> runGen cardGenerator genState
45+ pure $ R .div_
46+ [ R .button { onClick, children: [ R .text " Draw" ] }
47+ , R .div { style: css { fontSize: " 12em" }, children: [ R .text (viewCard card) ] }
48+ ]
4949
5050data Card
5151 = Ace
@@ -63,22 +63,21 @@ data Card
6363 | King
6464
6565cardGenerator :: Gen Card
66- cardGenerator =
67- elements
68- $ cons' Ace
69- [ Two
70- , Three
71- , Four
72- , Five
73- , Six
74- , Seven
75- , Eight
76- , Nine
77- , Ten
78- , Jack
79- , Queen
80- , King
81- ]
66+ cardGenerator = elements $
67+ cons' Ace
68+ [ Two
69+ , Three
70+ , Four
71+ , Five
72+ , Six
73+ , Seven
74+ , Eight
75+ , Nine
76+ , Ten
77+ , Jack
78+ , Queen
79+ , King
80+ ]
8281
8382viewCard :: Card -> String
8483viewCard = case _ of
0 commit comments