{"id":16347986,"url":"https://github.com/ismamz/next-transition-router","last_synced_at":"2025-04-04T18:02:51.453Z","repository":{"id":253139936,"uuid":"838359099","full_name":"ismamz/next-transition-router","owner":"ismamz","description":"Easily add animated transitions between pages using Next.js App Router and your favorite animation library.","archived":false,"fork":false,"pushed_at":"2025-02-28T01:24:46.000Z","size":1623,"stargazers_count":218,"open_issues_count":5,"forks_count":12,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-03-28T17:08:13.177Z","etag":null,"topics":["animation","app-router","framer-motion","gsap","nextjs","page-transitions","page-transitions-next","react","router-transitions","transitions","view-transitions"],"latest_commit_sha":null,"homepage":"https://next-transition-router.vercel.app","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ismamz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-05T13:31:45.000Z","updated_at":"2025-03-27T08:21:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"c9b32f0f-271d-4239-b14f-5072de1acfdc","html_url":"https://github.com/ismamz/next-transition-router","commit_stats":null,"previous_names":["ismamz/next-transition-router"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ismamz%2Fnext-transition-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ismamz%2Fnext-transition-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ismamz%2Fnext-transition-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ismamz%2Fnext-transition-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ismamz","download_url":"https://codeload.github.com/ismamz/next-transition-router/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247226211,"owners_count":20904465,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["animation","app-router","framer-motion","gsap","nextjs","page-transitions","page-transitions-next","react","router-transitions","transitions","view-transitions"],"created_at":"2024-10-11T00:47:50.287Z","updated_at":"2025-04-04T18:02:51.401Z","avatar_url":"https://github.com/ismamz.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/ismamz/next-transition-router/main/example/app/icon.svg\" alt=\"next-transition-router\" width=\"100\" height=\"100\" /\u003e\n  \u003ch1\u003enext-transition-router\u003c/h1\u003e\n\u003c/div\u003e\n\nEasily add animated transitions between pages using Next.js App Router and your favorite animation library.\n\n- [**Live Demo using GSAP**](https://next-transition-router.vercel.app) (source code: [/example](/example)).\n- [**Stackblitz Demo using Framer Motion**](https://stackblitz.com/edit/next-transition-router-framer-motion).\n\n## Features\n\n- Automatically detect internal links to handle page transitions ([optional `auto` flag](#auto-enabled)).\n- Use a custom `Link` component to manually handle page transitions ([when `auto` is disabled](#handle-links-custom-link-component-vs-auto-detection)).\n- Exclusively to be used with [Next.js App Router](https://nextjs.org/docs/app) (v14.0.0 or higher).\n- Quickly add animated transitions between pages using JavaScript or CSS.\n- Integrate seamlessly with [GSAP](https://gsap.com/resources/React/) or any other animation library of your choice (see [minimal GSAP example](#minimal-example-using-gsap)).\n- If JavaScript is disabled, the router's accessibility is not compromised.\n- It's really lightweight; the bundle size is less than 8 KB.\n- Focused on customizable animations, not targeting the [View Transitions API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API).\n\nIf you're looking to use the View Transitions API, check [next-view-transitions](https://github.com/shuding/next-view-transitions).\n\n\u003e [!WARNING]\n\u003e This project is currently in Beta. Please note that the API may change as features are enhanced and refined.\n\n## Installation\n\nInstall the package using your preferred package manager:\n\n```sh\npnpm add next-transition-router\n```\n\n```sh\nyarn add next-transition-router\n```\n\n```sh\nnpm install next-transition-router\n```\n\n## Usage\n\n### `TransitionRouter`\n\nCreate a client component (e.g.: `app/providers.tsx`) to use the `TransitionRouter` provider:\n\n```tsx\n\"use client\";\n\nimport { TransitionRouter } from \"next-transition-router\";\n\nexport function Providers({ children }: { children: React.ReactNode }) {\n  return (\n    \u003cTransitionRouter\n      leave={(next) =\u003e {\n        someAnimation().then(next);\n      }}\n      enter={(next) =\u003e {\n        anotherAnimation().then(next);\n      }}\n    \u003e\n      {children}\n    \u003c/TransitionRouter\u003e\n  );\n}\n```\n\n\u003e [!NOTE]\n\u003e It should be a client component because you have to pass DOM functions as props to the provider.\n\nAfter that, you should import that component in the layout component (e.g.: `app/layout.tsx`).\n\n#### Async Callbacks\n\nThe `leave` and `enter` callbacks support async functions.\n\n```tsx\n\"use client\";\n\nimport { TransitionRouter } from \"next-transition-router\";\n\nexport function Providers({ children }: { children: React.ReactNode }) {\n  return (\n    \u003cTransitionRouter\n      leave={async (next) =\u003e {\n        await someAsyncAnimation();\n        next();\n      }}\n      enter={async (next) =\u003e {\n        await anotherAsyncAnimation();\n        next();\n      }}\n    \u003e\n      {children}\n    \u003c/TransitionRouter\u003e\n  );\n}\n```\n\n#### `from` and `to` parameters for `leave` callback\n\nThe `leave` callback receives the `from` and `to` parameters, which are strings with the previous and next page paths. Useful if you want to animate the transition conditionally based on the page.\n\n```tsx\nconst onLeave = (next, from, to) =\u003e {\n  someAnimation(from, to).then(next);\n};\n```\n\n\u003e [!NOTE]\n\u003e When using `router.back()` method, the `to` parameter will be undefined. See [programmatic navigation](#programmatic-navigation).\n\n### Handling links (custom `Link` component vs auto-detection)\n\nTo determine how to handle links, `TransitionRouter` can receive an `auto` prop (`boolean`).\n\n#### `auto` disabled (default)\n\nUse the custom `Link` component instead of the native [`Link` component from Next.js](https://nextjs.org/docs/app/api-reference/components/link) to trigger transitions.\n\n```tsx\nimport { Link } from \"next-transition-router\";\n\nexport function Example() {\n  return \u003cLink href=\"/about\"\u003eAbout\u003c/Link\u003e;\n}\n```\n\n\u003e [!TIP]\n\u003e Use `import { Link as TransitionLink } from \"next-transition-router\"` to avoid naming conflicts.\n\n#### `auto` enabled\n\nWhen `auto` is enabled, the `TransitionRouter` intercepts click events on internal links, except anchor links, and triggers page transitions. In this case you don't need to use the custom `Link` component.\n\nTo ignore a link in this mode, simply add the `data-transition-ignore` attribute to the link.\n\n### Programmatic navigation\n\nUse the `useTransitionRouter` hook to manage navigation (`push`, `replace`, `back`).\n\nIt's similar to [Next.js `useRouter`](https://nextjs.org/docs/app/api-reference/functions/use-router) with added transition support.\n\n```tsx\n\"use client\";\n\nimport { useTransitionRouter } from \"next-transition-router\";\n\nexport function Programmatic() {\n  const router = useTransitionRouter();\n\n  return (\n    \u003cbutton\n      onClick={() =\u003e {\n        alert(\"Do something before navigating away\");\n        router.push(\"/about\");\n      }}\n    \u003e\n      Go to /about\n    \u003c/button\u003e\n  );\n}\n```\n\n\u003e [!IMPORTANT]\n\u003e Back and Forward browser navigation doesn't trigger page transitions, and [this is intentional](https://github.com/ismamz/next-transition-router/issues/2).\n\n### Transition state\n\nUse the `useTransitionState` hook to determine the current stage of the transition.\n\nPossible `stage` values: `'entering' | 'leaving' | 'none'`.\n\nAditionally, you have the `isReady` state (`boolean`).\n\n```tsx\n\"use client\";\n\nimport { useTransitionState } from \"next-transition-router\";\n\nexport function Example() {\n  const { stage, isReady } = useTransitionState();\n\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003eCurrent stage: {stage}\u003c/p\u003e\n      \u003cp\u003ePage ready: {isReady ? \"Yes\" : \"No\"}\u003c/p\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\n\u003e [!TIP]\n\u003e This is useful, for example, if you want to trigger a reveal animation after the page transition ends.\n\n### Cleanup\n\n`TransitionRouter` manages cleanup functions for `leave` and `enter` callbacks, to prevent memory leaks.\n\nSimilar to React's `useEffect` hook, you can return a cleanup function to cancel the animation.\n\n#### Minimal example using GSAP\n\n```tsx\n\"use client\";\n\nimport { gsap } from \"gsap\";\nimport { TransitionRouter } from \"next-transition-router\";\n\nexport function Providers({ children }: { children: React.ReactNode }) {\n  return (\n    \u003cTransitionRouter\n      leave={(next) =\u003e {\n        const tween = gsap.fromTo(\"main\", { autoAlpha: 1 }, { autoAlpha: 0, onComplete: next });\n        return () =\u003e tween.kill();\n      }}\n      enter={(next) =\u003e {\n        const tween = gsap.fromTo(\"main\", { autoAlpha: 0 }, { autoAlpha: 1, onComplete: next });\n        return () =\u003e tween.kill();\n      }}\n    \u003e\n      {children}\n    \u003c/TransitionRouter\u003e\n  );\n}\n```\n\n## API\n\n### `TransitionRouter`\n\n| Prop       | Type       | Default Value    | Description                                       |\n| ---------- | ---------- | ---------------- | ------------------------------------------------- |\n| `leave`    | `function` | `next =\u003e next()` | Function to handle the leaving animation          |\n| `enter`    | `function` | `next =\u003e next()` | Function to handle the entering animation         |\n| `auto`     | `boolean`  | `false`          | Flag to enable/disable auto-detection of links    |\n\n### `useTransitionState`\n\n| Property  | Type                                | Description                                        |\n|-----------|-------------------------------------|----------------------------------------------------|\n| `stage`   | `'entering' \\| 'leaving' \\| 'none'` | Indicates the current stage of the transition.     |\n| `isReady` | `boolean`                           | Indicates if the new page is ready to be animated. |\n\n## Disclaimer\n\nThis package may not cover every use case. If you require a specific scenario, please [open an issue](https://github.com/ismamz/next-transition-router/issues/new/choose), and we can explore the possibility of extending the functionality.\n\n## License\n\nMIT.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fismamz%2Fnext-transition-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fismamz%2Fnext-transition-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fismamz%2Fnext-transition-router/lists"}