-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRouter.tsx
More file actions
37 lines (35 loc) · 1.14 KB
/
Router.tsx
File metadata and controls
37 lines (35 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"use client";
import { Router as FunStackRotuter, type RouterProps } from "@funstack/router";
import { useEffect } from "react";
export const Router: React.FC<RouterProps> = (props) => {
// Auto scroll to top - this should be handled by the browser per spec,
// but currently Chrome and Safari do not follow the spec.
useEffect(() => {
const navigation = window.navigation;
if (!navigation) {
return;
}
const controller = new AbortController();
navigation.addEventListener(
"navigatesuccess",
() => {
const transition = navigation.transition;
if (
transition?.navigationType === "push" ||
transition?.navigationType === "replace"
) {
// Safari is known to ignore scrolling immediately after a push/replace navigation, so we wait a bit
// Also, Safari doesn't handle scrolling to 0, so we use the -1 trick
setTimeout(() => {
window.scrollTo(0, -1);
}, 10);
}
},
{ signal: controller.signal },
);
return () => {
controller.abort();
};
}, []);
return <FunStackRotuter {...props} />;
};