page-transition 0.1.0-beta.19 → 0.1.0-beta.20

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
@@ -1,8 +1,9 @@
1
1
  # page-transition
2
2
  A page transition library for the Next.js App Router.
3
3
 
4
- [🌐 Live Demo](https://simon-schindler.github.io/page-transition-demo/)
5
-
4
+ [![Live Demo ↗](https://img.shields.io/badge/Live_Demo_%E2%86%97-000000?style=for-the-badge)](https://simon-schindler.github.io/page-transition-demo/)
5
+
6
+
6
7
  ## Features
7
8
 
8
9
  - CSS & Manual mode
@@ -62,7 +62,8 @@ export function CssTransitionProvider({ header, options, children }) {
62
62
  reverse,
63
63
  delay,
64
64
  useUse,
65
- resolveUseFn
65
+ resolveUseFn,
66
+ pollRafId
66
67
  });
67
68
  useHandleLinkClick({
68
69
  container,
@@ -75,7 +76,8 @@ export function CssTransitionProvider({ header, options, children }) {
75
76
  delay,
76
77
  useUse,
77
78
  resolveUseFn,
78
- basePath
79
+ basePath,
80
+ pollRafId
79
81
  });
80
82
  useScrollRestoration();
81
83
  const removeEnter = useTransitionEndObj({
@@ -113,8 +115,7 @@ export function CssTransitionProvider({ header, options, children }) {
113
115
  state.current = 'entering';
114
116
  }
115
117
  });
116
- if (state.current === 'navigating')
117
- currentPathname.current = window.location.pathname;
118
+ currentPathname.current = window.location.pathname;
118
119
  reverse.current.lock = false;
119
120
  }, [pathname]);
120
121
  const value = useMemo(() => ({ handleLinkClick, overlayWrapper, overlayItems }), []);
@@ -1,15 +1,16 @@
1
- import { parseOpacityAll } from "./parseOpacityAll";
2
- import { parseClipPathAll } from "./parseClipPathAll";
1
+ import { parseFirstOpacity } from "./parseFirstOpacity";
2
+ import { parseFirstClipPath } from "./parseFirstClipPath";
3
3
  export function checkTransitionStart(transitionEndData, fn) {
4
+ console.log('checkTransitionStart run');
4
5
  const parse = {
5
- 'opacity': () => parseOpacityAll(transitionEndData.elArray),
6
- 'clip-path': () => parseClipPathAll(transitionEndData.elArray)
6
+ 'opacity': () => parseFirstOpacity(transitionEndData.elArray),
7
+ 'clip-path': () => parseFirstClipPath(transitionEndData.elArray)
7
8
  };
8
9
  if (parse[transitionEndData.propertyName]()) {
9
- return false;
10
- }
11
- else {
12
10
  fn?.();
13
11
  return true;
14
12
  }
13
+ else {
14
+ return false;
15
+ }
15
16
  }
@@ -0,0 +1 @@
1
+ export declare function parseFirstClipPath(elArray: (HTMLDivElement | null)[]): null | boolean;
@@ -0,0 +1,12 @@
1
+ export function parseFirstClipPath(elArray) {
2
+ if (!elArray[0])
3
+ return null;
4
+ const clipPath = getComputedStyle(elArray[0]).clipPath;
5
+ const m = clipPath.match(/inset\(([^)]+)\)/) ?? null;
6
+ const arr = m?.[1].split(/\s+/) ?? [];
7
+ for (const string of arr) {
8
+ if (parseFloat(string) < 100)
9
+ return true;
10
+ }
11
+ return false;
12
+ }
@@ -0,0 +1 @@
1
+ export declare function parseFirstOpacity(elArray: (HTMLDivElement | null)[]): null | boolean;
@@ -0,0 +1,8 @@
1
+ export function parseFirstOpacity(elArray) {
2
+ if (!elArray[0])
3
+ return null;
4
+ const opacity = parseFloat(getComputedStyle(elArray[0]).opacity);
5
+ if (opacity > 0)
6
+ return true;
7
+ return false;
8
+ }
@@ -1,5 +1,7 @@
1
1
  import { checkTransitionStart } from "./checkTransitionStart";
2
2
  export function pollTransitionStart({ id, fn, transitionEndData }) {
3
+ if (id.current !== null)
4
+ cancelAnimationFrame(id.current);
3
5
  id.current = requestAnimationFrame(() => {
4
6
  if (checkTransitionStart(transitionEndData.current, fn)) {
5
7
  return;
@@ -22,6 +22,7 @@ interface Props {
22
22
  useUse: RefObject<boolean>;
23
23
  resolveUseFn: RefObject<(() => void) | null>;
24
24
  basePath: RefObject<string | null>;
25
+ pollRafId: RefObject<number | null>;
25
26
  }
26
- export declare function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn, basePath }: Props): void;
27
+ export declare function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn, basePath, pollRafId }: Props): void;
27
28
  export {};
@@ -3,8 +3,8 @@ import { useCallback, useRef } from "react";
3
3
  import { useRouter } from "next/navigation";
4
4
  import { useTransitionEndObj } from "../hooks/useTransitionEndObj";
5
5
  import { transitionEnd } from "../functions/transitionEnd";
