Skip to content

Commit

Permalink
feat: Render pretty URLs in other adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Oct 22, 2024
1 parent cb30a20 commit 6618f45
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
19 changes: 13 additions & 6 deletions packages/nuqs/src/adapters/react-router.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { useSearchParams } from 'react-router-dom'
import { useNavigate, useSearchParams } from 'react-router-dom'
import { renderQueryString } from '../url-encoding'
import type { AdapterOptions } from './defs'
import { createAdapterProvider } from './internal.context'

function useNuqsReactRouterAdapter() {
const [searchParams, setSearchParams] = useSearchParams()
const navigate = useNavigate()
const [searchParams] = useSearchParams()
const updateUrl = (search: URLSearchParams, options: AdapterOptions) => {
setSearchParams(search, {
replace: options.history === 'replace',
preventScrollReset: !options.scroll
})
navigate(
{
search: renderQueryString(search)
},
{
replace: options.history === 'replace',
preventScrollReset: !options.scroll
}
)
}
return {
searchParams,
Expand Down
6 changes: 3 additions & 3 deletions packages/nuqs/src/adapters/react.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import mitt from 'mitt'
import { useEffect, useState } from 'react'
import { renderQueryString } from '../url-encoding'
import type { AdapterOptions } from './defs'
import { createAdapterProvider } from './internal.context'

const emitter = mitt<{ update: URLSearchParams }>()

function updateUrl(search: URLSearchParams, options: AdapterOptions) {
const url = new URL(location.href)
url.search = search.toString()
const href = url.toString()
url.search = renderQueryString(search)
const method =
options.history === 'push' ? history.pushState : history.replaceState
method.call(history, history.state, '', href)
method.call(history, history.state, '', url)
emitter.emit('update', search)
}

Expand Down
19 changes: 13 additions & 6 deletions packages/nuqs/src/adapters/remix.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { useSearchParams } from '@remix-run/react'
import { useNavigate, useSearchParams } from '@remix-run/react'
import { renderQueryString } from '../url-encoding'
import type { AdapterOptions } from './defs'
import { createAdapterProvider } from './internal.context'

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

0 comments on commit 6618f45

Please sign in to comment.