diff --git a/examples/ScrollView.re b/examples/ScrollView.re index fed70929c..807d2a7ae 100644 --- a/examples/ScrollView.re +++ b/examples/ScrollView.re @@ -26,9 +26,27 @@ let innerBox = module Sample = { let%component make = () => { + let%hook (scrollTop, setScrollTop) = Hooks.state(0); let%hook (bounce, setBounce) = Hooks.state(true); + let handleScrollTop = s => setScrollTop(_ => Float.to_int(s)); + Console.out("scrollTop: "); + Console.log(scrollTop); + + + - + setScrollTop(_ => v)}> { @@ -56,6 +57,19 @@ let%component make = Hooks.state(None); let%hook (actualScrollLeft, setScrollLeft) = Hooks.state(scrollLeft); let%hook (bouncingState, setBouncingState) = Hooks.state(Idle); + let%hook () = + Hooks.effect( + Always, + () => { + if (scrollTop != actualScrollTop) { + switch (onScroll) { + | Some(_) => dispatch(ScrollUpdated(scrollTop)) + | None => () + }; + }; + None; + }, + ); let%hook (actualScrollTop, _bounceAnimationState, resetBouncingAnimation) = switch (bouncingState) { @@ -114,11 +128,18 @@ let%component make = let isVerticalScrollbarVisible = maxHeight > 0; let isHorizontalScrollbarVisible = maxWidth > 0; + let updateScrollPos = v => { + dispatch(ScrollUpdated(v)); + switch (onScroll) { + | Some(f) => f(v) + | None => () + }; + }; let verticalScrollBar = isVerticalScrollbarVisible ? dispatch(ScrollUpdated(int_of_float(v)))} + onValueChanged={v => updateScrollPos(int_of_float(v))} minimumValue=0. maximumValue={float_of_int(maxHeight)} sliderLength={outerMeasurements.height} @@ -170,11 +191,13 @@ let%component make = | Bouncing(_) => () | Idle when !bounce && (isAtTop || isAtBottom) => let clampedScrollTop = isAtTop ? 0 : maxHeight; - dispatch(ScrollUpdated(clampedScrollTop)); + updateScrollPos(clampedScrollTop); | Idle when bounce && (isAtTop || isAtBottom) => setBouncingState(_ => Bouncing(- delta * 2)); - dispatch(ScrollUpdated(isAtTop ? 0 : maxHeight)); - | Idle => dispatch(ScrollUpdated(newScrollTop)) + updateScrollPos(isAtTop ? 0 : maxHeight); + | Idle => + dispatch(ScrollUpdated(newScrollTop)); + updateScrollPos(newScrollTop); }; }; (horizontalScrollbar, verticalScrollBar, scroll);