fix: save scroll position on mobile while switching pages

This commit is contained in:
JovannMC 2025-02-20 21:01:44 +03:00
parent 90ca53c2a5
commit d013fb0084
No known key found for this signature in database
2 changed files with 24 additions and 4 deletions

View File

@ -2,7 +2,11 @@
import { page } from "$app/state";
import { duration } from "$lib/animation";
import VertVBig from "$lib/assets/vert-bg.svg?component";
import { files, gradientColor, showGradient } from "$lib/store/index.svelte";
import {
files,
gradientColor,
showGradient,
} from "$lib/store/index.svelte";
import { quintOut } from "svelte/easing";
import { fade } from "$lib/animation";
</script>

View File

@ -1,6 +1,6 @@
<script lang="ts">
import { onMount } from "svelte";
import { goto } from "$app/navigation";
import { goto, beforeNavigate, afterNavigate } from "$app/navigation";
import { PUB_PLAUSIBLE_URL, PUB_HOSTNAME } from "$env/static/public";
import { VERT_NAME } from "$lib/consts";
@ -20,6 +20,21 @@
let { children } = $props();
let enablePlausible = $state(false);
let scrollPositions = new Map<string, number>();
beforeNavigate((nav) => {
if (!nav.from || !$isMobile) return;
scrollPositions.set(nav.from.url.pathname, window.scrollY);
});
afterNavigate((nav) => {
if (!$isMobile) return;
const scrollY = nav.to
? scrollPositions.get(nav.to.url.pathname) || 0
: 0;
window.scrollTo(0, scrollY);
});
const dropFiles = (e: DragEvent) => {
e.preventDefault();
dropping.set(false);
@ -49,7 +64,8 @@
$effect(() => {
// Enable plausible if enabled
enablePlausible = !!PUB_PLAUSIBLE_URL && Settings.instance.settings.plausible;
enablePlausible =
!!PUB_PLAUSIBLE_URL && Settings.instance.settings.plausible;
});
</script>