import { ref, watch, type Ref } from 'vue' type RouteLike = { fullPath?: string; name?: string } & Record /** * Simple per-route scroll position helper. * * - Stores `window.scrollY` for the route we leave * - Restores it when we navigate back to the same route * * Intended to be used with a reactive current route ref: * useRouteScrollPositions(currentRouteRef) */ export function useRouteScrollPositions(currentRoute: Ref) { const scrollPositions = ref>({}) watch( currentRoute, (to, from) => { // Spara position för route vi lämnar const fromKey = from?.fullPath ?? (typeof from?.name === 'string' ? (from.name as string) : undefined) if (fromKey) { scrollPositions.value[fromKey] = window.scrollY ?? 0 } // Återställ position för route vi går till, om sparad const toKey = to?.fullPath ?? (typeof to?.name === 'string' ? (to.name as string) : undefined) if (toKey && scrollPositions.value[toKey] !== undefined) { const y = scrollPositions.value[toKey] setTimeout(() => { window.scrollTo({ top: y, behavior: 'auto' }) }, 1000) } }, { flush: 'post' }, ) return { scrollPositions } }