Skip to content

Commit

Permalink
feat: Add custom adapters API (unstable)
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Oct 22, 2024
1 parent 9443f96 commit 3ef698f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/docs/content/docs/adapters/onejs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ core package.

## Step 1: Add the adapter code

```tsx title="app/nuqs-adapter.tsx"
<Callout type="warn">
The custom adapters APIs are not yet stable and may change in the future
in a minor or patch release (not following SemVer).
</Callout>

```tsx title="app/nuqs-one-adapter.tsx"
import {
type AdapterOptions,
createAdapterProvider,
type unstable_AdapterOptions as AdapterOptions,
unstable_createAdapterProvider as createAdapterProvider,
renderQueryString
} from 'nuqs/adapters/custom'
import { useActiveParams, useRouter } from 'one'
Expand Down Expand Up @@ -52,7 +57,7 @@ Integrate the adapter into the root layout file, by wrapping the `<Slot>`
component:

```tsx title="app/_layout.tsx" /NuqsAdapter/
import { NuqsAdapter } from './nuqs-adapter'
import { NuqsAdapter } from './nuqs-one-adapter'
import { Slot } from 'one'

export default function Layout() {
Expand Down
7 changes: 7 additions & 0 deletions packages/nuqs/adapters/custom.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/custom'`.
// 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/custom'
5 changes: 5 additions & 0 deletions packages/nuqs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"adapters/next/pages.d.ts",
"adapters/remix.d.ts",
"adapters/react-router.d.ts",
"adapters/custom.d.ts",
"adapters/testing.d.ts"
],
"type": "module",
Expand Down Expand Up @@ -78,6 +79,10 @@
"types": "./dist/adapters/react-router.d.ts",
"import": "./dist/adapters/react-router.js"
},
"./adapters/custom": {
"types": "./dist/adapters/custom.d.ts",
"import": "./dist/adapters/custom.js"
},
"./adapters/testing": {
"types": "./dist/adapters/testing.d.ts",
"import": "./dist/adapters/testing.js"
Expand Down
11 changes: 11 additions & 0 deletions packages/nuqs/src/adapters/custom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export { renderQueryString } from '../url-encoding'
export type {
AdapterInterface as unstable_AdapterInterface,
AdapterOptions as unstable_AdapterOptions,
UpdateUrlFunction as unstable_UpdateUrlFunction,
UseAdapterHook as unstable_UseAdapterHook
} from './defs'
export {
createAdapterProvider as unstable_createAdapterProvider,
type AdapterContext as unstable_AdapterContext
} from './internal.context'
8 changes: 8 additions & 0 deletions packages/nuqs/src/adapters/internal.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export const context = createContext<AdapterContext>({
})
context.displayName = 'NuqsAdapterContext'

/**
* Create a custom adapter (context provider) for nuqs to work with your framework / router.
*
* Adapters are based on React Context,
*
* @param useAdapter
* @returns
*/
export function createAdapterProvider(useAdapter: UseAdapterHook) {
return ({ children, ...props }: { children: ReactNode }) =>
createElement(
Expand Down
1 change: 1 addition & 0 deletions packages/nuqs/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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/custom': 'src/adapters/custom.ts',
'adapters/testing': 'src/adapters/testing.ts'
},
server: {
Expand Down

0 comments on commit 3ef698f

Please sign in to comment.