Resize.purs 1.34 KB
Newer Older
arturo's avatar
arturo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
module Gargantext.Hooks.Resize
  ( useResizeHandler
  , ResizeType(..)
  ) where

import Gargantext.Prelude

import DOM.Simple (Document, Window, window, document)
import Data.Generic.Rep (class Generic)
import Data.Show.Generic (genericShow)
import Data.String.Extra (kebabCase)
import Effect (Effect)
import Effect.Uncurried (EffectFn2, EffectFn5, runEffectFn2, runEffectFn5)
import Reactix as R


foreign import _add :: EffectFn5
  Window
  Document
  String
  String
  String
  Unit

add ::
     Window
  -> Document
  -> String
  -> String
  -> String
  -> Effect Unit
add = runEffectFn5 _add


foreign import _remove :: EffectFn2
  Document
  String
  Unit

remove ::
     Document
  -> String
  -> Effect Unit
remove = runEffectFn2 _remove

-- @XXX: React.ref element clunky design
--       Using string selector query instead...
type Output =
  ( add     :: String -> String -> ResizeType -> Effect Unit
  , remove  :: String -> Effect Unit
  )

useResizeHandler :: R.Hooks (Record Output)
useResizeHandler = pure
  { add: \source target t ->
      add window document source target (show t)
  , remove: remove document
  }

-----------------------------------------------


data ResizeType
  = Vertical
  | Horizontal
  | Both

derive instance Generic ResizeType _
derive instance Eq ResizeType
instance Show ResizeType where
  show = kebabCase <<< genericShow