Created
November 9, 2025 12:40
-
-
Save franky47/57d6b9d1ffc18faa81c97ecf2a46e274 to your computer and use it in GitHub Desktop.
Waku nuqs adapter
This file contains hidden or 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
| 'use client' | |
| import { | |
| type unstable_AdapterOptions as AdapterOptions, | |
| unstable_createAdapterProvider as createAdapterProvider, | |
| renderQueryString | |
| } from 'nuqs/adapters/custom' | |
| import { useRouter } from 'waku' | |
| function useNuqsAdapter() { | |
| const { path, query, push, replace } = useRouter() | |
| const searchParams = new URLSearchParams(query) | |
| const updateUrl = (search: URLSearchParams, options: AdapterOptions) => { | |
| const query = renderQueryString(search) | |
| const url = path + query + location.hash | |
| if (options.shallow) { | |
| options.history === 'push' | |
| ? history.pushState(null, '', url) | |
| : history.replaceState(null, '', url) | |
| } else { | |
| const updateMethod = options.history === 'push' ? push : replace | |
| // bypass waku's typesafe route check by using `as never` | |
| updateMethod(url as never) | |
| } | |
| // Waku router does not scroll unless the pathname changes | |
| if (options.scroll) { | |
| window.scrollTo(0, 0) | |
| } | |
| } | |
| return { | |
| searchParams, | |
| updateUrl | |
| } | |
| } | |
| export const NuqsAdapter = createAdapterProvider(useNuqsAdapter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment