{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
module Glazier.React.Element
( ReactElement
, unsafeCoerceElement
, mkBranchElement
, mkLeafElement
, textElement
, mkCombinedElements
) where
import Control.DeepSeq
import Control.Newtype.Generics
import Data.String
import qualified GHC.Generics as G
import qualified GHCJS.Marshal.Pure as J
import qualified GHCJS.Types as J
import qualified JavaScript.Array as JA
import qualified JavaScript.Extras as JE
import qualified JavaScript.Object as JO
newtype ReactElement = ReactElement JE.JSRep
deriving (G.Generic, Show, J.IsJSVal, J.PToJSVal, JE.ToJS, IsString, NFData)
instance Newtype ReactElement
unsafeCoerceElement :: J.JSVal -> ReactElement
unsafeCoerceElement = ReactElement . JE.JSRep
mkBranchElement :: JE.JSRep -> [JE.Property] -> [ReactElement] -> IO ReactElement
mkBranchElement n props xs =
js_mkBranchElement n (JE.fromProperties props) (JA.fromList $ JE.toJS <$> xs)
mkLeafElement :: JE.JSRep -> [JE.Property] -> IO ReactElement
mkLeafElement n props =
js_mkLeafElement n (JE.fromProperties props)
textElement :: J.JSString -> ReactElement
textElement = js_textElement
mkCombinedElements :: [ReactElement] -> IO ReactElement
mkCombinedElements xs = js_mkCombinedElements (JA.fromList $ JE.toJS <$> xs)
#ifdef __GHCJS__
foreign import javascript unsafe
"$r = hgr$React().createElement($1, $2, $3);"
js_mkBranchElement :: JE.JSRep -> JO.Object -> JA.JSArray -> IO ReactElement
foreign import javascript unsafe
"$r = hgr$React().createElement($1, $2);"
js_mkLeafElement :: JE.JSRep -> JO.Object -> IO ReactElement
foreign import javascript unsafe
"$r = $1;"
js_textElement :: J.JSString -> ReactElement
foreign import javascript unsafe
"$r = hgr$mkCombinedElements($1);"
js_mkCombinedElements :: JA.JSArray -> IO ReactElement
#else
js_mkBranchElement :: JE.JSRep -> JO.Object -> JA.JSArray -> IO ReactElement
js_mkBranchElement _ _ _ = pure (ReactElement $ JE.JSRep J.nullRef)
js_mkLeafElement :: JE.JSRep -> JO.Object -> IO ReactElement
js_mkLeafElement _ _ = pure (ReactElement $ JE.JSRep J.nullRef)
js_textElement :: J.JSString -> ReactElement
js_textElement _ = ReactElement $ JE.JSRep J.nullRef
js_mkCombinedElements :: JA.JSArray -> IO ReactElement
js_mkCombinedElements _ = pure (ReactElement $ JE.JSRep J.nullRef)
#endif