Repository navigation
Conversation
🦋 Changeset detectedLatest commit: 5718088 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
✔️ Deploy Preview for kit-demo canceled. 🔨 Explore the source changes: 5718088 🔍 Inspect the deploy log: https://app.netlify.com/sites/kit-demo/deploys/61fc6123eca3a40007d53251 |
scrollTop reset
|
Thank you — I don't yet 100% understand what this is doing 😅 but will try and wrap my head round it using the provided test. In the meantime though it's probably a good idea to hold off until @nikosgpet is able to provide a repro for #2733 (comment) and we can verify that it's fixed |
|
There were two use cases in which the page was not scrolled to the top using client side navigation.
There still might be other use cases in which the scrolling does not work ... however we cannot fix what we don't know 😄 |
|
Ok, I ran the new tests without the fixes and understand a bit better what is happening now: |
| const target_child_element = target_child.firstChild?.parentElement; | ||
| if (target_child_element && target_child_element.scrollTop > 0) { | ||
| target_child_element.scrollTo(0, 0); | ||
| break; |
There was a problem hiding this comment.
I'm not sure it's always safe to break here. What if you had one overflowed div inside another?
There was a problem hiding this comment.
I think I tested this exact usecase and it still worked. Elements that are siblings to the first overflown element don't matter as they cannot overflow more than the first overflown element. Overflowing elements inside the first overflown element also don't matter as they would only scroll inside their parent.
However it could be argued that after a page navigation every scroll position in the DOM should be at 0 because that's the "normal" behavior. But this could be very inefficient if there are many elements in the DOM. That's why I thought breaking as early as possible would be the best solution.
| // In some cases it is needed scroll first root element of target that | ||
| // is not scrolled to the top |
There was a problem hiding this comment.
| // In some cases it is needed scroll first root element of target that | |
| // is not scrolled to the top | |
| // If there is an element with the overflow CSS property it may have a scrollbar | |
| // We need to ensure the scroll is reset on these elements |
| }); | ||
|
|
||
| test.only('scroll to top works with overflowing root container', async ({ page, clicknav }) => { | ||
| await page.goto('/scroll-top/overflow'); |
There was a problem hiding this comment.
I feel like scroll-top isn't super descriptive about what this is testing because we have other scroll tests that test scrolling to the top. How about something like scroll-overflowed?
| // In some cases it is needed scroll first root element of target that | ||
| // is not scrolled to the top | ||
| for (const target_child of this.target.childNodes) { | ||
| const target_child_element = target_child.firstChild?.parentElement; |
There was a problem hiding this comment.
why do you get the parent of the child? wouldn't that just give you back the original element?
There was a problem hiding this comment.
target_child is the node and not the element itself. As element nodes must have at least one child in order to be an element-node (the node could also be just text) I implicitly check this by selecting the parent of the child. Also the node object does not have the same properties that an element object has. I'm happy to change the implementation if there is a better way 😄
There was a problem hiding this comment.
you can use this.target.children, which only includes elements
|
Thanks for your patience on this one while we fixed the other scroll handling issues. Handling the |
|
Ok, I've finally had a chance to try and get my head round this PR. Unfortunately I just don't think it can work. Because it restricts the scroll reset to direct children of the target element, and breaks when we first encounter a scrolled element, it fails when there are multiple scrolled elements or if there's an additional wrapper element anywhere. See this demo for an example — try scrolling both columns to the bottom, then clicking the button. There's another tricky problem we haven't yet solved: restoring the scroll positions of non-root elements on I'm not sure what a working solution does look like; it might be worth seeing how/if other frameworks tackle this case. |
|
@Rich-Harris I know that Next.js and Remix don't handle anything other than body/html, and I just checked Chrome doesn't handle it as well, so I don't think that this is a issue framework needs solving. Edit: I'm talkink about restoring non-root elements on popstate. |
|
It's probably a good idea to remove the 'scroll first overflow element of target'-thing and just scroll the body. We could then add a FAQ that explains what behaviour prevents scrolling and what the user can do to solve this. Thoughts? |
Is that different to what Kit currently does? I'm not 100% sure I understand
Right now I'm not sure there is much that a user could do. It would be good to brainstorm some possible solutions |
|
I just checked and I was right, Next, Remix and Nuxt all don't handle scroll to 0 for non-root elements on navigation, But one way we can at least partially solve it is to do something similar to this PR but remove break, and use top-level-components(like Layout) to limit the scope of the scroll elements, because statistically the most elements are going to be in the lower-level Slot components than top-level ones. The second option is to write it in FAQ/docs because user knows what elements are scrollable and can account(set scrollTop of those elements) for those elements in |
|
I also don't think it makes sense to tackle this automatically. If I were to intentionally add an overflowing element, there would be a reason behind it and it might be incompatible with some automatic scrolling. However, it might be worth considering whether we could give |
|
I think providing access to history state is the most promising solution, yeah. If we do a good job of designing it, we could address #3236 (comment) at the same time. I'm thinking along these lines: <script>
import { beforeNavigate, afterNavigate } from '$app/navigation';
let sidebar;
beforeNavigate(({ state }) => {
state.sidebar_scroll = sidebar.scrollTop;
});
afterNavigate(({ type, state }) => {
if (type === 'popstate') {
sidebar.scrollTop = state.sidebar_scroll;
}
});
</script>
<aside bind:this={sidebar}>
<!-- ... -->
</aside>
<style>
aside {
overflow-y: auto;
}
</style> |
I definitely understand the appeal, but something I've learned the hard way as an OSS maintainer is that it's better for something to remain broken than to appear fixed |
I understand and agree, but I did some testing(basic stuff just to see the stats) like this(it was conducted on a page with 3405 DOM nodes, so pretty large): const domNodes = Array.from(document.querySelectorAll('html, body, body *')); // length is 3405and then run this many times: console.time();
domNodes.forEach(node => node.scrollTo(0, 0));
console.timeEnd();and the times came at 12ms most of the times, at most 15ms |
|
It might be very performant, but it's still implementation specific. By the way, if we were to scroll them all to the top, I'd also expect us to save and restore their positions on history navigation—but how would we correctly identify the elements? Of note, even Chromium browsers don't save and scroll all overflown elements on history navigation, and they are operating on a level where uniquely identifying elements isn't an issue.. With that in mind, I propose we:
[1] 🐐 |
Yeah, I wrote that here #3629 (comment) |
Fixes #2733 by also scrolling the first child-element of #target that isn't scrolled to the top.
Currently if something like this
is inside of a __layout component the page is not scrolled all the way to the top when using client-side navigation. One example where this happens is when a DaisyUI Drawer Component is used.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpx changesetand following the prompts. All changesets should bepatchuntil SvelteKit 1.0