-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port all Halogen examples to Halogen Hooks recipes (#177)
* Add ComponentsInputHalogenHooks * Add ComponentsMultiTypeHalogenHooks * Add DriverIoHalogenHooks * Remove unused dependency: halogen-hooks-extra * Add DriverRoutingHalogenHooks * Add DriverWebSocketsHalogenHooks * Add InterpretHalogenHooks * Add KeyboardInputHalogenHooks * Add LifecycleHalogenHooks * Fix compiler warning about shadowed variable name * Import constructors; remove unused import
- Loading branch information
1 parent
0b49a62
commit 4e21666
Showing
49 changed files
with
1,064 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/bower_components/ | ||
/node_modules/ | ||
/.pulp-cache/ | ||
/output/ | ||
/generated-docs/ | ||
/.psc-package/ | ||
/.psc* | ||
/.purs* | ||
/.psa* | ||
/.spago | ||
/web-dist/ | ||
/prod-dist/ | ||
/prod/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# ComponentsInputHalogenHooks | ||
|
||
Each time a parent re-renders, it will pass a new input value into the child, and the child will update accordingly. | ||
|
||
## Expected Behavior: | ||
|
||
### Browser | ||
|
||
The parent stores an `Int`. Clicking the buttons will increment/decrement that value. The children will produce the result of a mathematical computation using that value. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ name = "ComponentsInputHalogenHooks" | ||
, dependencies = | ||
[ "console" | ||
, "effect" | ||
, "halogen-hooks" | ||
, "psci-support" | ||
] | ||
, packages = ../../packages.dhall | ||
, sources = [ "recipes/ComponentsInputHalogenHooks/src/**/*.purs" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module ComponentsInputHalogenHooks.Main where | ||
|
||
import Prelude hiding (top) | ||
|
||
import Data.Maybe (Maybe(..)) | ||
import Data.Symbol (SProxy(..)) | ||
import Data.Tuple.Nested ((/\)) | ||
import Effect (Effect) | ||
import Effect.Class (class MonadEffect) | ||
import Halogen as H | ||
import Halogen.Aff as HA | ||
import Halogen.HTML as HH | ||
import Halogen.HTML.Events as HE | ||
import Halogen.Hooks as Hooks | ||
import Halogen.VDom.Driver (runUI) | ||
|
||
main :: Effect Unit | ||
main = | ||
HA.runHalogenAff do | ||
body <- HA.awaitBody | ||
void $ runUI containerComponent unit body | ||
|
||
_button :: SProxy "button" | ||
_button = SProxy | ||
|
||
containerComponent | ||
:: forall unusedQuery unusedInput unusedOutput anyMonad | ||
. MonadEffect anyMonad | ||
=> H.Component HH.HTML unusedQuery unusedInput unusedOutput anyMonad | ||
containerComponent = Hooks.component \_ _ -> Hooks.do | ||
state /\ stateIdx <- Hooks.useState 0 | ||
Hooks.pure $ | ||
HH.div_ | ||
[ HH.ul_ | ||
[ HH.slot _display 1 displayComponent state absurd | ||
, HH.slot _display 2 displayComponent (state * 2) absurd | ||
, HH.slot _display 3 displayComponent (state * 3) absurd | ||
, HH.slot _display 4 displayComponent (state * 10) absurd | ||
, HH.slot _display 5 displayComponent (state * state) absurd | ||
] | ||
, HH.button | ||
[ HE.onClick \_ -> Just $ Hooks.modify_ stateIdx (_ + 1) ] | ||
[ HH.text "+1"] | ||
, HH.button | ||
[ HE.onClick \_ -> Just $ Hooks.modify_ stateIdx (_ - 1) ] | ||
[ HH.text "-1"] | ||
] | ||
|
||
_display = SProxy :: SProxy "display" | ||
|
||
displayComponent | ||
:: forall unusedQuery unusedOutput anyMonad | ||
. MonadEffect anyMonad | ||
=> H.Component HH.HTML unusedQuery Int unusedOutput anyMonad | ||
displayComponent = Hooks.component \_ input -> Hooks.do | ||
Hooks.pure $ | ||
HH.div_ | ||
[ HH.text "My input value is:" | ||
, HH.strong_ [ HH.text (show input) ] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>ComponentsInputHalogenHooks</title> | ||
</head> | ||
|
||
<body> | ||
<script src="./index.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"use strict"; | ||
require("../../../output/ComponentsInputHalogenHooks.Main/index.js").main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/bower_components/ | ||
/node_modules/ | ||
/.pulp-cache/ | ||
/output/ | ||
/generated-docs/ | ||
/.psc-package/ | ||
/.psc* | ||
/.purs* | ||
/.psa* | ||
/.spago | ||
/web-dist/ | ||
/prod-dist/ | ||
/prod/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# ComponentsMultiTypeHalogenHooks | ||
|
||
Demonstrates a component that can communicate with its children that have differing types. | ||
|
||
## Expected Behavior: | ||
|
||
### Browser | ||
|
||
A toggle button, a count button, and an input field are the children of the parent. The user can modify either of those children's values and then click the "Check now" button. The parent will get the latest state from each of its children and display all states. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ name = "ComponentsMultiTypeHalogenHooks" | ||
, dependencies = | ||
[ "console" | ||
, "effect" | ||
, "halogen-hooks" | ||
, "psci-support" | ||
] | ||
, packages = ../../packages.dhall | ||
, sources = [ "recipes/ComponentsMultiTypeHalogenHooks/src/**/*.purs" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
module ComponentsMultiTypeHalogenHooks.Main where | ||
|
||
import Prelude | ||
|
||
import Data.Maybe (Maybe(..)) | ||
import Data.Symbol (SProxy(..)) | ||
import Data.Tuple.Nested ((/\)) | ||
import Effect (Effect) | ||
import Halogen as H | ||
import Halogen.Aff as HA | ||
import Halogen.HTML as HH | ||
import Halogen.HTML.Events as HE | ||
import Halogen.HTML.Properties as HP | ||
import Halogen.Hooks as Hooks | ||
import Halogen.VDom.Driver (runUI) | ||
|
||
main :: Effect Unit | ||
main = | ||
HA.runHalogenAff do | ||
body <- HA.awaitBody | ||
void $ runUI containerComponent unit body | ||
|
||
_button :: SProxy "button" | ||
_button = SProxy | ||
|
||
containerComponent | ||
:: forall unusedQuery unusedInput unusedOutput anyMonad | ||
. H.Component HH.HTML unusedQuery unusedInput unusedOutput anyMonad | ||
containerComponent = Hooks.component \rec _ -> Hooks.do | ||
state /\ stateIdx <- Hooks.useState { a: Nothing, b: Nothing, c: Nothing } | ||
let | ||
_a = SProxy :: SProxy "a" | ||
_b = SProxy :: SProxy "b" | ||
_c = SProxy :: SProxy "c" | ||
|
||
Hooks.pure $ | ||
HH.div_ | ||
[ HH.div | ||
[ HP.class_ (H.ClassName "box")] | ||
[ HH.h1_ [ HH.text "Component A" ] | ||
, HH.slot _a unit componentA unit absurd | ||
] | ||
, HH.div | ||
[ HP.class_ (H.ClassName "box")] | ||
[ HH.h1_ [ HH.text "Component B" ] | ||
, HH.slot _b unit componentB unit absurd | ||
] | ||
, HH.div | ||
[ HP.class_ (H.ClassName "box")] | ||
[ HH.h1_ [ HH.text "Component C" ] | ||
, HH.slot _c unit componentC unit absurd | ||
] | ||
, HH.p_ | ||
[ HH.text "Last observed states:"] | ||
, HH.ul_ | ||
[ HH.li_ [ HH.text ("Component A: " <> show state.a) ] | ||
, HH.li_ [ HH.text ("Component B: " <> show state.b) ] | ||
, HH.li_ [ HH.text ("Component C: " <> show state.c) ] | ||
] | ||
, HH.button | ||
[ HE.onClick \_ -> Just do | ||
a <- Hooks.query rec.slotToken _a unit (H.request IsOn) | ||
b <- Hooks.query rec.slotToken _b unit (H.request GetCount) | ||
c <- Hooks.query rec.slotToken _c unit (H.request GetValue) | ||
Hooks.put stateIdx { a, b, c } | ||
] | ||
[ HH.text "Check states now" ] | ||
] | ||
|
||
data QueryA a = IsOn (Boolean -> a) | ||
|
||
componentA | ||
:: forall unusedInput unusedOutput anyMonad | ||
. H.Component HH.HTML QueryA unusedInput unusedOutput anyMonad | ||
componentA = Hooks.component \rec _ -> Hooks.do | ||
enabled /\ enabledIdx <- Hooks.useState false | ||
Hooks.useQuery rec.queryToken case _ of | ||
IsOn reply -> do | ||
isEnabled <- Hooks.get enabledIdx | ||
pure $ Just $ reply isEnabled | ||
Hooks.pure $ | ||
HH.div_ | ||
[ HH.p_ [ HH.text "Toggle me!" ] | ||
, HH.button | ||
[ HE.onClick \_ -> Just do | ||
Hooks.modify_ enabledIdx not ] | ||
[ HH.text (if enabled then "On" else "Off") ] | ||
] | ||
|
||
data QueryB a = GetCount (Int -> a) | ||
|
||
componentB | ||
:: forall unusedInput unusedOutput anyMonad | ||
. H.Component HH.HTML QueryB unusedInput unusedOutput anyMonad | ||
componentB = Hooks.component \rec _ -> Hooks.do | ||
count /\ countIdx <- Hooks.useState 0 | ||
Hooks.useQuery rec.queryToken case _ of | ||
GetCount reply -> do | ||
currentCount <- Hooks.get countIdx | ||
pure $ Just $ reply currentCount | ||
Hooks.pure $ | ||
HH.div_ | ||
[ HH.p_ | ||
[ HH.text "Current value: " | ||
, HH.strong_ [ HH.text (show count) ] | ||
] | ||
, HH.button | ||
[ HE.onClick \_ -> Just $ Hooks.modify_ countIdx (_ + 1) ] | ||
[ HH.text ("Increment") ] | ||
] | ||
|
||
data QueryC a = GetValue (String -> a) | ||
|
||
componentC | ||
:: forall unusedInput unusedOutput anyMonad | ||
. H.Component HH.HTML QueryC unusedInput unusedOutput anyMonad | ||
componentC = Hooks.component \rec _ -> Hooks.do | ||
state /\ stateIdx <- Hooks.useState "Hello" | ||
Hooks.useQuery rec.queryToken case _ of | ||
GetValue reply -> do | ||
value <- Hooks.get stateIdx | ||
pure $ Just $ reply value | ||
Hooks.pure $ | ||
HH.label_ | ||
[ HH.p_ [ HH.text "What do you have to say?" ] | ||
, HH.input | ||
[ HP.value state | ||
, HE.onValueInput (Just <<< Hooks.put stateIdx) | ||
] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>ComponentsMultiTypeHalogenHooks</title> | ||
</head> | ||
|
||
<body> | ||
<script src="./index.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"use strict"; | ||
require("../../../output/ComponentsMultiTypeHalogenHooks.Main/index.js").main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/bower_components/ | ||
/node_modules/ | ||
/.pulp-cache/ | ||
/output/ | ||
/generated-docs/ | ||
/.psc-package/ | ||
/.psc* | ||
/.purs* | ||
/.psa* | ||
/.spago | ||
/web-dist/ | ||
/prod-dist/ | ||
/prod/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# DriverIoHalogenHooks | ||
|
||
Demonstrates how to communicate with a Halogen application by sending messages to and receiving messages from the root-level component via the driver. | ||
|
||
## Expected Behavior: | ||
|
||
Prints: | ||
``` | ||
The button state is currently: (Just false) | ||
The button state is now: (Just true) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ name = "DriverIoHalogenHooks" | ||
, dependencies = | ||
[ "console" | ||
, "effect" | ||
, "halogen-hooks" | ||
, "psci-support" | ||
] | ||
, packages = ../../packages.dhall | ||
, sources = [ "recipes/DriverIoHalogenHooks/src/**/*.purs" ] | ||
} |
Oops, something went wrong.