-
-
Save vyorkin/ded408bc2c24733e27fa21854f16e915 to your computer and use it in GitHub Desktop.
Raw Halogen HTML Component
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
module ParentView where | |
import Prelude | |
import Halogen as H | |
import Halogen.HTML as HH | |
import Halogen.HTML.Events as HE | |
import Halogen.HTML.Properties as HP | |
import Data.Maybe (Maybe(..)) | |
import Control.Monad.Aff (Aff) | |
import Halogen.Component.Raw | |
type State = String | |
type ParentMonad e = (Aff (rawhtml :: RAWHTML | e)) | |
data Query a = ParentQuery a | |
myParentView :: forall e. H.Component HH.HTML Query State Void (ParentMonad e) | |
myParentView = | |
H.parentComponent | |
{ initialState: const initialState | |
, render | |
, eval | |
, receiver: const Nothing | |
} | |
where | |
initialState = "<b>Test</b>" | |
render :: State -> H.ParentHTML Query RawQuery Unit (ParentMonad e) | |
render state = | |
HH.div_ [HH.slot unit rawComponent {html: state, elRef: "raw"} (const Nothing)] | |
eval :: Query ~> H.ParentDSL State Query RawQuery Unit Void (ParentMonad e) | |
eval = case _ of | |
ParentQuery next -> pure next |
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
// module Halogen.Component.Raw | |
// | |
exports.setHTML = function(el) { | |
return function (html) { | |
return function() { | |
el.innerHTML = 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
module Halogen.Component.Raw where | |
import Prelude | |
import Halogen as H | |
import Halogen.HTML as HH | |
import Halogen.HTML.Properties as HP | |
import Control.Monad.Aff (Aff) | |
import Control.Monad.Eff (Eff, kind Effect) | |
import Control.Monad.Eff.Class (liftEff) | |
import DOM.HTML.Types (HTMLElement) | |
import Data.Maybe (Maybe(..)) | |
foreign import data RAWHTML :: Effect | |
foreign import setHTML :: forall e. HTMLElement -> String -> Eff (rawhtml :: RAWHTML | e) Unit | |
type RawState = | |
{ elRef :: String | |
, html :: String | |
} | |
data RawQuery a | |
= Initialize a | |
type RawOutput = Void | |
type RawMonad e = (Aff (rawhtml :: RAWHTML | e)) | |
type RawDSL e = H.ComponentDSL RawState RawQuery RawOutput (RawMonad e) | |
rawComponent :: forall e. H.Component HH.HTML RawQuery RawState RawOutput (RawMonad e) | |
rawComponent = | |
H.lifecycleComponent | |
{ render | |
, eval | |
, initialState : id | |
, initializer : Just (H.action Initialize) | |
, finalizer : Nothing | |
, receiver: const Nothing | |
} | |
where | |
render :: RawState -> H.ComponentHTML RawQuery | |
render s = HH.div [ HP.ref (H.RefLabel s.elRef) ] [] | |
eval :: RawQuery ~> RawDSL e | |
eval = case _ of | |
Initialize next -> do | |
updateHTML | |
pure next | |
updateHTML :: RawDSL e Unit | |
updateHTML = do | |
elRef <- H.gets _.elRef | |
H.getHTMLElementRef (H.RefLabel elRef) >>= case _ of | |
Nothing -> pure unit | |
Just el -> do | |
html <- H.gets _.html | |
liftEff $ setHTML el html | |
pure unit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment