page-transition 0.1.0-beta.32 → 0.1.0-beta.34

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.
@@ -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 { parseDelay } from "../functions/parseDelay";
14
+ import { parseDuration } from "../functions/parseDuration";
14
15
  import { pollTransitionStart } from "../functions/pollTransitionStart";
15
16
  export function CssTransitionProvider({ header, options, children }) {
16
17
  const defaultOptions = {
@@ -48,6 +49,7 @@ export function CssTransitionProvider({ header, options, children }) {
48
49
  }
49
50
  });
50
51
  const delayStep = useRef(null);
52
+ const duration = useRef(null);
51
53
  const handleLinkClick = useRef(() => { });
52
54
  const transitionEndHandler = useTransitionEndHandler();
53
55
  const transitionEndData = useTransitionEndData({
@@ -66,7 +68,6 @@ export function CssTransitionProvider({ header, options, children }) {
66
68
  transitionEndHandler,
67
69
  state,
68
70
  reverse,
69
- delayStep,
70
71
  useUse,
71
72
  resolveUseFn,
72
73
  pollRafId,
@@ -81,7 +82,6 @@ export function CssTransitionProvider({ header, options, children }) {
81
82
  transitionEndHandler,
82
83
  state,
83
84
  reverse,
84
- delayStep,
85
85
  useUse,
86
86
  resolveUseFn,
87
87
  basePath,
@@ -108,14 +108,18 @@ export function CssTransitionProvider({ header, options, children }) {
108
108
  mount.current.mounted = true;
109
109
  if (delayStep.current === null && defaultOptions.overlayCount > 1) {
110
110
  delayStep.current = parseDelay(transitionEndData.current.elArray);
111
+ transitionEndData.current.delayStep = delayStep.current ?? 0;
112
+ }
113
+ if (duration.current === null) {
114
+ duration.current = parseDuration(transitionEndData.current.elArray[0]);
115
+ transitionEndData.current.duration = duration.current ?? 0;
111
116
  }
112
117
  if (!container.current)
113
118
  return;
114
119
  transitionEnd({
115
120
  transitionEndObj: removeEnter,
116
121
  transitionEndHandler,
117
- reverse,
118
- delayStep
122
+ reverse
119
123
  });
120
124
  container.current.classList.remove('exit');
121
125
  container.current.classList.add('enter');
@@ -0,0 +1 @@
1
+ export declare function parseDuration(el: HTMLElement | null): number | null;
@@ -0,0 +1,6 @@
1
+ export function parseDuration(el) {
2
+ if (!el)
3
+ return null;
4
+ const duration = parseFloat(getComputedStyle(el).transitionDuration);
5
+ return duration ? duration : 250;
6
+ }
@@ -4,6 +4,8 @@ interface TransitionEndObj {
4
4
  transitionProperty: 'opacity' | 'clip-path';
5
5
  mirror: boolean;
6
6
  mirrorElArray: (HTMLDivElement | null)[];
7
+ delayStep: number;
8
+ duration: number;
7
9
  fn: (() => void) | null;
8
10
  }
9
11
  interface TransitionEndHandler {
@@ -17,7 +19,6 @@ interface Props {
17
19
  _lock: boolean;
18
20
  lock: boolean;
19
21
  }>;
20
- delayStep: RefObject<number | null>;
21
22
  }
22
- export declare function transitionEnd({ transitionEndObj, transitionEndHandler, reverse, delayStep }: Props): void;
23
+ export declare function transitionEnd({ transitionEndObj, transitionEndHandler, reverse, }: Props): void;
23
24
  export {};
@@ -1,5 +1,5 @@
1
1
  import { transtionEndReverse } from "./transitionEndReverse";
2
- export function transitionEnd({ transitionEndObj, transitionEndHandler, reverse, delayStep }) {
2
+ export function transitionEnd({ transitionEndObj, transitionEndHandler, reverse, }) {
3
3
  const lastIndex = transitionEndObj.current.elArray.length - 1;
4
4
  if (transitionEndHandler.current.handler &&
5
5
  transitionEndHandler.current.handlerEl) {
@@ -10,8 +10,7 @@ export function transitionEnd({ transitionEndObj, transitionEndHandler, reverse,
10
10
  transtionEndReverse({
11
11
  transitionEndObj,
12
12
  transitionEndHandler,
13
- reverse,
14
- delayStep
13
+ reverse
15
14
  });
16
15
  return;
17
16
  }
@@ -4,6 +4,8 @@ interface TransitionEndObj {
4
4
  transitionProperty: 'opacity' | 'clip-path';
5
5
  mirror: boolean;
6
6
  mirrorElArray: (HTMLDivElement | null)[];
7
+ delayStep: number;
8
+ duration: number;
7
9
  fn: (() => void) | null;
8
10
  }
9
11
  interface TransitionEndHandler {
@@ -17,7 +19,6 @@ interface Props {
17
19
  _lock: boolean;
18
20
  lock: boolean;
19
21
  }>;
20
- delayStep: RefObject<number | null>;
21
22
  }
22
- export declare function transtionEndReverse({ transitionEndObj, transitionEndHandler, reverse, delayStep }: Props): void;
23
+ export declare function transtionEndReverse({ transitionEndObj, transitionEndHandler, reverse }: Props): void;
23
24
  export {};
@@ -1,21 +1,20 @@
1
1
  import { setDelayReverse } from "./setDelayReverse";
2
2
  import { resetDelay } from "./resetDelay";
3
- export function transtionEndReverse({ transitionEndObj, transitionEndHandler, reverse, delayStep }) {
3
+ export function transtionEndReverse({ transitionEndObj, transitionEndHandler, reverse }) {
4
4
  if (transitionEndHandler.current.handler &&
5
5
  transitionEndHandler.current.handlerEl)
6
6
  transitionEndHandler.current.handlerEl.removeEventListener('transitionend', transitionEndHandler.current.handler);
7
7
  const endObj = transitionEndObj.current;
8
- console.log('transitionEndReverse setDelayReverse');
9
8
  setDelayReverse({
10
9
  elArray: endObj.elArray,
11
10
  transitionProperty: endObj.transitionProperty,
12
- delayStep: delayStep.current
11
+ delayStep: endObj.delayStep
13
12
  });
14
13
  if (endObj.mirror) {
15
14
  setDelayReverse({
16
15
  elArray: endObj.mirrorElArray,
17
16
  transitionProperty: endObj.transitionProperty,
18
- delayStep: delayStep.current
17
+ delayStep: endObj.delayStep
19
18
  });
20
19
  }
21
20
  const firstEl = endObj.elArray[0];
@@ -5,6 +5,7 @@ interface Props {
5
5
  elArray: (HTMLDivElement | null)[];
6
6
  transitionProperty: 'opacity' | 'clip-path';
7
7
  target: number;
8
+ duration: number;
8
9
  }
9
- export declare function updatePlaybackRate({ rafId, rejectFn, elArray, transitionProperty, target }: Props): Promise<void>;
10
+ export declare function updatePlaybackRate({ rafId, rejectFn, elArray, transitionProperty, target, duration }: Props): Promise<void>;
10
11
  export {};
@@ -1,16 +1,11 @@
1
- export function updatePlaybackRate({ rafId, rejectFn, elArray, transitionProperty, target }) {
1
+ export function updatePlaybackRate({ rafId, rejectFn, elArray, transitionProperty, target, duration }) {
2
2
  if (rafId.current !== null)
3
3
  cancelAnimationFrame(rafId.current);
4
- let duration = 0;
5
- if (elArray[0]) {
6
- duration = (parseFloat(getComputedStyle(elArray[0]).transitionDuration) * 1000) / 2;
7
- }
8
- if (duration <= 0) {
9
- console.warn('updatePlaybackRate: duration is 0, check if the element has a transition-duration set in CSS');
10
- duration = 300;
11
- }
4
+ if (duration === 0)
5
+ return Promise.resolve();
6
+ const d = (duration * 1000) / 2;
12
7
  let progress = 0;
13
- let summ = target ? 0 : duration;
8
+ let summ = target ? 0 : d;
14
9
  let animationArray = [];
15
10
  elArray.forEach((el) => {
16
11
  el?.getAnimations().forEach((obj) => {
@@ -30,9 +25,9 @@ export function updatePlaybackRate({ rafId, rejectFn, elArray, transitionPropert
30
25
  }
31
26
  const dt = now - before;
32
27
  before = now;
33
- summ = target ? Math.min(summ + dt, duration) : Math.max(summ - dt, 0);
34
- progress = summ / duration;
35
- animationArray.forEach((obj, i) => {
28
+ summ = target ? Math.min(summ + dt, d) : Math.max(summ - dt, 0);
29
+ progress = summ / d;
30
+ animationArray.forEach((obj) => {
36
31
  obj.updatePlaybackRate(progress);
37
32
  });
38
33
  if (target && progress >= 1 || !target && progress <= 0) {
@@ -4,6 +4,8 @@ interface TransitionEndData {
4
4
  transitionProperty: 'opacity' | 'clip-path';
5
5
  mirror: boolean;
6
6
  mirrorElArray: (HTMLDivElement | null)[];
7
+ delayStep: number;
8
+ duration: number;
7
9
  }
8
10
  interface TransitionEndHandler {
9
11
  handler: ((e: TransitionEvent) => void) | null;
@@ -20,7 +22,6 @@ interface Props {
20
22
  _lock: boolean;
21
23
  lock: boolean;
22
24
  }>;
23
- delayStep: RefObject<number | null>;
24
25
  useUse: RefObject<boolean>;
25
26
  resolveUseFn: RefObject<(() => void) | null>;
26
27
  basePath: RefObject<string | null>;
@@ -28,5 +29,5 @@ interface Props {
28
29
  rafId: RefObject<number | null>;
29
30
  rejectFn: RefObject<(() => void) | null>;
30
31
  }
31
- export declare function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delayStep, state, useUse, resolveUseFn, basePath, pollRafId, rafId, rejectFn }: Props): void;
32
+ export declare function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, state, useUse, resolveUseFn, basePath, pollRafId, rafId, rejectFn }: Props): void;
32
33
  export {};
@@ -6,7 +6,7 @@ import { transitionEnd } from "../functions/transitionEnd";
6
6
  import { pollTransitionStart } from "../functions/pollTransitionStart";
7
7
  import { updatePlaybackRate } from "../functions/updatePlaybackRate";
8
8
  import { setPlaybackRate } from "../functions/setPlaybackRate";
9
- export function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delayStep, state, useUse, resolveUseFn, basePath, pollRafId, rafId, rejectFn }) {
9
+ export function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, state, useUse, resolveUseFn, basePath, pollRafId, rafId, rejectFn }) {
10
10
  const router = useRouter();
11
11
  const afterExitFn = useRef(null);
12
12
  const nextPage = useRef(null);
@@ -36,8 +36,7 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
36
36
  transitionEnd({
37
37
  transitionEndObj: removeEnter,
38
38
  transitionEndHandler,
39
- reverse,
40
- delayStep
39
+ reverse
41
40
  });
42
41
  if (useUse.current) {
43
42
  resolveUseFn.current?.();
@@ -57,12 +56,27 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
57
56
  });
58
57
  }
59
58
  });
60
- const removeEnterAddExit = useTransitionEndObj({
59
+ const afterEntering = useTransitionEndObj({
61
60
  transitionEndData,
62
61
  fn: () => {
62
+ console.log('afterEntering transitionEnd');
63
+ if (rafId.current) {
64
+ cancelAnimationFrame(rafId.current);
65
+ rafId.current = null;
66
+ }
67
+ rejectFn.current?.();
63
68
  container.current?.classList.remove('enter');
64
- container.current?.classList.add('exit');
69
+ requestAnimationFrame(() => {
70
+ requestAnimationFrame(() => {
71
+ container.current?.classList.add('exit');
72
+ });
73
+ });
65
74
  state.current = 'exiting';
75
+ transitionEnd({
76
+ transitionEndObj: push,
77
+ transitionEndHandler,
78
+ reverse
79
+ });
66
80
  }
67
81
  });
68
82
  handleLinkClick.current = useCallback((e, url) => {
@@ -93,17 +107,15 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
93
107
  transitionEnd({
94
108
  transitionEndObj: afterExit,
95
109
  transitionEndHandler,
96
- reverse,
97
- delayStep
110
+ reverse
98
111
  });
99
112
  return;
100
113
  }
101
114
  if (state.current === 'entering') {
102
115
  transitionEnd({
103
- transitionEndObj: removeEnterAddExit,
116
+ transitionEndObj: afterEntering,
104
117
  transitionEndHandler,
105
- reverse,
106
- delayStep
118
+ reverse
107
119
  });
108
120
  let elArray = [];
109
121
  if (transitionEndData.current.mirror) {
@@ -117,13 +129,13 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
117
129
  rejectFn,
118
130
  elArray,
119
131
  transitionProperty: transitionEndData.current.transitionProperty,
120
- target: 0
132
+ target: 0,
133
+ duration: transitionEndData.current.duration
121
134
  }).then(() => {
122
135
  transitionEnd({
123
136
  transitionEndObj: push,
124
137
  transitionEndHandler,
125
- reverse,
126
- delayStep
138
+ reverse
127
139
  });
128
140
  container.current?.classList.remove('enter');
129
141
  container.current?.classList.add('exit');
@@ -138,7 +150,8 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
138
150
  rejectFn,
139
151
  elArray,
140
152
  transitionProperty: transitionEndData.current.transitionProperty,
141
- target: 1
153
+ target: 1,
154
+ duration: transitionEndData.current.duration
142
155
  }).catch(() => { });
143
156
  }).catch(() => { });
144
157
  return;
@@ -146,8 +159,7 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
146
159
  transitionEnd({
147
160
  transitionEndObj: push,
148
161
  transitionEndHandler,
149
- reverse,
150
- delayStep
162
+ reverse
151
163
  });
152
164
  container.current?.classList.remove('enter');
153
165
  container.current?.classList.add('exit');
@@ -4,6 +4,8 @@ interface TransitionEndData {
4
4
  transitionProperty: 'opacity' | 'clip-path';
5
5
  mirror: boolean;
6
6
  mirrorElArray: (HTMLDivElement | null)[];
7
+ delayStep: number;
8
+ duration: number;
7
9
  }
8
10
  interface TransitionEndHandler {
9
11
  handler: ((e: TransitionEvent) => void) | null;
@@ -18,7 +20,6 @@ interface Props {
18
20
  _lock: boolean;
19
21
  lock: boolean;
20
22
  }>;
21
- delayStep: RefObject<number | null>;
22
23
  state: RefObject<'idle' | 'exiting' | 'entering' | 'navigating'>;
23
24
  useUse: RefObject<boolean>;
24
25
  resolveUseFn: RefObject<(() => void) | null>;
@@ -26,5 +27,5 @@ interface Props {
26
27
  rafId: RefObject<number | null>;
27
28
  rejectFn: RefObject<(() => void) | null>;
28
29
  }
29
- export declare function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delayStep, state, useUse, resolveUseFn, pollRafId, rafId, rejectFn }: Props): void;
30
+ export declare function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, state, useUse, resolveUseFn, pollRafId, rafId, rejectFn }: Props): void;
30
31
  export {};
@@ -6,7 +6,7 @@ import { pollTransitionStart } from "../functions/pollTransitionStart";
6
6
  import { updatePlaybackRate } from "../functions/updatePlaybackRate";
7
7
  import { setPlaybackRate } from "../functions/setPlaybackRate";
8
8
  import { parseFirstOpacity } from "../functions/parseFirstOpacity";
9
- export function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delayStep, state, useUse, resolveUseFn, pollRafId, rafId, rejectFn }) {
9
+ export function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, state, useUse, resolveUseFn, pollRafId, rafId, rejectFn }) {
10
10
  const [promise, setPromise] = useState(null);
11
11
  if (promise) {
12
12
  useUse.current = true;
@@ -33,8 +33,7 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
33
33
  transitionEnd({
34
34
  transitionEndObj: removeEnter,
35
35
  transitionEndHandler,
36
- reverse,
37
- delayStep
36
+ reverse
38
37
  });
39
38
  state.current = 'navigating';
40
39
  container.current?.classList.remove('exit');
@@ -63,12 +62,27 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
63
62
  useUse.current = false;
64
63
  }
65
64
  });
66
- const removeEnterAddExit = useTransitionEndObj({
65
+ const afterEntering = useTransitionEndObj({
67
66
  transitionEndData,
68
67
  fn: () => {
68
+ console.log('afterEntering transitionEnd');
69
+ if (rafId.current) {
70
+ cancelAnimationFrame(rafId.current);
71
+ rafId.current = null;
72
+ }
73
+ rejectFn.current?.();
69
74
  container.current?.classList.remove('enter');
70
- container.current?.classList.add('exit');
75
+ requestAnimationFrame(() => {
76
+ requestAnimationFrame(() => {
77
+ container.current?.classList.add('exit');
78
+ });
79
+ });
71
80
  state.current = 'exiting';
81
+ transitionEnd({
82
+ transitionEndObj: pop,
83
+ transitionEndHandler,
84
+ reverse
85
+ });
72
86
  }
73
87
  });
74
88
  useEffect(() => {
@@ -100,25 +114,18 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
100
114
  transitionEnd({
101
115
  transitionEndObj: afterExit,
102
116
  transitionEndHandler,
103
- reverse,
104
- delayStep
117
+ reverse
105
118
  });
106
119
  return;
107
120
  }
108
121
  if (state.current === 'entering') {
109
122
  transitionEnd({
110
- transitionEndObj: removeEnterAddExit,
123
+ transitionEndObj: afterEntering,
111
124
  transitionEndHandler,
112
- reverse,
113
- delayStep
125
+ reverse
114
126
  });
115
- if (state.current !== 'entering') {
116
- console.log('state.current !== entering');
117
- console.log('state:', state.current);
118
- console.log('oben');
119
- return;
120
- }
121
- console.log('state.current === entering');
127
+ console.log('state:', state.current);
128
+ console.log('oben');
122
129
  let elArray = [];
123
130
  if (transitionEndData.current.mirror) {
124
131
  elArray = [...transitionEndData.current.mirrorElArray, ...transitionEndData.current.elArray];
@@ -131,22 +138,16 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
131
138
  rejectFn,
132
139
  elArray,
133
140
  transitionProperty: transitionEndData.current.transitionProperty,
134
- target: 0
141
+ target: 0,
142
+ duration: transitionEndData.current.duration
135
143
  }).then(() => {
136
- if (state.current !== 'entering') {
137
- console.log('state.current !== entering');
138
- console.log('state:', state.current);
139
- console.log('unten');
140
- return;
141
- }
142
- console.log('updatePlaybackRate done');
144
+ console.log('state:', state.current);
145
+ console.log('unten');
143
146
  transitionEnd({
144
- transitionEndObj: afterExit,
147
+ transitionEndObj: pop,
145
148
  transitionEndHandler,
146
- reverse,
147
- delayStep
149
+ reverse
148
150
  });
149
- console.log('transitionEnd done');
150
151
  container.current?.classList.remove('enter');
151
152
  container.current?.classList.add('exit');
152
153
  console.log('classList done');
@@ -162,7 +163,8 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
162
163
  rejectFn,
163
164
  elArray,
164
165
  transitionProperty: transitionEndData.current.transitionProperty,
165
- target: 1
166
+ target: 1,
167
+ duration: transitionEndData.current.duration
166
168
  }).catch(() => { console.log('updatePlaybackRate catch innen'); });
167
169
  }).catch(() => { console.log('updatePlaybackRate catch ausen'); });
168
170
  return;
@@ -170,8 +172,7 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
170
172
  transitionEnd({
171
173
  transitionEndObj: pop,
172
174
  transitionEndHandler,
173
- reverse,
174
- delayStep
175
+ reverse
175
176
  });
176
177
  container.current?.classList.remove('enter');
177
178
  container.current?.classList.add('exit');
@@ -4,6 +4,8 @@ interface TransitionEndData {
4
4
  transitionProperty: 'opacity' | 'clip-path';
5
5
  mirror: boolean;
6
6
  mirrorElArray: (HTMLDivElement | null)[];
7
+ delayStep: number;
8
+ duration: number;
7
9
  }
8
10
  interface Props {
9
11
  elArray: RefObject<HTMLDivElement | null | (HTMLDivElement | null)[]>;
@@ -5,7 +5,9 @@ export function useTransitionEndData({ elArray, transitionProperty, mirror, mirr
5
5
  elArray: [],
6
6
  transitionProperty,
7
7
  mirror,
8
- mirrorElArray: []
8
+ mirrorElArray: [],
9
+ delayStep: 0,
10
+ duration: 0
9
11
  });
10
12
  useEffect(() => {
11
13
  transitionEndData.current = {
@@ -4,6 +4,8 @@ interface TransitionEndData {
4
4
  transitionProperty: 'opacity' | 'clip-path';
5
5
  mirror: boolean;
6
6
  mirrorElArray: (HTMLDivElement | null)[];
7
+ delayStep: number;
8
+ duration: number;
7
9
  }
8
10
  interface Props {
9
11
  transitionEndData: RefObject<TransitionEndData>;
@@ -14,6 +16,8 @@ interface TransitionEndObj {
14
16
  transitionProperty: 'opacity' | 'clip-path';
15
17
  mirror: boolean;
16
18
  mirrorElArray: (HTMLDivElement | null)[];
19
+ delayStep: number;
20
+ duration: number;
17
21
  fn: (() => void) | null;
18
22
  }
19
23
  export declare function useTransitionEndObj({ transitionEndData, fn }: Props): RefObject<TransitionEndObj>;
@@ -6,6 +6,8 @@ export function useTransitionEndObj({ transitionEndData, fn }) {
6
6
  mirror: transitionEndData.current.mirror,
7
7
  mirrorElArray: [],
8
8
  fn: null,
9
+ delayStep: transitionEndData.current.delayStep,
10
+ duration: transitionEndData.current.duration
9
11
  });
10
12
  useEffect(() => {
11
13
  transitionEndObj.current = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "page-transition",
3
- "version": "0.1.0-beta.32",
3
+ "version": "0.1.0-beta.34",
4
4
  "description": "A page transition library for the Next.js App Router.",
5
5
  "license": "MIT",
6
6
  "files": [