page-transition 0.1.0-beta.53 → 0.1.0-beta.55
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 +15 -6
- package/dist/context/ManualTransitionProvider.js +9 -0
- package/dist/context/TransitionContext.d.ts +12 -6
- 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/package.json +1 -1
package/README.md
CHANGED
|
@@ -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
|
|
|
@@ -19,7 +19,7 @@ Import your page transition styles and wrap your page content with `TransitionPr
|
|
|
19
19
|
|
|
20
20
|
```tsx
|
|
21
21
|
import { TransitionProvider } from "page-transition"
|
|
22
|
-
import '
|
|
22
|
+
import './transition-styles.css'
|
|
23
23
|
|
|
24
24
|
export default function RootLayout({ children }: LayoutProps<"/">) {
|
|
25
25
|
return (
|
|
@@ -42,7 +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
|
+
| scroll | "auto" \| "top" | "auto" | Both |
|
|
46
46
|
| outside | JSX | - | CSS |
|
|
47
47
|
| options | *Options* | {} | CSS |
|
|
48
48
|
|
|
@@ -60,7 +60,7 @@ export default function RootLayout({ children }: LayoutProps<"/">) {
|
|
|
60
60
|
Select the transition mode.
|
|
61
61
|
|
|
62
62
|
**scroll**
|
|
63
|
-
Controls browser scroll restoration during back and forward navigation.
|
|
63
|
+
Controls browser scroll restoration during back and forward navigation.
|
|
64
64
|
`'auto'` Restores the previous scroll position.
|
|
65
65
|
`'top'` Scrolls to the top.
|
|
66
66
|
|
|
@@ -154,7 +154,12 @@ To access the page wrapper, overlay wrapper and its items, use `useTransitionCon
|
|
|
154
154
|
```ts
|
|
155
155
|
import { useTransitionContext } from "page-transition"
|
|
156
156
|
|
|
157
|
-
const {
|
|
157
|
+
const {
|
|
158
|
+
pageWrapper,
|
|
159
|
+
overlayWrapper,
|
|
160
|
+
overlayItems,
|
|
161
|
+
secondItems
|
|
162
|
+
} = useTransitionContext()
|
|
158
163
|
```
|
|
159
164
|
|
|
160
165
|
#### CSS/SCSS Examples
|
|
@@ -364,7 +369,11 @@ items: [10, 10]
|
|
|
364
369
|
In manual mode, you can control the transition using `useTransitionContext`.
|
|
365
370
|
|
|
366
371
|
|
|
367
|
-
After triggering the navigation, `state.current` is set to `exiting`. You can then call `resolveFn.current()` to proceed with the navigation.
|
|
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`.
|
|
368
377
|
|
|
369
378
|
|
|
370
379
|
```ts
|
|
@@ -11,11 +11,14 @@ export function ManualTransitionProvider({ scroll, children }) {
|
|
|
11
11
|
const handleLinkClick = useRef(() => { });
|
|
12
12
|
const resolveFn = useRef(null);
|
|
13
13
|
const state = useRef('idle');
|
|
14
|
+
const currentPathname = useRef(pathname);
|
|
15
|
+
const basePath = useRef(null);
|
|
14
16
|
const useUse = useRef(false);
|
|
15
17
|
const resolveUseFn = useRef(null);
|
|
16
18
|
usePopState({
|
|
17
19
|
resolveFn,
|
|
18
20
|
state,
|
|
21
|
+
currentPathname,
|
|
19
22
|
useUse,
|
|
20
23
|
resolveUseFn
|
|
21
24
|
});
|
|
@@ -23,12 +26,18 @@ export function ManualTransitionProvider({ scroll, children }) {
|
|
|
23
26
|
handleLinkClick,
|
|
24
27
|
resolveFn,
|
|
25
28
|
state,
|
|
29
|
+
currentPathname,
|
|
30
|
+
basePath,
|
|
26
31
|
useUse,
|
|
27
32
|
resolveUseFn
|
|
28
33
|
});
|
|
29
34
|
useScrollRestoration({ scroll });
|
|
30
35
|
useEffect(() => {
|
|
36
|
+
if (basePath.current === null) {
|
|
37
|
+
basePath.current = window.location.pathname.slice(0, -pathname.length);
|
|
38
|
+
}
|
|
31
39
|
state.current = 'entering';
|
|
40
|
+
currentPathname.current = window.location.pathname;
|
|
32
41
|
}, [pathname]);
|
|
33
42
|
const value = useMemo(() => ({ handleLinkClick, resolveFn, state }), []);
|
|
34
43
|
return (_jsx(TransitionContext.Provider, { value: value, children: children }));
|
|
@@ -11,17 +11,23 @@ interface ContextProps {
|
|
|
11
11
|
}
|
|
12
12
|
export declare const TransitionContext: import("react").Context<ContextProps | null>;
|
|
13
13
|
export declare function useTransitionContext(): ContextProps;
|
|
14
|
-
type Mode = 'css' | 'manual';
|
|
15
14
|
interface TransitionOptions {
|
|
16
15
|
transitionProperty: TransitionProperty;
|
|
17
16
|
items?: [number, number];
|
|
18
17
|
}
|
|
19
|
-
interface
|
|
20
|
-
outside?: ReactNode;
|
|
21
|
-
mode?: Mode;
|
|
18
|
+
interface BaseProps {
|
|
22
19
|
scroll?: 'auto' | 'top';
|
|
23
|
-
options: TransitionOptions;
|
|
24
20
|
children: ReactNode;
|
|
25
21
|
}
|
|
26
|
-
|
|
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;
|
|
27
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);
|