module React.Flux.Addons.React (
cssTransitionGroup
, CSSTransitionProps(..)
, defaultTransitionProps
, PerfAction(..)
, PerfPrint(..)
, perfToggleButton_
, perfA
) where
import Control.DeepSeq (NFData)
import Control.Monad (forM_)
import Data.Typeable (Typeable)
import GHC.Generics (Generic)
import React.Flux
#ifdef __GHCJS__
import GHCJS.Types (JSRef, JSString)
#endif
data CSSTransitionProps = CSSTransitionProps
{ transitionName :: String
, transitionAppear :: Bool
, transitionEnter :: Bool
, transitionLeave :: Bool
} deriving (Typeable)
defaultTransitionProps :: CSSTransitionProps
defaultTransitionProps = CSSTransitionProps
{ transitionName = "react-transition"
, transitionAppear = False
, transitionEnter = True
, transitionLeave = True
}
cssTransitionGroup :: CSSTransitionProps -> ReactElementM eventHandler a -> ReactElementM eventHandler a
#ifdef __GHCJS__
cssTransitionGroup p children = foreignClass js_CSSTransitionGroup props children
where
props = [ "transitionName" @= transitionName p
, "transitionAppear" @= transitionAppear p
, "transitionEnter" @= transitionEnter p
, "transitionLeave" @= transitionLeave p
]
foreign import javascript unsafe
"React['addons']['CSSTransitionGroup']"
js_CSSTransitionGroup :: JSRef ()
#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