Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added react-router v7 support #785

Merged
merged 14 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/nuqs/adapters/react-router/v6.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This file is needed for projects that have `moduleResolution` set to `node`
// in their tsconfig.json to be able to `import {} from 'nuqs/adapters/react-router'`.
// Other module resolutions strategies will look for the `exports` in `package.json`,
// but with `node`, TypeScript will look for a .d.ts file with that name at the
// root of the package.

export * from '../../dist/adapters/react-router/v6'
7 changes: 7 additions & 0 deletions packages/nuqs/adapters/react-router/v7.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This file is needed for projects that have `moduleResolution` set to `node`
// in their tsconfig.json to be able to `import {} from 'nuqs/adapters/react-router'`.
// Other module resolutions strategies will look for the `exports` in `package.json`,
// but with `node`, TypeScript will look for a .d.ts file with that name at the
// root of the package.

export * from '../../dist/adapters/react-router/v7'
16 changes: 15 additions & 1 deletion packages/nuqs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@
"import": "./dist/adapters/react-router.js",
"require": "./esm-only.cjs"
},
"./adapters/react-router/v6": {
"types": "./dist/adapters/react-router/v6.d.ts",
"import": "./dist/adapters/react-router/v6.js",
"require": "./esm-only.cjs"
},
"./adapters/react-router/v7": {
"types": "./dist/adapters/react-router/v7.d.ts",
"import": "./dist/adapters/react-router/v7.js",
"require": "./esm-only.cjs"
},
"./adapters/custom": {
"types": "./dist/adapters/custom.d.ts",
"import": "./dist/adapters/custom.js",
Expand Down Expand Up @@ -117,7 +127,8 @@
"@remix-run/react": ">=2",
"next": ">=14.2.0",
"react": ">=18.2.0 || ^19.0.0-0",
"react-router-dom": ">=6"
"react-router-dom": ">=6",
"react-router": ">=7"
},
"peerDependenciesMeta": {
"@remix-run/react": {
Expand All @@ -128,6 +139,9 @@
},
"react-router-dom": {
"optional": true
},
"react-router": {
"optional": true
}
},
"devDependencies": {
Expand Down
26 changes: 26 additions & 0 deletions packages/nuqs/src/adapters/react-router/v6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useNavigate, useSearchParams } from 'react-router-dom'
import { renderQueryString } from '../../url-encoding'
import type { AdapterOptions } from '../defs'
import { createAdapterProvider } from '../internal.context'

function useNuqsReactRouterV6Adapter() {
const navigate = useNavigate()
const [searchParams] = useSearchParams()
const updateUrl = (search: URLSearchParams, options: AdapterOptions) => {
navigate(
{
search: renderQueryString(search)
},
{
replace: options.history === 'replace',
preventScrollReset: !options.scroll
}
)
}
return {
searchParams,
updateUrl
}
}

export const NuqsAdapter = createAdapterProvider(useNuqsReactRouterV6Adapter)
26 changes: 26 additions & 0 deletions packages/nuqs/src/adapters/react-router/v7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useNavigate, useSearchParams } from 'react-router'
import { renderQueryString } from '../../url-encoding'
import type { AdapterOptions } from '../defs'
import { createAdapterProvider } from '../internal.context'

function useNuqsReactRouterV7Adapter() {
const navigate = useNavigate()
const [searchParams] = useSearchParams()
const updateUrl = (search: URLSearchParams, options: AdapterOptions) => {
navigate(
{
search: renderQueryString(search)
},
{
replace: options.history === 'replace',
preventScrollReset: !options.scroll
}
)
}
return {
searchParams,
updateUrl
}
}

export const NuqsAdapter = createAdapterProvider(useNuqsReactRouterV7Adapter)
4 changes: 3 additions & 1 deletion packages/nuqs/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const commonConfig = {
format: ['esm'],
experimentalDts: true,
outDir: 'dist',
external: ['next', 'react', '@remix-run/react', 'react-router-dom'],
external: ['next', 'react', '@remix-run/react', 'react-router-dom', 'react-router'],
splitting: true,
treeshake: true,
tsconfig: 'tsconfig.build.json'
Expand All @@ -22,6 +22,8 @@ const entrypoints = {
'adapters/next/pages': 'src/adapters/next/pages.ts',
'adapters/remix': 'src/adapters/remix.ts',
'adapters/react-router': 'src/adapters/react-router.ts',
'adapters/react-router/v6': 'src/adapters/react-router/v6.ts',
'adapters/react-router/v7': 'src/adapters/react-router/v7.ts',
'adapters/custom': 'src/adapters/custom.ts',
'adapters/testing': 'src/adapters/testing.ts'
},
Expand Down
35 changes: 32 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading