Reactix.purs 1.44 KB
Newer Older
James Laver's avatar
James Laver committed
1 2 3
module Gargantext.Utils.Reactix
  where

4 5 6 7 8 9 10
import Prelude
import Data.Maybe ( Maybe(..) )
import Data.Nullable ( Nullable, null, toMaybe )
import Data.Traversable ( traverse_ )
import Data.Tuple ( Tuple(..) )
import Data.Tuple.Nested ( (/\) )
import DOM.Simple.Event as DE
11
import Effect (Effect)
12
import FFI.Simple ( (...), defineProperty )
James Laver's avatar
James Laver committed
13 14
import React ( ReactElement )
import Reactix as R
15 16
import Reactix.DOM.HTML (ElemFactory)
import Reactix.React (createElement)
17
import Reactix.SyntheticEvent as RE
James Laver's avatar
James Laver committed
18
import Unsafe.Coerce ( unsafeCoerce )
19
newtype Point = Point { x :: Number, y :: Number }
James Laver's avatar
James Laver committed
20 21 22 23 24 25 26 27 28 29 30

-- | Turns a ReactElement into a Reactix Element
-- | buff (v.) to polish
buff :: ReactElement -> R.Element
buff = unsafeCoerce

-- | Turns a Reactix Element into a ReactElement.
-- | scuff (v.) to spoil the gloss or finish of. 
scuff :: R.Element -> ReactElement
scuff = unsafeCoerce

31 32
mousePosition :: RE.SyntheticEvent DE.MouseEvent -> Point
mousePosition e = Point { x: RE.clientX e, y: RE.clientY e }
James Laver's avatar
James Laver committed
33

34 35 36
-- | This is naughty, it quietly mutates the input and returns it
named :: forall o. String -> o -> o
named = flip $ defineProperty "name"
37 38 39

overState :: forall t. (t -> t) -> R.State t -> Effect Unit
overState f (state /\ setState) = setState $ f state
40 41 42 43 44

useLayoutEffect1' :: forall a. a -> (Unit -> Effect Unit) -> R.Hooks Unit
useLayoutEffect1' a f = R.useLayoutEffect1 a $ \_ ->
  do f unit
     pure $ \_ -> pure unit
45 46 47 48


select :: ElemFactory
select = createElement "select"