-
Notifications
You must be signed in to change notification settings - Fork 391
feat: start support for tanstack in @clerk/elements
#6917
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
Open
tunnckoCore
wants to merge
3
commits into
clerk:main
Choose a base branch
from
tunnckoCore:feat/add-tanstack-to-clerk-elements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+142
−48
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { ClerkHostRouter } from '@clerk/types'; | ||
import { useRouter } from '@tanstack/react-router'; | ||
|
||
import { usePathnameWithoutCatchAll } from '../utils/path-inference/tanstack'; | ||
|
||
/** | ||
* Clerk Elements router integration hook for TanStack Router. | ||
* | ||
* Provides a standardized router interface (`ClerkHostRouter`) that Clerk Elements | ||
* can use to navigate and read URL state within a TanStack Router application. | ||
* | ||
* @returns A `ClerkHostRouter` object with navigation methods (`push`, `replace`, `shallowPush`), | ||
* URL state accessors (`pathname`, `searchParams`), and path inference (`inferredBasePath`). | ||
* | ||
* @example | ||
* ```tsx | ||
* import { useTanStackRouter } from '@clerk/elements/tanstack'; | ||
* | ||
* function MyComponent() { | ||
* const router = useTanStackRouter(); | ||
* // Clerk Elements will use this router for navigation | ||
* } | ||
* ``` | ||
* | ||
* @remarks | ||
* - Requires TanStack Router v1.x+ to be installed and configured in your application. | ||
* - The hook must be called within a TanStack Router context (inside a `<RouterProvider>`). | ||
*/ | ||
export const useTanStackRouter = (): ClerkHostRouter => { | ||
const router = useRouter(); | ||
const pathname = router.location.pathname; | ||
const searchString = router.location.search; | ||
const inferredBasePath = usePathnameWithoutCatchAll(); // | ||
const getSearchParams = () => new URLSearchParams(searchString); | ||
|
||
return { | ||
mode: 'path', | ||
name: 'TanStackRouter', | ||
push: (path: string) => router.navigate({ to: path }), | ||
replace: (path: string) => router.navigate({ to: path, replace: true }), | ||
shallowPush: (path: string) => | ||
// In TanStack Router, all navigations are already shallow by default. | ||
router.navigate({ to: path }), | ||
pathname: () => pathname, | ||
searchParams: () => getSearchParams(), | ||
inferredBasePath: () => inferredBasePath, | ||
}; | ||
}; |
40 changes: 40 additions & 0 deletions
40
packages/elements/src/react/utils/path-inference/tanstack.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useParams, useRouter } from '@tanstack/react-router'; | ||
import React from 'react'; | ||
|
||
import { removeOptionalCatchAllSegment } from './utils'; | ||
|
||
/** | ||
* This hook grabs the current pathname and removes any (optional) catch-all segments. | ||
* Adapted from Next.js App Router logic for TanStack Router. | ||
* @example | ||
* Route: /user/$[id]/profile/$[...rest] (or file: user.[id].profile.[[...rest]].tsx) | ||
* Pathname: /user/123/profile/security | ||
* Params: { id: '123', rest: ['security'] } | ||
* Returns: /user/123/profile | ||
* @returns The pathname without any catch-all segments | ||
*/ | ||
export const usePathnameWithoutCatchAll = (): string => { | ||
const router = useRouter(); | ||
|
||
const pathname = router?.location.pathname || ''; | ||
const params = useParams() as Record<string, string | string[] | undefined>; | ||
|
||
return React.useMemo(() => { | ||
const processedPath = removeOptionalCatchAllSegment(pathname); | ||
const pathParts = processedPath.split('/').filter(Boolean); | ||
const catchAllParams = Object.values(params || {}) | ||
.filter((v): v is string[] => Array.isArray(v)) | ||
.flat(Infinity); | ||
|
||
if (!pathname || catchAllParams.length === 0) { | ||
return pathname.replace(/\/$/, '') || '/'; | ||
} | ||
|
||
// Slice off the trailing segments matching the catch-all length | ||
// E.g., pathParts = ['user', '123', 'profile', 'security'], length=1 → slice(0, 3) = /user/123/profile | ||
const baseParts = pathParts.slice(0, pathParts.length - catchAllParams.length); | ||
const basePath = `/${baseParts.join('/')}`; | ||
|
||
return basePath.replace(/\/$/, '') || '/'; | ||
}, [pathname, params]); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Add TanStack Router as an optional peer dependency for consistency.
The package follows a pattern where framework-specific integrations (like Next.js) are listed as both
devDependencies
(for testing) and optionalpeerDependencies
(for proper resolution in user projects). TanStack Router should follow the same pattern to ensure users can manage their own version while@clerk/elements
adapts to it.Apply this diff to add TanStack Router as an optional peer dependency:
🤖 Prompt for AI Agents