Skip to content

Instantly share code, notes, and snippets.

@ibrahimsag
Created January 24, 2017 15:45
Show Gist options
  • Select an option

  • Save ibrahimsag/e142652bcad3c8ade14a727ae952937a to your computer and use it in GitHub Desktop.

Select an option

Save ibrahimsag/e142652bcad3c8ade14a727ae952937a to your computer and use it in GitHub Desktop.
Raw Halogen Component
// module Halogen.Component.Raw
//
exports.setHTML = function(el) {
return function (html) {
return function() {
el.innerHTML = html;
};
};
};
module Halogen.Component.Raw 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 Control.Monad.Aff (Aff)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Class (liftEff)
import DOM.HTML.Types (HTMLElement)
import Data.Maybe (Maybe(..))
foreign import data RAWHTML :: !
foreign import setHTML :: forall e. HTMLElement -> String -> Eff (rawhtml :: RAWHTML | e) Unit
type RawState =
{ element :: Maybe HTMLElement
, html :: String
}
initialRawState :: RawState
initialRawState =
{ element: Nothing
, html : ""
}
data RawQuery a
= SetElement (Maybe HTMLElement) a
| Initialize a
| Finalize a
| ChangeHTML String a
type RawOutput = Void
type RawMonad e = (Aff (rawhtml :: RAWHTML | e))
type RawDSL e = H.ComponentDSL RawState RawQuery RawOutput (RawMonad e)
rawComponent :: forall e. String -> H.Component HH.HTML RawQuery RawOutput (RawMonad e)
rawComponent initialHTML =
H.lifecycleComponent
{ render
, eval
, initialState : initialRawState
, initializer : Just (H.action Initialize)
, finalizer : Just (H.action Finalize)
}
where
render :: RawState -> H.ComponentHTML RawQuery
render = const $ HH.div [ HP.ref (HE.input SetElement) ] []
eval :: RawQuery ~> RawDSL e
eval = case _ of
SetElement el next -> do
H.modify (_ { element = el })
pure next
Initialize next -> do
H.modify (_ { html = initialHTML })
updateHTML
pure next
Finalize next -> pure next
ChangeHTML html next -> do
H.modify (_ { html = html })
updateHTML
pure next
updateHTML :: RawDSL e Unit
updateHTML = do
el <- H.gets _.element
html <- H.gets _.html
case el of
Nothing -> pure unit
Just el' -> do
liftEff $ setHTML el' html
pure unit
@ibrahimsag
Copy link
Author

HH.slot unit (defer \_ -> rawComponent "<a>test</a>") (const Nothing)

@ibrahimsag
Copy link
Author

ibrahimsag commented Jan 24, 2017

Should not be used without some sanitization though.

@flip111
Copy link

flip111 commented Apr 4, 2017

In halogen 1.2.1

Raw.purs line 40, column 1 - line 40, column 88

  Could not match kind

    Type

  with kind

    Type -> Type


while checking the kind of String -> Component HTML RawQuery RawOutput (RawMonad e)
in value declaration rawComponent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment