Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ScrollView to be controllable via scrollTop #740

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion examples/ScrollView.re
Original file line number Diff line number Diff line change
Expand Up @@ -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);

<View style=containerStyle>
<Text
text="Scroll top"
style=Style.[
marginBottom(10),
fontFamily("Roboto-Regular.ttf"),
fontSize(20.),
]
/>
<Slider
onValueChanged=handleScrollTop
value={Float.of_int(scrollTop)}
maximumValue=450.0
/>
<Text
text="Bounce"
style=Style.[
Expand All @@ -42,7 +60,8 @@ module Sample = {
checked=bounce
style=Style.[marginBottom(10)]
/>
<ScrollView style=outerBox bounce>
<ScrollView
style=outerBox bounce scrollTop onScroll={v => setScrollTop(_ => v)}>
<Image
src="outrun-logo.png"
/* Exercise the case in #579 */
Expand Down
31 changes: 27 additions & 4 deletions src/UI_Components/ScrollView.re
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ let%component make =
~scrollLeft=0,
~scrollTop=0,
~bounce=defaultBounce,
~onScroll=?,
~children=React.empty,
(),
) => {
Expand All @@ -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;
},
);
Comment on lines +60 to +72
Copy link
Member

@glennsl glennsl Jan 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will work if ~scrollTop is omitted, since it would then always reset to 0, its default value. So either scrollTop and onScroll needs to be mandatory, which I don't think is desirable, or scrollTop needs to be optional but without a default value so this can be skipped if it's not set.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glennsl I just tested it by adding an additional ScrollView to the example and leaving off the onScroll and scrollTop. Everything seemed to work fine in that case. Then I passed just a scrollTop and it rendered as expected. Are you referring to a case where scrollTop is missing but onScroll is provided?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, yeah that's a necessary condition too. It should check for the presence of scrollTop instead of onScroll. It's not very intuitive that hard-coding scrollTop and not setting onScroll would result in scrollTop being ignored.

I also don't think the effect hook is really necessary, and mostly just complicates the logic. Instead you could rename actualScrollTop to something like InternalScrollTop and then set actualScrollTop to scrollTop if present, or internalScrollTop if not.


let%hook (actualScrollTop, _bounceAnimationState, resetBouncingAnimation) =
switch (bouncingState) {
Expand Down Expand Up @@ -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
? <Slider
onValueChanged={v => dispatch(ScrollUpdated(int_of_float(v)))}
onValueChanged={v => updateScrollPos(int_of_float(v))}
minimumValue=0.
maximumValue={float_of_int(maxHeight)}
sliderLength={outerMeasurements.height}
Expand Down Expand Up @@ -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);
Expand Down