Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit c691507

Browse files
author
Je
committed
feat: add scrollTo property for popstate event
1 parent 42c9441 commit c691507

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

aleph.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference lib="dom" />
12
import React, { ComponentType, useCallback, useEffect, useRef, useState } from 'https://esm.sh/react'
23
import { DataContext, RouterContext } from './context.ts'
34
import { E404Page, E501App, E501Page, ErrorBoundary } from './error.ts'
@@ -33,7 +34,7 @@ export function ALEPH({ initial }: {
3334
const { url, pageProps } = initial
3435
return { pageProps, url }
3536
})
36-
const onpopstate = useCallback(async () => {
37+
const onpopstate = useCallback(async (e: any) => {
3738
const { routing } = ref.current
3839
const { baseUrl } = routing
3940
const [url, pageModuleTree] = routing.createRouter()
@@ -70,6 +71,9 @@ export function ALEPH({ initial }: {
7071
return c
7172
}, pageProps)
7273
setPage({ url, pageProps })
74+
if (util.isInt(e.scrollTo)) {
75+
window.scrollTo(e.scrollTo, 0)
76+
}
7377
} else {
7478
setPage({ url, pageProps: null })
7579
}
@@ -193,7 +197,7 @@ export async function redirect(url: string, replace?: boolean) {
193197
} else {
194198
history.pushState(null, '', url)
195199
}
196-
events.emit('popstate', { type: 'popstate' })
200+
events.emit('popstate', { type: 'popstate', scrollTo: 0 })
197201
}
198202

199203
export function getModuleImportUrl(baseUrl: string, mod: Module, forceFetch = false) {

0 commit comments

Comments
 (0)