page-transition 0.1.0-beta.22 → 0.1.0-beta.24

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.
@@ -57,6 +57,8 @@ export function CssTransitionProvider({ header, options, children }) {
57
57
  mirrorElArray: mirrorItems
58
58
  });
59
59
  const pollRafId = useRef(null);
60
+ const rafId = useRef(null);
61
+ const rejectFn = useRef(null);
60
62
  usePopState({
61
63
  container,
62
64
  currentPathname,
@@ -67,7 +69,9 @@ export function CssTransitionProvider({ header, options, children }) {
67
69
  delayStep,
68
70
  useUse,
69
71
  resolveUseFn,
70
- pollRafId
72
+ pollRafId,
73
+ rafId,
74
+ rejectFn
71
75
  });
72
76
  useHandleLinkClick({
73
77
  container,
@@ -81,7 +85,9 @@ export function CssTransitionProvider({ header, options, children }) {
81
85
  useUse,
82
86
  resolveUseFn,
83
87
  basePath,
84
- pollRafId
88
+ pollRafId,
89
+ rafId,
90
+ rejectFn
85
91
  });
86
92
  useScrollRestoration();
87
93
  const removeEnter = useTransitionEndObj({
@@ -1,6 +1,6 @@
1
1
  interface TransitionEndData {
2
2
  elArray: (HTMLDivElement | null)[];
3
- propertyName: 'opacity' | 'clip-path';
3
+ transitionProperty: 'opacity' | 'clip-path';
4
4
  }
5
5
  export declare function checkTransitionStart(transitionEndData: TransitionEndData, fn: (() => void) | null): boolean;
6
6
  export {};
@@ -5,7 +5,7 @@ export function checkTransitionStart(transitionEndData, fn) {
5
5
  'opacity': () => parseFirstOpacity(transitionEndData.elArray),
6
6
  'clip-path': () => parseFirstClipPath(transitionEndData.elArray)
7
7
  };
8
- if (parse[transitionEndData.propertyName]()) {
8
+ if (parse[transitionEndData.transitionProperty]()) {
9
9
  fn?.();
10
10
  return true;
11
11
  }
@@ -1,7 +1,7 @@
1
1
  import type { RefObject } from "react";
2
2
  interface TransitionEndData {
3
3
  elArray: (HTMLDivElement | null)[];
4
- propertyName: 'opacity' | 'clip-path';
4
+ transitionProperty: 'opacity' | 'clip-path';
5
5
  }
6
6
  interface Props {
7
7
  id: RefObject<number | null>;
@@ -0,0 +1,7 @@
1
+ interface Props {
2
+ elArray: (HTMLDivElement | null)[];
3
+ transitionProperty: 'opacity' | 'clip-path';
4
+ value: number;
5
+ }
6
+ export declare function setPlaybackRate({ elArray, transitionProperty, value }: Props): void;
7
+ export {};
@@ -0,0 +1,10 @@
1
+ export function setPlaybackRate({ elArray, transitionProperty, value }) {
2
+ elArray.forEach((el) => {
3
+ el?.getAnimations().forEach((obj) => {
4
+ if (obj instanceof CSSTransition &&
5
+ obj.transitionProperty === transitionProperty) {
6
+ obj.updatePlaybackRate(value);
7
+ }
8
+ });
9
+ });
10
+ }
@@ -1,7 +1,7 @@
1
1
  import type { RefObject } from "react";
2
2
  interface TransitionEndObj {
3
3
  elArray: (HTMLDivElement | null)[];
4
- propertyName: 'opacity' | 'clip-path';
4
+ transitionProperty: 'opacity' | 'clip-path';
5
5
  mirror: boolean;
6
6
  mirrorElArray: (HTMLDivElement | null)[];
7
7
  fn: (() => void) | null;
@@ -18,8 +18,8 @@ export function transitionEnd({ transitionEndObj, transitionEndHandler, reverse,
18
18
  const handler = (e) => {
19
19
  if (e.target !== e.currentTarget)
20
20
  return;
21
- if (endObj.propertyName &&
22
- e.propertyName !== endObj.propertyName)
21
+ if (endObj.transitionProperty &&
22
+ e.propertyName !== endObj.transitionProperty)
23
23
  return;
24
24
  endObj.elArray[lastIndex]?.removeEventListener('transitionend', handler);
25
25
  endObj.fn?.();
@@ -1,7 +1,7 @@
1
1
  import type { RefObject } from "react";
2
2
  interface TransitionEndObj {
3
3
  elArray: (HTMLDivElement | null)[];
4
- propertyName: 'opacity' | 'clip-path';
4
+ transitionProperty: 'opacity' | 'clip-path';
5
5
  mirror: boolean;
6
6
  mirrorElArray: (HTMLDivElement | null)[];
7
7
  fn: (() => void) | null;
@@ -7,19 +7,19 @@ export function transtionEndReverse({ transitionEndObj, transitionEndHandler, re
7
7
  const endObj = transitionEndObj.current;
8
8
  setDelayReverse({
9
9
  elArray: endObj.elArray,
10
- transitionProperty: endObj.propertyName,
10
+ transitionProperty: endObj.transitionProperty,
11
11
  delayStep: delayStep.current
12
12
  });
13
13
  if (endObj.mirror) {
14
14
  setDelayReverse({
15
15
  elArray: endObj.mirrorElArray,
16
- transitionProperty: endObj.propertyName,
16
+ transitionProperty: endObj.transitionProperty,
17
17
  delayStep: delayStep.current
18
18
  });
19
19
  }
20
20
  const firstEl = endObj.elArray[0];
21
21
  const handler = (e) => {
22
- if (e.propertyName !== endObj.propertyName ||
22
+ if (e.propertyName !== endObj.transitionProperty ||
23
23
  e.target !== e.currentTarget)
24
24
  return;
25
25
  resetDelay(endObj.elArray);
@@ -0,0 +1,10 @@
1
+ import type { RefObject } from "react";
2
+ interface Props {
3
+ rafId: RefObject<number | null>;
4
+ rejectFn: RefObject<(() => void) | null>;
5
+ elArray: (HTMLDivElement | null)[];
6
+ transitionProperty: 'opacity' | 'clip-path';
7
+ target: number;
8
+ }
9
+ export declare function updatePlaybackRate({ rafId, rejectFn, elArray, transitionProperty, target }: Props): Promise<void>;
10
+ export {};
@@ -0,0 +1,47 @@
1
+ export function updatePlaybackRate({ rafId, rejectFn, elArray, transitionProperty, target }) {
2
+ if (rafId.current !== null)
3
+ cancelAnimationFrame(rafId.current);
4
+ let duration = 0;
5
+ if (elArray[0]) {
6
+ duration = (parseFloat(getComputedStyle(elArray[0]).transitionDuration) * 1000) / 2;
7
+ }
8
+ let progress = 0;
9
+ let summ = target ? 0 : duration;
10
+ let animationArray = [];
11
+ elArray.forEach((el) => {
12
+ el?.getAnimations().forEach((obj) => {
13
+ if (obj instanceof CSSTransition &&
14
+ obj.transitionProperty === transitionProperty) {
15
+ animationArray.push(obj);
16
+ }
17
+ });
18
+ });
19
+ let before = null;
20
+ return new Promise((resolve, reject) => {
21
+ rejectFn.current?.();
22
+ rejectFn.current = reject;
23
+ const step = (now) => {
24
+ if (before === null) {
25
+ before = now;
26
+ }
27
+ const dt = now - before;
28
+ before = now;
29
+ summ = target ? Math.min(summ + dt, duration) : Math.max(summ - dt, 0);
30
+ progress = summ / duration;
31
+ animationArray.forEach((obj, i) => {
32
+ obj.updatePlaybackRate(progress);
33
+ });
34
+ if (target && progress >= 1 || !target && progress <= 0) {
35
+ if (rafId.current !== null) {
36
+ cancelAnimationFrame(rafId.current);
37
+ rafId.current = null;
38
+ }
39
+ rejectFn.current = null;
40
+ resolve();
41
+ return;
42
+ }
43
+ rafId.current = requestAnimationFrame(step);
44
+ };
45
+ rafId.current = requestAnimationFrame(step);
46
+ });
47
+ }
@@ -1,7 +1,7 @@
1
1
  import type { RefObject } from "react";
2
2
  interface TransitionEndData {
3
3
  elArray: (HTMLDivElement | null)[];
4
- propertyName: 'opacity' | 'clip-path';
4
+ transitionProperty: 'opacity' | 'clip-path';
5
5
  mirror: boolean;
6
6
  mirrorElArray: (HTMLDivElement | null)[];
7
7
  }
@@ -25,6 +25,8 @@ interface Props {
25
25
  resolveUseFn: RefObject<(() => void) | null>;
26
26
  basePath: RefObject<string | null>;
27
27
  pollRafId: RefObject<number | null>;
28
+ rafId: RefObject<number | null>;
29
+ rejectFn: RefObject<(() => void) | null>;
28
30
  }
29
- export declare function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delayStep, state, useUse, resolveUseFn, basePath, pollRafId }: Props): void;
31
+ export declare function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delayStep, state, useUse, resolveUseFn, basePath, pollRafId, rafId, rejectFn }: Props): void;
30
32
  export {};
@@ -4,7 +4,9 @@ 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, reverse, delayStep, state, useUse, resolveUseFn, basePath, pollRafId }) {
7
+ import { updatePlaybackRate } from "../functions/updatePlaybackRate";
8
+ import { setPlaybackRate } from "../functions/setPlaybackRate";
9
+ export function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delayStep, state, useUse, resolveUseFn, basePath, pollRafId, rafId, rejectFn }) {
8
10
  const router = useRouter();
9
11
  const afterExitFn = useRef(null);
10
12
  const nextPage = useRef(null);
@@ -88,6 +90,45 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
88
90
  });
89
91
  return;
90
92
  }
93
+ if (state.current === 'entering') {
94
+ let elArray = [];
95
+ if (transitionEndData.current.mirror) {
96
+ elArray = [...transitionEndData.current.mirrorElArray, ...transitionEndData.current.elArray];
97
+ }
98
+ else {
99
+ elArray = [...transitionEndData.current.elArray];
100
+ }
101
+ updatePlaybackRate({
102
+ rafId,
103
+ rejectFn,
104
+ elArray,
105
+ transitionProperty: transitionEndData.current.transitionProperty,
106
+ target: 0
107
+ }).then(() => {
108
+ transitionEnd({
109
+ transitionEndObj: push,
110
+ transitionEndHandler,
111
+ reverse,
112
+ delayStep
113
+ });
114
+ container.current?.classList.remove('enter');
115
+ container.current?.classList.add('exit');
116
+ state.current = 'exiting';
117
+ setPlaybackRate({
118
+ elArray,
119
+ transitionProperty: transitionEndData.current.transitionProperty,
120
+ value: 0
121
+ });
122
+ updatePlaybackRate({
123
+ rafId,
124
+ rejectFn,
125
+ elArray,
126
+ transitionProperty: transitionEndData.current.transitionProperty,
127
+ target: 1
128
+ }).catch(() => { });
129
+ }).catch(() => { });
130
+ return;
131
+ }
91
132
  transitionEnd({
92
133
  transitionEndObj: push,
93
134
  transitionEndHandler,
@@ -1,7 +1,7 @@
1
1
  import type { RefObject } from "react";
2
2
  interface TransitionEndData {
3
3
  elArray: (HTMLDivElement | null)[];
4
- propertyName: 'opacity' | 'clip-path';
4
+ transitionProperty: 'opacity' | 'clip-path';
5
5
  mirror: boolean;
6
6
  mirrorElArray: (HTMLDivElement | null)[];
7
7
  }
@@ -23,6 +23,8 @@ interface Props {
23
23
  useUse: RefObject<boolean>;
24
24
  resolveUseFn: RefObject<(() => void) | null>;
25
25
  pollRafId: RefObject<number | null>;
26
+ rafId: RefObject<number | null>;
27
+ rejectFn: RefObject<(() => void) | null>;
26
28
  }
27
- export declare function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delayStep, state, useUse, resolveUseFn, pollRafId }: Props): void;
29
+ export declare function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delayStep, state, useUse, resolveUseFn, pollRafId, rafId, rejectFn }: Props): void;
28
30
  export {};
@@ -3,7 +3,9 @@ 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, reverse, delayStep, state, useUse, resolveUseFn, pollRafId }) {
6
+ import { updatePlaybackRate } from "../functions/updatePlaybackRate";
7
+ import { setPlaybackRate } from "../functions/setPlaybackRate";
8
+ export function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delayStep, state, useUse, resolveUseFn, pollRafId, rafId, rejectFn }) {
7
9
  const [promise, setPromise] = useState(null);
8
10
  if (promise) {
9
11
  useUse.current = true;
@@ -86,6 +88,45 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
86
88
  });
87
89
  return;
88
90
  }
