-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
687b692
commit 18e95b3
Showing
19 changed files
with
1,844 additions
and
5,055 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,38 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# Dependencies | ||
node_modules | ||
.pnp | ||
.pnp.js | ||
|
||
# Local env files | ||
.env | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
# Testing | ||
coverage | ||
|
||
# Turbo | ||
.turbo | ||
|
||
# Vercel | ||
.vercel | ||
|
||
# Build Outputs | ||
.next/ | ||
out/ | ||
build | ||
dist | ||
*.local | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
|
||
# Debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Misc | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? | ||
*.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"name": "@hyperjumptech/react-next-pathname-nextjs", | ||
"description": "Instantly tracks the next pathname on link click.", | ||
"version": "1.0.2", | ||
"main": "dist/index.mjs", | ||
"typings": "dist/index.d.mts", | ||
"scripts": { | ||
"build": "tsup", | ||
"test": "vitest run", | ||
"test:coverage": "vitest run --coverage" | ||
}, | ||
"devDependencies": { | ||
"@testing-library/jest-dom": "^6.4.6", | ||
"@testing-library/react": "^16.0.0", | ||
"@vitest/coverage-v8": "^1.6.0", | ||
"jsdom": "^24.1.0", | ||
"tsup": "^8.1.0", | ||
"vitest": "^1.6.0" | ||
}, | ||
"peerDependencies": { | ||
"next": "*", | ||
"react": "*", | ||
"react-dom": "*" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"license": "MIT", | ||
"author": "Kevin Hermawan", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/hyperjumptech/react-next-pathname.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/hyperjumptech/react-next-pathname/issues" | ||
}, | ||
"homepage": "https://github.com/hyperjumptech/react-next-pathname#readme", | ||
"keywords": [ | ||
"react", | ||
"pathname", | ||
"navigation", | ||
"link", | ||
"routing", | ||
"next pathname" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"use client"; | ||
|
||
import { useRouter } from "next/router.js"; | ||
import type { ReactNode } from "react"; | ||
import React, { createContext, useContext, useEffect, useState } from "react"; | ||
|
||
type NextPathnameContextType = { | ||
nextPathname: string; | ||
}; | ||
|
||
const defaultNextPathname = "/"; | ||
|
||
export const NextPathnameContext = createContext<NextPathnameContextType>({ | ||
nextPathname: defaultNextPathname, | ||
}); | ||
|
||
export function NextPathnameProvider({ children }: { children: ReactNode }) { | ||
const router = useRouter(); | ||
const [nextPathname, setNextPathname] = useState(router.pathname); | ||
|
||
useEffect(() => { | ||
router.events.on("routeChangeStart", setNextPathname); | ||
|
||
return () => { | ||
router.events.off("routeChangeStart", setNextPathname); | ||
}; | ||
}, [router.events]); | ||
|
||
return ( | ||
<NextPathnameContext.Provider value={{ nextPathname }}> | ||
{children} | ||
</NextPathnameContext.Provider> | ||
); | ||
} | ||
|
||
export function useNextPathname() { | ||
return useContext(NextPathnameContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { RouterEvent, useRouter } from "next/router"; | ||
import React from "react"; | ||
import { afterEach, describe, expect, it, Mock, vi } from "vitest"; | ||
|
||
import { act, cleanup, render, waitFor } from "@testing-library/react"; | ||
|
||
import { NextPathnameProvider, useNextPathname } from "../src"; | ||
|
||
vi.mock("next/router", () => { | ||
const originalModule = vi.importActual( | ||
"next/router" | ||
) as unknown as typeof import("next/router"); | ||
|
||
return { | ||
__esModule: true, | ||
...originalModule, | ||
useRouter: vi.fn(), | ||
}; | ||
}); | ||
|
||
type MockRouter = { | ||
pathname: string; | ||
events: { | ||
on: (event: RouterEvent, handler: (...args: any[]) => void) => void; | ||
off: (event: RouterEvent, handler: (...args: any[]) => void) => void; | ||
emit: (event: RouterEvent, ...args: any[]) => void; | ||
}; | ||
mockHandler?: (...args: any[]) => void; | ||
}; | ||
|
||
const mockRouter: MockRouter = { | ||
pathname: "/", | ||
events: { | ||
on: vi.fn((event, handler) => { | ||
if (event === "routeChangeStart") { | ||
mockRouter.mockHandler = handler; | ||
} | ||
}), | ||
off: vi.fn(), | ||
emit: vi.fn(), | ||
}, | ||
}; | ||
|
||
(useRouter as Mock).mockImplementation(() => mockRouter); | ||
|
||
describe("NextPathnameProvider", () => { | ||
afterEach(() => { | ||
cleanup(); | ||
|
||
mockRouter.pathname = "/"; | ||
}); | ||
|
||
const TestComponent: React.FC = () => { | ||
const { nextPathname } = useNextPathname(); | ||
|
||
return <div data-testid="pathname">{nextPathname}</div>; | ||
}; | ||
|
||
it("provides the initial pathname", () => { | ||
const { getByTestId } = render( | ||
<NextPathnameProvider> | ||
<TestComponent /> | ||
</NextPathnameProvider> | ||
); | ||
|
||
expect(getByTestId("pathname").textContent).toBe("/"); | ||
}); | ||
|
||
it("updates the pathname on route change", async () => { | ||
render( | ||
<NextPathnameProvider> | ||
<TestComponent /> | ||
</NextPathnameProvider> | ||
); | ||
|
||
act(() => { | ||
mockRouter.mockHandler && mockRouter.mockHandler("/new-path"); | ||
}); | ||
|
||
await waitFor(() => { | ||
const pathnameElement = document.querySelector( | ||
'[data-testid="pathname"]' | ||
); | ||
|
||
expect(pathnameElement?.textContent).toBe("/new-path"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { defineConfig } from "tsup"; | ||
|
||
export default defineConfig({ | ||
dts: true, | ||
clean: true, | ||
outDir: "dist", | ||
format: ["esm"], | ||
entry: ["src/index.tsx"], | ||
external: ["react", "react-dom", "next"], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "@hyperjumptech/react-next-pathname", | ||
"description": "Instantly tracks the next pathname on link click.", | ||
"version": "1.0.3", | ||
"main": "dist/index.mjs", | ||
"typings": "dist/index.d.mts", | ||
"scripts": { | ||
"build": "tsup", | ||
"test": "vitest run", | ||
"test:coverage": "vitest run --coverage" | ||
}, | ||
"devDependencies": { | ||
"@testing-library/jest-dom": "^6.4.6", | ||
"@testing-library/react": "^16.0.0", | ||
"@vitest/coverage-v8": "^1.6.0", | ||
"jsdom": "^24.1.0", | ||
"tsup": "^8.1.0", | ||
"vitest": "^1.6.0" | ||
}, | ||
"peerDependencies": { | ||
"react": "*", | ||
"react-dom": "*" | ||
}, | ||
"files": ["dist"], | ||
"license": "MIT", | ||
"author": "Kevin Hermawan", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/hyperjumptech/react-next-pathname.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/hyperjumptech/react-next-pathname/issues" | ||
}, | ||
"homepage": "https://github.com/hyperjumptech/react-next-pathname#readme", | ||
"keywords": [ | ||
"react", | ||
"pathname", | ||
"navigation", | ||
"link", | ||
"routing", | ||
"next pathname" | ||
] | ||
} |
File renamed without changes.
Oops, something went wrong.