page-transition 0.1.0-beta.33 → 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,10 +56,27 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
57
56
  });
58
57
  }
59
58
  });
60
- const bug = useTransitionEndObj({
59
+ const afterEntering = useTransitionEndObj({
61
60
  transitionEndData,
62
61
  fn: () => {
63
- console.log('bug');
62
+ console.log('afterEntering transitionEnd');
63
+ if (rafId.current) {
64
+ cancelAnimationFrame(rafId.current);
65
+ rafId.current = null;
66
+ }
67
+ rejectFn.current?.();
68
+ container.current?.classList.remove('enter');
69
+ requestAnimationFrame(() => {
70
+ requestAnimationFrame(() => {
71
+ container.current?.classList.add('exit');
72
+ });
73
+ });
74
+ state.current = 'exiting';
75
+ transitionEnd({
76
+ transitionEndObj: push,
77
+ transitionEndHandler,
78
+ reverse
79
+ });
64
80
  }
65
81
  });
66
82
  handleLinkClick.current = useCallback((e, url) => {
@@ -91,17 +107,15 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
91
107
  transitionEnd({
92
108
  transitionEndObj: afterExit,
93
109
  transitionEndHandler,
94
- reverse,
95
- delayStep
110
+ reverse
96
111
  });
97
112
  return;
98
113
  }
99
114
  if (state.current === 'entering') {
100
115
  transitionEnd({
101
- transitionEndObj: bug,
116
+ transitionEndObj: afterEntering,
102
117
  transitionEndHandler,
103
- reverse,
104
- delayStep
118
+ reverse
105
119
  });
106
120
  let elArray = [];
107
121
  if (transitionEndData.current.mirror) {
@@ -115,13 +129,13 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
115
129
  rejectFn,
116
130
  elArray,
117
131
  transitionProperty: transitionEndData.current.transitionProperty,
118
- target: 0
132
+ target: 0,
133
+ duration: transitionEndData.current.duration
119
134
  }).then(() => {
120
135
  transitionEnd({
121
136
  transitionEndObj: push,
122
137
  transitionEndHandler,
123
- reverse,
124
- delayStep
138
+ reverse
125
139
  });
126
140
  container.current?.classList.remove('enter');
127
141
  container.current?.classList.add('exit');
@@ -136,7 +150,8 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
136
150
  rejectFn,
137
151
  elArray,
138
152
  transitionProperty: transitionEndData.current.transitionProperty,
139
- target: 1
153
+ target: 1,
154
+ duration: transitionEndData.current.duration
140
155
  }).catch(() => { });
141
156
  }).catch(() => { });
142
157
  return;
@@ -144,8 +159,7 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
144
159
  transitionEnd({
145
160
  transitionEndObj: push,
146
161
  transitionEndHandler,
147
- reverse,
148
- delayStep
162
+ reverse
149
163
  });
150
164
  container.current?.classList.remove('enter');
151
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,10 +62,27 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
63
62
  useUse.current = false;
64
63
  }
65
64
  });
66
- const bug = useTransitionEndObj({
65
+ const afterEntering = useTransitionEndObj({
67
66
  transitionEndData,
68
67
  fn: () => {
69
- console.log('bug');
68
+ console.log('afterEntering transitionEnd');
69
+ if (rafId.current) {
70
+ cancelAnimationFrame(rafId.current);
71
+ rafId.current = null;
72
+ }
73
+ rejectFn.current?.();
74
+ container.current?.classList.remove('enter');
75
+ requestAnimationFrame(() => {
76
+ requestAnimationFrame(() => {
77
+ container.current?.classList.add('exit');
78
+ });
79
+ });
80
+ state.current = 'exiting';
81
+ transitionEnd({
82
+ transitionEndObj: pop,
83
+ transitionEndHandler,
84
+ reverse
85
+ });
70
86
  }
71
87
  });
72
88
  useEffect(() => {
@@ -98,25 +114,18 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
98
114
  transitionEnd({
99
115
  transitionEndObj: afterExit,
100
116
  transitionEndHandler,
101
- reverse,
102
- delayStep
117
+ reverse
103
118
  });
104
119
  return;
105
120
  }
106
121
  if (state.current === 'entering') {
107
122
  transitionEnd({
108
- transitionEndObj: bug,
123
+ transitionEndObj: afterEntering,
109
124
  transitionEndHandler,
110
- reverse,
111
- delayStep
125
+ reverse
112
126
  });
113
- if (state.current !== 'entering') {
114
- console.log('state.current !== entering');
115
- console.log('state:', state.current);
116
- console.log('oben');
117
- return;
118
- }
119
- console.log('state.current === entering');
127
+ console.log('state:', state.current);
128
+ console.log('oben');
120
129
  let elArray = [];
121
130
  if (transitionEndData.current.mirror) {
122
131
  elArray = [...transitionEndData.current.mirrorElArray, ...transitionEndData.current.elArray];
@@ -129,21 +138,16 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
129
138
  rejectFn,
130
139
  elArray,
131
140
  transitionProperty: transitionEndData.current.transitionProperty,
132
- target: 0
141
+ target: 0,
142
+ duration: transitionEndData.current.duration
133
143
  }).then(() => {
134
- if (state.current !== 'entering') {
135
- console.log('state.current !== entering');
136
- console.log('state:', state.current);
137
- console.log('unten');
138
- }
139
- console.log('updatePlaybackRate done');
144
+ console.log('state:', state.current);
145
+ console.log('unten');
140
146
  transitionEnd({
141
147
  transitionEndObj: pop,
142
148
  transitionEndHandler,
143
- reverse,
144
- delayStep
149
+ reverse
145
150
  });
146
- console.log('transitionEnd done');
147
151
  container.current?.classList.remove('enter');
148
152
  container.current?.classList.add('exit');
149
153
  console.log('classList done');
@@ -159,7 +163,8 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
159
163
  rejectFn,
160
164
  elArray,
161
165
  transitionProperty: transitionEndData.current.transitionProperty,
162
- target: 1
166
+ target: 1,
167
+ duration: transitionEndData.current.duration
163
168
  }).catch(() => { console.log('updatePlaybackRate catch innen'); });
164
169
  }).catch(() => { console.log('updatePlaybackRate catch ausen'); });
165
170
  return;
@@ -167,8 +172,7 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
167
172
  transitionEnd({
168
173
  transitionEndObj: pop,
169
174
  transitionEndHandler,
170
- reverse,
171
- delayStep
175
+ reverse
172
176
  });
173
177
  container.current?.classList.remove('enter');
174
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.33",
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": [