Skip to content
This repository has been archived by the owner on Oct 1, 2021. It is now read-only.

implement scrollToIndex() #97

Merged
merged 6 commits into from
Jun 13, 2018
Merged

implement scrollToIndex() #97

merged 6 commits into from
Jun 13, 2018

Conversation

valdrinkoshi
Copy link
Collaborator

@valdrinkoshi valdrinkoshi commented Jun 13, 2018

Fixes #88 by providing scrollToIndex() method, documentation and demo. This works both with all layouts we ship.

@domenic feel free to update the README.md as you see fit.

We invalidate the _scrollToIndex only when receiving scroll position changes (in other words, when user scrolls).

This implementation differs from #83 in the following: we don't compute the scroll anchor when user scrolls, nor allow customizing anchoring via itemAnchor and viewportAnchor.

README.md Outdated
@@ -97,6 +97,17 @@ This re-renders all of the currently-displayed elements, updating them from thei

This generally needs to be called any time the data to be displayed changes. This includes additions, removals, and modifications to the data. See our [examples below](#data-manipulation-using-itemschanged) for more information.

### `scrollToIndex(index: number, position: string = "start")` method
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

maybe rename position to viewportPosition? @domenic wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

It feels a bit like the align properties for flexbox. The position here also depends on the scroll direction.

Maybe you want to change the names of the values instead? position: "viewport-start" or position: "scroll-direction-start"

Copy link
Contributor

@kenchris kenchris Jun 13, 2018

Choose a reason for hiding this comment

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

I see that scrollIntoView uses "start", "center", "end", or "nearest" so let's keep that

alignToTop: If true, the top of the element will be aligned to the top of the visible area of the scrollable ancestor. Corresponds to scrollIntoViewOptions: {block: "start", inline: "nearest"}. This is the default value.

Here the platform is using block and inline which is more a what than position. It seems a bit more future proof to create a scrollToIndexOptions dictionary

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 on using an options dictionary. I didn't realize that there was also a "behavior" member (hidden in https://drafts.csswg.org/cssom-view/#dictdef-scrolloptions) which we may want to add in the future.

That still leaves the question of what to call it. I think "position" is pretty good though. "Scroll to index 5 at the end position" sounds OK when said out loud.

(Working on updating this now.)

DESIGN.md Outdated
layout.scrollToIndex(19, 'end');

// Scroll to the 100th item, position it at the end of the viewport
// if we are scrolled above it already, otherwisef position it to the start.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/overwisef/overwise

DESIGN.md Outdated

// Scroll to the 100th item, position it at the end of the viewport
// if we are scrolled above it already, otherwisef position it to the start.
layout.scrollToIndex(99, 'nearest');
Copy link
Contributor

@kenchris kenchris Jun 13, 2018

Choose a reason for hiding this comment

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

Are these supposed to scroll abruptly - or do some smooth scrolling, like scrollIntoView on the web platform can do?

https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

abruptly for now, i'm not sure how to implement smooth scrolling...probably we should move this method into VirtualScroller, which could check if it has the dom for the desired index, and call the native scrollIntoView..!

.then((resp) => resp.json())
.then((contacts) => virtualScroller.itemSource = contacts);

window.scrollToIndex = function(index, position) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason why you mix arrow functions and non arrow functions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

not really, i can change this to arrow function 👌

Copy link
Collaborator

@domenic domenic left a comment

Choose a reason for hiding this comment

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

Made some tweaks. Overall looks great, although I know you had more work you might want to do; happy to re-review later.

I added a TODO about how we should probably handle out-of-range indices ourselves; currently it's handled in the demo. Based on how the other DOM scrolling methods work, it seems like it should clamp to 0/max number of items.

@valdrinkoshi
Copy link
Collaborator Author

I'll merge this as is, we can add "behavior" implementation in a separate PR

@valdrinkoshi valdrinkoshi merged commit b1e03b5 into master Jun 13, 2018
@valdrinkoshi valdrinkoshi deleted the fix-88-scroll-to-index branch June 13, 2018 23:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants