page-transition 0.1.0-beta.52 → 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 +28 -15
- package/dist/context/CssTransitionProvider.d.ts +2 -1
- package/dist/context/CssTransitionProvider.js +3 -3
- package/dist/context/ManualTransitionProvider.d.ts +2 -1
- package/dist/context/ManualTransitionProvider.js +3 -1
- package/dist/context/TransitionContext.d.ts +6 -3
- package/dist/context/TransitionContext.js +3 -3
- package/dist/hooks/useScrollRestoration.d.ts +5 -1
- package/dist/hooks/useScrollRestoration.js +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# page-transition
|
|
2
2
|
A page transition library for the Next.js App Router.
|
|
3
3
|
<br/>
|
|
4
4
|
[](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
|
-
 
|
|
14
14
|
|
|
15
15
|
`npm install page-transition@beta`
|
|
16
16
|
|
|
@@ -42,6 +42,7 @@ export default function RootLayout({ children }: LayoutProps<"/">) {
|
|
|
42
42
|
|Prop|Type|Default|Mode|
|
|
43
43
|
|---|---|---|---|
|
|
44
44
|
| mode | "css" \| "manual" | "css" | Both |
|
|
45
|
+
| scroll | "auto"\|"top" | "auto" | Both |
|
|
45
46
|
| outside | JSX | - | CSS |
|
|
46
47
|
| options | *Options* | {} | CSS |
|
|
47
48
|
|
|
@@ -58,6 +59,11 @@ export default function RootLayout({ children }: LayoutProps<"/">) {
|
|
|
58
59
|
**mode**
|
|
59
60
|
Select the transition mode.
|
|
60
61
|
|
|
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
|
+
|
|
61
67
|
**outside**
|
|
62
68
|
Components rendered outside .page-wrapper, but still inside the transition container.
|
|
63
69
|
|
|
@@ -114,6 +120,20 @@ 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
|
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
Once the transition ends, the scroll position is restored and navigation is performed.
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
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.
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
For overlay animations, an `.overlay-wrapper` is available. You can configure the number of overlay items using the options.items array.
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
117
137
|
```tsx
|
|
118
138
|
<div data-transition>
|
|
119
139
|
{outside}
|
|
@@ -127,23 +147,14 @@ The `TransitionProvider` uses a container with a `data-transition` attribute. Wh
|
|
|
127
147
|
{children}
|
|
128
148
|
</div>
|
|
129
149
|
</div>
|
|
130
|
-
|
|
131
150
|
```
|
|
132
151
|
|
|
133
|
-
Once the transition ends, the scroll position is restored and navigation is performed.
|
|
134
|
-
|
|
135
|
-
After navigation, the `exit` class is removed and the `enter` class is added. Once the next transitionend event fires, the enter class is removed.
|
|
136
|
-
|
|
137
|
-
For overlay animations, an `.overlay-wrapper` is available. You can configure the number of overlay items using the options.items array.
|
|
138
|
-
<br/>
|
|
139
|
-
<br/>
|
|
140
|
-
To access the overlay wrapper and its items, use `useTransitionContext`.
|
|
141
152
|
|
|
153
|
+
To access the page wrapper, overlay wrapper and its items, use `useTransitionContext`.
|
|
142
154
|
```ts
|
|
143
155
|
import { useTransitionContext } from "page-transition"
|
|
144
156
|
|
|
145
|
-
const { overlayWrapper, overlayItems } = useTransitionContext()
|
|
146
|
-
|
|
157
|
+
const { pageWrapper, overlayWrapper, overlayItems, secondItems } = useTransitionContext()
|
|
147
158
|
```
|
|
148
159
|
|
|
149
160
|
#### CSS/SCSS Examples
|
|
@@ -347,11 +358,13 @@ items: [10, 10]
|
|
|
347
358
|
</details>
|
|
348
359
|
|
|
349
360
|
|
|
350
|
-
|
|
361
|
+
|
|
362
|
+
### Manual Mode
|
|
351
363
|
|
|
352
364
|
In manual mode, you can control the transition using `useTransitionContext`.
|
|
353
365
|
|
|
354
|
-
|
|
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`.
|
|
355
368
|
|
|
356
369
|
|
|
357
370
|
```ts
|
|
@@ -6,8 +6,9 @@ interface TransitionOptions {
|
|
|
6
6
|
}
|
|
7
7
|
interface ProviderProps {
|
|
8
8
|
outside?: ReactNode;
|
|
9
|
+
scroll: 'auto' | 'top';
|
|
9
10
|
options: TransitionOptions;
|
|
10
11
|
children: ReactNode;
|
|
11
12
|
}
|
|
12
|
-
export declare function CssTransitionProvider({ outside, 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({ outside, 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({ outside, 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,7 +94,7 @@ export function CssTransitionProvider({ outside, options, children }) {
|
|
|
94
94
|
});
|
|
95
95
|
currentPathname.current = window.location.pathname;
|
|
96
96
|
}, [pathname]);
|
|
97
|
-
const value = useMemo(() => ({ handleLinkClick, overlayWrapper, overlayItems }), []);
|
|
97
|
+
const value = useMemo(() => ({ handleLinkClick, pageWrapper, overlayWrapper, overlayItems, secondItems }), []);
|
|
98
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
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
100
|
overlayItems.current[i] = el;
|
|
@@ -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
|
-
|
|
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;
|
|
@@ -17,8 +19,9 @@ interface TransitionOptions {
|
|
|
17
19
|
interface ProviderProps {
|
|
18
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({ outside, 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({ outside, mode = 'css', options, children }) {
|
|
13
|
+
export function TransitionProvider({ outside, mode = 'css', scroll = 'auto', options, children }) {
|
|
14
14
|
return (mode === 'css' ?
|
|
15
|
-
_jsx(CssTransitionProvider, { outside: outside, 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
|
}
|
|
@@ -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
|
};
|