Popover.purs 1.06 KB
Newer Older
1 2
module Gargantext.Utils.Popover where

arturo's avatar
arturo committed
3 4 5
import Gargantext.Prelude

import DOM.Simple as DOM
6
import Data.Maybe (maybe)
7
import Data.Nullable (Nullable, toMaybe)
8
import Effect (Effect)
9
import Effect.Uncurried (EffectFn2, runEffectFn2)
10
import Reactix as R
arturo's avatar
arturo committed
11
import Record as Record
12

13 14
type PopoverRef = R.Ref (Nullable DOM.Element)

15 16
type Props =
  (
17 18
    arrow :: Boolean
  , open :: Boolean
19 20
  , onClose :: Unit -> Effect Unit
  , onOpen :: Unit -> Effect Unit
21
  , ref :: PopoverRef
22
  )
23 24 25

foreign import popoverCpt :: R.Component Props

arturo's avatar
arturo committed
26
-- | https://github.com/vaheqelyan/react-awesome-popover
27
popover :: Record Props -> Array R.Element -> R.Element
arturo's avatar
arturo committed
28 29 30
popover props children = R.rawCreateElement popoverCpt props' children
  where
    props' = Record.merge props { className: "awesome-popover" }
31

32
foreign import _setState :: forall a. EffectFn2 DOM.Element a Unit
33

34
setState :: forall a. DOM.Element -> a -> Effect Unit
35 36 37
setState = runEffectFn2 _setState

setOpen :: PopoverRef -> Boolean -> Effect Unit
38
setOpen ref val = maybe (pure unit) (\p -> setState p {open: val}) $ toMaybe $ R.readRef ref