-
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 1 commit
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { ClerkHostRouter } from '@clerk/types'; | ||
import { useRouter } from '@tanstack/react-router'; | ||
|
||
// Assume you adapt or define this constant similarly; e.g., for TanStack Router v1.0+ | ||
import { usePathnameWithoutCatchAll } from '../utils/path-inference/tanstack'; // Assume you create/adapt this util for TanStack (e.g., strip catch-all params like [...slug] from pathname) | ||
|
||
/** | ||
* Clerk Elements router integration with TanStack Router. | ||
*/ | ||
export const useTanStackRouter = (): ClerkHostRouter => { | ||
const router = useRouter(); | ||
const pathname = router.location.pathname; | ||
const searchString = router.location.search; // Raw search string for URLSearchParams | ||
const inferredBasePath = usePathnameWithoutCatchAll(); // Adapt your custom util for TanStack routing | ||
|
||
// TanStack Router always uses history APIs under the hood for SPA navigation, preserving state without full re-renders. | ||
// No version check needed unless integrating with very early betas; assume support for v1.x+. | ||
const canUseHistoryAPIs = typeof window !== 'undefined'; | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
// Helper to create URLSearchParams from search string (mimics Next.js useSearchParams return type) | ||
const getSearchParams = () => new URLSearchParams(searchString); | ||
|
||
return { | ||
mode: 'path', | ||
name: 'TanStackRouter', | ||
push: (path: string) => router.navigate({ to: path }), | ||
replace: (path: string) => | ||
canUseHistoryAPIs ? window.history.replaceState(null, '', path) : router.navigate({ to: path, replace: true }), | ||
shallowPush: (path: string) => | ||
// In TanStack Router, all navigations are "shallow" by default (no full reload, preserves state). | ||
// Use standard push; if you need to avoid re-fetching data, integrate with TanStack Query's stale-while-revalidate or disable refetch. | ||
canUseHistoryAPIs ? window.history.pushState(null, '', path) : router.navigate({ to: path }), | ||
tunnckoCore marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
pathname: () => pathname, | ||
searchParams: () => getSearchParams(), | ||
inferredBasePath: () => inferredBasePath, | ||
}; | ||
}; |
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
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 || ''; // Equivalent to usePathname() | ||
|
||
// Early return for no router (SSR initial or error) | ||
if (!pathname) { | ||
return '/'; | ||
} | ||
|
||
// Equivalent to useParams() – gets params for the current (leaf) route, which includes catch-alls | ||
const params = useParams() as Record<string, string | string[] | undefined>; // Typed as needed | ||
|
||
return React.useMemo(() => { | ||
// Apply optional catch-all heuristic first (mirrors Next.js fallback) | ||
const processedPath = removeOptionalCatchAllSegment(pathname); | ||
|
||
// For resolved pathnames in TanStack: Split into parts (exclude leading /) | ||
const pathParts = processedPath.split('/').filter(Boolean); | ||
|
||
// Identify catch-all params: Those that are arrays (splats like [...rest]) | ||
const catchAllParams = Object.values(params || {}) | ||
.filter((v): v is string[] => Array.isArray(v)) | ||
.flat(Infinity); // Flatten all (handles multiple/nested, though rare) | ||
|
||
// If no catch-all segments, return full path | ||
if (catchAllParams.length === 0) { | ||
return pathname.replace(/\/$/, '') || '/'; // Normalize trailing slash | ||
} | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
// 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('/')}`; | ||
|
||
// Normalize: Ensure absolute and no trailing slash unless root | ||
return basePath.replace(/\/$/, '') || '/'; | ||
}, [pathname, params]); // Dependencies: Recompute on navigation or param changes | ||
}; |
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.
Uh oh!
There was an error while loading. Please reload this page.