page-transition 0.1.2 → 0.2.0
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 +18 -1
- package/dist/context/ActiveLinkContext.d.ts +10 -0
- package/dist/context/ActiveLinkContext.js +45 -0
- package/dist/context/CssTransitionProvider.js +4 -1
- package/dist/context/ManualTransitionProvider.js +3 -0
- package/dist/context/TransitionProvider.js +5 -4
- package/dist/functions/pollTransitionStart.js +3 -2
- package/dist/hooks/manual/useHandleLinkClick.js +2 -0
- package/dist/hooks/manual/usePopState.d.ts +2 -1
- package/dist/hooks/manual/usePopState.js +4 -1
- package/dist/hooks/useHandleLinkClick.d.ts +2 -2
- package/dist/hooks/useHandleLinkClick.js +3 -1
- package/dist/hooks/usePopState.d.ts +2 -1
- package/dist/hooks/usePopState.js +4 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
- package/dist/functions/checkTransitionStart.d.ts +0 -2
- package/dist/functions/checkTransitionStart.js +0 -10
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`
|
|
16
16
|
|
|
@@ -412,4 +412,21 @@ import { useTransitionContext } from "page-transition/manual"
|
|
|
412
412
|
|
|
413
413
|
const { resolveFn, state } = useTransitionContext()
|
|
414
414
|
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
## Helpers
|
|
418
|
+
|
|
419
|
+
**useActiveLink**
|
|
420
|
+
Automatically adds the active class to the element when its href matches.
|
|
421
|
+
|
|
422
|
+
```tsx
|
|
423
|
+
import { useActiveLink } from "page-transition"
|
|
424
|
+
const { saveRef } = useActiveLink()
|
|
425
|
+
|
|
426
|
+
<li ref={saveRef(href)}>
|
|
427
|
+
<TransitionLink href={href}>
|
|
428
|
+
{linkName}
|
|
429
|
+
</TransitionLink>
|
|
430
|
+
</li>
|
|
431
|
+
|
|
415
432
|
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
interface Props {
|
|
3
|
+
saveRef: (href: string) => (el: HTMLElement | null) => void;
|
|
4
|
+
}
|
|
5
|
+
export declare function useActiveLinkContext(): Props;
|
|
6
|
+
export declare function dispatchNextPathname(href: string): void;
|
|
7
|
+
export declare function ActiveLinkProvider({ children }: {
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
}): import("react").JSX.Element;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useContext, useEffect, useRef } from "react";
|
|
4
|
+
const ActiveLinkContext = createContext(null);
|
|
5
|
+
export function useActiveLinkContext() {
|
|
6
|
+
const context = useContext(ActiveLinkContext);
|
|
7
|
+
if (!context)
|
|
8
|
+
throw new Error('useNextPathname in Provider!');
|
|
9
|
+
return context;
|
|
10
|
+
}
|
|
11
|
+
let pathname = "";
|
|
12
|
+
export function dispatchNextPathname(href) {
|
|
13
|
+
pathname = href;
|
|
14
|
+
window.dispatchEvent(new CustomEvent('nextPathname', { detail: href }));
|
|
15
|
+
}
|
|
16
|
+
export function ActiveLinkProvider({ children }) {
|
|
17
|
+
const map = useRef(new Map());
|
|
18
|
+
function saveRef(href) {
|
|
19
|
+
let element;
|
|
20
|
+
return (ref) => {
|
|
21
|
+
if (ref) {
|
|
22
|
+
element = ref;
|
|
23
|
+
map.current.set(ref, href);
|
|
24
|
+
}
|
|
25
|
+
if (!ref)
|
|
26
|
+
map.current.delete(element);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
const handleActiveLink = (e) => {
|
|
31
|
+
for (const [el, href] of map.current) {
|
|
32
|
+
el.classList.toggle('active', href === e.detail);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
for (const [el, href] of map.current) {
|
|
36
|
+
el.classList.toggle('active', href === pathname);
|
|
37
|
+
}
|
|
38
|
+
window.addEventListener('nextPathname', handleActiveLink);
|
|
39
|
+
return () => {
|
|
40
|
+
window.removeEventListener('nextPathname', handleActiveLink);
|
|
41
|
+
map.current.clear();
|
|
42
|
+
};
|
|
43
|
+
}, []);
|
|
44
|
+
return (_jsx(ActiveLinkContext.Provider, { value: { saveRef }, children: children }));
|
|
45
|
+
}
|
|
@@ -11,6 +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
|
+
import { dispatchNextPathname } from "./ActiveLinkContext";
|
|
14
15
|
export function CssTransitionProvider({ outside, scroll, options, children }) {
|
|
15
16
|
const defaultOptions = {
|
|
16
17
|
overlays: 0,
|
|
@@ -49,6 +50,7 @@ export function CssTransitionProvider({ outside, scroll, options, children }) {
|
|
|
49
50
|
usePopState({
|
|
50
51
|
container,
|
|
51
52
|
currentPathname,
|
|
53
|
+
basePath,
|
|
52
54
|
transitionEndData,
|
|
53
55
|
transitionEndHandler,
|
|
54
56
|
state,
|
|
@@ -60,12 +62,12 @@ export function CssTransitionProvider({ outside, scroll, options, children }) {
|
|
|
60
62
|
container,
|
|
61
63
|
handleLinkClick,
|
|
62
64
|
currentPathname,
|
|
65
|
+
basePath,
|
|
63
66
|
transitionEndData,
|
|
64
67
|
transitionEndHandler,
|
|
65
68
|
state,
|
|
66
69
|
useUse,
|
|
67
70
|
resolveUseFn,
|
|
68
|
-
basePath,
|
|
69
71
|
pollRafId
|
|
70
72
|
});
|
|
71
73
|
useScrollRestoration({ scroll });
|
|
@@ -79,6 +81,7 @@ export function CssTransitionProvider({ outside, scroll, options, children }) {
|
|
|
79
81
|
useEffect(() => {
|
|
80
82
|
if (mount.current.mounted === false &&
|
|
81
83
|
mount.current.initPathname === pathname) {
|
|
84
|
+
dispatchNextPathname(pathname);
|
|
82
85
|
currentPathname.current = window.location.pathname;
|
|
83
86
|
basePath.current = window.location.pathname.slice(0, -pathname.length);
|
|
84
87
|
return;
|
|
@@ -6,6 +6,7 @@ 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
|
+
import { dispatchNextPathname } from "./ActiveLinkContext";
|
|
9
10
|
export function ManualTransitionProvider({ scroll, children }) {
|
|
10
11
|
const pathname = usePathname();
|
|
11
12
|
const handleLinkClick = useRef(() => { });
|
|
@@ -23,6 +24,7 @@ export function ManualTransitionProvider({ scroll, children }) {
|
|
|
23
24
|
resolveFn,
|
|
24
25
|
state,
|
|
25
26
|
currentPathname,
|
|
27
|
+
basePath,
|
|
26
28
|
useUse,
|
|
27
29
|
resolveUseFn
|
|
28
30
|
});
|
|
@@ -39,6 +41,7 @@ export function ManualTransitionProvider({ scroll, children }) {
|
|
|
39
41
|
useEffect(() => {
|
|
40
42
|
if (mount.current.mounted === false &&
|
|
41
43
|
mount.current.initPathname === pathname) {
|
|
44
|
+
dispatchNextPathname(pathname);
|
|
42
45
|
currentPathname.current = window.location.pathname;
|
|
43
46
|
basePath.current = window.location.pathname.slice(0, -pathname.length);
|
|
44
47
|
return;
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
3
|
import { CssTransitionProvider } from "./CssTransitionProvider";
|
|
4
4
|
import { ManualTransitionProvider } from "./ManualTransitionProvider";
|
|
5
|
+
import { ActiveLinkProvider } from "./ActiveLinkContext";
|
|
5
6
|
export function TransitionProvider(props) {
|
|
6
7
|
const { scroll = 'auto', children } = props;
|
|
7
|
-
return (props.mode === 'manual' ?
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
return (_jsx(ActiveLinkProvider, { children: props.mode === 'manual' ?
|
|
9
|
+
_jsx(ManualTransitionProvider, { scroll: scroll, children: children })
|
|
10
|
+
:
|
|
11
|
+
_jsx(CssTransitionProvider, { scroll: scroll, outside: props.outside, options: props.options, children: children }) }));
|
|
11
12
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { transitionRunning } from "./transitionRunning";
|
|
2
2
|
export function pollTransitionStart({ id, fn, transitionEndData }) {
|
|
3
3
|
if (id.current !== null)
|
|
4
4
|
cancelAnimationFrame(id.current);
|
|
5
5
|
id.current = requestAnimationFrame(() => {
|
|
6
|
-
if (
|
|
6
|
+
if (transitionRunning(transitionEndData.current.elArray, transitionEndData.current.transitionProperty, transitionEndData.current.group)) {
|
|
7
|
+
fn();
|
|
7
8
|
return;
|
|
8
9
|
}
|
|
9
10
|
pollTransitionStart({ id, fn, transitionEndData });
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { useCallback } from "react";
|
|
3
3
|
import { useRouter } from "next/navigation";
|
|
4
|
+
import { dispatchNextPathname } from "../../context/ActiveLinkContext";
|
|
4
5
|
export function useHandleLinkClick({ handleLinkClick, resolveFn, state, currentPathname, basePath, useUse, resolveUseFn }) {
|
|
5
6
|
const router = useRouter();
|
|
6
7
|
handleLinkClick.current = useCallback((e, url) => {
|
|
7
8
|
e.preventDefault();
|
|
9
|
+
dispatchNextPathname(url);
|
|
8
10
|
const basePathUrl = basePath.current ? basePath.current + url : url;
|
|
9
11
|
new Promise((resolve) => {
|
|
10
12
|
resolveFn.current = resolve;
|
|
@@ -3,8 +3,9 @@ interface Props {
|
|
|
3
3
|
resolveFn: RefObject<(() => void) | null>;
|
|
4
4
|
state: RefObject<'idle' | 'exiting' | 'entering'>;
|
|
5
5
|
currentPathname: RefObject<string>;
|
|
6
|
+
basePath: RefObject<string | null>;
|
|
6
7
|
useUse: RefObject<boolean>;
|
|
7
8
|
resolveUseFn: RefObject<(() => void) | null>;
|
|
8
9
|
}
|
|
9
|
-
export declare function usePopState({ resolveFn, state, currentPathname, useUse, resolveUseFn }: Props): void;
|
|
10
|
+
export declare function usePopState({ resolveFn, state, currentPathname, basePath, useUse, resolveUseFn }: Props): void;
|
|
10
11
|
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { useEffect, useState, useRef, use } from "react";
|
|
3
|
-
|
|
3
|
+
import { dispatchNextPathname } from "../../context/ActiveLinkContext";
|
|
4
|
+
export function usePopState({ resolveFn, state, currentPathname, basePath, useUse, resolveUseFn }) {
|
|
4
5
|
const [_, rerender] = useState(0);
|
|
5
6
|
const promise = useRef(null);
|
|
6
7
|
if (promise.current) {
|
|
@@ -12,6 +13,8 @@ export function usePopState({ resolveFn, state, currentPathname, useUse, resolve
|
|
|
12
13
|
}
|
|
13
14
|
useEffect(() => {
|
|
14
15
|
const handlePopstate = () => {
|
|
16
|
+
const nextPathname = window.location.pathname.slice(basePath.current?.length ?? 0);
|
|
17
|
+
dispatchNextPathname(nextPathname);
|
|
15
18
|
promise.current = new Promise((resolve, reject) => {
|
|
16
19
|
resolveFn.current = resolve;
|
|
17
20
|
resolveUseFn.current = reject;
|
|
@@ -4,13 +4,13 @@ interface Props {
|
|
|
4
4
|
container: RefObject<HTMLDivElement | null>;
|
|
5
5
|
handleLinkClick: RefObject<((e: React.MouseEvent, url: string) => void)>;
|
|
6
6
|
currentPathname: RefObject<string>;
|
|
7
|
+
basePath: RefObject<string | null>;
|
|
7
8
|
transitionEndData: RefObject<TransitionEndData>;
|
|
8
9
|
transitionEndHandler: RefObject<TransitionEndHandler>;
|
|
9
10
|
state: RefObject<'idle' | 'exiting' | 'entering' | 'navigating'>;
|
|
10
11
|
useUse: RefObject<boolean>;
|
|
11
12
|
resolveUseFn: RefObject<(() => void) | null>;
|
|
12
|
-
basePath: RefObject<string | null>;
|
|
13
13
|
pollRafId: RefObject<number | null>;
|
|
14
14
|
}
|
|
15
|
-
export declare function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, state, useUse, resolveUseFn,
|
|
15
|
+
export declare function useHandleLinkClick({ container, handleLinkClick, currentPathname, basePath, transitionEndData, transitionEndHandler, state, useUse, resolveUseFn, pollRafId }: Props): void;
|
|
16
16
|
export {};
|
|
@@ -4,7 +4,8 @@ import { useRouter } from "next/navigation";
|
|
|
4
4
|
import { useTransitionEndObj } from "../hooks/useTransitionEndObj";
|
|
5
5
|
import { transitionEnd } from "../functions/transitionEnd";
|
|
6
6
|
import { pollTransitionStart } from "../functions/pollTransitionStart";
|
|
7
|
-
|
|
7
|
+
import { dispatchNextPathname } from "../context/ActiveLinkContext";
|
|
8
|
+
export function useHandleLinkClick({ container, handleLinkClick, currentPathname, basePath, transitionEndData, transitionEndHandler, state, useUse, resolveUseFn, pollRafId }) {
|
|
8
9
|
const router = useRouter();
|
|
9
10
|
const afterExitFn = useRef(null);
|
|
10
11
|
const nextPage = useRef(null);
|
|
@@ -68,6 +69,7 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
|
|
|
68
69
|
});
|
|
69
70
|
handleLinkClick.current = useCallback((e, url) => {
|
|
70
71
|
e.preventDefault();
|
|
72
|
+
dispatchNextPathname(url);
|
|
71
73
|
nextPage.current = url;
|
|
72
74
|
const basePathUrl = basePath.current ? basePath.current + url : url;
|
|
73
75
|
new Promise((resolve) => {
|
|
@@ -3,6 +3,7 @@ import type { TransitionEndData, TransitionEndHandler } from "../types/transitio
|
|
|
3
3
|
interface Props {
|
|
4
4
|
container: RefObject<HTMLDivElement | null>;
|
|
5
5
|
currentPathname: RefObject<string>;
|
|
6
|
+
basePath: RefObject<string | null>;
|
|
6
7
|
transitionEndData: RefObject<TransitionEndData>;
|
|
7
8
|
transitionEndHandler: RefObject<TransitionEndHandler>;
|
|
8
9
|
state: RefObject<'idle' | 'exiting' | 'entering' | 'navigating'>;
|
|
@@ -10,5 +11,5 @@ interface Props {
|
|
|
10
11
|
resolveUseFn: RefObject<(() => void) | null>;
|
|
11
12
|
pollRafId: RefObject<number | null>;
|
|
12
13
|
}
|
|
13
|
-
export declare function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, state, useUse, resolveUseFn, pollRafId }: Props): void;
|
|
14
|
+
export declare function usePopState({ container, currentPathname, basePath, transitionEndData, transitionEndHandler, state, useUse, resolveUseFn, pollRafId }: Props): void;
|
|
14
15
|
export {};
|
|
@@ -3,7 +3,8 @@ import { useEffect, useRef, useState, use } from "react";
|
|
|
3
3
|
import { useTransitionEndObj } from "./useTransitionEndObj";
|
|
4
4
|
import { transitionEnd } from "../functions/transitionEnd";
|
|
5
5
|
import { pollTransitionStart } from "../functions/pollTransitionStart";
|
|
6
|
-
|
|
6
|
+
import { dispatchNextPathname } from "../context/ActiveLinkContext";
|
|
7
|
+
export function usePopState({ container, currentPathname, basePath, transitionEndData, transitionEndHandler, state, useUse, resolveUseFn, pollRafId }) {
|
|
7
8
|
const [_, rerender] = useState(0);
|
|
8
9
|
const promise = useRef(null);
|
|
9
10
|
if (promise.current) {
|
|
@@ -69,6 +70,8 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
|
|
|
69
70
|
});
|
|
70
71
|
useEffect(() => {
|
|
71
72
|
const handlePopstate = () => {
|
|
73
|
+
const nextPathname = window.location.pathname.slice(basePath.current?.length ?? 0);
|
|
74
|
+
dispatchNextPathname(nextPathname);
|
|
72
75
|
promise.current = new Promise((resolve, reject) => {
|
|
73
76
|
resolveFn.current = resolve;
|
|
74
77
|
resolveUseFn.current = reject;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { transitionRunning } from "./transitionRunning";
|
|
2
|
-
export function checkTransitionStart(transitionEndData, fn) {
|
|
3
|
-
if (transitionRunning(transitionEndData.elArray, transitionEndData.transitionProperty, transitionEndData.group)) {
|
|
4
|
-
fn?.();
|
|
5
|
-
return true;
|
|
6
|
-
}
|
|
7
|
-
else {
|
|
8
|
-
return false;
|
|
9
|
-
}
|
|
10
|
-
}
|