flekschas/svelte-simple-modal

Page scrolling to the top on load, even if an anchor is specified

mquandalle opened this issue · 5 comments

When the Modal component is used, the page is scrolled to the top on first load, or during in-app navigation.
This is unwanted in case we link to an anchor.

Reproduction : https://repro-svelte-simple-modal-anchor-issue-maximequandalle.vercel.app/#anchor
Source : https://github.com/mquandalle/repro-svelte-simple-modal-anchor-issue/blob/master/src/routes/index.svelte

It seems that this bug was introduced in #31.

Sorry for the glitch and thanks for reporting the issue. Can you test it with the following version of the Modal?

https://svelte.dev/repl/2eb021eff87d45019549565136b0e6ed?version=3.44.1

In my local tests with your repo this fixes the issue.

Yes it fixes the issue.

The problem was indeed the reactivity of isMounted variable in the following block, causing close() to be called on mount:

let isMounted = false;
$: {
if (isMounted) {
if (isFunction(show)) {
open(show);
} else {
close();
}
}
}
svelte.onDestroy(() => {
if (isMounted) close();
});
svelte.onMount(() => {
isMounted = true;
});

New version with a fix is out

Thank you for the quick fix!