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

Commit 1c94a03

Browse files
author
Je
committed
feat: add suuport hash anchtor scroll behavior
1 parent 102262c commit 1c94a03

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

aleph.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,13 @@ export function ALEPH({ initial }: {
5353
await import(getModuleImportUrl(baseUrl, { id: dep.url.replace(reModuleExt, '.js'), hash: dep.hash }, e.forceRefetch))
5454
}
5555
if (mod.asyncDeps.filter(({ url }) => url.startsWith('#useDeno.')).length > 0) {
56-
await import(`/_aleph/data${[url.pathname, url.query.toString()].filter(Boolean).join('@')}/data.js` + (e.forceRefetch ? `?t=${Date.now()}` : '')).then(({ default: data }) => {
57-
if (util.isPlainObject(data)) {
58-
for (const key in data) {
59-
const useDenoUrl = `useDeno://${url.pathname}?${url.query.toString()}#${key}`
60-
Object.assign(window, { [useDenoUrl]: data[key] })
61-
}
56+
const { default: data } = await import(`/_aleph/data${[url.pathname, url.query.toString()].filter(Boolean).join('@')}/data.js` + (e.forceRefetch ? `?t=${Date.now()}` : ''))
57+
if (util.isPlainObject(data)) {
58+
for (const key in data) {
59+
const useDenoUrl = `useDeno://${url.pathname}?${url.query.toString()}#${key}`
60+
Object.assign(window, { [useDenoUrl]: data[key] })
6261
}
63-
})
62+
}
6463
}
6564
}
6665
const pc = ctree.find(pc => pc.id === mod.id)
@@ -70,8 +69,8 @@ export function ALEPH({ initial }: {
7069
})
7170
await Promise.all(imports)
7271
setRoute({ ...createPageProps(ctree), url })
73-
if (util.isInt(e.scrollTo)) {
74-
(window as any).scrollTo(e.scrollTo, 0)
72+
if (e.resetScroll) {
73+
(window as any).scrollTo(0, 0)
7574
}
7675
} else {
7776
setRoute({ Page: null, pageProps: {}, url })
@@ -150,14 +149,13 @@ export function ALEPH({ initial }: {
150149
await import(getModuleImportUrl(baseUrl, { id: dep.url.replace(reModuleExt, '.js'), hash: dep.hash }))
151150
}
152151
if (mod.asyncDeps.filter(({ url }) => url.startsWith('#useDeno.')).length > 0) {
153-
await import(`/_aleph/data${[url.pathname, url.query.toString()].filter(Boolean).join('@')}/data.js`).then(({ default: data }) => {
154-
if (util.isPlainObject(data)) {
155-
for (const key in data) {
156-
const useDenoUrl = `useDeno://${url.pathname}?${url.query.toString()}#${key}`
157-
Object.assign(window, { [useDenoUrl]: data[key] })
158-
}
152+
const { default: data } = await import(`/_aleph/data${[url.pathname, url.query.toString()].filter(Boolean).join('@')}/data.js`)
153+
if (util.isPlainObject(data)) {
154+
for (const key in data) {
155+
const useDenoUrl = `useDeno://${url.pathname}?${url.query.toString()}#${key}`
156+
Object.assign(window, { [useDenoUrl]: data[key] })
159157
}
160-
})
158+
}
161159
}
162160
}
163161
})
@@ -176,6 +174,23 @@ export function ALEPH({ initial }: {
176174
}
177175
}, [ref])
178176

177+
useEffect(() => {
178+
const win = window as any
179+
const { location, document, scrollX, scrollY, ALEPH } = win
180+
const { hashAnchorScroll } = ALEPH
181+
if (location.hash) {
182+
const anchor = document.getElementById(location.hash.slice(1))
183+
if (anchor) {
184+
const { left, top } = anchor.getBoundingClientRect()
185+
win.scroll({
186+
top: top + scrollY - (hashAnchorScroll?.offset?.top || 0),
187+
left: left + scrollX - (hashAnchorScroll?.offset?.left || 0),
188+
behavior: hashAnchorScroll?.behavior
189+
})
190+
}
191+
}
192+
}, [route])
193+
179194
return (
180195
React.createElement(
181196
ErrorBoundary,
@@ -211,7 +226,7 @@ export async function redirect(url: string, replace?: boolean) {
211226
} else {
212227
history.pushState(null, '', url)
213228
}
214-
events.emit('popstate', { type: 'popstate', scrollTo: 0 })
229+
events.emit('popstate', { type: 'popstate', resetScroll: true })
215230
}
216231

217232
export function getModuleImportUrl(baseUrl: string, mod: RouteModule, forceRefetch = false) {

0 commit comments

Comments
 (0)