-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(refactor): Router: Splitting things up into smaller files (#9988)
- Loading branch information
Showing
17 changed files
with
173 additions
and
170 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,8 @@ | ||
import { createContext } from 'react' | ||
|
||
/** Create a React Context with the given name. */ | ||
export function createNamedContext<T>(name: string, defaultValue?: T) { | ||
const Ctx = createContext<T | undefined>(defaultValue) | ||
Ctx.displayName = name | ||
return Ctx | ||
} |
This file contains 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 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,46 @@ | ||
'use client' | ||
|
||
// This needs to be a client component because it uses onClick, and the onClick | ||
// event handler can't be serialized when passed as an RSC Flight response | ||
|
||
import { forwardRef } from 'react' | ||
|
||
import { navigate } from './history' | ||
|
||
export interface LinkProps { | ||
to: string | ||
onClick?: React.MouseEventHandler<HTMLAnchorElement> | ||
} | ||
|
||
export const Link = forwardRef< | ||
HTMLAnchorElement, | ||
LinkProps & React.AnchorHTMLAttributes<HTMLAnchorElement> | ||
>(({ to, onClick, ...rest }, ref) => ( | ||
<a | ||
href={to} | ||
ref={ref} | ||
{...rest} | ||
onClick={(event) => { | ||
if ( | ||
event.button !== 0 || | ||
event.altKey || | ||
event.ctrlKey || | ||
event.metaKey || | ||
event.shiftKey | ||
) { | ||
return | ||
} | ||
|
||
event.preventDefault() | ||
|
||
if (onClick) { | ||
const result = onClick(event) | ||
if (typeof result !== 'boolean' || result) { | ||
navigate(to) | ||
} | ||
} else { | ||
navigate(to) | ||
} | ||
}} | ||
/> | ||
)) |
This file was deleted.
Oops, something went wrong.
This file contains 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 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 @@ | ||
'use client' | ||
|
||
import { forwardRef } from 'react' | ||
|
||
import { Link, type LinkProps } from './link' | ||
import { useMatch } from './useMatch' | ||
import type { FlattenSearchParams } from './util' | ||
import { flattenSearchParams } from './util' | ||
|
||
interface NavLinkProps extends LinkProps { | ||
activeClassName: string | ||
activeMatchParams?: FlattenSearchParams | ||
matchSubPaths?: boolean | ||
} | ||
|
||
export const NavLink = forwardRef< | ||
HTMLAnchorElement, | ||
NavLinkProps & React.AnchorHTMLAttributes<HTMLAnchorElement> | ||
>( | ||
( | ||
{ | ||
to, | ||
activeClassName, | ||
activeMatchParams, | ||
matchSubPaths, | ||
className, | ||
onClick, | ||
...rest | ||
}, | ||
ref | ||
) => { | ||
// Separate pathname and search parameters, USVString expected | ||
const [pathname, queryString] = to.split('?') | ||
const searchParams = activeMatchParams || flattenSearchParams(queryString) | ||
const matchInfo = useMatch(pathname, { | ||
searchParams, | ||
matchSubPaths, | ||
}) | ||
const theClassName = [className, matchInfo.match && activeClassName] | ||
.filter(Boolean) | ||
.join(' ') | ||
|
||
return ( | ||
<Link | ||
ref={ref} | ||
to={to} | ||
onClick={onClick} | ||
className={theClassName} | ||
{...rest} | ||
/> | ||
) | ||
} | ||
) |
This file contains 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
Oops, something went wrong.