elk/plugins/remember-scroll-position.client.ts
2023-01-07 16:27:09 +01:00

24 lines
757 B
TypeScript

import type { Status } from 'masto'
import { STORAGE_KEY_LAST_SCROLL_POSITION } from '~/constants'
export default defineNuxtPlugin(() => {
const lastStatus = useSessionStorage<Record<string, string>>(STORAGE_KEY_LAST_SCROLL_POSITION, {})
return {
provide: {
restoreScrollPosition: () => {
const statusId = lastStatus.value[useRoute().fullPath]
if (statusId) {
const el = document.getElementById(`status-${statusId}`)
if (el)
nextTick().then(() => el?.scrollIntoView())
else
delete lastStatus.value[useRoute().fullPath]
}
},
rememberScrollPosition: (status: Status) => {
lastStatus.value[useRoute().fullPath] = status.id
},
},
}
})