page-transition 0.1.0-beta.4 → 0.1.0-beta.6

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,5 +1,7 @@
1
1
  # page-transition
2
- A page transition library for the Next.js App Router.
2
+ A page transition library for the Next.js App Router.
3
+
4
+ [🌐 Live Demo](https://simon-schindler.github.io/page-transition-demo/)
3
5
 
4
6
  ## Features
5
7
 
@@ -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,25 @@ 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');
29
31
  const useUse = useRef(false);
32
+ const resolveUseFn = useRef(null);
30
33
  const reverse = useRef({
31
34
  _lock: true,
32
35
  get lock() {
@@ -45,9 +48,10 @@ export function CssTransitionProvider({ header, options = {
45
48
  const handleLinkClick = useRef(() => { });
46
49
  const transitionEndHandler = useTransitionEndHandler();
47
50
  const transitionEndData = useTransitionEndData({
48
- elArray: options.overlayCount === 0 ? container : overlayItems,
49
- transitionProperty: options.transitionProperty
51
+ elArray: defaultOptions.overlayCount === 0 ? container : overlayItems,
52
+ transitionProperty: defaultOptions.transitionProperty
50
53
  });
54
+ const pollRafId = useRef(null);
51
55
  usePopState({
52
56
  container,
53
57
  currentPathname,
@@ -99,12 +103,18 @@ export function CssTransitionProvider({ header, options = {
99
103
  });
100
104
  container.current.classList.remove('exit');
101
105
  container.current.classList.add('enter');
106
+ pollTransitionStart({
107
+ transitionEndData,
108
+ id: pollRafId,
109
+ fn: () => {
110
+ state.current = 'entering';
111
+ }
112
+ });
102
113
  currentPathname.current = pathname;
103
- state.current = 'entering';
104
114
  reverse.current.lock = false;
105
115
  }, [pathname]);
106
116
  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) => {
117
+ 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
118
  overlayItems.current[i] = el;
109
119
  }, className: "overlay-item" }, i))) }), children] })] }));
110
120
  }
@@ -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,15 @@
1
+ import { parseOpacityAll } from "./parseOpacityAll";
2
+ import { parseClipPathAll } from "./parseClipPathAll";
3
+ export function checkTransitionStart(transitionEndData, fn) {
4
+ const parse = {
5
+ 'opacity': () => parseOpacityAll(transitionEndData.elArray),
6
+ 'clip-path': () => parseClipPathAll(transitionEndData.elArray)
7
+ };
8
+ if (parse[transitionEndData.propertyName]()) {
9
+ return false;
10
+ }
11
+ else {
12
+ fn?.();
13
+ return true;
14
+ }
15
+ }
@@ -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,9 @@
1
+ import { checkTransitionStart } from "./checkTransitionStart";
2
+ export function pollTransitionStart({ id, fn, transitionEndData }) {
3
+ id.current = requestAnimationFrame(() => {
4
+ if (checkTransitionStart(transitionEndData.current, fn)) {
5
+ return;
6
+ }
7
+ pollTransitionStart({ id, fn, transitionEndData });
8
+ });
9
+ }
@@ -53,6 +53,7 @@ export function useHandleLinkClick({ container, handleLinkClick, currentPathname
53
53
  nextPage.current = url;
54
54
  new Promise((resolve) => {
55
55
  if (state.current === 'navigating') {
56
+ console.log('state.current === navigating');
56
57
  if (url === currentPathname.current) {
57
58
  return;
58
59
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "page-transition",
3
- "version": "0.1.0-beta.4",
3
+ "version": "0.1.0-beta.6",
4
4
  "description": "A page transition library for the Next.js App Router.",
5
5
  "license": "MIT",
6
6
  "files": [