module React.Flux.Addons.React (
cssTransitionGroup
, PerfAction(..)
, PerfPrint(..)
, perfToggleButton_
, perfA
) where
import Control.DeepSeq (NFData)
import Control.Monad (forM_)
import GHC.Generics (Generic)
import React.Flux
#ifdef __GHCJS__
import GHCJS.Types (JSVal, JSString)
#endif
cssTransitionGroup :: [PropertyOrHandler eventHandler] -> ReactElementM eventHandler a -> ReactElementM eventHandler a
#ifdef __GHCJS__
cssTransitionGroup props children = foreignClass js_CSSTransitionGroup props children
foreign import javascript unsafe
"React['addons']['CSSTransitionGroup']"
js_CSSTransitionGroup :: JSVal
#else
cssTransitionGroup _ x = x
#endif
data PerfStoreData = PerfStoreData { perfIsActive :: Bool }
data PerfPrint = PerfPrintInclusive
| PerfPrintExclusive
| PerfPrintWasted
| PerfPrintDOM
deriving (Show, Eq, Generic)
instance NFData PerfPrint
data PerfAction = PerfStart
| PerfStopAndPrint [PerfPrint]
deriving (Show, Eq, Generic)
instance NFData PerfAction
instance StoreData PerfStoreData where
type StoreAction PerfStoreData = PerfAction
transform PerfStart _ = do
js_perf "start"
return $ PerfStoreData True
transform (PerfStopAndPrint toPrint) _ = do
js_perf "stop"
forM_ toPrint $ \action -> do
js_perf $ case action of
PerfPrintInclusive -> "printInclusive"
PerfPrintExclusive -> "printExclusive"
PerfPrintWasted -> "printWasted"
PerfPrintDOM -> "printDOM"
return $ PerfStoreData False
perfStore :: ReactStore PerfStoreData
perfStore = mkStore $ PerfStoreData False
perfA :: PerfAction -> SomeStoreAction
perfA = SomeStoreAction perfStore
perfToggleButton :: ReactView [PerfPrint]
perfToggleButton = defineControllerView "perf toggle button" perfStore $ \sData toPrint ->
button_ [ onClick $ \_ _ ->
if perfIsActive sData
then [perfA $ PerfStopAndPrint toPrint]
else [perfA PerfStart]
] $
if perfIsActive sData then "Stop perf measurement" else "Start perf measurement"
perfToggleButton_ :: [PerfPrint] -> ReactElementM handler ()
perfToggleButton_ toPrint = view perfToggleButton toPrint mempty
#ifdef __GHCJS__
foreign import javascript unsafe
"React['addons']['Perf'][$1]()"
js_perf :: JSString -> IO ()
#else
js_perf :: String -> IO ()
js_perf _ = return ()
#endif