@@ -2,40 +2,61 @@ module React.Basic.Hooks.UseGesture where
22
33import Prelude
44import Data.Array ((!!))
5−import Data.Foldable (fold)
6−import Data.Monoid.Additive (Additive(..))
7−import Data.Newtype (ala)
5+import Data.Maybe (Maybe, fromMaybe')
6+import Data.Nullable (Nullable)
87import Data.Tuple.Nested ((/\), type (/\))
8+import Debug.Trace (spy)
99import Effect (Effect)
10−import Effect.Uncurried (EffectFn1, runEffectFn1)
10+import Effect.Aff.Compat (runEffectFn2)
11+import Effect.Uncurried (EffectFn2)
1112import Effect.Unsafe (unsafePerformEffect)
13+import Partial.Unsafe (unsafeCrashWith)
1214import Prim.Row (class Lacks, class Nub)
1315import React.Basic.Events (EventHandler)
14−import React.Basic.Hooks (Hook, unsafeHook)
16+import React.Basic.Helpers (orUndefined)
17+import React.Basic.Hooks (Hook, Ref, unsafeHook)
1518import Record (disjointUnion)
19+import Web.DOM (Node)
1620import Yoga.Helpers ((?||))
1721
1822foreign import data UseDrag ∷ Type -> Type -> Type
1923
20−type DragHandler
21− = { down ∷ Boolean, movement ∷ Number /\ Number } -> Effect Unit
24+type DragHandler a
25+ = { arg ∷ a, down ∷ Boolean, movement ∷ Number /\ Number } -> Effect Unit
2226
23−type DragHandlerImpl
24− = { down ∷ Boolean, movement ∷ Array Number } -> Unit
27+type DragHandlerImpl a
28+ = { args ∷ Array a, down ∷ Boolean, movement ∷ Array Number } -> Unit
2529
2630type DragProps
2731 = { onMouseDown ∷ EventHandler, onTouchStart ∷ EventHandler }
2832
29−foreign import useDragImpl ∷ EffectFn1 DragHandlerImpl (Effect DragProps)
33+type GenericOptionsImpl r
34+ = { domTarget ∷ Ref (Nullable Node) | r }
3035
31−useDrag ∷ DragHandler -> Hook (UseDrag Unit) (Effect DragProps)
32−useDrag dragHandler = unsafeHook (runEffectFn1 useDragImpl f)
36+type GenericOptions r
37+ = { domTarget ∷ Maybe (Ref (Nullable Node)) | r }
38+
39+type DragOptions
40+ = GenericOptions ()
41+
42+type DragOptionsImpl
43+ = GenericOptionsImpl ()
44+
45+dragOptionsToImpl ∷ DragOptions -> DragOptionsImpl
46+dragOptionsToImpl { domTarget } = { domTarget: domTarget # orUndefined }
47+
48+foreign import useDragImpl ∷ ∀ a. EffectFn2 (DragHandlerImpl a) DragOptionsImpl (a -> DragProps)
49+
50+useDrag ∷ ∀ a. DragOptions -> DragHandler a -> Hook (UseDrag Unit) (a -> DragProps)
51+useDrag dragOptions dragHandler = unsafeHook (runEffectFn2 useDragImpl dragHandlerImpl (dragOptionsToImpl dragOptions))
3352 where
34− f x =
53+ dragHandlerImpl ∷ DragHandlerImpl a
54+ dragHandlerImpl x =
3555 (unsafePerformEffect <<< dragHandler)
36− { down: x.down, movement: mx /\ my
56+ { arg, down: x.down, movement: mx /\ my
3757 }
3858 where
59+ arg = x.args !! 0 # fromMaybe' (\_ -> unsafeCrashWith "Bollox")
3960 mx = x.movement !! 0 ?|| 0.0
4061 my = x.movement !! 1 ?|| 0.0
4162
@@ -47,6 +68,6 @@ withDragProps ∷
4768 ( onMouseDown ∷ EventHandler, onTouchStart ∷ EventHandler | attrs )
4869 ( onMouseDown ∷ EventHandler, onTouchStart ∷ EventHandler | attrs ) =>
4970 { | attrs } ->
50− Effect DragProps ->
71+ DragProps ->
5172 { onMouseDown ∷ EventHandler, onTouchStart ∷ EventHandler | attrs }
52−withDragProps x (y ∷ Effect DragProps) = disjointUnion (unsafePerformEffect y) x
73+withDragProps x (y ∷ DragProps) = disjointUnion y x