page-transition 0.1.1 → 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 CHANGED
@@ -10,9 +10,9 @@ 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-000000) ![82.5 KB](https://img.shields.io/badge/82.5_KB-1677FF)
13
+ ![Version](https://img.shields.io/badge/v0.2.0-000000) ![86.1 KB](https://img.shields.io/badge/86.1_KB-1677FF)
14
14
 
15
- `npm install page-transition@beta`
15
+ `npm install page-transition`
16
16
 
17
17
  ## Usage
18
18
  Import your page transition styles and wrap your page content with `TransitionProvider` in your root layout.
@@ -197,8 +197,8 @@ overlays: 1
197
197
  background: white;
198
198
 
199
199
  opacity: 0;
200
- pointer-events: none;
201
200
  will-change: opacity;
201
+ pointer-events: none;
202
202
  }
203
203
  [data-transition].exit .overlay{
204
204
  opacity: 1;
@@ -232,7 +232,6 @@ overlays: 2
232
232
 
233
233
  opacity: 0;
234
234
  pointer-events: none;
235
- will-change: opacity;
236
235
  }
237
236
  [data-transition].exit .overlay:nth-child(1){
238
237
  opacity: 1;
@@ -251,8 +250,9 @@ overlays: 2
251
250
 
252
251
  background: black;
253
252
  transition: none;
254
- will-change: clip-path;
255
253
  clip-path: inset(100% 0 0 0);
254
+ will-change: clip-path;
255
+ pointer-events: none;
256
256
  }
257
257
  [data-transition].exit .overlay:nth-child(2){
258
258
  clip-path: inset(0 0 0 0);
@@ -282,6 +282,7 @@ items: [10, 0]
282
282
 
283
283
  display: grid;
284
284
  grid-template-columns: repeat(10, 1fr);
285
+ pointer-events: none;
285
286
  }
286
287
  [data-transition] .first-item{
287
288
  background: black;
@@ -328,6 +329,7 @@ items: [10, 10]
328
329
  display: grid;
329
330
  grid-template-columns: repeat(10, 1fr);
330
331
  grid-template-rows: repeat(2, 1fr);
332
+ pointer-events: none;
331
333
  }
332
334
  [data-transition] .first-item{
333
335
  grid-row: 1;
@@ -398,7 +400,9 @@ In manual mode, you can control the transition using `useTransitionContext`.
398
400
 
399
401
  After triggering the navigation, `state.current` is set to `exiting`. You can then call `resolveFn.current()` to proceed with the navigation.
400
402
 
401
- Before navigating, you should call `window.scrollTo(0, window.history.state.scrollY ?? 0)` to restore the previous scroll position.
403
+ Before navigating, you should call
404
+ `window.scrollTo(0, window.history.state.scrollY ?? 0)`
405
+ to restore the previous scroll position, or scroll to the top.
402
406
 
403
407
  Once `state.current` is set to `entering`, the navigation has occurred. After the animation finishes, `state.current` should be set to `idle`.
404
408
 
@@ -409,3 +413,20 @@ import { useTransitionContext } from "page-transition/manual"
409
413
  const { resolveFn, state } = useTransitionContext()
410
414
 
411
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
+
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,
@@ -36,9 +37,7 @@ export function CssTransitionProvider({ outside, scroll, options, children }) {
36
37
  const resolveUseFn = useRef(null);
37
38
  const handleLinkClick = useRef(() => { });
38
39
  const transitionEndHandler = useTransitionEndHandler();
39
- const elArray = defaultOptions.items[0] === 0 &&
40
- defaultOptions.overlays === 0 ?
41
- pageWrapper : defaultOptions.items[0] > 0 ? firstItems :
40
+ const elArray = defaultOptions.items[0] > 0 ? firstItems :
42
41
  defaultOptions.overlays > 0 ? overlays : pageWrapper;
43
42
  group.current = defaultOptions.overlays > 0 && defaultOptions.items[0] === 0 ?
44
43
  'overlay' : null;
@@ -51,6 +50,7 @@ export function CssTransitionProvider({ outside, scroll, options, children }) {
51
50
  usePopState({
52
51
  container,
53
52
  currentPathname,
53
+ basePath,
54
54
  transitionEndData,
55
55
  transitionEndHandler,
56
56
  state,
@@ -62,12 +62,12 @@ export function CssTransitionProvider({ outside, scroll, options, children }) {
62
62
  container,
63
63
  handleLinkClick,
64
64
  currentPathname,
65
+ basePath,
65
66
  transitionEndData,
66
67
  transitionEndHandler,
67
68
  state,
68
69
  useUse,
69
70
  resolveUseFn,
70
- basePath,
71
71
  pollRafId
72
72
  });
73
73
  useScrollRestoration({ scroll });
@@ -81,6 +81,7 @@ export function CssTransitionProvider({ outside, scroll, options, children }) {
81
81
  useEffect(() => {
82
82
  if (mount.current.mounted === false &&
83
83
  mount.current.initPathname === pathname) {
84
+ dispatchNextPathname(pathname);
84
85
  currentPathname.current = window.location.pathname;
85
86
  basePath.current = window.location.pathname.slice(0, -pathname.length);
86
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(() => { });
@@ -15,10 +16,15 @@ export function ManualTransitionProvider({ scroll, children }) {
15
16
  const basePath = useRef(null);
16
17
  const useUse = useRef(false);
17
18
  const resolveUseFn = useRef(null);
19
+ const mount = useRef({
20
+ mounted: false,
21
+ initPathname: pathname
22
+ });
18
23
  usePopState({
19
24
  resolveFn,
20
25
  state,
21
26
  currentPathname,
27
+ basePath,
22
28
  useUse,
23
29
  resolveUseFn
24
30
  });
@@ -33,9 +39,14 @@ export function ManualTransitionProvider({ scroll, children }) {
33
39
  });
34
40
  useScrollRestoration({ scroll });
35
41
  useEffect(() => {
36
- if (basePath.current === null) {
42
+ if (mount.current.mounted === false &&
43
+ mount.current.initPathname === pathname) {
44
+ dispatchNextPathname(pathname);
45
+ currentPathname.current = window.location.pathname;
37
46
  basePath.current = window.location.pathname.slice(0, -pathname.length);
47
+ return;
38
48
  }
49
+ mount.current.mounted = true;
39
50
  state.current = 'entering';
40
51
  currentPathname.current = window.location.pathname;
41
52
  }, [pathname]);
@@ -2,9 +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
- _jsx(ManualTransitionProvider, { scroll: scroll, children: children }) :
9
- _jsx(CssTransitionProvider, { scroll: scroll, outside: props.outside, options: props.options, children: children }));
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 }) }));
10
12
  }
@@ -1,9 +1,10 @@
1
- import { checkTransitionStart } from "./checkTransitionStart";
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 (checkTransitionStart(transitionEndData.current, fn)) {
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,26 +1,33 @@
1
1
  "use client";
2
- import { useEffect, useState, use } from "react";
3
- export function usePopState({ resolveFn, state, currentPathname, useUse, resolveUseFn }) {
4
- const [promise, setPromise] = useState(null);
5
- if (promise) {
2
+ import { useEffect, useState, useRef, use } from "react";
3
+ import { dispatchNextPathname } from "../../context/ActiveLinkContext";
4
+ export function usePopState({ resolveFn, state, currentPathname, basePath, useUse, resolveUseFn }) {
5
+ const [_, rerender] = useState(0);
6
+ const promise = useRef(null);
7
+ if (promise.current) {
6
8
  useUse.current = true;
7
- use(promise);
9
+ use(promise.current);
8
10
  }
9
11
  else {
10
12
  useUse.current = false;
11
13
  }
12
14
  useEffect(() => {
13
15
  const handlePopstate = () => {
14
- setPromise(new Promise((resolve, reject) => {
16
+ const nextPathname = window.location.pathname.slice(basePath.current?.length ?? 0);
17
+ dispatchNextPathname(nextPathname);
18
+ promise.current = new Promise((resolve, reject) => {
15
19
  resolveFn.current = resolve;
16
20
  resolveUseFn.current = reject;
17
21
  state.current = 'exiting';
18
22
  }).then(() => {
19
- setPromise(null);
23
+ promise.current = null;
20
24
  if (window.location.pathname === currentPathname.current) {
21
25
  state.current = 'entering';
22
26
  }
23
- }).catch(() => setPromise(null)));
27
+ }).catch(() => {
28
+ promise.current = null;
29
+ });
30
+ rerender((prev) => ++prev);
24
31
  };
25
32
  window.addEventListener('popstate', handlePopstate);
26
33
  return () => window.removeEventListener('popstate', handlePopstate);
@@ -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, basePath, pollRafId }: Props): void;
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
- export function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, state, useUse, resolveUseFn, basePath, pollRafId }) {
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,11 +3,13 @@ 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
- export function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, state, useUse, resolveUseFn, pollRafId }) {
7
- const [promise, setPromise] = useState(null);
8
- if (promise) {
6
+ import { dispatchNextPathname } from "../context/ActiveLinkContext";
7
+ export function usePopState({ container, currentPathname, basePath, transitionEndData, transitionEndHandler, state, useUse, resolveUseFn, pollRafId }) {
8
+ const [_, rerender] = useState(0);
9
+ const promise = useRef(null);
10
+ if (promise.current) {
9
11
  useUse.current = true;
10
- use(promise);
12
+ use(promise.current);
11
13
  }
12
14
  else {
13
15
  useUse.current = false;
@@ -32,7 +34,7 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
32
34
  container.current?.classList.remove('exit');
33
35
  container.current?.classList.add('enter');
34
36
  resolveFn.current?.();
35
- setPromise(null);
37
+ promise.current = null;
36
38
  useUse.current = false;
37
39
  pollTransitionStart({
38
40
  id: pollRafId,
@@ -49,7 +51,7 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
49
51
  state.current = 'navigating';
50
52
  window.scrollTo(0, window.history.state.scrollY ?? 0);
51
53
  resolveFn.current?.();
52
- setPromise(null);
54
+ promise.current = null;
53
55
  useUse.current = false;
54
56
  }
55
57
  });
@@ -68,7 +70,9 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
68
70
  });
69
71
  useEffect(() => {
70
72
  const handlePopstate = () => {
71
- setPromise(new Promise((resolve, reject) => {
73
+ const nextPathname = window.location.pathname.slice(basePath.current?.length ?? 0);
74
+ dispatchNextPathname(nextPathname);
75
+ promise.current = new Promise((resolve, reject) => {
72
76
  resolveFn.current = resolve;
73
77
  resolveUseFn.current = reject;
74
78
  new Promise((resolveNavigating) => {
@@ -76,7 +80,7 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
76
80
  if (currentPathname.current !== window.location.pathname) {
77
81
  state.current = 'navigating';
78
82
  resolveFn.current?.();
79
- setPromise(null);
83
+ promise.current = null;
80
84
  return;
81
85
  }
82
86
  else if (currentPathname.current === window.location.pathname) {
@@ -119,7 +123,10 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
119
123
  });
120
124
  }
121
125
  });
122
- }).catch(() => setPromise(null)));
126
+ }).catch(() => {
127
+ promise.current = null;
128
+ });
129
+ rerender((prev) => ++prev);
123
130
  };
124
131
  window.addEventListener('popstate', handlePopstate);
125
132
  return () => window.removeEventListener('popstate', handlePopstate);
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export { TransitionProvider } from "./context/TransitionProvider";
2
+ export { useActiveLinkContext as useActiveLink } from "./context/ActiveLinkContext";
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  "use client";
2
2
  export { TransitionProvider } from "./context/TransitionProvider";
3
+ export { useActiveLinkContext as useActiveLink } from "./context/ActiveLinkContext";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "page-transition",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "A page transition library for the Next.js App Router.",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -1,2 +0,0 @@
1
- import type { TransitionEndData } from "../types/transitionEnd.types";
2
- export declare function checkTransitionStart(transitionEndData: TransitionEndData, fn: (() => void) | null): boolean;
@@ -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
- }