page-transition 0.1.0 → 0.1.2

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,9 +10,9 @@ 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-000000) ![82.5 KB](https://img.shields.io/badge/82.5_KB-1677FF)
13
+ ![Version](https://img.shields.io/badge/v0.1.2-000000) ![83.1 KB](https://img.shields.io/badge/82.5_KB-1677FF)
14
14
 
15
- `npm install page-transition@beta`
15
+ `npm install page-transition`
16
16
 
17
17
  ## Usage
18
18
  Import your page transition styles and wrap your page content with `TransitionProvider` in your root layout.
@@ -197,8 +197,8 @@ overlays: 1
197
197
  background: white;
198
198
 
199
199
  opacity: 0;
200
- pointer-events: none;
201
200
  will-change: opacity;
201
+ pointer-events: none;
202
202
  }
203
203
  [data-transition].exit .overlay{
204
204
  opacity: 1;
@@ -232,7 +232,6 @@ overlays: 2
232
232
 
233
233
  opacity: 0;
234
234
  pointer-events: none;
235
- will-change: opacity;
236
235
  }
237
236
  [data-transition].exit .overlay:nth-child(1){
238
237
  opacity: 1;
@@ -251,8 +250,9 @@ overlays: 2
251
250
 
252
251
  background: black;
253
252
  transition: none;
254
- will-change: clip-path;
255
253
  clip-path: inset(100% 0 0 0);
254
+ will-change: clip-path;
255
+ pointer-events: none;
256
256
  }
257
257
  [data-transition].exit .overlay:nth-child(2){
258
258
  clip-path: inset(0 0 0 0);
@@ -282,6 +282,7 @@ items: [10, 0]
282
282
 
283
283
  display: grid;
284
284
  grid-template-columns: repeat(10, 1fr);
285
+ pointer-events: none;
285
286
  }
286
287
  [data-transition] .first-item{
287
288
  background: black;
@@ -328,6 +329,7 @@ items: [10, 10]
328
329
  display: grid;
329
330
  grid-template-columns: repeat(10, 1fr);
330
331
  grid-template-rows: repeat(2, 1fr);
332
+ pointer-events: none;
331
333
  }
332
334
  [data-transition] .first-item{
333
335
  grid-row: 1;
@@ -398,7 +400,9 @@ In manual mode, you can control the transition using `useTransitionContext`.
398
400
 
399
401
  After triggering the navigation, `state.current` is set to `exiting`. You can then call `resolveFn.current()` to proceed with the navigation.
400
402
 
401
- Before navigating, you should call `window.scrollTo(0, window.history.state.scrollY ?? 0)` to restore the previous scroll position.
403
+ Before navigating, you should call
404
+ `window.scrollTo(0, window.history.state.scrollY ?? 0)`
405
+ to restore the previous scroll position, or scroll to the top.
402
406
 
403
407
  Once `state.current` is set to `entering`, the navigation has occurred. After the animation finishes, `state.current` should be set to `idle`.
404
408
 
@@ -408,4 +412,4 @@ import { useTransitionContext } from "page-transition/manual"
408
412
 
409
413
  const { resolveFn, state } = useTransitionContext()
410
414
 
411
- ```
415
+ ```
@@ -36,9 +36,7 @@ export function CssTransitionProvider({ outside, scroll, options, children }) {
36
36
  const resolveUseFn = useRef(null);
37
37
  const handleLinkClick = useRef(() => { });
38
38
  const transitionEndHandler = useTransitionEndHandler();
39
- const elArray = defaultOptions.items[0] === 0 &&
40
- defaultOptions.overlays === 0 ?
41
- pageWrapper : defaultOptions.items[0] > 0 ? firstItems :
39
+ const elArray = defaultOptions.items[0] > 0 ? firstItems :
42
40
  defaultOptions.overlays > 0 ? overlays : pageWrapper;
43
41
  group.current = defaultOptions.overlays > 0 && defaultOptions.items[0] === 0 ?
44
42
  'overlay' : null;
@@ -15,6 +15,10 @@ export function ManualTransitionProvider({ scroll, children }) {
15
15
  const basePath = useRef(null);
16
16
  const useUse = useRef(false);
17
17
  const resolveUseFn = useRef(null);
18
+ const mount = useRef({
19
+ mounted: false,
20
+ initPathname: pathname
21
+ });
18
22
  usePopState({
19
23
  resolveFn,
20
24
  state,
@@ -33,9 +37,13 @@ export function ManualTransitionProvider({ scroll, children }) {
33
37
  });
34
38
  useScrollRestoration({ scroll });
35
39
  useEffect(() => {
36
- if (basePath.current === null) {
40
+ if (mount.current.mounted === false &&
41
+ mount.current.initPathname === pathname) {
42
+ currentPathname.current = window.location.pathname;
37
43
  basePath.current = window.location.pathname.slice(0, -pathname.length);
44
+ return;
38
45
  }
46
+ mount.current.mounted = true;
39
47
  state.current = 'entering';
40
48
  currentPathname.current = window.location.pathname;
41
49
  }, [pathname]);
@@ -5,6 +5,7 @@ import { ManualTransitionProvider } from "./ManualTransitionProvider";
5
5
  export function TransitionProvider(props) {
6
6
  const { scroll = 'auto', children } = props;
7
7
  return (props.mode === 'manual' ?
8
- _jsx(ManualTransitionProvider, { scroll: scroll, children: children }) :
9
- _jsx(CssTransitionProvider, { scroll: scroll, outside: props.outside, options: props.options, children: children }));
8
+ _jsx(ManualTransitionProvider, { scroll: scroll, children: children })
9
+ :
10
+ _jsx(CssTransitionProvider, { scroll: scroll, outside: props.outside, options: props.options, children: children }));
10
11
  }
@@ -1,26 +1,30 @@
1
1
  "use client";
2
- import { useEffect, useState, use } from "react";
2
+ import { useEffect, useState, useRef, use } from "react";
3
3
  export function usePopState({ resolveFn, state, currentPathname, useUse, resolveUseFn }) {
4
- const [promise, setPromise] = useState(null);
5
- if (promise) {
4
+ const [_, rerender] = useState(0);
5
+ const promise = useRef(null);
6
+ if (promise.current) {
6
7
  useUse.current = true;
7
- use(promise);
8
+ use(promise.current);
8
9
  }
9
10
  else {
10
11
  useUse.current = false;
11
12
  }
12
13
  useEffect(() => {
13
14
  const handlePopstate = () => {
14
- setPromise(new Promise((resolve, reject) => {
15
+ promise.current = new Promise((resolve, reject) => {
15
16
  resolveFn.current = resolve;
16
17
  resolveUseFn.current = reject;
17
18
  state.current = 'exiting';
18
19
  }).then(() => {
19
- setPromise(null);
20
+ promise.current = null;
20
21
  if (window.location.pathname === currentPathname.current) {
21
22
  state.current = 'entering';
22
23
  }
23
- }).catch(() => setPromise(null)));
24
+ }).catch(() => {
25
+ promise.current = null;
26
+ });
27
+ rerender((prev) => ++prev);
24
28
  };
25
29
  window.addEventListener('popstate', handlePopstate);
26
30
  return () => window.removeEventListener('popstate', handlePopstate);
@@ -4,10 +4,11 @@ import { useTransitionEndObj } from "./useTransitionEndObj";
4
4
  import { transitionEnd } from "../functions/transitionEnd";
5
5
  import { pollTransitionStart } from "../functions/pollTransitionStart";
6
6
  export function usePopState({ container, currentPathname, transitionEndData, transitionEndHandler, state, useUse, resolveUseFn, pollRafId }) {
7
- const [promise, setPromise] = useState(null);
8
- if (promise) {
7
+ const [_, rerender] = useState(0);
8
+ const promise = useRef(null);
9
+ if (promise.current) {
9
10
  useUse.current = true;
10
- use(promise);
11
+ use(promise.current);
11
12
  }
12
13
  else {
13
14
  useUse.current = false;
@@ -32,7 +33,7 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
32
33
  container.current?.classList.remove('exit');
33
34
  container.current?.classList.add('enter');
34
35
  resolveFn.current?.();
35
- setPromise(null);
36
+ promise.current = null;
36
37
  useUse.current = false;
37
38
  pollTransitionStart({
38
39
  id: pollRafId,
@@ -49,7 +50,7 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
49
50
  state.current = 'navigating';
50
51
  window.scrollTo(0, window.history.state.scrollY ?? 0);
51
52
  resolveFn.current?.();
52
- setPromise(null);
53
+ promise.current = null;
53
54
  useUse.current = false;
54
55
  }
55
56
  });
@@ -68,7 +69,7 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
68
69
  });
69
70
  useEffect(() => {
70
71
  const handlePopstate = () => {
71
- setPromise(new Promise((resolve, reject) => {
72
+ promise.current = new Promise((resolve, reject) => {
72
73
  resolveFn.current = resolve;
73
74
  resolveUseFn.current = reject;
74
75
  new Promise((resolveNavigating) => {
@@ -76,7 +77,7 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
76
77
  if (currentPathname.current !== window.location.pathname) {
77
78
  state.current = 'navigating';
78
79
  resolveFn.current?.();
79
- setPromise(null);
80
+ promise.current = null;
80
81
  return;
81
82
  }
82
83
  else if (currentPathname.current === window.location.pathname) {
@@ -119,7 +120,10 @@ export function usePopState({ container, currentPathname, transitionEndData, tra
119
120
  });
120
121
  }
121
122
  });
122
- }).catch(() => setPromise(null)));
123
+ }).catch(() => {
124
+ promise.current = null;
125
+ });
126
+ rerender((prev) => ++prev);
123
127
  };
124
128
  window.addEventListener('popstate', handlePopstate);
125
129
  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",
3
+ "version": "0.1.2",
4
4
  "description": "A page transition library for the Next.js App Router.",
5
5
  "license": "MIT",
6
6
  "files": [