page-transition 0.1.0-beta.51 → 0.1.0-beta.53

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.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # <img src="./assets/logo.svg" width="32"/>&nbsp;&nbsp;page-transition
1
+ # page-transition
2
2
  A page transition library for the Next.js App Router.
3
3
  <br/>
4
4
  [![Live Demo ↗](https://img.shields.io/badge/Live_Demo_%E2%86%97-000000?style=for-the-badge)](https://simon-schindler.github.io/page-transition-demo/)
@@ -10,7 +10,7 @@ A page transition library for the Next.js App Router.
10
10
  - Scroll restoration
11
11
 
12
12
  ## Install
13
- ![Version](https://img.shields.io/badge/v_0.1.0--beta.48-000000) ![68.3 KB](https://img.shields.io/badge/68.3_KB-1677FF)
13
+ ![Version](https://img.shields.io/badge/v_0.1.0--beta.52-000000) ![68.7 KB](https://img.shields.io/badge/68.7_KB-1677FF)
14
14
 
15
15
  `npm install page-transition@beta`
16
16
 
@@ -42,7 +42,8 @@ export default function RootLayout({ children }: LayoutProps<"/">) {
42
42
  |Prop|Type|Default|Mode|
43
43
  |---|---|---|---|
44
44
  | mode | "css" \| "manual" | "css" | Both |
45
- | header | JSX | - | CSS |
45
+ | scroll | "auto"\|"top" | "auto" | Both |
46
+ | outside | JSX | - | CSS |
46
47
  | options | *Options* | {} | CSS |
47
48
 
48
49
  *Options*
@@ -58,8 +59,13 @@ export default function RootLayout({ children }: LayoutProps<"/">) {
58
59
  **mode**
59
60
  Select the transition mode.
60
61
 
61
- **header**
62
- The component rendered outside the .page-wrapper.
62
+ **scroll**
63
+ Controls browser scroll restoration during back and forward navigation.
64
+ `'auto'` Restores the previous scroll position.
65
+ `'top'` Scrolls to the top.
66
+
67
+ **outside**
68
+ Components rendered outside .page-wrapper, but still inside the transition container.
63
69
 
64
70
  **options.transitionProperty**
65
71
  Defines which property transitionend waits for.
@@ -75,7 +81,7 @@ Number of items in the second group. Each item receives the `.second-item` class
75
81
 
76
82
  ```tsx
77
83
  <TransitionProvider
78
- header={<Header/>}
84
+ outside={<Header/>}
79
85
  options={{
80
86
  transitionProperty: "clip-path",
81
87
  items: [1, 0]
@@ -114,20 +120,41 @@ Triggers the transition.
114
120
 
115
121
  The `TransitionProvider` uses a container with a `data-transition` attribute. When navigation starts, it adds the exit class and waits for the transitionend event of the configured transition property. By default, this is the `.page-wrapper` containing the page's children. If options.items is configured, the event is instead tracked on the last item of the first group.
116
122
 
117
- Once the transition ends, the scroll position is restored and navigation is performed.
123
+
124
+
125
+ Once the transition ends, the scroll position is restored and navigation is performed.
126
+
127
+
118
128
 
119
129
  After navigation, the `exit` class is removed and the `enter` class is added. Once the next transitionend event fires, the enter class is removed.
120
130
 
131
+
132
+
121
133
  For overlay animations, an `.overlay-wrapper` is available. You can configure the number of overlay items using the options.items array.
122
- <br/>
123
- <br/>
124
- To access the overlay wrapper and its items, use `useTransitionContext`.
125
134
 
135
+
136
+
137
+ ```tsx
138
+ <div data-transition>
139
+ {outside}
140
+ {
141
+ (options.items[0] > 0 || options.items[1] > 0) &&
142
+ <div className="overlay-wrapper">
143
+ ...
144
+ </div>
145
+ }
146
+ <div className={"page-wrapper"}>
147
+ {children}
148
+ </div>
149
+ </div>
150
+ ```
151
+
152
+
153
+ To access the page wrapper, overlay wrapper and its items, use `useTransitionContext`.
126
154
  ```ts
127
155
  import { useTransitionContext } from "page-transition"
128
156
 
129
- const { overlayWrapper, overlayItems } = useTransitionContext()
130
-
157
+ const { pageWrapper, overlayWrapper, overlayItems, secondItems } = useTransitionContext()
131
158
  ```
132
159
 
133
160
  #### CSS/SCSS Examples
@@ -331,11 +358,13 @@ items: [10, 10]
331
358
  </details>
332
359
 
333
360
 
334
- ### Manual Mode
361
+
362
+ ### Manual Mode
335
363
 
336
364
  In manual mode, you can control the transition using `useTransitionContext`.
337
365
 
338
- After triggering the navigation, `state.current` is set to `exiting`. You can then call `resolveFn.current()` to proceed with the navigation. Once `state.current` is set to `entering`, the navigation has occurred. After the animation finishes, `state.current` should be set to `idle`.
366
+
367
+ After triggering the navigation, `state.current` is set to `exiting`. You can then call `resolveFn.current()` to proceed with the navigation. Before navigating, you should call `window.scrollTo(0, window.history.state.scrollY ?? 0)` to restore the previous scroll position. Once `state.current` is set to `entering`, the navigation has occurred. After the animation finishes, `state.current` should be set to `idle`.
339
368
 
340
369
 
341
370
  ```ts
@@ -5,9 +5,10 @@ interface TransitionOptions {
5
5
  items?: [number, number];
6
6
  }
7
7
  interface ProviderProps {
8
- header?: ReactNode;
8
+ outside?: ReactNode;
9
+ scroll: 'auto' | 'top';
9
10
  options: TransitionOptions;
10
11
  children: ReactNode;
11
12
  }
12
- export declare function CssTransitionProvider({ header, options, children }: ProviderProps): import("react").JSX.Element;
13
+ export declare function CssTransitionProvider({ outside, scroll, options, children }: ProviderProps): import("react").JSX.Element;
13
14
  export {};
@@ -11,7 +11,7 @@ import { useHandleLinkClick } from "../hooks/useHandleLinkClick";
11
11
  import { useScrollRestoration } from "../hooks/useScrollRestoration";
12
12
  import { usePopState } from "../hooks/usePopState";
13
13
  import { pollTransitionStart } from "../functions/pollTransitionStart";
14
- export function CssTransitionProvider({ header, options, children }) {
14
+ export function CssTransitionProvider({ outside, scroll, options, children }) {
15
15
  const defaultOptions = {
16
16
  items: [0, 0],
17
17
  ...options
@@ -61,7 +61,7 @@ export function CssTransitionProvider({ header, options, children }) {
61
61
  basePath,
62
62
  pollRafId
63
63
  });
64
- useScrollRestoration();
64
+ useScrollRestoration({ scroll });
65
65
  const removeEnter = useTransitionEndObj({
66
66
  transitionEndData,
67
67
  fn: () => {
@@ -94,10 +94,11 @@ export function CssTransitionProvider({ header, options, children }) {
94
94
  });
95
95
  currentPathname.current = window.location.pathname;
96
96
  }, [pathname]);
97
- const value = useMemo(() => ({ handleLinkClick, overlayWrapper, overlayItems }), []);
98
- return (_jsx(TransitionContext.Provider, { value: value, children: _jsxs("div", { ref: container, "data-transition": true, children: [header, _jsxs("div", { ref: overlayWrapper, className: "overlay-wrapper", children: [defaultOptions.items[0] > 0 && Array.from({ length: defaultOptions.items[0] }).map((_, i) => (_jsx("div", { ref: (el) => {
99
- overlayItems.current[i] = el;
100
- }, className: "overlay-item" }, i))), defaultOptions.items[1] > 0 && Array.from({ length: defaultOptions.items[1] }).map((_, i) => (_jsx("div", { ref: (el) => {
101
- secondItems.current[i] = el;
102
- }, className: "second-item" }, `mirror${i}`)))] }), _jsx("div", { ref: pageWrapper, className: "page-wrapper", children: children })] }) }));
97
+ const value = useMemo(() => ({ handleLinkClick, pageWrapper, overlayWrapper, overlayItems, secondItems }), []);
98
+ return (_jsx(TransitionContext.Provider, { value: value, children: _jsxs("div", { ref: container, "data-transition": true, children: [outside, (defaultOptions.items[0] > 0 || defaultOptions.items[1] > 0) &&
99
+ _jsxs("div", { ref: overlayWrapper, className: "overlay-wrapper", children: [defaultOptions.items[0] > 0 && Array.from({ length: defaultOptions.items[0] }).map((_, i) => (_jsx("div", { ref: (el) => {
100
+ overlayItems.current[i] = el;
101
+ }, className: "overlay-item" }, i))), defaultOptions.items[1] > 0 && Array.from({ length: defaultOptions.items[1] }).map((_, i) => (_jsx("div", { ref: (el) => {
102
+ secondItems.current[i] = el;
103
+ }, className: "second-item" }, `mirror${i}`)))] }), _jsx("div", { ref: pageWrapper, className: "page-wrapper", children: children })] }) }));
103
104
  }
@@ -1,6 +1,7 @@
1
1
  import type { ReactNode } from "react";
2
2
  interface ProviderProps {
3
+ scroll: 'auto' | 'top';
3
4
  children: ReactNode;
4
5
  }
5
- export declare function ManualTransitionProvider({ children }: ProviderProps): import("react").JSX.Element;
6
+ export declare function ManualTransitionProvider({ scroll, children }: ProviderProps): import("react").JSX.Element;
6
7
  export {};
@@ -5,7 +5,8 @@ import { useEffect, useRef, useMemo } from "react";
5
5
  import { usePathname } from "next/navigation";
6
6
  import { usePopState } from "../hooks/manual/usePopState";
7
7
  import { useHandleLinkClick } from "../hooks/manual/useHandleLinkClick";
8
- export function ManualTransitionProvider({ children }) {
8
+ import { useScrollRestoration } from "../hooks/useScrollRestoration";
9
+ export function ManualTransitionProvider({ scroll, children }) {
9
10
  const pathname = usePathname();
10
11
  const handleLinkClick = useRef(() => { });
11
12
  const resolveFn = useRef(null);
@@ -25,6 +26,7 @@ export function ManualTransitionProvider({ children }) {
25
26
  useUse,
26
27
  resolveUseFn
27
28
  });
29
+ useScrollRestoration({ scroll });
28
30
  useEffect(() => {
29
31
  state.current = 'entering';
30
32
  }, [pathname]);
@@ -2,10 +2,12 @@ import type { ReactNode, RefObject } from "react";
2
2
  import type { TransitionProperty } from "../types/transitionPropertys.types";
3
3
  interface ContextProps {
4
4
  handleLinkClick: RefObject<((e: React.MouseEvent, url: string) => void)>;
5
- overlayWrapper?: RefObject<HTMLDivElement | null>;
6
- overlayItems?: RefObject<(HTMLDivElement | null)[]>;
7
5
  resolveFn?: RefObject<(() => void) | null>;
8
6
  state?: RefObject<'idle' | 'exiting' | 'entering'>;
7
+ pageWrapper?: RefObject<HTMLDivElement | null>;
8
+ overlayWrapper?: RefObject<HTMLDivElement | null>;
9
+ overlayItems?: RefObject<(HTMLDivElement | null)[]>;
10
+ secondItems?: RefObject<(HTMLDivElement | null)[]>;
9
11
  }
10
12
  export declare const TransitionContext: import("react").Context<ContextProps | null>;
11
13
  export declare function useTransitionContext(): ContextProps;
@@ -15,10 +17,11 @@ interface TransitionOptions {
15
17
  items?: [number, number];
16
18
  }
17
19
  interface ProviderProps {
18
- header?: ReactNode;
20
+ outside?: ReactNode;
19
21
  mode?: Mode;
22
+ scroll?: 'auto' | 'top';
20
23
  options: TransitionOptions;
21
24
  children: ReactNode;
22
25
  }
23
- export declare function TransitionProvider({ header, mode, options, children }: ProviderProps): import("react").JSX.Element;
26
+ export declare function TransitionProvider({ outside, mode, scroll, options, children }: ProviderProps): import("react").JSX.Element;
24
27
  export {};
@@ -10,8 +10,8 @@ export function useTransitionContext() {
10
10
  throw new Error('useTransitionContext in Provider!');
11
11
  return context;
12
12
  }
13
- export function TransitionProvider({ header, mode = 'css', options, children }) {
13
+ export function TransitionProvider({ outside, mode = 'css', scroll = 'auto', options, children }) {
14
14
  return (mode === 'css' ?
15
- _jsx(CssTransitionProvider, { header: header, options: options, children: children }) :
16
- _jsx(ManualTransitionProvider, { children: children }));
15
+ _jsx(CssTransitionProvider, { outside: outside, scroll: scroll, options: options, children: children }) :
16
+ _jsx(ManualTransitionProvider, { scroll: scroll, children: children }));
17
17
  }
@@ -73,7 +73,6 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
73
73
  new Promise((resolve) => {
74
74
  if (state.current === 'navigating') {
75
75
  if (basePathUrl === currentPathname.current) {
76
- afterExitFn.current?.();
77
76
  return;
78
77
  }
79
78
  else if (basePathUrl !== currentPathname.current) {
@@ -1 +1,5 @@
1
- export declare function useScrollRestoration(): void;
1
+ interface Props {
2
+ scroll: 'auto' | 'top';
3
+ }
4
+ export declare function useScrollRestoration({ scroll }: Props): void;
5
+ export {};
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  import { useEffect, useRef } from "react";
3
3
  import { debounce } from "../functions/debounce";
4
- export function useScrollRestoration() {
4
+ export function useScrollRestoration({ scroll }) {
5
5
  const saveScrollY = () => {
6
6
  window.history.replaceState({
7
7
  ...window.history.state,
@@ -14,6 +14,9 @@ export function useScrollRestoration() {
14
14
  });
15
15
  useEffect(() => {
16
16
  window.history.scrollRestoration = 'manual';
17
+ if (scroll === 'top') {
18
+ return () => window.history.scrollRestoration = 'auto';
19
+ }
17
20
  const handleScroll = () => {
18
21
  debounce({ ref: debounceRef });
19
22
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "page-transition",
3
- "version": "0.1.0-beta.51",
3
+ "version": "0.1.0-beta.53",
4
4
  "description": "A page transition library for the Next.js App Router.",
5
5
  "license": "MIT",
6
6
  "files": [