6
- import { checkTransitionFinish } from "../functions/checkTransitionFinish";
7
- export function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn, basePath }) {
6
+ import { pollTransitionStart } from "../functions/pollTransitionStart";
7
+ export function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn, basePath, pollRafId }) {
8
8
  const router = useRouter();
9
9
  const afterExitFn = useRef(null);
10
10
  const nextPage = useRef(null);
@@ -42,10 +42,17 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
42
42
  state.current = 'navigating';
43
43
  router.push(currentPathname.current);
44
44
  }
45
+ state.current = 'navigating';
45
46
  container.current?.classList.remove('exit');
46
47
  container.current?.classList.add('enter');
47
- state.current = 'entering';
48
- reverse.current.lock = false;
48
+ pollTransitionStart({
49
+ id: pollRafId,
50
+ transitionEndData,
51
+ fn: () => {
52
+ state.current = 'entering';
53
+ reverse.current.lock = false;
54
+ }
55
+ });
49
56
  }
50
57
  });
51
58
  handleLinkClick.current = useCallback((e, url) => {
@@ -54,20 +61,16 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
54
61
  const basePathUrl = basePath.current ? basePath.current + url : url;
55
62
  new Promise((resolve) => {
56
63
  if (state.current === 'navigating') {
57
- console.log('state.current === navigating');
58
64
  if (basePathUrl === currentPathname.current) {
59
65
  return;
60
66
  }
61
67
  else if (basePathUrl !== currentPathname.current) {
62
- if (checkTransitionFinish(transitionEndData.current, () => {
63
- if (!nextPage.current)
64
- return;
65
- state.current = 'navigating';
66
- router.push(nextPage.current);
67
- nextPage.current = null;
68
- }))
68
+ if (!nextPage.current)
69
69
  return;
70
- resolve();
70
+ state.current = 'navigating';
71
+ router.push(nextPage.current);
72
+ nextPage.current = null;
73
+ return;
71
74
  }
72
75
  }
73
76
  else {
@@ -20,6 +20,7 @@ interface Props {
20
20
  state: RefObject<'idle' | 'exiting' | 'entering' | 'navigating'>;
21
21
  useUse: RefObject<boolean>;
22
22
  resolveUseFn: RefObject<(() => void) | null>;
23
+ pollRafId: RefObject<number | null>;
23
24
  }
24
- export declare function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn }: Props): void;
25
+ export declare function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn, pollRafId }: Props): void;
25
26
  export {};
@@ -2,8 +2,8 @@
2
2
  import { useEffect, useRef, useState, use } from "react";
3
3
  import { useTransitionEndObj } from "./useTransitionEndObj";
4
4
  import { transitionEnd } from "../functions/transitionEnd";
5
- import { checkTransitionFinish } from "../functions/checkTransitionFinish";
6
- export function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn }) {
5
+ import { pollTransitionStart } from "../functions/pollTransitionStart";
6
+ export function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn, pollRafId }) {
7
7
  const [promise, setPromise] = useState(null);
8
8
  if (promise) {
9
9
  useUse.current = true;
@@ -31,13 +31,20 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
31
31
  reverse,
32
32
  delay
33
33
  });
34
+ state.current = 'navigating';
34
35
  container.current?.classList.remove('exit');
35
36
  container.current?.classList.add('enter');
36
- state.current = 'entering';
37
- reverse.current.lock = false;
38
37
  resolveFn.current?.();
39
38
  setPromise(null);
40
39
  useUse.current = false;
40
+ pollTransitionStart({
41
+ id: pollRafId,
42
+ transitionEndData,
43
+ fn: () => {
44
+ state.current = 'entering';
45
+ reverse.current.lock = false;
46
+ }
47
+ });
41
48
  }
42
49
  });
43
50
  const pop = useTransitionEndObj({
@@ -46,7 +53,6 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
46
53
  state.current = 'navigating';
47
54
  window.scrollTo(0, window.history.state.scrollY ?? 0);
48
55
  resolveFn.current?.();
49
- console.log('pop transitionEnd called after resolveFn');
50
56
  setPromise(null);
51
57
  useUse.current = false;
52
58
  }
@@ -59,19 +65,12 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
59
65
  new Promise((resolveNavigating) => {
60
66
  if (state.current === "navigating") {
61
67
  if (currentPathname.current !== window.location.pathname) {
62
- if (checkTransitionFinish(transitionEndData.current, () => {
63
- resolveFn.current?.();
64
- setPromise(null);
65
- }))
66
- return;
68
+ resolveFn.current?.();
69
+ setPromise(null);
70
+ return;
67
71
  }
68
72
  else if (currentPathname.current === window.location.pathname) {
69
- if (checkTransitionFinish(transitionEndData.current, afterExitFn.current)) {
70
- return;
71
- }
72
- else {
73
- resolveNavigating();
74
- }
73
+ afterExitFn.current?.();
75
74
  }
76
75
  }
77
76
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "page-transition",
3
- "version": "0.1.0-beta.19",
3
+ "version": "0.1.0-beta.20",
4
4
  "description": "A page transition library for the Next.js App Router.",
5
5
  "license": "MIT",
6
6
  "files": [