91
+ if (state.current === 'entering') {
92
+ let elArray = [];
93
+ if (transitionEndData.current.mirror) {
94
+ elArray = [...transitionEndData.current.mirrorElArray, ...transitionEndData.current.elArray];
95
+ }
96
+ else {
97
+ elArray = [...transitionEndData.current.elArray];
98
+ }
99
+ updatePlaybackRate({
100
+ rafId,
101
+ rejectFn,
102
+ elArray,
103
+ transitionProperty: transitionEndData.current.transitionProperty,
104
+ target: 0
105
+ }).then(() => {
106
+ transitionEnd({
107
+ transitionEndObj: pop,
108
+ transitionEndHandler,
109
+ reverse,
110
+ delayStep
111
+ });
112
+ container.current?.classList.remove('enter');
113
+ container.current?.classList.add('exit');
114
+ state.current = 'exiting';
115
+ setPlaybackRate({
116
+ elArray,
117
+ transitionProperty: transitionEndData.current.transitionProperty,
118
+ value: 0
119
+ });
120
+ updatePlaybackRate({
121
+ rafId,
122
+ rejectFn,
123
+ elArray,
124
+ transitionProperty: transitionEndData.current.transitionProperty,
125
+ target: 1
126
+ }).catch(() => { });
127
+ }).catch(() => { });
128
+ return;
129
+ }
89
130
  transitionEnd({
90
131
  transitionEndObj: pop,
91
132
  transitionEndHandler,
@@ -1,7 +1,7 @@
1
1
  import type { RefObject } from "react";
2
2
  interface TransitionEndData {
3
3
  elArray: (HTMLDivElement | null)[];
4
- propertyName: 'opacity' | 'clip-path';
4
+ transitionProperty: 'opacity' | 'clip-path';
5
5
  mirror: boolean;
6
6
  mirrorElArray: (HTMLDivElement | null)[];
7
7
  }
@@ -3,7 +3,7 @@ import { useEffect, useRef } from "react";
3
3
  export function useTransitionEndData({ elArray, transitionProperty, mirror, mirrorElArray }) {
4
4
  const transitionEndData = useRef({
5
5
  elArray: [],
6
- propertyName: transitionProperty,
6
+ transitionProperty,
7
7
  mirror,
8
8
  mirrorElArray: []
9
9
  });
@@ -1,7 +1,7 @@
1
1
  import type { RefObject } from "react";
2
2
  interface TransitionEndData {
3
3
  elArray: (HTMLDivElement | null)[];
4
- propertyName: 'opacity' | 'clip-path';
4
+ transitionProperty: 'opacity' | 'clip-path';
5
5
  mirror: boolean;
6
6
  mirrorElArray: (HTMLDivElement | null)[];
7
7
  }
@@ -11,7 +11,7 @@ interface Props {
11
11
  }
12
12
  interface TransitionEndObj {
13
13
  elArray: (HTMLDivElement | null)[];
14
- propertyName: 'opacity' | 'clip-path';
14
+ transitionProperty: 'opacity' | 'clip-path';
15
15
  mirror: boolean;
16
16
  mirrorElArray: (HTMLDivElement | null)[];
17
17
  fn: (() => void) | null;
@@ -2,7 +2,7 @@ import { useRef, useEffect } from "react";
2
2
  export function useTransitionEndObj({ transitionEndData, fn }) {
3
3
  const transitionEndObj = useRef({
4
4
  elArray: [],
5
- propertyName: transitionEndData.current.propertyName,
5
+ transitionProperty: transitionEndData.current.transitionProperty,
6
6
  mirror: transitionEndData.current.mirror,
7
7
  mirrorElArray: [],
8
8
  fn: null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "page-transition",
3
- "version": "0.1.0-beta.22",
3
+ "version": "0.1.0-beta.24",
4
4
  "description": "A page transition library for the Next.js App Router.",
5
5
  "license": "MIT",
6
6
  "files": [