page-transition 0.1.0-beta.2 → 0.1.0-beta.21

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 +114 -16
  2. package/dist/components/TransitionLink.d.ts +7 -2
  3. package/dist/components/TransitionLink.js +3 -2
  4. package/dist/context/CssTransitionProvider.d.ts +3 -2
  5. package/dist/context/CssTransitionProvider.js +43 -21
  6. package/dist/context/TransitionContext.d.ts +3 -2
  7. package/dist/context/TransitionContext.js +1 -4
  8. package/dist/functions/{checkTransitionFinish.d.ts → checkTransitionStart.d.ts} +1 -1
  9. package/dist/functions/checkTransitionStart.js +15 -0
  10. package/dist/functions/parseDelay.js +1 -1
  11. package/dist/functions/parseFirstClipPath.d.ts +1 -0
  12. package/dist/functions/parseFirstClipPath.js +12 -0
  13. package/dist/functions/parseFirstOpacity.d.ts +1 -0
  14. package/dist/functions/parseFirstOpacity.js +8 -0
  15. package/dist/functions/parseOpacity.js +1 -1
  16. package/dist/functions/parseProperty.d.ts +1 -0
  17. package/dist/functions/parseProperty.js +11 -0
  18. package/dist/functions/pollTransitionStart.d.ts +12 -0
  19. package/dist/functions/pollTransitionStart.js +11 -0
  20. package/dist/functions/setDelayReverse.d.ts +7 -0
  21. package/dist/functions/setDelayReverse.js +13 -0
  22. package/dist/functions/transitionEnd.d.ts +4 -2
  23. package/dist/functions/transitionEnd.js +9 -8
  24. package/dist/functions/transitionEndReverse.d.ts +4 -2
  25. package/dist/functions/transitionEndReverse.js +19 -21
  26. package/dist/hooks/manual/usePopState.js +5 -3
  27. package/dist/hooks/useHandleLinkClick.d.ts +6 -2
  28. package/dist/hooks/useHandleLinkClick.js +27 -19
  29. package/dist/hooks/usePopState.d.ts +5 -2
  30. package/dist/hooks/usePopState.js +27 -21
  31. package/dist/hooks/useTransitionEndData.d.ts +5 -1
  32. package/dist/hooks/useTransitionEndData.js +6 -4
  33. package/dist/hooks/useTransitionEndObj.d.ts +4 -0
  34. package/dist/hooks/useTransitionEndObj.js +3 -1
  35. package/package.json +1 -1
  36. package/dist/functions/checkTransitionFinish.js +0 -13
  37. package/dist/functions/parseClipPathAll.d.ts +0 -1
  38. package/dist/functions/parseClipPathAll.js +0 -20
  39. package/dist/functions/parseOpacityAll.d.ts +0 -1
  40. package/dist/functions/parseOpacityAll.js +0 -13
  41. package/dist/functions/wait.d.ts +0 -2
  42. package/dist/functions/wait.js +0 -12
package/README.md CHANGED
@@ -1,16 +1,20 @@
1
1
  # page-transition
2
- A page transition library for the Next.js App Router.
3
-
2
+ A page transition library for the Next.js App Router.
3
+
4
+ [![Live Demo ↗](https://img.shields.io/badge/Live_Demo_%E2%86%97-000000?style=for-the-badge)](https://simon-schindler.github.io/page-transition-demo/)
5
+
6
+
4
7
  ## Features
5
8
 
6
9
  - CSS & Manual mode
7
10
  - Scroll restoration
8
11
 
9
- ## Install
10
-
11
- `npm install page-transition@beta`
12
-
13
- ## Usage
12
+ ## Install
13
+ ![Version](https://img.shields.io/badge/v_0.1.0--beta.21-000000) ![68.7 KB](https://img.shields.io/badge/68.7_KB-1677FF)
14
+
15
+ `npm install page-transition@beta`
16
+
17
+ ## Usage
14
18
  Import your page transition styles and wrap your page content with `TransitionProvider` in your root layout.
15
19
 
16
20
  ```tsx
@@ -47,6 +51,7 @@ export default function RootLayout({ children }: LayoutProps<"/">) {
47
51
  |---|---|---|
48
52
  | transitionProperty | "opacity" \| "clip-path" | "opacity" |
49
53
  | overlayCount | number | 0 |
54
+ | mirror | boolean | false |
50
55
 
51
56
  **Prop Descriptions**
52
57
 
@@ -57,11 +62,14 @@ Select the transition mode.
57
62
  The component rendered outside the transition container.
58
63
 
59
64
  **options.transitionProperty**
60
- Defines which property transitionend waits for.
61
-
65
+ Defines which property transitionend waits for.
66
+
62
67
  **options.overlayCount**
63
68
  Defines how many overlay items should be created.
64
69
 
70
+ **options.mirror**
71
+ Creates the same number of items as specified by overlayCount.
72
+
65
73
 
66
74
  ```tsx
67
75
  <TransitionProvider
@@ -78,9 +86,16 @@ options={{
78
86
  ### `TransitionLink`
79
87
  #### Props
80
88
 
81
- |Prop|type|default|
82
- |---|---|---|
83
- | href | string | - |
89
+ |Prop|Type|Default|Required
90
+ |---|---|---|---|
91
+ | href | string | - | Yes |
92
+ | className | string | - | - |
93
+ | scroll | boolean | true | - |
94
+ | prefetch | "auto" \| null | null | - |
95
+ | onClick | (e: React.MouseEvent)=>void | - | - |
96
+
97
+ **Prop Descriptions**
98
+ TransitionLink uses the Next.js Link component internally. For details about navigation behavior and supported props, refer to the official Next.js documentation.
84
99
 
85
100
  **Description**
86
101
  Triggers the transition.
@@ -102,6 +117,8 @@ After navigation, the `exit` class is removed and the `enter` class is added. On
102
117
 
103
118
  For overlay animations, a `.overlay-wrapper` is available. You can add overlay items by setting the `overlayCount` option.
104
119
 
120
+ If mirror is set to true, the same number of div elements specified by overlayCount are added to the `.overlaywrapper` with the class `mirror-item`. The transitionend dont wait for mirror items. This ideal for mirror effects.
121
+
105
122
  To access the overlay wrapper and its items, use `useTransitionContext`.
106
123
 
107
124
  ```ts
@@ -196,10 +213,10 @@ overlayCount: 10
196
213
  background: black;
197
214
  transition: none;
198
215
  will-change: clip-path;
199
- clip-path: inset(100% 0 0 0);
216
+ clip-path: inset(100% -1px 0 -1px);
200
217
  }
201
218
  [data-transition].exit .overlay-item{
202
- clip-path: inset(0 0 0 0);
219
+ clip-path: inset(0 -1px 0 -1px);
203
220
  transition: clip-path 1.0s ease;
204
221
  @for $i from 0 to 10{
205
222
  &:nth-child(#{$i+1}){
@@ -208,7 +225,7 @@ overlayCount: 10
208
225
  }
209
226
  }
210
227
  [data-transition].enter .overlay-item{
211
- clip-path: inset(0 0 100% 0);
228
+ clip-path: inset(0 -1px 100% -1px);
212
229
  transition: clip-path 1.0s ease;
213
230
  @for $i from 0 to 10{
214
231
  &:nth-child(#{$i+1}){
@@ -219,11 +236,92 @@ overlayCount: 10
219
236
  ```
220
237
  </details>
221
238
 
239
+ <details>
240
+ <summary>Mirrored Staggered Overlays</summary>
241
+ <br/>
242
+ options
243
+
244
+ ```ts
245
+ transitionProperty: "clip-path"
246
+ overlayCount: 10
247
+ mirror: true
248
+ ```
249
+
250
+ ```scss
251
+ [data-transition] .overlay-wrapper{
252
+ position: fixed;
253
+ inset: 0;
254
+
255
+ display: grid;
256
+ grid-template-columns: repeat(10, 1fr);
257
+ grid-template-rows: repeat(2, 1fr);
258
+ }
259
+ [data-transition] .overlay-item{
260
+ grid-row: 1;
261
+ background: black;
262
+
263
+ transition: none;
264
+ will-change: clip-path;
265
+ clip-path: inset(0 0 100% 0);
266
+ }
267
+ [data-transition].exit .overlay-item{
268
+ grid-row: 1;
269
+ clip-path: inset(0% 0 0 0);
270
+ transition: clip-path 1.2s ease;
271
+ @for $i from 0 to 10{
272
+ &:nth-child(#{$i+1}){
273
+ transition-delay: $i * 0.3s;
274
+ }
275
+ }
276
+ }
277
+ [data-transition].enter .overlay-item{
278
+ grid-row: 1;
279
+ clip-path: inset(0 0 100% 0);
280
+ transition: clip-path 1.2s ease;
281
+ @for $i from 0 to 10{
282
+ &:nth-child(#{$i+1}){
283
+ transition-delay: $i * 0.3s;
284
+ }
285
+ }
286
+ }
287
+ // .mirror-item
288
+ [data-transition] .mirror-item{
289
+ grid-row: 2;
290
+ background: black;
291
+
292
+ transition: none;
293
+ will-change: clip-path;
294
+ clip-path: inset(100% 0 0 0);
295
+ }
296
+ [data-transition].exit .mirror-item{
297
+ grid-row: 2;
298
+ clip-path: inset(0 0 0 0);
299
+ transition: clip-path 1.2s ease;
300
+ @for $i from 0 to 10{
301
+ &:nth-child(#{$i+11}){
302
+ transition-delay: $i * 0.3s;
303
+ }
304
+ }
305
+ }
306
+ [data-transition].enter .mirror-item{
307
+ grid-row: 2;
308
+ clip-path: inset(100% 0 0 0);
309
+ transition: clip-path 1.2s ease;
310
+ @for $i from 0 to 10{
311
+ &:nth-child(#{$i+11}){
312
+ transition-delay: $i * 0.3s;
313
+ }
314
+ }
315
+ }
316
+
317
+ ```
318
+ </details>
319
+
222
320
  ### Manual Mode
223
321
 
224
322
  In manual mode, you can control the transition using `useTransitionContext`.
225
323
 
226
- After triggering the navigation, `state.current` is set to `exiting`. You can then call `resolveFn.current()` to proceed with the navigation. Once `state.current` is set to `entering`, the new page has loaded. After the animation finishes, `state.current` should be set to `idle`.
324
+ After triggering the navigation, `state.current` is set to `exiting`. You can then call `resolveFn.current()` to proceed with the navigation. Once `state.current` is set to `entering`, the navigation has occurred. After the animation finishes, `state.current` should be set to `idle`.
227
325
 
228
326
 
229
327
  ```ts
@@ -1,5 +1,10 @@
1
1
  import type { ReactNode } from "react";
2
- export declare function TransitionLink({ href, children }: {
2
+ interface Props {
3
3
  href: string;
4
+ className?: string;
5
+ prefetch?: boolean;
6
+ onClick?: (e: React.MouseEvent) => void;
4
7
  children: ReactNode;
5
- }): import("react").JSX.Element;
8
+ }
9
+ export declare function TransitionLink({ href, className, prefetch, onClick, children }: Props): import("react").JSX.Element;
10
+ export {};
@@ -1,9 +1,10 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import Link from "next/link";
3
3
  import { useTransitionContext } from "../context/TransitionContext";
4
- export function TransitionLink({ href, children }) {
4
+ export function TransitionLink({ href, className, prefetch, onClick, children }) {
5
5
  const { handleLinkClick } = useTransitionContext();
6
6
  return (_jsx(Link, { href: href, onClick: (e) => {
7
7
  handleLinkClick.current(e, href);
8
- }, children: children }));
8
+ onClick?.(e);
9
+ }, className: className, prefetch: prefetch, children: children }));
9
10
  }
@@ -1,7 +1,8 @@
1
1
  import type { ReactNode } from "react";
2
2
  interface TransitionOptions {
3
- transitionProperty: 'opacity' | 'clip-path';
4
- overlayCount: number;
3
+ transitionProperty?: 'opacity' | 'clip-path';
4
+ overlayCount?: number;
5
+ mirror?: boolean;
5
6
  }
6
7
  interface ProviderProps {
7
8
  header?: ReactNode;
@@ -11,29 +11,35 @@ 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
- export function CssTransitionProvider({ header, options = {
15
- transitionProperty: 'opacity',
16
- overlayCount: 0
17
- }, children }) {
14
+ import { pollTransitionStart } from "../functions/pollTransitionStart";
15
+ export function CssTransitionProvider({ header, options, children }) {
16
+ const defaultOptions = {
17
+ transitionProperty: 'opacity',
18
+ overlayCount: 0,
19
+ mirror: false,
20
+ ...options
21
+ };
18
22
  const container = useRef(null);
19
23
  const overlayWrapper = useRef(null);
20
24
  const overlayItems = useRef([]);
25
+ const mirrorItems = useRef([]);
21
26
  const pathname = usePathname();
22
27
  const currentPathname = useRef(pathname);
23
- const resolveUseFn = useRef(null);
24
28
  const mount = useRef({
25
29
  mounted: false,
26
30
  initPathname: pathname
27
31
  });
28
32
  const state = useRef('idle');
33
+ const basePath = useRef(null);
29
34
  const useUse = useRef(false);
35
+ const resolveUseFn = useRef(null);
30
36
  const reverse = useRef({
31
37
  _lock: true,
32
38
  get lock() {
33
39
  return this._lock;
34
40
  },
35
41
  set lock(value) {
36
- if (delay.current === 0 || overlayItems.current.length < 2) {
42
+ if (delayStep.current === 0 || overlayItems.current.length < 2) {
37
43
  if (this._lock === false)
38
44
  this._lock = true;
39
45
  return;
@@ -41,13 +47,16 @@ export function CssTransitionProvider({ header, options = {
41
47
  this._lock = value;
42
48
  }
43
49
  });
44
- const delay = useRef(null);
50
+ const delayStep = useRef(null);
45
51
  const handleLinkClick = useRef(() => { });
46
52
  const transitionEndHandler = useTransitionEndHandler();
47
53
  const transitionEndData = useTransitionEndData({
48
- elArray: options.overlayCount === 0 ? container : overlayItems,
49
- transitionProperty: options.transitionProperty
54
+ elArray: defaultOptions.overlayCount === 0 ? container : overlayItems,
55
+ transitionProperty: defaultOptions.transitionProperty,
56
+ mirror: defaultOptions.mirror,
57
+ mirrorElArray: mirrorItems
50
58
  });
59
+ const pollRafId = useRef(null);
51
60
  usePopState({
52
61
  container,
53
62
  currentPathname,
@@ -55,9 +64,10 @@ export function CssTransitionProvider({ header, options = {
55
64
  transitionEndHandler,
56
65
  state,
57
66
  reverse,
58
- delay,
67
+ delayStep,
59
68
  useUse,
60
- resolveUseFn
69
+ resolveUseFn,
70
+ pollRafId
61
71
  });
62
72
  useHandleLinkClick({
63
73
  container,
@@ -67,9 +77,11 @@ export function CssTransitionProvider({ header, options = {
67
77
  transitionEndHandler,
68
78
  state,
69
79
  reverse,
70
- delay,
80
+ delayStep,
71
81
  useUse,
72
- resolveUseFn
82
+ resolveUseFn,
83
+ basePath,
84
+ pollRafId
73
85
  });
74
86
  useScrollRestoration();
75
87
  const removeEnter = useTransitionEndObj({
@@ -83,11 +95,13 @@ export function CssTransitionProvider({ header, options = {
83
95
  useEffect(() => {
84
96
  if (mount.current.mounted === false &&
85
97
  mount.current.initPathname === pathname) {
98
+ currentPathname.current = window.location.pathname;
99
+ basePath.current = window.location.pathname.slice(0, -pathname.length);
86
100
  return;
87
101
  }
88
102
  mount.current.mounted = true;
89
- if (delay.current === null) {
90
- delay.current = parseDelay(transitionEndData.current.elArray);
103
+ if (delayStep.current === null && defaultOptions.overlayCount > 1) {
104
+ delayStep.current = parseDelay(transitionEndData.current.elArray);
91
105
  }
92
106
  if (!container.current)
93
107
  return;
@@ -95,16 +109,24 @@ export function CssTransitionProvider({ header, options = {
95
109
  transitionEndObj: removeEnter,
96
110
  transitionEndHandler,
97
111
  reverse,
98
- delay
112
+ delayStep
99
113
  });
100
114
  container.current.classList.remove('exit');
101
115
  container.current.classList.add('enter');
102
- currentPathname.current = pathname;
103
- state.current = 'entering';
116
+ pollTransitionStart({
117
+ transitionEndData,
118
+ id: pollRafId,
119
+ fn: () => {
120
+ state.current = 'entering';
121
+ }
122
+ });
123
+ currentPathname.current = window.location.pathname;
104
124
  reverse.current.lock = false;
105
125
  }, [pathname]);
106
126
  const value = useMemo(() => ({ handleLinkClick, overlayWrapper, overlayItems }), []);
107
- return (_jsxs(TransitionContext.Provider, { value: value, children: [header, _jsxs("div", { ref: container, "data-transition": true, children: [_jsx("div", { ref: overlayWrapper, className: "overlay-wrapper", children: Array.from({ length: options.overlayCount }).map((_, i) => (_jsx("div", { ref: (el) => {
108
- overlayItems.current[i] = el;
109
- }, className: "overlay-item" }, i))) }), children] })] }));
127
+ 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.overlayCount && Array.from({ length: defaultOptions.overlayCount }).map((_, i) => (_jsx("div", { ref: (el) => {
128
+ overlayItems.current[i] = el;
129
+ }, className: "overlay-item" }, i))), defaultOptions.mirror && Array.from({ length: defaultOptions.overlayCount }).map((_, i) => (_jsx("div", { ref: (el) => {
130
+ mirrorItems.current[i] = el;
131
+ }, className: "mirror-item" }, `mirror${i}`)))] }), children] })] }));
110
132
  }
@@ -11,8 +11,9 @@ export declare function useTransitionContext(): ContextProps;
11
11
  type Mode = 'css' | 'manual';
12
12
  type TransitionProperty = 'opacity' | 'clip-path';
13
13
  interface TransitionOptions {
14
- transitionProperty: TransitionProperty;
15
- overlayCount: number;
14
+ transitionProperty?: TransitionProperty;
15
+ overlayCount?: number;
16
+ mirror?: boolean;
16
17
  }
17
18
  interface ProviderProps {
18
19
  header?: ReactNode;
@@ -10,10 +10,7 @@ export function useTransitionContext() {
10
10
  throw new Error('useTransitionContext in Provider!');
11
11
  return context;
12
12
  }
13
- export function TransitionProvider({ header, mode = 'css', options = {
14
- transitionProperty: 'opacity',
15
- overlayCount: 0
16
- }, children, }) {
13
+ export function TransitionProvider({ header, mode = 'css', options, children, }) {
17
14
  return (mode === 'css' ?
18
15
  _jsx(CssTransitionProvider, { header: header, options: options, children: children }) :
19
16
  _jsx(ManualTransitionProvider, { children: children }));
@@ -2,5 +2,5 @@ interface TransitionEndData {
2
2
  elArray: (HTMLDivElement | null)[];
3
3
  propertyName: 'opacity' | 'clip-path';
4
4
  }
5
- export declare function checkTransitionFinish(transitionEndData: TransitionEndData, fn: (() => void) | null): boolean;
5
+ export declare function checkTransitionStart(transitionEndData: TransitionEndData, fn: (() => void) | null): boolean;
6
6
  export {};
@@ -0,0 +1,15 @@
1
+ import { parseFirstOpacity } from "./parseFirstOpacity";
2
+ import { parseFirstClipPath } from "./parseFirstClipPath";
3
+ export function checkTransitionStart(transitionEndData, fn) {
4
+ const parse = {
5
+ 'opacity': () => parseFirstOpacity(transitionEndData.elArray),
6
+ 'clip-path': () => parseFirstClipPath(transitionEndData.elArray)
7
+ };
8
+ if (parse[transitionEndData.propertyName]()) {
9
+ fn?.();
10
+ return true;
11
+ }
12
+ else {
13
+ return false;
14
+ }
15
+ }
@@ -6,5 +6,5 @@ export function parseDelay(elArray) {
6
6
  if (!lastEl)
7
7
  return null;
8
8
  const lastDelay = parseFloat(getComputedStyle(lastEl).transitionDelay);
9
- return lastDelay / (lastIndex || 1);
9
+ return lastDelay / lastIndex;
10
10
  }
@@ -0,0 +1 @@
1
+ export declare function parseFirstClipPath(elArray: (HTMLDivElement | null)[]): null | boolean;
@@ -0,0 +1,12 @@
1
+ export function parseFirstClipPath(elArray) {
2
+ if (!elArray[0])
3
+ return null;
4
+ const clipPath = getComputedStyle(elArray[0]).clipPath;
5
+ const m = clipPath.match(/inset\(([^)]+)\)/) ?? null;
6
+ const arr = m?.[1].split(/\s+/) ?? [];
7
+ for (const string of arr) {
8
+ if (parseFloat(string) < 100)
9
+ return true;
10
+ }
11
+ return false;
12
+ }
@@ -0,0 +1 @@
1
+ export declare function parseFirstOpacity(elArray: (HTMLDivElement | null)[]): null | boolean;
@@ -0,0 +1,8 @@
1
+ export function parseFirstOpacity(elArray) {
2
+ if (!elArray[0])
3
+ return null;
4
+ const opacity = parseFloat(getComputedStyle(elArray[0]).opacity);
5
+ if (opacity > 0)
6
+ return true;
7
+ return false;
8
+ }
@@ -2,5 +2,5 @@ export function parseOpacity(el) {
2
2
  if (!el)
3
3
  return null;
4
4
  const opacity = getComputedStyle(el).opacity;
5
- return 1 - parseFloat(opacity);
5
+ return parseFloat(opacity);
6
6
  }
@@ -0,0 +1 @@
1
+ export declare function parseProperty(transitionProperty: 'opacity' | 'clip-path', el: HTMLDivElement | null): number | null;
@@ -0,0 +1,11 @@
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
+ }
@@ -0,0 +1,12 @@
1
+ import type { RefObject } from "react";
2
+ interface TransitionEndData {
3
+ elArray: (HTMLDivElement | null)[];
4
+ propertyName: 'opacity' | 'clip-path';
5
+ }
6
+ interface Props {
7
+ id: RefObject<number | null>;
8
+ fn: () => void;
9
+ transitionEndData: RefObject<TransitionEndData>;
10
+ }
11
+ export declare function pollTransitionStart({ id, fn, transitionEndData }: Props): void;
12
+ export {};
@@ -0,0 +1,11 @@
1
+ import { checkTransitionStart } from "./checkTransitionStart";
2
+ export function pollTransitionStart({ id, fn, transitionEndData }) {
3
+ if (id.current !== null)
4
+ cancelAnimationFrame(id.current);
5
+ id.current = requestAnimationFrame(() => {
6
+ if (checkTransitionStart(transitionEndData.current, fn)) {
7
+ return;
8
+ }
9
+ pollTransitionStart({ id, fn, transitionEndData });
10
+ });
11
+ }
@@ -0,0 +1,7 @@
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 {};
@@ -0,0 +1,13 @@
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
+ }
@@ -2,6 +2,8 @@ import type { RefObject } from "react";
2
2
  interface TransitionEndObj {
3
3
  elArray: (HTMLDivElement | null)[];
4
4
  propertyName: 'opacity' | 'clip-path';
5
+ mirror: boolean;
6
+ mirrorElArray: (HTMLDivElement | null)[];
5
7
  fn: (() => void) | null;
6
8
  }
7
9
  interface TransitionEndHandler {
@@ -15,7 +17,7 @@ interface Props {
15
17
  _lock: boolean;
16
18
  lock: boolean;
17
19
  }>;
18
- delay: RefObject<number | null>;
20
+ delayStep: RefObject<number | null>;
19
21
  }
20
- export declare function transitionEnd({ transitionEndObj, transitionEndHandler, reverse, delay }: Props): void;
22
+ export declare function transitionEnd({ transitionEndObj, transitionEndHandler, reverse, delayStep }: Props): void;
21
23
  export {};
@@ -1,29 +1,30 @@
1
1
  import { transtionEndReverse } from "./transitionEndReverse";
2
- export function transitionEnd({ transitionEndObj, transitionEndHandler, reverse, delay }) {
2
+ export function transitionEnd({ transitionEndObj, transitionEndHandler, reverse, delayStep }) {
3
3
  const lastIndex = transitionEndObj.current.elArray.length - 1;
4
4
  if (transitionEndHandler.current.handler &&
5
5
  transitionEndHandler.current.handlerEl) {
6
6
  transitionEndHandler.current.handlerEl.removeEventListener('transitionend', transitionEndHandler.current.handler);
7
7
  }
8
+ const endObj = transitionEndObj.current;
8
9
  if (!reverse.current.lock) {
9
10
  transtionEndReverse({
10
11
  transitionEndObj,
11
12
  transitionEndHandler,
12
13
  reverse,
13
- delay
14
+ delayStep
14
15
  });
15
16
  return;
16
17
  }
17
18
  const handler = (e) => {
18
19
  if (e.target !== e.currentTarget)
19
20
  return;
20
- if (transitionEndObj.current.propertyName &&
21
- e.propertyName !== transitionEndObj.current.propertyName)
21
+ if (endObj.propertyName &&
22
+ e.propertyName !== endObj.propertyName)
22
23
  return;
23
- transitionEndObj.current.elArray[lastIndex]?.removeEventListener('transitionend', handler);
24
- transitionEndObj.current.fn?.();
24
+ endObj.elArray[lastIndex]?.removeEventListener('transitionend', handler);
25
+ endObj.fn?.();
25
26
  };
26
27
  transitionEndHandler.current.handler = handler;
27
- transitionEndHandler.current.handlerEl = transitionEndObj.current.elArray[lastIndex];
28
- transitionEndObj.current.elArray[lastIndex]?.addEventListener('transitionend', transitionEndHandler.current.handler);
28
+ transitionEndHandler.current.handlerEl = endObj.elArray[lastIndex];
29
+ endObj.elArray[lastIndex]?.addEventListener('transitionend', transitionEndHandler.current.handler);
29
30
  }
@@ -2,6 +2,8 @@ import type { RefObject } from "react";
2
2
  interface TransitionEndObj {
3
3
  elArray: (HTMLDivElement | null)[];
4
4
  propertyName: 'opacity' | 'clip-path';
5
+ mirror: boolean;
6
+ mirrorElArray: (HTMLDivElement | null)[];
5
7
  fn: (() => void) | null;
6
8
  }
7
9
  interface TransitionEndHandler {
@@ -15,7 +17,7 @@ interface Props {
15
17
  _lock: boolean;
16
18
  lock: boolean;
17
19
  }>;
18
- delay: RefObject<number | null>;
20
+ delayStep: RefObject<number | null>;
19
21
  }
20
- export declare function transtionEndReverse({ transitionEndObj, transitionEndHandler, reverse, delay }: Props): void;
22
+ export declare function transtionEndReverse({ transitionEndObj, transitionEndHandler, reverse, delayStep }: Props): void;
21
23
  export {};
@@ -1,34 +1,32 @@
1
- import { parseClipPath } from "./parseClipPath";
2
- import { parseOpacity } from "./parseOpacity";
1
+ import { setDelayReverse } from "./setDelayReverse";
3
2
  import { resetDelay } from "./resetDelay";
4
- export function transtionEndReverse({ transitionEndObj, transitionEndHandler, reverse, delay }) {
3
+ export function transtionEndReverse({ transitionEndObj, transitionEndHandler, reverse, delayStep }) {
5
4
  if (transitionEndHandler.current.handler &&
6
5
  transitionEndHandler.current.handlerEl)
7
6
  transitionEndHandler.current.handlerEl.removeEventListener('transitionend', transitionEndHandler.current.handler);
8
- const obj = transitionEndObj.current;
9
- const property = transitionEndObj.current.propertyName;
10
- const parseFn = {
11
- opacity: parseOpacity,
12
- 'clip-path': parseClipPath
13
- };
14
- let summ = 0;
15
- obj.elArray.toReversed().forEach((el) => {
16
- if (!el)
17
- return;
18
- const progress = parseFn[property](el);
19
- const d = (delay.current ?? 0) * (progress ?? 0);
20
- el.style.transitionDelay = `${summ}s`;
21
- summ += d;
7
+ const endObj = transitionEndObj.current;
8
+ setDelayReverse({
9
+ elArray: endObj.elArray,
10
+ transitionProperty: endObj.propertyName,
11
+ delayStep: delayStep.current
22
12
  });
23
- const firstEl = obj.elArray[0];
13
+ if (endObj.mirror) {
14
+ setDelayReverse({
15
+ elArray: endObj.mirrorElArray,
16
+ transitionProperty: endObj.propertyName,
17
+ delayStep: delayStep.current
18
+ });
19
+ }
20
+ const firstEl = endObj.elArray[0];
24
21
  const handler = (e) => {
25
- if (e.propertyName !== obj.propertyName ||
22
+ if (e.propertyName !== endObj.propertyName ||
26
23
  e.target !== e.currentTarget)
27
24
  return;
28
- resetDelay(obj.elArray);
25
+ resetDelay(endObj.elArray);
26
+ resetDelay(endObj.mirrorElArray);
29
27
  reverse.current.lock = true;
30
28
  firstEl?.removeEventListener('transitionend', handler);
31
- obj.fn?.();
29
+ endObj.fn?.();
32
30
  };
33
31
  transitionEndHandler.current.handler = handler;
34
32
  transitionEndHandler.current.handlerEl = firstEl;
@@ -5,15 +5,17 @@ export function usePopState({ resolveFn, state, useUse, resolveUseFn }) {
5
5
  if (promise) {
6
6
  useUse.current = true;
7
7
  use(promise);
8
+ }
9
+ else {
8
10
  useUse.current = false;
9
11
  }
10
12
  useEffect(() => {
11
13
  const handlePopstate = () => {
12
- setPromise(new Promise((resolve) => {
14
+ setPromise(new Promise((resolve, reject) => {
13
15
  resolveFn.current = resolve;
14
- resolveUseFn.current = resolve;
16
+ resolveUseFn.current = reject;
15
17
  state.current = 'exiting';
16
- }));
18
+ }).then(() => setPromise(null)).catch(() => setPromise(null)));
17
19
  };
18
20
  window.addEventListener('popstate', handlePopstate);
19
21
  return () => window.removeEventListener('popstate', handlePopstate);
@@ -2,6 +2,8 @@ import type { RefObject } from "react";
2
2
  interface TransitionEndData {
3
3
  elArray: (HTMLDivElement | null)[];
4
4
  propertyName: 'opacity' | 'clip-path';
5
+ mirror: boolean;
6
+ mirrorElArray: (HTMLDivElement | null)[];
5
7
  }
6
8
  interface TransitionEndHandler {
7
9
  handler: ((e: TransitionEvent) => void) | null;
@@ -18,9 +20,11 @@ interface Props {
18
20
  _lock: boolean;
19
21
  lock: boolean;
20
22
  }>;
21
- delay: RefObject<number | null>;
23
+ delayStep: RefObject<number | null>;
22
24
  useUse: RefObject<boolean>;
23
25
  resolveUseFn: RefObject<(() => void) | null>;
26
+ basePath: RefObject<string | null>;
27
+ pollRafId: RefObject<number | null>;
24
28
  }
25
- export declare function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn }: Props): void;
29
+ export declare function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delayStep, state, useUse, resolveUseFn, basePath, pollRafId }: Props): void;
26
30
  export {};
@@ -3,8 +3,8 @@ import { useCallback, useRef } from "react";
3
3
  import { useRouter } from "next/navigation";
4
4
  import { useTransitionEndObj } from "../hooks/useTransitionEndObj";
5
5
  import { transitionEnd } from "../functions/transitionEnd";
6
- import { checkTransitionFinish } from "../functions/checkTransitionFinish";
7
- export function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn }) {
6
+ import { pollTransitionStart } from "../functions/pollTransitionStart";
7
+ export function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delayStep, state, useUse, resolveUseFn, basePath, pollRafId }) {
8
8
  const router = useRouter();
9
9
  const afterExitFn = useRef(null);
10
10
  const nextPage = useRef(null);
@@ -35,48 +35,56 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
35
35
  transitionEndObj: removeEnter,
36
36
  transitionEndHandler,
37
37
  reverse,
38
- delay
38
+ delayStep
39
39
  });
40
- if (useUse.current)
40
+ if (useUse.current) {
41
41
  resolveUseFn.current?.();
42
+ state.current = 'navigating';
43
+ router.push(currentPathname.current);
44
+ }
45
+ state.current = 'navigating';
42
46
  container.current?.classList.remove('exit');
43
47
  container.current?.classList.add('enter');
44
- reverse.current.lock = false;
45
- state.current = 'entering';
48
+ pollTransitionStart({
49
+ id: pollRafId,
50
+ transitionEndData,
51
+ fn: () => {
52
+ state.current = 'entering';
53
+ reverse.current.lock = false;
54
+ }
55
+ });
46
56
  }
47
57
  });
48
58
  handleLinkClick.current = useCallback((e, url) => {
49
59
  e.preventDefault();
50
60
  nextPage.current = url;
61
+ const basePathUrl = basePath.current ? basePath.current + url : url;
51
62
  new Promise((resolve) => {
52
63
  if (state.current === 'navigating') {
53
- if (url === currentPathname.current) {
64
+ if (basePathUrl === currentPathname.current) {
54
65
  return;
55
66
  }
56
- else if (url !== currentPathname.current) {
57
- if (checkTransitionFinish(transitionEndData.current, () => {
58
- if (!nextPage.current)
59
- return;
60
- state.current = 'navigating';
61
- router.push(nextPage.current);
62
- nextPage.current = null;
63
- }))
67
+ else if (basePathUrl !== currentPathname.current) {
68
+ if (!nextPage.current)
64
69
  return;
65
- resolve();
70
+ state.current = 'navigating';
71
+ router.push(nextPage.current);
72
+ nextPage.current = null;
73
+ return;
66
74
  }
67
75
  }
68
76
  else {
69
77
  resolve();
70
78
  }
71
79
  }).then(() => {
72
- if (url === currentPathname.current) {
80
+ if (basePathUrl === currentPathname.current) {
73
81
  if (state.current !== 'exiting')
74
82
  return;
75
83
  transitionEnd({
76
84
  transitionEndObj: afterExit,
77
85
  transitionEndHandler,
78
86
  reverse,
79
- delay
87
+ delayStep
80
88
  });
81
89
  return;
82
90
  }
@@ -84,7 +92,7 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
84
92
  transitionEndObj: push,
85
93
  transitionEndHandler,
86
94
  reverse,
87
- delay
95
+ delayStep
88
96
  });
89
97
  container.current?.classList.remove('enter');
90
98
  container.current?.classList.add('exit');
@@ -2,6 +2,8 @@ import type { RefObject } from "react";
2
2
  interface TransitionEndData {
3
3
  elArray: (HTMLDivElement | null)[];
4
4
  propertyName: 'opacity' | 'clip-path';
5
+ mirror: boolean;
6
+ mirrorElArray: (HTMLDivElement | null)[];
5
7
  }
6
8
  interface TransitionEndHandler {
7
9
  handler: ((e: TransitionEvent) => void) | null;
@@ -16,10 +18,11 @@ interface Props {
16
18
  _lock: boolean;
17
19
  lock: boolean;
18
20
  }>;
19
- delay: RefObject<number | null>;
21
+ delayStep: RefObject<number | null>;
20
22
  state: RefObject<'idle' | 'exiting' | 'entering' | 'navigating'>;
21
23
  useUse: RefObject<boolean>;
22
24
  resolveUseFn: RefObject<(() => void) | null>;
25
+ pollRafId: RefObject<number | null>;
23
26
  }
24
- export declare function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn }: Props): void;
27
+ export declare function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delayStep, state, useUse, resolveUseFn, pollRafId }: Props): void;
25
28
  export {};
@@ -2,12 +2,14 @@
2
2
  import { useEffect, useRef, useState, use } from "react";
3
3
  import { useTransitionEndObj } from "./useTransitionEndObj";
4
4
  import { transitionEnd } from "../functions/transitionEnd";
5
- import { checkTransitionFinish } from "../functions/checkTransitionFinish";
6
- export function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn }) {
5
+ import { pollTransitionStart } from "../functions/pollTransitionStart";
6
+ export function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delayStep, state, useUse, resolveUseFn, pollRafId }) {
7
7
  const [promise, setPromise] = useState(null);
8
8
  if (promise) {
9
9
  useUse.current = true;
10
10
  use(promise);
11
+ }
12
+ else {
11
13
  useUse.current = false;
12
14
  }
13
15
  const resolveFn = useRef(null);
@@ -27,13 +29,22 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
27
29
  transitionEndObj: removeEnter,
28
30
  transitionEndHandler,
29
31
  reverse,
30
- delay
32
+ delayStep
31
33
  });
34
+ state.current = 'navigating';
32
35
  container.current?.classList.remove('exit');
33
36
  container.current?.classList.add('enter');
34
- state.current = 'entering';
35
- reverse.current.lock = false;
36
37
  resolveFn.current?.();
38
+ setPromise(null);
39
+ useUse.current = false;
40
+ pollTransitionStart({
41
+ id: pollRafId,
42
+ transitionEndData,
43
+ fn: () => {
44
+ state.current = 'entering';
45
+ reverse.current.lock = false;
46
+ }
47
+ });
37
48
  }
38
49
  });
39
50
  const pop = useTransitionEndObj({
@@ -42,29 +53,24 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
42
53
  state.current = 'navigating';
43
54
  window.scrollTo(0, window.history.state.scrollY ?? 0);
44
55
  resolveFn.current?.();
45
- resolveFn.current = null;
56
+ setPromise(null);
57
+ useUse.current = false;
46
58
  }
47
59
  });
48
60
  useEffect(() => {
49
61
  const handlePopstate = () => {
50
- setPromise(new Promise((resolve) => {
62
+ setPromise(new Promise((resolve, reject) => {
51
63
  resolveFn.current = resolve;
52
- resolveUseFn.current = resolve;
64
+ resolveUseFn.current = reject;
53
65
  new Promise((resolveNavigating) => {
54
66
  if (state.current === "navigating") {
55
67
  if (currentPathname.current !== window.location.pathname) {
56
- if (checkTransitionFinish(transitionEndData.current, () => {
57
- resolveFn.current?.();
58
- }))
59
- return;
68
+ resolveFn.current?.();
69
+ setPromise(null);
70
+ return;
60
71
  }
61
72
  else if (currentPathname.current === window.location.pathname) {
62
- if (checkTransitionFinish(transitionEndData.current, afterExitFn.current)) {
63
- return;
64
- }
65
- else {
66
- resolveNavigating();
67
- }
73
+ afterExitFn.current?.();
68
74
  }
69
75
  }
70
76
  else {
@@ -76,7 +82,7 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
76
82
  transitionEndObj: afterExit,
77
83
  transitionEndHandler,
78
84
  reverse,
79
- delay
85
+ delayStep
80
86
  });
81
87
  return;
82
88
  }
@@ -84,13 +90,13 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
84
90
  transitionEndObj: pop,
85
91
  transitionEndHandler,
86
92
  reverse,
87
- delay
93
+ delayStep
88
94
  });
89
95
  container.current?.classList.remove('enter');
90
96
  container.current?.classList.add('exit');
91
97
  state.current = 'exiting';
92
98
  });
93
- }));
99
+ }).catch(() => setPromise(null)));
94
100
  };
95
101
  window.addEventListener('popstate', handlePopstate);
96
102
  return () => window.removeEventListener('popstate', handlePopstate);
@@ -2,10 +2,14 @@ import type { RefObject } from "react";
2
2
  interface TransitionEndData {
3
3
  elArray: (HTMLDivElement | null)[];
4
4
  propertyName: 'opacity' | 'clip-path';
5
+ mirror: boolean;
6
+ mirrorElArray: (HTMLDivElement | null)[];
5
7
  }
6
8
  interface Props {
7
9
  elArray: RefObject<HTMLDivElement | null | (HTMLDivElement | null)[]>;
8
10
  transitionProperty: 'opacity' | 'clip-path';
11
+ mirror: boolean;
12
+ mirrorElArray: RefObject<(HTMLDivElement | null)[]>;
9
13
  }
10
- export declare function useTransitionEndData({ elArray, transitionProperty }: Props): RefObject<TransitionEndData>;
14
+ export declare function useTransitionEndData({ elArray, transitionProperty, mirror, mirrorElArray }: Props): RefObject<TransitionEndData>;
11
15
  export {};
@@ -1,16 +1,18 @@
1
1
  "use client";
2
2
  import { useEffect, useRef } from "react";
3
- export function useTransitionEndData({ elArray, transitionProperty }) {
3
+ export function useTransitionEndData({ elArray, transitionProperty, mirror, mirrorElArray }) {
4
4
  const transitionEndData = useRef({
5
5
  elArray: [],
6
- propertyName: transitionProperty
6
+ propertyName: transitionProperty,
7
+ mirror,
8
+ mirrorElArray: []
7
9
  });
8
10
  useEffect(() => {
9
11
  transitionEndData.current = {
10
12
  ...transitionEndData.current,
11
13
  elArray: Array.isArray(elArray.current) ?
12
- elArray.current :
13
- [elArray.current],
14
+ elArray.current : [elArray.current],
15
+ mirrorElArray: mirrorElArray.current
14
16
  };
15
17
  }, []);
16
18
  return transitionEndData;
@@ -2,6 +2,8 @@ import type { RefObject } from "react";
2
2
  interface TransitionEndData {
3
3
  elArray: (HTMLDivElement | null)[];
4
4
  propertyName: 'opacity' | 'clip-path';
5
+ mirror: boolean;
6
+ mirrorElArray: (HTMLDivElement | null)[];
5
7
  }
6
8
  interface Props {
7
9
  transitionEndData: RefObject<TransitionEndData>;
@@ -10,6 +12,8 @@ interface Props {
10
12
  interface TransitionEndObj {
11
13
  elArray: (HTMLDivElement | null)[];
12
14
  propertyName: 'opacity' | 'clip-path';
15
+ mirror: boolean;
16
+ mirrorElArray: (HTMLDivElement | null)[];
13
17
  fn: (() => void) | null;
14
18
  }
15
19
  export declare function useTransitionEndObj({ transitionEndData, fn }: Props): RefObject<TransitionEndObj>;
@@ -1,8 +1,10 @@
1
1
  import { useRef, useEffect } from "react";
2
2
  export function useTransitionEndObj({ transitionEndData, fn }) {
3
3
  const transitionEndObj = useRef({
4
- elArray: [null],
4
+ elArray: [],
5
5
  propertyName: transitionEndData.current.propertyName,
6
+ mirror: transitionEndData.current.mirror,
7
+ mirrorElArray: [],
6
8
  fn: null,
7
9
  });
8
10
  useEffect(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "page-transition",
3
- "version": "0.1.0-beta.2",
3
+ "version": "0.1.0-beta.21",
4
4
  "description": "A page transition library for the Next.js App Router.",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -1,13 +0,0 @@
1
- import { parseOpacityAll } from "./parseOpacityAll";
2
- import { parseClipPathAll } from "./parseClipPathAll";
3
- export function checkTransitionFinish(transitionEndData, fn) {
4
- const parse = {
5
- 'opacity': () => parseOpacityAll(transitionEndData.elArray),
6
- 'clip-path': () => parseClipPathAll(transitionEndData.elArray)
7
- };
8
- if (parse[transitionEndData.propertyName]()) {
9
- fn?.();
10
- return true;
11
- }
12
- return false;
13
- }
@@ -1 +0,0 @@
1
- export declare function parseClipPathAll(elArray: (HTMLDivElement | null)[]): null | boolean;
@@ -1,20 +0,0 @@
1
- export function parseClipPathAll(elArray) {
2
- if (elArray.length === 0)
3
- return null;
4
- const array = [];
5
- elArray.forEach((el) => {
6
- if (!el)
7
- return;
8
- const clipPath = getComputedStyle(el).clipPath;
9
- const m = clipPath.match(/inset\(([^)]+)\)/);
10
- const result = m?.[1]?.split(/\s+/) ?? null;
11
- const number = result?.map((string) => parseFloat(string)).find(n => n > 0) ?? 0;
12
- array.push(number);
13
- });
14
- for (const number of array) {
15
- if (number < 100) {
16
- return false;
17
- }
18
- }
19
- return true;
20
- }
@@ -1 +0,0 @@
1
- export declare function parseOpacityAll(elArray: (HTMLDivElement | null)[]): boolean | null;
@@ -1,13 +0,0 @@
1
- export function parseOpacityAll(elArray) {
2
- if (elArray.length === 0)
3
- return null;
4
- for (const el of elArray) {
5
- if (!el)
6
- continue;
7
- const opacity = parseFloat(getComputedStyle(el).opacity);
8
- if (opacity > 0) {
9
- return false;
10
- }
11
- }
12
- return true;
13
- }
@@ -1,2 +0,0 @@
1
- import type { RefObject } from "react";
2
- export declare function wait(fn: () => void, count: number, rafId: RefObject<null | number>): void;
@@ -1,12 +0,0 @@
1
- export function wait(fn, count, rafId) {
2
- if (count < 1) {
3
- fn();
4
- rafId.current = null;
5
- return;
6
- }
7
- if (rafId.current !== null)
8
- cancelAnimationFrame(rafId.current);
9
- rafId.current = requestAnimationFrame(() => {
10
- wait(fn, --count, rafId);
11
- });
12
- }