@akanjs/next 0.9.3 → 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -522,27 +522,27 @@ const useCsrValues = (rootRouteGuide, pathRoutes) => {
522
522
  });
523
523
  const getRouter = (0, import_react2.useCallback)(() => {
524
524
  const router3 = {
525
- push: (href) => {
525
+ push: (href, { scrollToTop } = {}) => {
526
526
  const location2 = getCurrentLocation();
527
527
  const scrollTop = pageContentRef.current?.scrollTop ?? 0;
528
- setHistoryForward({ type: "push", location: getLocation(href), scrollTop });
528
+ setHistoryForward({ type: "push", location: getLocation(href), scrollTop, scrollToTop });
529
529
  setLocationState({ location: getCurrentLocation(), prevLocation: location2 });
530
530
  window.history.pushState({}, "", href);
531
531
  },
532
- replace: (href) => {
532
+ replace: (href, { scrollToTop } = {}) => {
533
533
  const scrollTop = pageContentRef.current?.scrollTop ?? 0;
534
- setHistoryForward({ type: "replace", location: getLocation(href), scrollTop });
534
+ setHistoryForward({ type: "replace", location: getLocation(href), scrollTop, scrollToTop });
535
535
  setLocationState({ location: getCurrentLocation(), prevLocation });
536
536
  window.history.replaceState({}, "", href);
537
537
  },
538
538
  refresh: () => {
539
539
  window.location.reload();
540
540
  },
541
- back: async () => {
541
+ back: async ({ scrollToTop } = {}) => {
542
542
  const location2 = getCurrentLocation();
543
543
  await onBack.current[location2.pathRoute.pageState.transition]?.();
544
544
  const scrollTop = pageContentRef.current?.scrollTop ?? 0;
545
- setHistoryBack({ type: "back", location: location2, scrollTop });
545
+ setHistoryBack({ type: "back", location: location2, scrollTop, scrollToTop });
546
546
  setLocationState({ location: getCurrentLocation(), prevLocation: getPrevLocation() });
547
547
  window.history.back();
548
548
  }
package/cjs/useHistory.js CHANGED
@@ -31,9 +31,13 @@ const useHistory = (locations = []) => {
31
31
  cachedLocationMap: /* @__PURE__ */ new Map(),
32
32
  idx: 0
33
33
  });
34
- const setHistoryForward = (0, import_react.useCallback)(({ type, location, scrollTop = 0 }) => {
34
+ const setHistoryForward = (0, import_react.useCallback)(({ type, location, scrollTop = 0, scrollToTop = false }) => {
35
+ const currentLocation = history.current.locations[history.current.idx];
36
+ if (currentLocation)
37
+ history.current.scrollMap.set(currentLocation.pathname, scrollTop);
38
+ if (scrollToTop)
39
+ history.current.scrollMap.set(location.pathname, 0);
35
40
  history.current.type = "forward";
36
- history.current.scrollMap.set(location.pathname, scrollTop);
37
41
  history.current.idxMap.set(location.pathname, history.current.idx);
38
42
  if (type === "push")
39
43
  history.current.locations = [...history.current.locations.slice(0, history.current.idx + 1), location];
@@ -44,7 +48,10 @@ const useHistory = (locations = []) => {
44
48
  if (type === "push" || type === "popForward")
45
49
  history.current.idx++;
46
50
  }, []);
47
- const setHistoryBack = (0, import_react.useCallback)(({ location, scrollTop = 0 }) => {
51
+ const setHistoryBack = (0, import_react.useCallback)(({ location, scrollTop = 0, scrollToTop = false }) => {
52
+ const prevLocation = history.current.locations[history.current.idx - 1];
53
+ if (prevLocation && scrollToTop)
54
+ history.current.scrollMap.set(prevLocation.pathname, 0);
48
55
  history.current.type = "back";
49
56
  history.current.scrollMap.set(location.pathname, scrollTop);
50
57
  history.current.idxMap.set(location.pathname, history.current.idx);
@@ -504,27 +504,27 @@ const useCsrValues = (rootRouteGuide, pathRoutes) => {
504
504
  });
505
505
  const getRouter = useCallback(() => {
506
506
  const router3 = {
507
- push: (href) => {
507
+ push: (href, { scrollToTop } = {}) => {
508
508
  const location2 = getCurrentLocation();
509
509
  const scrollTop = pageContentRef.current?.scrollTop ?? 0;
510
- setHistoryForward({ type: "push", location: getLocation(href), scrollTop });
510
+ setHistoryForward({ type: "push", location: getLocation(href), scrollTop, scrollToTop });
511
511
  setLocationState({ location: getCurrentLocation(), prevLocation: location2 });
512
512
  window.history.pushState({}, "", href);
513
513
  },
514
- replace: (href) => {
514
+ replace: (href, { scrollToTop } = {}) => {
515
515
  const scrollTop = pageContentRef.current?.scrollTop ?? 0;
516
- setHistoryForward({ type: "replace", location: getLocation(href), scrollTop });
516
+ setHistoryForward({ type: "replace", location: getLocation(href), scrollTop, scrollToTop });
517
517
  setLocationState({ location: getCurrentLocation(), prevLocation });
518
518
  window.history.replaceState({}, "", href);
519
519
  },
520
520
  refresh: () => {
521
521
  window.location.reload();
522
522
  },
523
- back: async () => {
523
+ back: async ({ scrollToTop } = {}) => {
524
524
  const location2 = getCurrentLocation();
525
525
  await onBack.current[location2.pathRoute.pageState.transition]?.();
526
526
  const scrollTop = pageContentRef.current?.scrollTop ?? 0;
527
- setHistoryBack({ type: "back", location: location2, scrollTop });
527
+ setHistoryBack({ type: "back", location: location2, scrollTop, scrollToTop });
528
528
  setLocationState({ location: getCurrentLocation(), prevLocation: getPrevLocation() });
529
529
  window.history.back();
530
530
  }
package/esm/useHistory.js CHANGED
@@ -9,9 +9,13 @@ const useHistory = (locations = []) => {
9
9
  cachedLocationMap: /* @__PURE__ */ new Map(),
10
10
  idx: 0
11
11
  });
12
- const setHistoryForward = useCallback(({ type, location, scrollTop = 0 }) => {
12
+ const setHistoryForward = useCallback(({ type, location, scrollTop = 0, scrollToTop = false }) => {
13
+ const currentLocation = history.current.locations[history.current.idx];
14
+ if (currentLocation)
15
+ history.current.scrollMap.set(currentLocation.pathname, scrollTop);
16
+ if (scrollToTop)
17
+ history.current.scrollMap.set(location.pathname, 0);
13
18
  history.current.type = "forward";
14
- history.current.scrollMap.set(location.pathname, scrollTop);
15
19
  history.current.idxMap.set(location.pathname, history.current.idx);
16
20
  if (type === "push")
17
21
  history.current.locations = [...history.current.locations.slice(0, history.current.idx + 1), location];
@@ -22,7 +26,10 @@ const useHistory = (locations = []) => {
22
26
  if (type === "push" || type === "popForward")
23
27
  history.current.idx++;
24
28
  }, []);
25
- const setHistoryBack = useCallback(({ location, scrollTop = 0 }) => {
29
+ const setHistoryBack = useCallback(({ location, scrollTop = 0, scrollToTop = false }) => {
30
+ const prevLocation = history.current.locations[history.current.idx - 1];
31
+ if (prevLocation && scrollToTop)
32
+ history.current.scrollMap.set(prevLocation.pathname, 0);
26
33
  history.current.type = "back";
27
34
  history.current.scrollMap.set(location.pathname, scrollTop);
28
35
  history.current.idxMap.set(location.pathname, history.current.idx);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akanjs/next",
3
- "version": "0.9.3",
3
+ "version": "0.9.5",
4
4
  "sourceType": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
package/useHistory.d.ts CHANGED
@@ -3,16 +3,18 @@ interface setForwardOptions {
3
3
  type: "push" | "replace" | "popForward";
4
4
  location: Location;
5
5
  scrollTop?: number;
6
+ scrollToTop?: boolean;
6
7
  }
7
8
  interface setBackOptions {
8
9
  type: "back" | "popBack";
9
10
  location: Location;
10
11
  scrollTop?: number;
12
+ scrollToTop?: boolean;
11
13
  }
12
14
  export declare const useHistory: (locations?: Location[]) => {
13
15
  history: import("react").MutableRefObject<History>;
14
- setHistoryForward: ({ type, location, scrollTop }: setForwardOptions) => void;
15
- setHistoryBack: ({ location, scrollTop }: setBackOptions) => void;
16
+ setHistoryForward: ({ type, location, scrollTop, scrollToTop }: setForwardOptions) => void;
17
+ setHistoryBack: ({ location, scrollTop, scrollToTop }: setBackOptions) => void;
16
18
  getCurrentLocation: () => Location;
17
19
  getPrevLocation: () => Location | null;
18
20
  getScrollTop: (pathname?: string) => number;