page-transition 0.1.0-beta.52 → 0.1.0-beta.54
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 +37 -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 +12 -1
- package/dist/context/TransitionContext.d.ts +17 -8
- package/dist/context/TransitionContext.js +5 -4
- package/dist/hooks/manual/useHandleLinkClick.d.ts +3 -1
- package/dist/hooks/manual/useHandleLinkClick.js +5 -1
- package/dist/hooks/manual/usePopState.d.ts +2 -1
- package/dist/hooks/manual/usePopState.js +7 -2
- 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,19 @@ 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 {
|
|
146
|
-
|
|
157
|
+
const {
|
|
158
|
+
pageWrapper,
|
|
159
|
+
overlayWrapper,
|
|
160
|
+
overlayItems,
|
|
161
|
+
secondItems
|
|
162
|
+
} = useTransitionContext()
|
|
147
163
|
```
|
|
148
164
|
|
|
149
165
|
#### CSS/SCSS Examples
|
|
@@ -347,11 +363,17 @@ items: [10, 10]
|
|
|
347
363
|
</details>
|
|
348
364
|
|
|
349
365
|
|
|
350
|
-
|
|
366
|
+
|
|
367
|
+
### Manual Mode
|
|
351
368
|
|
|
352
369
|
In manual mode, you can control the transition using `useTransitionContext`.
|
|
353
370
|
|
|
354
|
-
|
|
371
|
+
|
|
372
|
+
After triggering the navigation, `state.current` is set to `exiting`. You can then call `resolveFn.current()` to proceed with the navigation.
|
|
373
|
+
|
|
374
|
+
Before navigating, you should call `window.scrollTo(0, window.history.state.scrollY ?? 0)` to restore the previous scroll position.
|
|
375
|
+
|
|
376
|
+
Once `state.current` is set to `entering`, the navigation has occurred. After the animation finishes, `state.current` should be set to `idle`.
|
|
355
377
|
|
|
356
378
|
|
|
357
379
|
```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,16 +5,20 @@ 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);
|
|
12
13
|
const state = useRef('idle');
|
|
14
|
+
const currentPathname = useRef(pathname);
|
|
15
|
+
const basePath = useRef(null);
|
|
13
16
|
const useUse = useRef(false);
|
|
14
17
|
const resolveUseFn = useRef(null);
|
|
15
18
|
usePopState({
|
|
16
19
|
resolveFn,
|
|
17
20
|
state,
|
|
21
|
+
currentPathname,
|
|
18
22
|
useUse,
|
|
19
23
|
resolveUseFn
|
|
20
24
|
});
|
|
@@ -22,11 +26,18 @@ export function ManualTransitionProvider({ children }) {
|
|
|
22
26
|
handleLinkClick,
|
|
23
27
|
resolveFn,
|
|
24
28
|
state,
|
|
29
|
+
currentPathname,
|
|
30
|
+
basePath,
|
|
25
31
|
useUse,
|
|
26
32
|
resolveUseFn
|
|
27
33
|
});
|
|
34
|
+
useScrollRestoration({ scroll });
|
|
28
35
|
useEffect(() => {
|
|
36
|
+
if (basePath.current === null) {
|
|
37
|
+
basePath.current = window.location.pathname.slice(0, -pathname.length);
|
|
38
|
+
}
|
|
29
39
|
state.current = 'entering';
|
|
40
|
+
currentPathname.current = window.location.pathname;
|
|
30
41
|
}, [pathname]);
|
|
31
42
|
const value = useMemo(() => ({ handleLinkClick, resolveFn, state }), []);
|
|
32
43
|
return (_jsx(TransitionContext.Provider, { value: value, children: children }));
|
|
@@ -2,23 +2,32 @@ 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;
|
|
12
|
-
type Mode = 'css' | 'manual';
|
|
13
14
|
interface TransitionOptions {
|
|
14
15
|
transitionProperty: TransitionProperty;
|
|
15
16
|
items?: [number, number];
|
|
16
17
|
}
|
|
17
|
-
interface
|
|
18
|
-
|
|
19
|
-
mode?: Mode;
|
|
20
|
-
options: TransitionOptions;
|
|
18
|
+
interface BaseProps {
|
|
19
|
+
scroll?: 'auto' | 'top';
|
|
21
20
|
children: ReactNode;
|
|
22
21
|
}
|
|
23
|
-
|
|
22
|
+
type ModeProps = {
|
|
23
|
+
mode?: 'css';
|
|
24
|
+
outside?: ReactNode;
|
|
25
|
+
options: TransitionOptions;
|
|
26
|
+
} | {
|
|
27
|
+
mode: 'manual';
|
|
28
|
+
outside?: never;
|
|
29
|
+
options?: never;
|
|
30
|
+
};
|
|
31
|
+
type ProviderProps = BaseProps & ModeProps;
|
|
32
|
+
export declare function TransitionProvider(props: ProviderProps): import("react").JSX.Element;
|
|
24
33
|
export {};
|
|
@@ -10,8 +10,9 @@ export function useTransitionContext() {
|
|
|
10
10
|
throw new Error('useTransitionContext in Provider!');
|
|
11
11
|
return context;
|
|
12
12
|
}
|
|
13
|
-
export function TransitionProvider(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
_jsx(
|
|
13
|
+
export function TransitionProvider(props) {
|
|
14
|
+
const { scroll = 'auto' } = props;
|
|
15
|
+
return (props.mode === 'css' ?
|
|
16
|
+
_jsx(CssTransitionProvider, { outside: props.outside, scroll: scroll, options: props.options, children: props.children }) :
|
|
17
|
+
_jsx(ManualTransitionProvider, { scroll: scroll, children: props.children }));
|
|
17
18
|
}
|
|
@@ -3,8 +3,10 @@ interface Props {
|
|
|
3
3
|
handleLinkClick: RefObject<((e: React.MouseEvent, url: string) => void)>;
|
|
4
4
|
resolveFn: RefObject<(() => void) | null>;
|
|
5
5
|
state: RefObject<'idle' | 'exiting' | 'entering'>;
|
|
6
|
+
currentPathname: RefObject<string>;
|
|
7
|
+
basePath: RefObject<string | null>;
|
|
6
8
|
useUse: RefObject<boolean>;
|
|
7
9
|
resolveUseFn: RefObject<(() => void) | null>;
|
|
8
10
|
}
|
|
9
|
-
export declare function useHandleLinkClick({ handleLinkClick, resolveFn, state, useUse, resolveUseFn }: Props): void;
|
|
11
|
+
export declare function useHandleLinkClick({ handleLinkClick, resolveFn, state, currentPathname, basePath, useUse, resolveUseFn }: Props): void;
|
|
10
12
|
export {};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { useCallback } from "react";
|
|
3
3
|
import { useRouter } from "next/navigation";
|
|
4
|
-
export function useHandleLinkClick({ handleLinkClick, resolveFn, state, useUse, resolveUseFn }) {
|
|
4
|
+
export function useHandleLinkClick({ handleLinkClick, resolveFn, state, currentPathname, basePath, useUse, resolveUseFn }) {
|
|
5
5
|
const router = useRouter();
|
|
6
6
|
handleLinkClick.current = useCallback((e, url) => {
|
|
7
7
|
e.preventDefault();
|
|
8
|
+
const basePathUrl = basePath.current ? basePath.current + url : url;
|
|
8
9
|
new Promise((resolve) => {
|
|
9
10
|
resolveFn.current = resolve;
|
|
10
11
|
state.current = 'exiting';
|
|
@@ -13,6 +14,9 @@ export function useHandleLinkClick({ handleLinkClick, resolveFn, state, useUse,
|
|
|
13
14
|
resolveUseFn.current?.();
|
|
14
15
|
router.push(url);
|
|
15
16
|
resolveFn.current = null;
|
|
17
|
+
if (basePathUrl === currentPathname.current) {
|
|
18
|
+
state.current = 'entering';
|
|
19
|
+
}
|
|
16
20
|
});
|
|
17
21
|
}, []);
|
|
18
22
|
}
|
|
@@ -2,8 +2,9 @@ import type { RefObject } from "react";
|
|
|
2
2
|
interface Props {
|
|
3
3
|
resolveFn: RefObject<(() => void) | null>;
|
|
4
4
|
state: RefObject<'idle' | 'exiting' | 'entering'>;
|
|
5
|
+
currentPathname: RefObject<string>;
|
|
5
6
|
useUse: RefObject<boolean>;
|
|
6
7
|
resolveUseFn: RefObject<(() => void) | null>;
|
|
7
8
|
}
|
|
8
|
-
export declare function usePopState({ resolveFn, state, useUse, resolveUseFn }: Props): void;
|
|
9
|
+
export declare function usePopState({ resolveFn, state, currentPathname, useUse, resolveUseFn }: Props): void;
|
|
9
10
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { useEffect, useState, use } from "react";
|
|
3
|
-
export function usePopState({ resolveFn, state, useUse, resolveUseFn }) {
|
|
3
|
+
export function usePopState({ resolveFn, state, currentPathname, useUse, resolveUseFn }) {
|
|
4
4
|
const [promise, setPromise] = useState(null);
|
|
5
5
|
if (promise) {
|
|
6
6
|
useUse.current = true;
|
|
@@ -15,7 +15,12 @@ export function usePopState({ resolveFn, state, useUse, resolveUseFn }) {
|
|
|
15
15
|
resolveFn.current = resolve;
|
|
16
16
|
resolveUseFn.current = reject;
|
|
17
17
|
state.current = 'exiting';
|
|
18
|
-
}).then(() =>
|
|
18
|
+
}).then(() => {
|
|
19
|
+
setPromise(null);
|
|
20
|
+
if (window.location.pathname === currentPathname.current) {
|
|
21
|
+
state.current = 'entering';
|
|
22
|
+
}
|
|
23
|
+
}).catch(() => setPromise(null)));
|
|
19
24
|
};
|
|
20
25
|
window.addEventListener('popstate', handlePopstate);
|
|
21
26
|
return () => window.removeEventListener('popstate', handlePopstate);
|
|
@@ -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
|
};
|