@@ -15,25 +15,27 @@ import Data.Maybe (Maybe(..), fromMaybe, isJust)
15
15
import Data.Show.Generic (genericShow )
16
16
import Effect (Effect )
17
17
import Effect.Exception (throw )
18
- import React.Basic.DOM (CSS , css , render )
18
+ import React.Basic.DOM (CSS , css )
19
19
import React.Basic.DOM as R
20
+ import React.Basic.DOM.Client (createRoot , renderRoot )
20
21
import React.Basic.Events (EventHandler , handler_ )
21
22
import React.Basic.Hooks (Component , JSX , Reducer , component , keyed , mkReducer , useReducer , (/\))
22
23
import React.Basic.Hooks as React
24
+ import Web.DOM.NonElementParentNode (getElementById )
23
25
import Web.HTML (window )
24
- import Web.HTML.HTMLDocument (body )
25
- import Web.HTML.HTMLElement (toElement )
26
+ import Web.HTML.HTMLDocument (toNonElementParentNode )
26
27
import Web.HTML.Window (document )
27
28
28
29
main :: Effect Unit
29
30
main = do
30
- body <- body =<< document =<< window
31
- case body of
32
- Nothing -> throw " Could not find body."
33
- Just b -> do
34
- game <- mkGame
35
- render (game unit) (toElement b)
36
- mempty
31
+ doc <- document =<< window
32
+ root <- getElementById " root" $ toNonElementParentNode doc
33
+ case root of
34
+ Nothing -> throw " Could not find root."
35
+ Just container -> do
36
+ reactRoot <- createRoot container
37
+ app <- mkApp
38
+ renderRoot reactRoot (app {})
37
39
38
40
mkSquare :: Component { onClick :: EventHandler , value :: Square }
39
41
mkSquare =
@@ -71,8 +73,7 @@ mkBoard
71
73
mkBoard = do
72
74
square <- mkSquare
73
75
component " Board" \props -> React .do
74
- let
75
- renderSquare i j = square { value: props.squares i j, onClick: props.onClick i j }
76
+ let renderSquare i j = square { value: props.squares i j, onClick: props.onClick i j }
76
77
pure
77
78
$ R .div_
78
79
[ R .div
@@ -142,8 +143,8 @@ initialState =
142
143
, xIsNext: true
143
144
}
144
145
145
- mkGame :: Component Unit
146
- mkGame = do
146
+ mkApp :: Component { }
147
+ mkApp = do
147
148
board <- mkBoard
148
149
reducer <- reducerFn
149
150
component " Game" \_ -> React .do
@@ -168,7 +169,9 @@ mkGame = do
168
169
moves :: Array JSX
169
170
moves =
170
171
renderMove " Go to start" 0
171
- : Array .mapWithIndex (\i _ -> renderMove (" Go to move #" <> show (i + 1 )) (i + 1 )) (NonEmpty .tail state.history)
172
+ : Array .mapWithIndex
173
+ (\i _ -> renderMove (" Go to move #" <> show (i + 1 )) (i + 1 ))
174
+ (NonEmpty .tail state.history)
172
175
173
176
status :: String
174
177
status = case calculateWinner current of
@@ -222,13 +225,7 @@ calculateWinner f =
222
225
223
226
-- These styles could be provided by a proper stylesheet, we are only
224
227
-- defining them here for the sake of compatibility with TryPureScript
225
- styles
226
- :: { list :: CSS
227
- , boardRow :: CSS
228
- , square :: CSS
229
- , game :: CSS
230
- , gameInfo :: CSS
231
- }
228
+ styles :: { list :: CSS , boardRow :: CSS , square :: CSS , game :: CSS , gameInfo :: CSS }
232
229
styles =
233
230
{ list:
234
231
css
0 commit comments