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

[cssom-view] Consider making scroll methods return promises #1562

Open
markcellus opened this issue Jun 25, 2017 · 5 comments
Open

[cssom-view] Consider making scroll methods return promises #1562

markcellus opened this issue Jun 25, 2017 · 5 comments

Comments

@markcellus
Copy link

markcellus commented Jun 25, 2017

Smooth scrolling has been introduced into the spec where the scroll happens over a period of time, which is great. However, I think it would be even more useful if scroll method returned a promise so the developer could wait until the scroll finishes before moving on to next set of tasks for things like triggering analytics data after scrolling, unloading resources when they've been scrolled out of view, parallax scrolling, etc.

Obviously scroll()ing an element that has scroll-behavior option set to auto makes scroll instant and is therefore not inherently asynchronous. But I think, to keep the API consistent, the scroll method should return a promise every time it is used.

So if you had the following HTML...

<div id="scroll-container" style="scroll-behavior: smooth;">
   <!-- other content here --> 
</div>

you can do this...

let element = document.getElementById('scroll-container');
element.scroll(0, 400).then(() => {
   // ...scroll has finished
});
@markcellus
Copy link
Author

FWIW, here is a link to the relevant discussion where others have also +1'd this feature.

@dartess
Copy link

dartess commented Apr 25, 2018

in 2018 it is still very much needed, only now we will use it this way:

let element = document.getElementById('scroll-container');
await element.scroll(0, 400);
// ...scroll has finished

use case: set focus after scroll

@tabatkins
Copy link
Member

Since all of the scrolling methods currently return void, they're very compatible with being upgraded into returning promises. I think this would be a fine feature, we just need implementor interest. I'll Agenda+ it to gauge support.

@css-meeting-bot
Copy link
Member

The Working Group just discussed Consider making scroll methods return promises, and agreed to the following:

  • RESOLVED: add returning a promise to all scroll functions
The full IRC log of that discussion <dael> Topic: Consider making scroll methods return promises
<dael> github: https://github.com//issues/1562
<dael> TabAtkins: Right now all scrolling methods are void, return undefined. When they were instant that wwas fine. Now that there's smoothe scrolling it may be worthwhile to have something tell you when it's finished. Someone suggested that scroll methods return promises.
<dael> TabAtkins: I don't think they would ever fail.
<dael> TabAtkins: Upgrading void methods into promise returning is a standard way to do this and should be compat. As long as there's no impl I'd like to add.
<dael> Rossen_: Defined behavior for overlapping scroll timelines?
<dael> TabAtkins: Don't know, use case?
<dael> Rossen_: You want to impl your own overscroll behavior for a spring effect. When your scroll is complete you want to scroll down and back. I can see someone doing that. So I start a scroll for something fairly long in time and then I'll wait for the promise to do the little overscroll. But another scroll in the opposite dir happens.
<dael> TabAtkins: If a scroll is started and interrupted it's done and you fulfill promise.
<dael> Rossen_: So inturupt or complete fulfils.
<dael> TabAtkins: We only reject promises on errors and there's no sense the scroll throws an error
<dael> dbaron: You might want data on if inturpt.
<dael> TabAtkins: Agree. Should fulfill, but argument should give data
<dael> majidvp: Can you not read scroll offset with actual offset and it means inturrupt?
<dael> TabAtkins: No because you're interrupted mid-way. Using that as a method an interrupted will be forever pending unless you hit it by luck.
<dael> dbaron: I think it's an alternative to data, not tot he promise.
<dael> dbaron: That said, I think there are a bunch of scrolling operations where you don't know destination offset. YOu can cause scroll by page and you don't know pixel.
<dael> TabAtkins: And scroll into view is at least difficutl to cal
<dael> Rossen_: I would want to have something cleaner and richer for hints. When you intersect this with scroll snapping and you stop for a different reason...if you scroll by and need to scroll up more because a scroll snap has a mandatory scroll you'll overscroll based on target.
<dael> Rossen_: Meta point, I don't think anyone objects use case of promise pattern. I think the addition to this is adding some data that describes how this was fulfilled.
<dael> TabAtkins: I can see fulfill with an object that's an enum of completed or interrupted or something like that.
<dael> AmeliaBR: I suggest looking at web animations spec. Smoothe scroll is a type of animation and web animation uses lots of promises and chaining things like web animation after a complete smooth scroll. Compat here is useful.
<dael> TabAtkins: Agree. I didn't know about that but I will take a look at it.
<dael> majidvp: Another case is a scroll-into-view where we scroll multi scrollers and we need details on when we fulfill. I imagine after all scrolls are complete.
<dael> TabAtkins: Yes, if you have 2 nested scrollers inner is a bit and outer is a lot so inner completes fast. Makes sense. Good detail.
<dael> Rossen_: Is there a reason we wouldn't want this as an event that you can subscribe instead of adding promises to all scroll functions?
<AmeliaBR> s/promises and chaining/promises and I could see a use case for chaining/
<dbaron> https://developer.mozilla.org/en-US/docs/Web/Events/scroll
<dael> TabAtkins: Possibly. But if you only care about a given scroll you have to subscribe and then unsubscribe. Also there may be another scroll and you might get the wrong one. Event is useful, but doesn't do the use case of I want to know when this scroll is complete.
<dael> Rossen_: Okay. Makes sense.
<dael> Rossen_: Sounds like a feature that merits addition of promise. Likely details to work out.
<dael> Rossen_: Additional comments or objections to add returning a promise from all scroll functions?
<dael> RESOLVED: add returning a promise to all scroll functions

@Nantris
Copy link

Nantris commented Nov 24, 2019

Is there an ETA for this being spec?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants