page-transition 0.1.0-beta.44 → 0.1.0-beta.46

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.
Files changed (42) hide show
  1. package/README.md +7 -17
  2. package/dist/context/CssTransitionProvider.d.ts +3 -3
  3. package/dist/context/CssTransitionProvider.js +4 -51
  4. package/dist/context/TransitionContext.d.ts +3 -4
  5. package/dist/functions/checkTransitionStart.js +2 -7
  6. package/dist/functions/transitionEnd.d.ts +1 -6
  7. package/dist/functions/transitionEnd.js +1 -11
  8. package/dist/functions/transitionRunning.d.ts +2 -0
  9. package/dist/functions/transitionRunning.js +19 -0
  10. package/dist/hooks/useHandleLinkClick.d.ts +1 -10
  11. package/dist/hooks/useHandleLinkClick.js +28 -128
  12. package/dist/hooks/usePopState.d.ts +1 -10
  13. package/dist/hooks/usePopState.js +25 -123
  14. package/dist/hooks/useTransitionEndData.d.ts +2 -1
  15. package/dist/types/transitionEnd.types.d.ts +3 -2
  16. package/dist/types/transitionPropertys.types.d.ts +1 -0
  17. package/dist/types/transitionPropertys.types.js +1 -0
  18. package/package.json +1 -1
  19. package/dist/functions/parseClipPath.d.ts +0 -1
  20. package/dist/functions/parseClipPath.js +0 -9
  21. package/dist/functions/parseDelay.d.ts +0 -1
  22. package/dist/functions/parseDelay.js +0 -10
  23. package/dist/functions/parseDuration.d.ts +0 -1
  24. package/dist/functions/parseDuration.js +0 -5
  25. package/dist/functions/parseFirstClipPath.d.ts +0 -1
  26. package/dist/functions/parseFirstClipPath.js +0 -12
  27. package/dist/functions/parseFirstOpacity.d.ts +0 -1
  28. package/dist/functions/parseFirstOpacity.js +0 -8
  29. package/dist/functions/parseOpacity.d.ts +0 -1
  30. package/dist/functions/parseOpacity.js +0 -6
  31. package/dist/functions/parseProperty.d.ts +0 -1
  32. package/dist/functions/parseProperty.js +0 -11
  33. package/dist/functions/resetDelay.d.ts +0 -1
  34. package/dist/functions/resetDelay.js +0 -7
  35. package/dist/functions/setDelayReverse.d.ts +0 -7
  36. package/dist/functions/setDelayReverse.js +0 -13
  37. package/dist/functions/setPlaybackRate.d.ts +0 -7
  38. package/dist/functions/setPlaybackRate.js +0 -10
  39. package/dist/functions/transitionEndReverse.d.ts +0 -13
  40. package/dist/functions/transitionEndReverse.js +0 -36
  41. package/dist/functions/updatePlaybackRate.d.ts +0 -11
  42. package/dist/functions/updatePlaybackRate.js +0 -53
package/README.md CHANGED
@@ -10,7 +10,7 @@ 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--beta.43-000000) ![86.9 KB](https://img.shields.io/badge/86.9_KB-1677FF)
13
+ ![Version](https://img.shields.io/badge/v_0.1.0--beta.44-000000) ![86.9 KB](https://img.shields.io/badge/86.9_KB-1677FF)
14
14
 
15
15
  `npm install page-transition@beta`
16
16
 
@@ -47,11 +47,10 @@ export default function RootLayout({ children }: LayoutProps<"/">) {
47
47
 
48
48
  *Options*
49
49
 
50
- | Option | Type | Default |
51
- |---|---|---|
52
- | transitionProperty | "opacity" \| "clip-path" | "opacity" |
53
- | wait | boolean | false |
54
- | items | [number, number] | [0, 0] |
50
+ | Option | Type | Default | Required |
51
+ |---|---|---|---|
52
+ | transitionProperty | "opacity" \| "clip-path" \| "transform" | - | Yes |
53
+ | items | [number, number] | [0, 0] | - |
55
54
 
56
55
 
57
56
  **Prop Descriptions**
@@ -65,16 +64,12 @@ The component rendered outside the transition container.
65
64
  **options.transitionProperty**
66
65
  Defines which property transitionend waits for.
67
66
 
68
- **options.wait**
69
- false: Reverses the current enter transition.
70
- true: Waits for the current enter transition to finish before starting the new transition.
71
-
72
67
  **options.items**
73
68
  <br/>
74
- options.items[0]
69
+ `options.items[0]`
75
70
  Number of items in the first group. Each item receives the `.overlay-item` class.
76
71
  <br/>
77
- options.items[1]
72
+ `options.items[1]`
78
73
  Number of items in the second group. Each item receives the `.second-item` class.
79
74
 
80
75
 
@@ -124,11 +119,6 @@ After navigation, the `exit` class is removed and the `enter` class is added. On
124
119
 
125
120
  For overlay animations, an `.overlay-wrapper` is available. You can configure the number of overlay items using the options.items array. The transition waits for the first item group to finish its transition.
126
121
  <br/>
127
- When `wait` is `false`, both item groups must contain the same number of items for reverse delays to work correctly. Both groups will then receive the same delay durations when reversing a staggered enter transition.
128
-
129
-
130
- *At the moment, it only supports duration and delay in seconds.*
131
-
132
122
  To access the overlay wrapper and its items, use `useTransitionContext`.
133
123
 
134
124
  ```ts
@@ -1,12 +1,12 @@
1
1
  import type { ReactNode } from "react";
2
+ import type { TransitionProperty } from "../types/transitionPropertys.types";
2
3
  interface TransitionOptions {
3
- transitionProperty?: 'opacity' | 'clip-path';
4
- wait?: boolean;
4
+ transitionProperty: TransitionProperty;
5
5
  items?: [number, number];
6
6
  }
7
7
  interface ProviderProps {
8
8
  header?: ReactNode;
9
- options?: TransitionOptions;
9
+ options: TransitionOptions;
10
10
  children: ReactNode;
11
11
  }
12
12
  export declare function CssTransitionProvider({ header, options, children }: ProviderProps): import("react").JSX.Element;
@@ -10,13 +10,9 @@ import { transitionEnd } from "../functions/transitionEnd";
10
10
  import { useHandleLinkClick } from "../hooks/useHandleLinkClick";
11
11
  import { useScrollRestoration } from "../hooks/useScrollRestoration";
12
12
  import { usePopState } from "../hooks/usePopState";
13
- import { parseDelay } from "../functions/parseDelay";
14
- import { parseDuration } from "../functions/parseDuration";
15
13
  import { pollTransitionStart } from "../functions/pollTransitionStart";
16
14
  export function CssTransitionProvider({ header, options, children }) {
17
15
  const defaultOptions = {
18
- transitionProperty: 'opacity',
19
- wait: false,
20
16
  items: [0, 0],
21
17
  ...options
22
18
  };
@@ -34,24 +30,6 @@ export function CssTransitionProvider({ header, options, children }) {
34
30
  const basePath = useRef(null);
35
31
  const useUse = useRef(false);
36
32
  const resolveUseFn = useRef(null);
37
- const reverse = useRef({
38
- _lock: true,
39
- get lock() {
40
- return this._lock;
41
- },
42
- set lock(value) {
43
- if (delayStep.current === 0 ||
44
- overlayItems.current.length < 2 ||
45
- defaultOptions.wait === true) {
46
- if (this._lock === false)
47
- this._lock = true;
48
- return;
49
- }
50
- this._lock = value;
51
- }
52
- });
53
- const delayStep = useRef(null);
54
- const duration = useRef(null);
55
33
  const handleLinkClick = useRef(() => { });
56
34
  const transitionEndHandler = useTransitionEndHandler();
57
35
  const transitionEndData = useTransitionEndData({
@@ -60,23 +38,15 @@ export function CssTransitionProvider({ header, options, children }) {
60
38
  secondElArray: secondItems
61
39
  });
62
40
  const pollRafId = useRef(null);
63
- const rafId = useRef(null);
64
- const rejectFn = useRef(null);
65
41
  usePopState({
66
42
  container,
67
43
  currentPathname,
68
44
  transitionEndData,
69
45
  transitionEndHandler,
70
46
  state,
71
- reverse,
72
47
  useUse,
73
48
  resolveUseFn,
74
- pollRafId,
75
- rafId,
76
- rejectFn,
77
- delayStep,
78
- duration,
79
- wait: defaultOptions.wait
49
+ pollRafId
80
50
  });
81
51
  useHandleLinkClick({
82
52
  container,
@@ -85,16 +55,10 @@ export function CssTransitionProvider({ header, options, children }) {
85
55
  transitionEndData,
86
56
  transitionEndHandler,
87
57
  state,
88
- reverse,
89
58
  useUse,
90
59
  resolveUseFn,
91
60
  basePath,
92
- pollRafId,
93
- rafId,
94
- rejectFn,
95
- delayStep,
96
- duration,
97
- wait: defaultOptions.wait
61
+ pollRafId
98
62
  });
99
63
  useScrollRestoration();
100
64
  const removeEnter = useTransitionEndObj({
@@ -102,7 +66,6 @@ export function CssTransitionProvider({ header, options, children }) {
102
66
  fn: () => {
103
67
  container.current?.classList.remove('enter');
104
68
  state.current = 'idle';
105
- reverse.current.lock = true;
106
69
  }
107
70
  });
108
71
  useEffect(() => {
@@ -117,9 +80,7 @@ export function CssTransitionProvider({ header, options, children }) {
117
80
  return;
118
81
  transitionEnd({
119
82
  transitionEndObj: removeEnter,
120
- transitionEndHandler,
121
- reverse,
122
- delayStep
83
+ transitionEndHandler
123
84
  });
124
85
  container.current.classList.remove('exit');
125
86
  container.current.classList.add('enter');
@@ -131,19 +92,11 @@ export function CssTransitionProvider({ header, options, children }) {
131
92
  }
132
93
  });
133
94
  currentPathname.current = window.location.pathname;
134
- reverse.current.lock = false;
135
- if (delayStep.current === null && defaultOptions.items[0] > 1) {
136
- delayStep.current = defaultOptions.wait ? 0 :
137
- parseDelay(transitionEndData.current.elArray);
138
- }
139
- if (duration.current === null && !defaultOptions.wait) {
140
- duration.current = parseDuration(transitionEndData.current.elArray[0]) * 1000 / 3;
141
- }
142
95
  }, [pathname]);
143
96
  const value = useMemo(() => ({ handleLinkClick, overlayWrapper, overlayItems }), []);
144
97
  return (_jsxs(TransitionContext.Provider, { value: value, children: [header, _jsxs("div", { ref: container, "data-transition": true, children: [_jsxs("div", { ref: overlayWrapper, className: "overlay-wrapper", children: [defaultOptions.items[0] > 0 && Array.from({ length: defaultOptions.items[0] }).map((_, i) => (_jsx("div", { ref: (el) => {
145
98
  overlayItems.current[i] = el;
146
99
  }, className: "overlay-item" }, i))), defaultOptions.items[1] > 0 && Array.from({ length: defaultOptions.items[1] }).map((_, i) => (_jsx("div", { ref: (el) => {
147
100
  secondItems.current[i] = el;
148
- }, className: "mirror-item" }, `mirror${i}`)))] }), children] })] }));
101
+ }, className: "second-item" }, `mirror${i}`)))] }), children] })] }));
149
102
  }
@@ -1,4 +1,5 @@
1
1
  import type { ReactNode, RefObject } from "react";
2
+ import type { TransitionProperty } from "../types/transitionPropertys.types";
2
3
  interface ContextProps {
3
4
  handleLinkClick: RefObject<((e: React.MouseEvent, url: string) => void)>;
4
5
  overlayWrapper?: RefObject<HTMLDivElement | null>;
@@ -9,16 +10,14 @@ interface ContextProps {
9
10
  export declare const TransitionContext: import("react").Context<ContextProps | null>;
10
11
  export declare function useTransitionContext(): ContextProps;
11
12
  type Mode = 'css' | 'manual';
12
- type TransitionProperty = 'opacity' | 'clip-path';
13
13
  interface TransitionOptions {
14
- transitionProperty?: TransitionProperty;
15
- wait?: boolean;
14
+ transitionProperty: TransitionProperty;
16
15
  items?: [number, number];
17
16
  }
18
17
  interface ProviderProps {
19
18
  header?: ReactNode;
20
19
  mode?: Mode;
21
- options?: TransitionOptions;
20
+ options: TransitionOptions;
22
21
  children: ReactNode;
23
22
  }
24
23
  export declare function TransitionProvider({ header, mode, options, children, }: ProviderProps): import("react").JSX.Element;
@@ -1,11 +1,6 @@
1
- import { parseFirstOpacity } from "./parseFirstOpacity";
2
- import { parseFirstClipPath } from "./parseFirstClipPath";
1
+ import { transitionRunning } from "./transitionRunning";
3
2
  export function checkTransitionStart(transitionEndData, fn) {
4
- const parse = {
5
- 'opacity': () => parseFirstOpacity(transitionEndData.elArray),
6
- 'clip-path': () => parseFirstClipPath(transitionEndData.elArray)
7
- };
8
- if (parse[transitionEndData.transitionProperty]()) {
3
+ if (transitionRunning(transitionEndData.elArray, transitionEndData.transitionProperty)) {
9
4
  fn?.();
10
5
  return true;
11
6
  }
@@ -3,11 +3,6 @@ import type { TransitionEndObj, TransitionEndHandler } from "../types/transition
3
3
  interface Props {
4
4
  transitionEndObj: RefObject<TransitionEndObj>;
5
5
  transitionEndHandler: RefObject<TransitionEndHandler>;
6
- reverse: RefObject<{
7
- _lock: boolean;
8
- lock: boolean;
9
- }>;
10
- delayStep: RefObject<number | null>;
11
6
  }
12
- export declare function transitionEnd({ transitionEndObj, transitionEndHandler, reverse, delayStep }: Props): void;
7
+ export declare function transitionEnd({ transitionEndObj, transitionEndHandler }: Props): void;
13
8
  export {};
@@ -1,20 +1,10 @@
1
- import { transtionEndReverse } from "./transitionEndReverse";
2
- export function transitionEnd({ transitionEndObj, transitionEndHandler, reverse, delayStep }) {
1
+ export function transitionEnd({ transitionEndObj, transitionEndHandler }) {
3
2
  const lastIndex = transitionEndObj.current.elArray.length - 1;
4
3
  if (transitionEndHandler.current.handler &&
5
4
  transitionEndHandler.current.handlerEl) {
6
5
  transitionEndHandler.current.handlerEl.removeEventListener('transitionend', transitionEndHandler.current.handler);
7
6
  }
8
7
  const endObj = transitionEndObj.current;
9
- if (!reverse.current.lock) {
10
- transtionEndReverse({
11
- transitionEndObj,
12
- transitionEndHandler,
13
- reverse,
14
- delayStep
15
- });
16
- return;
17
- }
18
8
  const handler = (e) => {
19
9
  if (e.target !== e.currentTarget)
20
10
  return;
@@ -0,0 +1,2 @@
1
+ import type { TransitionProperty } from "../types/transitionPropertys.types";
2
+ export declare function transitionRunning(elArray: (HTMLDivElement | null)[], transitionProperty: TransitionProperty): null | boolean;
@@ -0,0 +1,19 @@
1
+ export function transitionRunning(elArray, transitionProperty) {
2
+ if (!elArray[0])
3
+ return null;
4
+ const property = {
5
+ opacity: 'opacity',
6
+ 'clip-path': 'clipPath',
7
+ 'transform': 'transform'
8
+ };
9
+ if (elArray[0].getAnimations().some((obj) => {
10
+ return obj.playState === 'running' &&
11
+ obj.effect instanceof KeyframeEffect &&
12
+ obj.effect.getKeyframes().some(frame => {
13
+ return frame[property[transitionProperty]] !== undefined;
14
+ });
15
+ })) {
16
+ return true;
17
+ }
18
+ return false;
19
+ }
@@ -7,19 +7,10 @@ interface Props {
7
7
  transitionEndData: RefObject<TransitionEndData>;
8
8
  transitionEndHandler: RefObject<TransitionEndHandler>;
9
9
  state: RefObject<'idle' | 'exiting' | 'entering' | 'navigating'>;
10
- reverse: RefObject<{
11
- _lock: boolean;
12
- lock: boolean;
13
- }>;
14
10
  useUse: RefObject<boolean>;
15
11
  resolveUseFn: RefObject<(() => void) | null>;
16
12
  basePath: RefObject<string | null>;
17
13
  pollRafId: RefObject<number | null>;
18
- rafId: RefObject<number | null>;
19
- rejectFn: RefObject<(() => void) | null>;
20
- delayStep: RefObject<number | null>;
21
- duration: RefObject<number | null>;
22
- wait: boolean;
23
14
  }
24
- export declare function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, state, useUse, resolveUseFn, basePath, pollRafId, rafId, rejectFn, delayStep, duration, wait }: Props): void;
15
+ export declare function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, state, useUse, resolveUseFn, basePath, pollRafId }: Props): void;
25
16
  export {};
@@ -4,9 +4,7 @@ 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
- import { updatePlaybackRate } from "../functions/updatePlaybackRate";
8
- import { setPlaybackRate } from "../functions/setPlaybackRate";
9
- export function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, state, useUse, resolveUseFn, basePath, pollRafId, rafId, rejectFn, delayStep, duration, wait }) {
7
+ export function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, state, useUse, resolveUseFn, basePath, pollRafId }) {
10
8
  const router = useRouter();
11
9
  const afterExitFn = useRef(null);
12
10
  const nextPage = useRef(null);
@@ -27,7 +25,6 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
27
25
  fn: () => {
28
26
  container.current?.classList.remove('enter');
29
27
  state.current = 'idle';
30
- reverse.current.lock = true;
31
28
  }
32
29
  });
33
30
  const afterExit = useTransitionEndObj({
@@ -35,9 +32,7 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
35
32
  fn: afterExitFn.current = () => {
36
33
  transitionEnd({
37
34
  transitionEndObj: removeEnter,
38
- transitionEndHandler,
39
- reverse,
40
- delayStep
35
+ transitionEndHandler
41
36
  });
42
37
  if (useUse.current) {
43
38
  resolveUseFn.current?.();
@@ -46,13 +41,13 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
46
41
  }
47
42
  state.current = 'navigating';
48
43
  container.current?.classList.remove('exit');
44
+ void container.current?.offsetWidth;
49
45
  container.current?.classList.add('enter');
50
46
  pollTransitionStart({
51
47
  id: pollRafId,
52
48
  transitionEndData,
53
49
  fn: () => {
54
50
  state.current = 'entering';
55
- reverse.current.lock = false;
56
51
  }
57
52
  });
58
53
  }
@@ -60,20 +55,13 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
60
55
  const afterEntering = useTransitionEndObj({
61
56
  transitionEndData,
62
57
  fn: () => {
63
- if (rafId.current) {
64
- cancelAnimationFrame(rafId.current);
65
- rafId.current = null;
66
- }
67
- rejectFn.current?.();
68
58
  container.current?.classList.remove('enter');
69
59
  void container.current?.offsetWidth;
70
60
  container.current?.classList.add('exit');
71
61
  state.current = 'exiting';
72
62
  transitionEnd({
73
63
  transitionEndObj: push,
74
- transitionEndHandler,
75
- reverse,
76
- delayStep
64
+ transitionEndHandler
77
65
  });
78
66
  }
79
67
  });
@@ -99,128 +87,40 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
99
87
  resolve();
100
88
  }
101
89
  }).then(() => {
102
- if (wait) {
103
- if (state.current === 'idle' &&
104
- currentPathname.current === basePathUrl)
105
- return;
106
- if (currentPathname.current === basePathUrl
107
- && state.current === 'exiting') {
108
- transitionEnd({
109
- transitionEndObj: afterExit,
110
- transitionEndHandler,
111
- reverse,
112
- delayStep
113
- });
114
- return;
115
- }
116
- if (state.current === 'entering' &&
117
- currentPathname.current === basePathUrl) {
118
- transitionEnd({
119
- transitionEndObj: removeEnter,
120
- transitionEndHandler,
121
- reverse,
122
- delayStep
123
- });
124
- return;
125
- }
126
- if (state.current === 'idle' || state.current === 'exiting') {
127
- transitionEnd({
128
- transitionEndObj: push,
129
- transitionEndHandler,
130
- reverse,
131
- delayStep
132
- });
133
- container.current?.classList.remove('enter');
134
- container.current?.classList.add('exit');
135
- state.current = 'exiting';
136
- }
137
- else {
138
- transitionEnd({
139
- transitionEndObj: afterEntering,
140
- transitionEndHandler,
141
- reverse,
142
- delayStep
143
- });
144
- }
90
+ if (state.current === 'idle' &&
91
+ currentPathname.current === basePathUrl)
145
92
  return;
146
- }
147
- if (basePathUrl === currentPathname.current) {
148
- if (state.current !== 'exiting')
149
- return;
93
+ if (currentPathname.current === basePathUrl
94
+ && state.current === 'exiting') {
150
95
  transitionEnd({
151
96
  transitionEndObj: afterExit,
152
- transitionEndHandler,
153
- reverse,
154
- delayStep
97
+ transitionEndHandler
155
98
  });
156
99
  return;
157
100
  }
158
- if (state.current === 'entering') {
101
+ if (state.current === 'entering' &&
102
+ currentPathname.current === basePathUrl) {
159
103
  transitionEnd({
160
- transitionEndObj: afterEntering,
161
- transitionEndHandler,
162
- reverse,
163
- delayStep
104
+ transitionEndObj: removeEnter,
105
+ transitionEndHandler
164
106
  });
165
- let elArray = [];
166
- if (transitionEndData.current.secondElArray.length > 0) {
167
- elArray = [...transitionEndData.current.secondElArray, ...transitionEndData.current.elArray];
168
- }
169
- else {
170
- elArray = [...transitionEndData.current.elArray];
171
- }
172
- updatePlaybackRate({
173
- rafId,
174
- rejectFn,
175
- elArray,
176
- transitionProperty: transitionEndData.current.transitionProperty,
177
- target: 0,
178
- duration: duration.current ?? 0
179
- }).then(() => {
180
- if (nextPage.current === currentPathname.current) {
181
- transitionEnd({
182
- transitionEndObj: afterExit,
183
- transitionEndHandler,
184
- reverse,
185
- delayStep
186
- });
187
- }
188
- else {
189
- transitionEnd({
190
- transitionEndObj: push,
191
- transitionEndHandler,
192
- reverse,
193
- delayStep
194
- });
195
- }
196
- container.current?.classList.remove('enter');
197
- container.current?.classList.add('exit');
198
- state.current = 'exiting';
199
- setPlaybackRate({
200
- elArray,
201
- transitionProperty: transitionEndData.current.transitionProperty,
202
- value: 0
203
- });
204
- updatePlaybackRate({
205
- rafId,
206
- rejectFn,
207
- elArray,
208
- transitionProperty: transitionEndData.current.transitionProperty,
209
- target: 1,
210
- duration: duration.current ?? 0
211
- }).catch(() => { });
212
- }).catch(() => { });
213
107
  return;
214
108
  }
215
- transitionEnd({
216
- transitionEndObj: push,
217
- transitionEndHandler,
218
- reverse,
219
- delayStep
220
- });
221
- container.current?.classList.remove('enter');
222
- container.current?.classList.add('exit');
223
- state.current = 'exiting';
109
+ if (state.current === 'idle' || state.current === 'exiting') {
110
+ transitionEnd({
111
+ transitionEndObj: push,
112
+ transitionEndHandler
113
+ });
114
+ container.current?.classList.remove('enter');
115
+ container.current?.classList.add('exit');
116
+ state.current = 'exiting';
117
+ }
118
+ else {
119
+ transitionEnd({
120
+ transitionEndObj: afterEntering,
121
+ transitionEndHandler
122
+ });
123
+ }
224
124
  });
225
125
  }, [router]);
226
126
  }
@@ -5,19 +5,10 @@ interface Props {
5
5
  currentPathname: RefObject<string>;
6
6
  transitionEndData: RefObject<TransitionEndData>;
7
7
  transitionEndHandler: RefObject<TransitionEndHandler>;
8
- reverse: RefObject<{
9
- _lock: boolean;
10
- lock: boolean;
11
- }>;
12
8
  state: RefObject<'idle' | 'exiting' | 'entering' | 'navigating'>;
13
9
  useUse: RefObject<boolean>;
14
10
  resolveUseFn: RefObject<(() => void) | null>;
15
11
  pollRafId: RefObject<number | null>;
16
- rafId: RefObject<number | null>;
17
- rejectFn: RefObject<(() => void) | null>;
18
- delayStep: RefObject<number | null>;
19
- duration: RefObject<number | null>;
20
- wait: boolean;
21
12
  }
22
- export declare function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, state, useUse, resolveUseFn, pollRafId, rafId, rejectFn, delayStep, duration, wait }: Props): void;
13
+ export declare function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, state, useUse, resolveUseFn, pollRafId }: Props): void;
23
14
  export {};
@@ -3,9 +3,7 @@ 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
- import { updatePlaybackRate } from "../functions/updatePlaybackRate";
7
- import { setPlaybackRate } from "../functions/setPlaybackRate";
8
- export function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, state, useUse, resolveUseFn, pollRafId, rafId, rejectFn, delayStep, duration, wait }) {
6
+ export function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, state, useUse, resolveUseFn, pollRafId }) {
9
7
  const [promise, setPromise] = useState(null);
10
8
  if (promise) {
11
9
  useUse.current = true;
@@ -21,7 +19,6 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
21
19
  fn: () => {
22
20
  container.current?.classList.remove('enter');
23
21
  state.current = 'idle';
24
- reverse.current.lock = true;
25
22
  }
26
23
  });
27
24
  const afterExit = useTransitionEndObj({
@@ -29,9 +26,7 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
29
26
  fn: afterExitFn.current = () => {
30
27
  transitionEnd({
31
28
  transitionEndObj: removeEnter,
32
- transitionEndHandler,
33
- reverse,
34
- delayStep
29
+ transitionEndHandler
35
30
  });
36
31
  state.current = 'navigating';
37
32
  container.current?.classList.remove('exit');
@@ -44,7 +39,6 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
44
39
  transitionEndData,
45
40
  fn: () => {
46
41
  state.current = 'entering';
47
- reverse.current.lock = false;
48
42
  }
49
43
  });
50
44
  }
@@ -62,20 +56,13 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
62
56
  const afterEntering = useTransitionEndObj({
63
57
  transitionEndData,
64
58
  fn: () => {
65
- if (rafId.current) {
66
- cancelAnimationFrame(rafId.current);
67
- rafId.current = null;
68
- }
69
- rejectFn.current?.();
70
59
  container.current?.classList.remove('enter');
71
60
  void container.current?.offsetWidth;
72
61
  container.current?.classList.add('exit');
73
62
  state.current = 'exiting';
74
63
  transitionEnd({
75
64
  transitionEndObj: pop,
76
- transitionEndHandler,
77
- reverse,
78
- delayStep
65
+ transitionEndHandler
79
66
  });
80
67
  }
81
68
  });
@@ -87,6 +74,7 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
87
74
  new Promise((resolveNavigating) => {
88
75
  if (state.current === "navigating") {
89
76
  if (currentPathname.current !== window.location.pathname) {
77
+ state.current = 'navigating';
90
78
  resolveFn.current?.();
91
79
  setPromise(null);
92
80
  return;
@@ -99,123 +87,37 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
99
87
  resolveNavigating();
100
88
  }
101
89
  }).then(() => {
102
- if (wait) {
103
- if (currentPathname.current === window.location.pathname
104
- && state.current === 'exiting') {
105
- transitionEnd({
106
- transitionEndObj: afterExit,
107
- transitionEndHandler,
108
- reverse,
109
- delayStep
110
- });
111
- return;
112
- }
113
- if (state.current === 'entering' &&
114
- currentPathname.current === window.location.pathname) {
115
- transitionEnd({
116
- transitionEndObj: removeEnter,
117
- transitionEndHandler,
118
- reverse,
119
- delayStep
120
- });
121
- return;
122
- }
123
- if (state.current === 'idle' || state.current === 'exiting') {
124
- transitionEnd({
125
- transitionEndObj: pop,
126
- transitionEndHandler,
127
- reverse,
128
- delayStep
129
- });
130
- container.current?.classList.remove('enter');
131
- container.current?.classList.add('exit');
132
- state.current = 'exiting';
133
- }
134
- else {
135
- transitionEnd({
136
- transitionEndObj: afterEntering,
137
- transitionEndHandler,
138
- reverse,
139
- delayStep
140
- });
141
- }
90
+ if (currentPathname.current === window.location.pathname
91
+ && state.current === 'exiting') {
92
+ transitionEnd({
93
+ transitionEndObj: afterExit,
94
+ transitionEndHandler
95
+ });
142
96
  return;
143
97
  }
144
- if (currentPathname.current === window.location.pathname) {
98
+ if (state.current === 'entering' &&
99
+ currentPathname.current === window.location.pathname) {
145
100
  transitionEnd({
146
- transitionEndObj: afterExit,
147
- transitionEndHandler,
148
- reverse,
149
- delayStep
101
+ transitionEndObj: removeEnter,
102
+ transitionEndHandler
150
103
  });
151
104
  return;
152
105
  }
153
- if (state.current === 'entering') {
106
+ if (state.current === 'idle' || state.current === 'exiting') {
107
+ transitionEnd({
108
+ transitionEndObj: pop,
109
+ transitionEndHandler
110
+ });
111
+ container.current?.classList.remove('enter');
112
+ container.current?.classList.add('exit');
113
+ state.current = 'exiting';
114
+ }
115
+ else {
154
116
  transitionEnd({
155
117
  transitionEndObj: afterEntering,
156
- transitionEndHandler,
157
- reverse,
158
- delayStep
118
+ transitionEndHandler
159
119
  });
160
- let elArray = [];
161
- if (transitionEndData.current.secondElArray.length > 0) {
162
- elArray = [...transitionEndData.current.secondElArray, ...transitionEndData.current.elArray];
163
- }
164
- else {
165
- elArray = [...transitionEndData.current.elArray];
166
- }
167
- updatePlaybackRate({
168
- rafId,
169
- rejectFn,
170
- elArray,
171
- transitionProperty: transitionEndData.current.transitionProperty,
172
- target: 0,
173
- duration: duration.current ?? 0
174
- }).then(() => {
175
- if (currentPathname.current === window.location.pathname) {
176
- transitionEnd({
177
- transitionEndObj: afterExit,
178
- transitionEndHandler,
179
- reverse,
180
- delayStep
181
- });
182
- }
183
- else {
184
- transitionEnd({
185
- transitionEndObj: pop,
186
- transitionEndHandler,
187
- reverse,
188
- delayStep
189
- });
190
- }
191
- container.current?.classList.remove('enter');
192
- container.current?.classList.add('exit');
193
- state.current = 'exiting';
194
- setPlaybackRate({
195
- elArray,
196
- transitionProperty: transitionEndData.current.transitionProperty,
197
- value: 0
198
- });
199
- updatePlaybackRate({
200
- rafId,
201
- rejectFn,
202
- elArray,
203
- transitionProperty: transitionEndData.current.transitionProperty,
204
- target: 1,
205
- duration: duration.current ?? 0
206
- }).catch(() => { });
207
- }).catch(() => { });
208
- return;
209
120
  }
210
- transitionEnd({
211
- transitionEndObj: pop,
212
- transitionEndHandler,
213
- reverse,
214
- delayStep
215
- });
216
- container.current?.classList.remove('enter');
217
- container.current?.classList.add('exit');
218
- state.current = 'exiting';
219
121
  });
220
122
  }).catch(() => setPromise(null)));
221
123
  };
@@ -1,8 +1,9 @@
1
1
  import type { RefObject } from "react";
2
2
  import type { TransitionEndData } from "../types/transitionEnd.types";
3
+ import type { TransitionProperty } from "../types/transitionPropertys.types";
3
4
  interface Props {
4
5
  elArray: RefObject<HTMLDivElement | null | (HTMLDivElement | null)[]>;
5
- transitionProperty: 'opacity' | 'clip-path';
6
+ transitionProperty: TransitionProperty;
6
7
  secondElArray: RefObject<(HTMLDivElement | null)[]>;
7
8
  }
8
9
  export declare function useTransitionEndData({ elArray, transitionProperty, secondElArray }: Props): RefObject<TransitionEndData>;
@@ -1,11 +1,12 @@
1
+ import type { TransitionProperty } from "./transitionPropertys.types";
1
2
  export interface TransitionEndData {
2
3
  elArray: (HTMLDivElement | null)[];
3
- transitionProperty: 'opacity' | 'clip-path';
4
+ transitionProperty: TransitionProperty;
4
5
  secondElArray: (HTMLDivElement | null)[];
5
6
  }
6
7
  export interface TransitionEndObj {
7
8
  elArray: (HTMLDivElement | null)[];
8
- transitionProperty: 'opacity' | 'clip-path';
9
+ transitionProperty: TransitionProperty;
9
10
  secondElArray: (HTMLDivElement | null)[];
10
11
  fn: (() => void) | null;
11
12
  }
@@ -0,0 +1 @@
1
+ export type TransitionProperty = 'opacity' | 'clip-path' | 'transform';
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "page-transition",
3
- "version": "0.1.0-beta.44",
3
+ "version": "0.1.0-beta.46",
4
4
  "description": "A page transition library for the Next.js App Router.",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -1 +0,0 @@
1
- export declare function parseClipPath(el: HTMLDivElement | null): number | null;
@@ -1,9 +0,0 @@
1
- export function parseClipPath(el) {
2
- if (!el)
3
- return null;
4
- const clipPath = getComputedStyle(el).clipPath;
5
- const match = clipPath.match(/inset\(([^)]+)\)/);
6
- const result = match?.[1]?.split(/\s+/) ?? null;
7
- const number = result?.map(value => parseFloat(value)).find(value => value > 0) ?? 0;
8
- return number / 100;
9
- }
@@ -1 +0,0 @@
1
- export declare function parseDelay(elArray: (HTMLDivElement | null)[]): number | null;
@@ -1,10 +0,0 @@
1
- export function parseDelay(elArray) {
2
- if (elArray.length === 0)
3
- return null;
4
- const lastIndex = elArray.length - 1;
5
- const lastEl = elArray[lastIndex];
6
- if (!lastEl)
7
- return null;
8
- const lastDelay = parseFloat(getComputedStyle(lastEl).transitionDelay);
9
- return lastDelay / lastIndex;
10
- }
@@ -1 +0,0 @@
1
- export declare function parseDuration(el: HTMLElement | null): number;
@@ -1,5 +0,0 @@
1
- export function parseDuration(el) {
2
- if (!el)
3
- return 0;
4
- return parseFloat(getComputedStyle(el).transitionDuration);
5
- }
@@ -1 +0,0 @@
1
- export declare function parseFirstClipPath(elArray: (HTMLDivElement | null)[]): null | boolean;
@@ -1,12 +0,0 @@
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
- }
@@ -1 +0,0 @@
1
- export declare function parseFirstOpacity(elArray: (HTMLDivElement | null)[]): null | boolean;
@@ -1,8 +0,0 @@
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 +0,0 @@
1
- export declare function parseOpacity(el: HTMLDivElement | null): number | null;
@@ -1,6 +0,0 @@
1
- export function parseOpacity(el) {
2
- if (!el)
3
- return null;
4
- const opacity = getComputedStyle(el).opacity;
5
- return parseFloat(opacity);
6
- }
@@ -1 +0,0 @@
1
- export declare function parseProperty(transitionProperty: 'opacity' | 'clip-path', el: HTMLDivElement | null): number | null;
@@ -1,11 +0,0 @@
1
- import { parseOpacity } from "./parseOpacity";
2
- import { parseClipPath } from "./parseClipPath";
3
- export function parseProperty(transitionProperty, el) {
4
- if (!el)
5
- return null;
6
- const parse = {
7
- 'opacity': parseOpacity,
8
- 'clip-path': parseClipPath
9
- };
10
- return parse[transitionProperty](el);
11
- }
@@ -1 +0,0 @@
1
- export declare function resetDelay(elArray: (HTMLDivElement | null)[]): void;
@@ -1,7 +0,0 @@
1
- export function resetDelay(elArray) {
2
- elArray.forEach((el) => {
3
- if (!el)
4
- return;
5
- el.style.transitionDelay = "";
6
- });
7
- }
@@ -1,7 +0,0 @@
1
- interface Props {
2
- elArray: (HTMLDivElement | null)[];
3
- transitionProperty: 'opacity' | 'clip-path';
4
- delayStep: number | null;
5
- }
6
- export declare function setDelayReverse({ elArray, transitionProperty, delayStep }: Props): void;
7
- export {};
@@ -1,13 +0,0 @@
1
- import { parseProperty } from "./parseProperty";
2
- export function setDelayReverse({ elArray, transitionProperty, delayStep }) {
3
- if (elArray.length < 2 || !delayStep)
4
- return;
5
- let summ = 0;
6
- elArray.toReversed().forEach((el, i) => {
7
- if (!el)
8
- return;
9
- el.style.transitionDelay = `${summ}s`;
10
- const progress = parseProperty(transitionProperty, el) ?? 0;
11
- summ += (delayStep * progress);
12
- });
13
- }
@@ -1,7 +0,0 @@
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 {};
@@ -1,10 +0,0 @@
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.playbackRate = value;
7
- }
8
- });
9
- });
10
- }
@@ -1,13 +0,0 @@
1
- import type { RefObject } from "react";
2
- import type { TransitionEndObj, TransitionEndHandler } from "../types/transitionEnd.types";
3
- interface Props {
4
- transitionEndObj: RefObject<TransitionEndObj>;
5
- transitionEndHandler: RefObject<TransitionEndHandler>;
6
- reverse: RefObject<{
7
- _lock: boolean;
8
- lock: boolean;
9
- }>;
10
- delayStep: RefObject<number | null>;
11
- }
12
- export declare function transtionEndReverse({ transitionEndObj, transitionEndHandler, reverse, delayStep }: Props): void;
13
- export {};
@@ -1,36 +0,0 @@
1
- import { setDelayReverse } from "./setDelayReverse";
2
- import { resetDelay } from "./resetDelay";
3
- export function transtionEndReverse({ transitionEndObj, transitionEndHandler, reverse, delayStep }) {
4
- if (transitionEndHandler.current.handler &&
5
- transitionEndHandler.current.handlerEl)
6
- transitionEndHandler.current.handlerEl.removeEventListener('transitionend', transitionEndHandler.current.handler);
7
- const endObj = transitionEndObj.current;
8
- setDelayReverse({
9
- elArray: endObj.elArray,
10
- transitionProperty: endObj.transitionProperty,
11
- delayStep: delayStep.current
12
- });
13
- if (endObj.secondElArray.length === endObj.elArray.length) {
14
- setDelayReverse({
15
- elArray: endObj.secondElArray,
16
- transitionProperty: endObj.transitionProperty,
17
- delayStep: delayStep.current
18
- });
19
- }
20
- const firstEl = endObj.elArray[0];
21
- const handler = (e) => {
22
- if (e.propertyName !== endObj.transitionProperty ||
23
- e.target !== e.currentTarget)
24
- return;
25
- resetDelay(endObj.elArray);
26
- if (endObj.secondElArray.length === endObj.elArray.length) {
27
- resetDelay(endObj.secondElArray);
28
- }
29
- reverse.current.lock = true;
30
- firstEl?.removeEventListener('transitionend', handler);
31
- endObj.fn?.();
32
- };
33
- transitionEndHandler.current.handler = handler;
34
- transitionEndHandler.current.handlerEl = firstEl;
35
- firstEl?.addEventListener('transitionend', handler);
36
- }
@@ -1,11 +0,0 @@
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
- duration: number;
9
- }
10
- export declare function updatePlaybackRate({ rafId, rejectFn, elArray, transitionProperty, target, duration }: Props): Promise<void>;
11
- export {};
@@ -1,53 +0,0 @@
1
- export function updatePlaybackRate({ rafId, rejectFn, elArray, transitionProperty, target, duration }) {
2
- if (rafId.current !== null)
3
- cancelAnimationFrame(rafId.current);
4
- if (duration === 0)
5
- return Promise.resolve();
6
- let progress = 0;
7
- let summ = target ? 0 : duration;
8
- let animationArray = [];
9
- let start = false;
10
- elArray.forEach((el) => {
11
- el?.getAnimations().forEach((obj) => {
12
- if (obj instanceof CSSTransition &&
13
- obj.transitionProperty === transitionProperty) {
14
- animationArray.push(obj);
15
- }
16
- });
17
- });
18
- let before = null;
19
- return new Promise((resolve, reject) => {
20
- rejectFn.current?.();
21
- rejectFn.current = reject;
22
- const step = (now) => {
23
- if (before === null) {
24
- before = now;
25
- }
26
- const dt = Math.min(now - before, 30);
27
- before = now;
28
- summ = target ? Math.min(summ + dt, duration) : Math.max(summ - dt, 0);
29
- progress = summ / duration;
30
- animationArray.forEach((obj) => {
31
- const currentTime = obj.currentTime;
32
- if (target && typeof currentTime === 'number' && currentTime > 0)
33
- start = true;
34
- if (obj.playState !== "running" ||
35
- typeof currentTime !== "number" ||
36
- currentTime <= 0 && start)
37
- return;
38
- obj.updatePlaybackRate(progress);
39
- });
40
- if (target && progress >= 1 || !target && progress <= 0) {
41
- if (rafId.current !== null) {
42
- cancelAnimationFrame(rafId.current);
43
- rafId.current = null;
44
- }
45
- rejectFn.current = null;
46
- resolve();
47
- return;
48
- }
49
- rafId.current = requestAnimationFrame(step);
50
- };
51
- rafId.current = requestAnimationFrame(step);
52
- });
53
- }