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

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