fix: add scrollable area check and scrollable area attribute#8723
fix: add scrollable area check and scrollable area attribute#8723teemingc wants to merge 3 commits intosveltejs:masterfrom teemingc:fix-scrolling-to-top
Conversation
|
dummdidumm
left a comment
There was a problem hiding this comment.
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. |
3f22176 remove story:build from workflows (Graeme Byrne) 76d7665 fix readingTime error (Graeme Byrne) eb96c60 fix linting errors (Graeme Byrne) 7b33824 migrate to svelte 5 and update dependencies (Graeme Byrne) Pull request description: * Migrate from Svelte 4 to Svelte 5 as mentioned in this [issue](#106). * Update dependencies in `package.json` such as Vite which were preventing the successful migration to Svelte 5. * Upgrade ESLint to v9 as v8 has recently reached end of life and is no longer maintained, as mentioned [here](https://eslint.org/docs/latest/use/migrate-to-9.0.0). * Upgrade Sass as current syntax is deprecated as mentioned [here](https://sass-lang.com/documentation/breaking-changes/legacy-js-api/) * Remove `/contributors` route as it is redundant since we now display GitHub contributors on home page and on Community page. Existing individual contributor pages have been retained and are accessible by clicking on the author's name in the blog post. * Remove Histoire as it doesn't support Vite at the minute. There is currently a [PR](histoire-dev/histoire#770) open to fix this, so Histoire can be added back later if needed. * When navigating to a new page, the site wouldn't scroll up to the top as would be expected. As found [here](sveltejs/kit#8723), this seems to be a Svelte issue. Solve this by adding the below code to the script block of routes/+layout.svelte and bind to page content: ``` import { onNavigate } from '$app/navigation'; let contentDiv: HTMLElement | null = null; onNavigate((navigation) => { return new Promise((resolve) => { const transition = document.startViewTransition(async () => { if (contentDiv) { // Fix scroll contentDiv.scrollTop = 0; } resolve(); await navigation.complete; }); }); }); ... <div id="app-container"> <Header /> <div bind:this={contentDiv} class="content"> {@render children?.()} <Footer /> </div> </div> ``` ACKs for top commit: josecelano: ACK 3f22176 Tree-SHA512: 09e1042539a441e581eb8cfd759454ac675971acfbf2a0c3fa9eb74693270add836ac2e4864077e09cf29d4cb3be4d232c0dab607685e3f813e2548504ba9cef
fixes #2733
Adds a check to see if
htmlorbodyis scrollable.Otherwise, it looks for an element with the attribute
data-sveltekit-main-scrollableto 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 testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.