-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
fix: add scrollable area check and scrollable area attribute #8723
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is tempting, although I'm wondering how much code people really save by doing that. I'm also wondering if they would always prefer the explicitly set scrollable area to be used for scrolling. Also, how often will be html element be non-scrollable while the body will be? Feels like both won't have that if someone does that.
I agree. The thread showed that many people were able to solve it with their own workarounds. The common patterns I've gathered from the issue thread are:
|
I think warning + documenting is the most important thing - it is very non-obvious that this will happen, mainly because as a user you're not thinking about how the magic happens.
But I really like the idea of providing a proper solution - as a mortal Sveltekit user, I don't feel like I understand the workaround we have done enough to know that I've done it right. Like, I added the check for when Please let me know if I can help testing this - and if so, how. 🙏 |
In this case, it would be preferable to always require the explicit scrolling area set. I hesitated earlier about this when I recalled a conversation about the issues of having too many framework specific attributes.
I had the idea to add tests for the new attribute following the procedure:
However, I'm waiting for more feedback from everyone before continuing this PR (and probably some help with refining the code). |
I wonder if #8710 is an adequate solution to this, if properly documented? It's a tiny bit more work than adding an attribute, but it uses existing API surface area and teaches people a technique that they can then generalise to other problems. |
It would also need to address the issue of correctly scrolling to the top when navigating to a new page. My current understanding of snapshots is that it only restores scroll when navigating backwards / forwards to visited pages. |
It also doesn't stop the mistake being made, I don't think? We only discovered it late in a cycle when testing on real mobile devices, so the buggy scrolling went into production. So we need something like this As an aside, I'm not sure I've seen an explanation here of what the relevant difference is in mobile browsers that causes this behaviour to manifest (or - rather - not manifest reliably on desktop). 🙏 |
Ah, that's true. You need a combination of /** @type {HTMLElement} */
let container;
afterNavigate(() => {
container.scrollTo(0, 0);
});
/** @type {import('./$types').Snapshot<number>} */
export const snapshot = {
capture: () => container.scrollTop,
restore: (y) => container.scrollTo(0, y)
}; (Snapshots are restored immediately after An attribute would definitely be easier, though I do worry slightly about adding too many more moving parts to
IIUC the bugginess is something separate, related to |
I think documenting that recipe would be a nice solution for now. |
@Rich-Harris said:
Unfortunately I'm seeing afterNavigate being called after snapshot restore, so this doesn't work. I can just workaround with a setTimeout/requestAnimationFrame on the restore... but that's a workaround for a workaround so I'm starting to feel a bit icky. |
I was very confused that this was not in the sveltekit docs. |
fixes #2733
Adds a check to see if
html
orbody
is scrollable.Otherwise, it looks for an element with the attribute
data-sveltekit-main-scrollable
to determine which element to scroll during navigation.Looking for feedback as this solution still feels quite rough. @dummdidumm
TODO
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.