Commit bf93eb01 authored by James Laver's avatar James Laver

Reactix utils

parent 35e85826
...@@ -23,9 +23,6 @@ import Gargantext.Utils.Reactix as R2 ...@@ -23,9 +23,6 @@ import Gargantext.Utils.Reactix as R2
type Props t = ( x :: Number, y :: Number, setMenu :: R2.StateSetter (Maybe t) ) type Props t = ( x :: Number, y :: Number, setMenu :: R2.StateSetter (Maybe t) )
getPortalHost :: R.Hooks DOM.Element
getPortalHost = R.unsafeHooksEffect $ delay unit $ \_ -> pure $ document ... "getElementById" $ ["menu-portal"]
contextMenu :: forall t. Record (Props t) -> Array R.Element -> R.Element contextMenu :: forall t. Record (Props t) -> Array R.Element -> R.Element
contextMenu = R.createElement contextMenuCpt contextMenu = R.createElement contextMenuCpt
...@@ -33,7 +30,7 @@ contextMenuCpt :: forall t. R.Component (Props t) ...@@ -33,7 +30,7 @@ contextMenuCpt :: forall t. R.Component (Props t)
contextMenuCpt = R.hooksComponent "ContextMenu" cpt contextMenuCpt = R.hooksComponent "ContextMenu" cpt
where where
cpt menu children = do cpt menu children = do
host <- getPortalHost host <- R2.getPortalHost
root <- R.useRef null root <- R.useRef null
rect /\ setRect <- R.useState $ \_ -> Nothing rect /\ setRect <- R.useState $ \_ -> Nothing
R.useLayoutEffect1 (R.readRef root) $ do R.useLayoutEffect1 (R.readRef root) $ do
......
...@@ -3,6 +3,9 @@ module Gargantext.Utils.Reactix ...@@ -3,6 +3,9 @@ module Gargantext.Utils.Reactix
import Prelude import Prelude
import DOM.Simple as DOM
import DOM.Simple.Document (document)
import DOM.Simple.Event as DE
import Data.Maybe (Maybe(..)) import Data.Maybe (Maybe(..))
import Data.Nullable (Nullable, null, toMaybe) import Data.Nullable (Nullable, null, toMaybe)
import Data.Traversable (traverse_) import Data.Traversable (traverse_)
...@@ -13,12 +16,14 @@ import DOM.Simple.Event as DE ...@@ -13,12 +16,14 @@ import DOM.Simple.Event as DE
import DOM.Simple as DOM import DOM.Simple as DOM
import Effect (Effect) import Effect (Effect)
import Effect.Class (liftEffect) import Effect.Class (liftEffect)
import Effect.Aff (Aff, launchAff, launchAff_, killFiber)
import Effect.Exception (error)
import Effect.Uncurried (EffectFn1, mkEffectFn1) import Effect.Uncurried (EffectFn1, mkEffectFn1)
import FFI.Simple ((...), defineProperty) import FFI.Simple ((...), defineProperty, delay)
import React (ReactClass, ReactElement, Children, class IsReactElement, class ReactPropFields) import React (ReactClass, ReactElement, Children, class IsReactElement, class ReactPropFields)
import React as React import React as React
import Reactix as R import Reactix as R
import Reactix.DOM.HTML (ElemFactory) import Reactix.DOM.HTML (ElemFactory, text)
import Reactix.React (createDOMElement) import Reactix.React (createDOMElement)
import Reactix.SyntheticEvent as RE import Reactix.SyntheticEvent as RE
import Thermite (Spec, simpleSpec, Render, defaultPerformAction) import Thermite (Spec, simpleSpec, Render, defaultPerformAction)
...@@ -26,7 +31,7 @@ import Unsafe.Coerce (unsafeCoerce) ...@@ -26,7 +31,7 @@ import Unsafe.Coerce (unsafeCoerce)
newtype Point = Point { x :: Number, y :: Number } newtype Point = Point { x :: Number, y :: Number }
type StateSetter a = (a -> a) -> Effect Unit type Setter t = (t -> t) -> Effect Unit
-- | Turns a ReactElement into aReactix Element -- | Turns a ReactElement into aReactix Element
-- | buff (v.) to polish -- | buff (v.) to polish
...@@ -79,6 +84,12 @@ instance isComponentReactClass ...@@ -79,6 +84,12 @@ instance isComponentReactClass
React.createElement reactClass props children React.createElement reactClass props children
-} -}
-- | Turns an aff into a useEffect-compatible Effect (Effect Unit)
affEffect :: forall a. String -> Aff a -> Effect (Effect Unit)
affEffect errmsg aff = do
fiber <- launchAff aff
pure $ launchAff_ $ killFiber (error errmsg) fiber
mousePosition :: RE.SyntheticEvent DE.MouseEvent -> Point mousePosition :: RE.SyntheticEvent DE.MouseEvent -> Point
mousePosition e = Point { x: RE.clientX e, y: RE.clientY e } mousePosition e = Point { x: RE.clientX e, y: RE.clientY e }
...@@ -91,7 +102,6 @@ named = flip $ defineProperty "name" ...@@ -91,7 +102,6 @@ named = flip $ defineProperty "name"
overState :: forall t. (t -> t) -> R.State t -> Effect Unit overState :: forall t. (t -> t) -> R.State t -> Effect Unit
overState f (_state /\ setState) = setState f overState f (_state /\ setState) = setState f
select :: ElemFactory select :: ElemFactory
select = createDOMElement "select" select = createDOMElement "select"
...@@ -129,3 +139,27 @@ readPositionRef :: R.Ref (Nullable DOM.Element) -> Maybe DOM.DOMRect ...@@ -129,3 +139,27 @@ readPositionRef :: R.Ref (Nullable DOM.Element) -> Maybe DOM.DOMRect
readPositionRef el = do readPositionRef el = do
let posRef = R.readRef el let posRef = R.readRef el
Element.boundingRect <$> toMaybe posRef Element.boundingRect <$> toMaybe posRef
unsafeEventTarget :: forall event. event -> DOM.Element
unsafeEventTarget e = (unsafeCoerce e).target
getElementById :: String -> Effect (Maybe DOM.Element)
getElementById = (flip delay) h
where
h id = pure $ toMaybe $ document ... "getElementById" $ [id]
-- We just assume it works, so make sure it's in the html
getPortalHost :: R.Hooks DOM.Element
getPortalHost = R.unsafeHooksEffect $ delay unit $ \_ -> pure $ document ... "getElementById" $ ["portal"]
useLayoutEffectOnce :: Effect (Effect Unit) -> R.Hooks Unit
useLayoutEffectOnce e = R.unsafeUseLayoutEffect e []
singleParent :: forall props. R.Component props -> Record props -> R.Element -> R.Element
singleParent cpt props child = R.createElement cpt props [ child ]
childless :: forall props. R.Component props -> Record props -> R.Element
childless cpt props = R.createElement cpt props []
showText :: forall s. Show s => s -> R.Element
showText = text <<< show
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment