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

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
@@ -1,6 +1,9 @@
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
@@ -78,9 +81,16 @@ options={{
78
81
  ### `TransitionLink`
79
82
  #### Props
80
83
 
81
- |Prop|type|default|
82
- |---|---|---|
83
- | href | string | - |
84
+ |Prop|Type|Default|Required
85
+ |---|---|---|---|
86
+ | href | string | - | Yes |
87
+ | className | string | - | - |
88
+ | scroll | boolean | true | - |
89
+ | prefetch | "auto" \| null | null | - |
90
+ | onClick | (e: React.MouseEvent)=>void | - | - |
91
+
92
+ **Prop Descriptions**
93
+ TransitionLink uses the Next.js Link component internally. For details about navigation behavior and supported props, refer to the official Next.js documentation.
84
94
 
85
95
  **Description**
86
96
  Triggers the transition.
@@ -196,10 +206,10 @@ overlayCount: 10
196
206
  background: black;
197
207
  transition: none;
198
208
  will-change: clip-path;
199
- clip-path: inset(100% 0 0 0);
209
+ clip-path: inset(100% -1px 0 -1px);
200
210
  }
201
211
  [data-transition].exit .overlay-item{
202
- clip-path: inset(0 0 0 0);
212
+ clip-path: inset(0 -1px 0 -1px);
203
213
  transition: clip-path 1.0s ease;
204
214
  @for $i from 0 to 10{
205
215
  &:nth-child(#{$i+1}){
@@ -208,7 +218,7 @@ overlayCount: 10
208
218
  }
209
219
  }
210
220
  [data-transition].enter .overlay-item{
211
- clip-path: inset(0 0 100% 0);
221
+ clip-path: inset(0 -1px 100% -1px);
212
222
  transition: clip-path 1.0s ease;
213
223
  @for $i from 0 to 10{
214
224
  &:nth-child(#{$i+1}){
@@ -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,7 @@
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
5
  }
6
6
  interface ProviderProps {
7
7
  header?: ReactNode;
@@ -11,22 +11,26 @@ 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
+ ...options
20
+ };
18
21
  const container = useRef(null);
19
22
  const overlayWrapper = useRef(null);
20
23
  const overlayItems = useRef([]);
21
24
  const pathname = usePathname();
22
25
  const currentPathname = useRef(pathname);
23
- const resolveUseFn = useRef(null);
24
26
  const mount = useRef({
25
27
  mounted: false,
26
28
  initPathname: pathname
27
29
  });
28
30
  const state = useRef('idle');
31
+ const basePath = useRef(null);
29
32
  const useUse = useRef(false);
33
+ const resolveUseFn = useRef(null);
30
34
  const reverse = useRef({
31
35
  _lock: true,
32
36
  get lock() {
@@ -45,9 +49,10 @@ export function CssTransitionProvider({ header, options = {
45
49
  const handleLinkClick = useRef(() => { });
46
50
  const transitionEndHandler = useTransitionEndHandler();
47
51
  const transitionEndData = useTransitionEndData({
48
- elArray: options.overlayCount === 0 ? container : overlayItems,
49
- transitionProperty: options.transitionProperty
52
+ elArray: defaultOptions.overlayCount === 0 ? container : overlayItems,
53
+ transitionProperty: defaultOptions.transitionProperty
50
54
  });
55
+ const pollRafId = useRef(null);
51
56
  usePopState({
52
57
  container,
53
58
  currentPathname,
@@ -57,7 +62,8 @@ export function CssTransitionProvider({ header, options = {
57
62
  reverse,
58
63
  delay,
59
64
  useUse,
60
- resolveUseFn
65
+ resolveUseFn,
66
+ pollRafId
61
67
  });
62
68
  useHandleLinkClick({
63
69
  container,
@@ -69,7 +75,9 @@ export function CssTransitionProvider({ header, options = {
69
75
  reverse,
70
76
  delay,
71
77
  useUse,
72
- resolveUseFn
78
+ resolveUseFn,
79
+ basePath,
80
+ pollRafId
73
81
  });
74
82
  useScrollRestoration();
75
83
  const removeEnter = useTransitionEndObj({
@@ -83,6 +91,7 @@ export function CssTransitionProvider({ header, options = {
83
91
  useEffect(() => {
84
92
  if (mount.current.mounted === false &&
85
93
  mount.current.initPathname === pathname) {
94
+ basePath.current = window.location.pathname.slice(0, -pathname.length);
86
95
  return;
87
96
  }
88
97
  mount.current.mounted = true;
@@ -99,12 +108,18 @@ export function CssTransitionProvider({ header, options = {
99
108
  });
100
109
  container.current.classList.remove('exit');
101
110
  container.current.classList.add('enter');
102
- currentPathname.current = pathname;
103
- state.current = 'entering';
111
+ pollTransitionStart({
112
+ transitionEndData,
113
+ id: pollRafId,
114
+ fn: () => {
115
+ state.current = 'entering';
116
+ }
117
+ });
118
+ currentPathname.current = window.location.pathname;
104
119
  reverse.current.lock = false;
105
120
  }, [pathname]);
106
121
  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) => {
122
+ 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: defaultOptions.overlayCount }).map((_, i) => (_jsx("div", { ref: (el) => {
108
123
  overlayItems.current[i] = el;
109
124
  }, className: "overlay-item" }, i))) }), children] })] }));
110
125
  }
@@ -11,8 +11,8 @@ 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
16
  }
17
17
  interface ProviderProps {
18
18
  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 }));
@@ -0,0 +1,6 @@
1
+ interface TransitionEndData {
2
+ elArray: (HTMLDivElement | null)[];
3
+ propertyName: 'opacity' | 'clip-path';
4
+ }
5
+ export declare function checkTransitionStart(transitionEndData: TransitionEndData, fn: (() => void) | null): boolean;
6
+ export {};
@@ -0,0 +1,16 @@
1
+ import { parseFirstOpacity } from "./parseFirstOpacity";
2
+ import { parseFirstClipPath } from "./parseFirstClipPath";
3
+ export function checkTransitionStart(transitionEndData, fn) {
4
+ console.log('checkTransitionStart run');
5
+ const parse = {
6
+ 'opacity': () => parseFirstOpacity(transitionEndData.elArray),
7
+ 'clip-path': () => parseFirstClipPath(transitionEndData.elArray)
8
+ };
9
+ if (parse[transitionEndData.propertyName]()) {
10
+ fn?.();
11
+ return true;
12
+ }
13
+ else {
14
+ return false;
15
+ }
16
+ }
@@ -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
+ }
@@ -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
+ }
@@ -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);
@@ -21,6 +21,8 @@ interface Props {
21
21
  delay: RefObject<number | null>;
22
22
  useUse: RefObject<boolean>;
23
23
  resolveUseFn: RefObject<(() => void) | null>;
24
+ basePath: RefObject<string | null>;
25
+ pollRafId: RefObject<number | null>;
24
26
  }
25
- export declare function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn }: Props): void;
27
+ export declare function useHandleLinkClick({ container, handleLinkClick, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn, basePath, pollRafId }: Props): void;
26
28
  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, delay, state, useUse, resolveUseFn, basePath, pollRafId }) {
8
8
  const router = useRouter();
9
9
  const afterExitFn = useRef(null);
10
10
  const nextPage = useRef(null);
@@ -37,39 +37,47 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
37
37
  reverse,
38
38
  delay
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({
@@ -20,6 +20,7 @@ interface Props {
20
20
  state: RefObject<'idle' | 'exiting' | 'entering' | 'navigating'>;
21
21
  useUse: RefObject<boolean>;
22
22
  resolveUseFn: RefObject<(() => void) | null>;
23
+ pollRafId: RefObject<number | null>;
23
24
  }
24
- export declare function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn }: Props): void;
25
+ export declare function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, reverse, delay, state, useUse, resolveUseFn, pollRafId }: Props): void;
25
26
  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, delay, 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);
@@ -29,11 +31,20 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
29
31
  reverse,
30
32
  delay
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 {
@@ -90,7 +96,7 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
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);
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.20",
4
4
  "description": "A page transition library for the Next.js App Router.",
5
5
  "license": "MIT",
6
6
  "files": [