This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b43aa8
commit 3829095
Showing
5 changed files
with
103 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import type { ComponentProps, ComponentType } from 'react' | ||
import { Platform, TextProps, ViewProps, View } from 'react-native' | ||
import { useLinkProps } from '@react-navigation/native' | ||
import NextLink from 'next/link' | ||
|
||
import { Pressable, Text } from 'design-system' | ||
import { parseNextPath } from './parse-next-path' | ||
|
||
type Props = { | ||
children: React.ReactNode | ||
} & Omit<ComponentProps<typeof NextLink>, 'passHref'> | ||
|
||
function LinkCore({ | ||
children, | ||
href, | ||
as, | ||
componentProps, | ||
Component, | ||
...props | ||
}: Props & { | ||
Component: ComponentType<any> | ||
componentProps?: any | ||
}) { | ||
if (Platform.OS === 'web') { | ||
return ( | ||
<NextLink {...props} href={href} as={as} passHref> | ||
<Component {...componentProps}>{children}</Component> | ||
</NextLink> | ||
) | ||
} | ||
|
||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
const linkProps = useLinkProps({ | ||
to: parseNextPath(as ?? href), // TODO: should this prefer href or as? | ||
}) | ||
|
||
return ( | ||
<Component {...componentProps} {...linkProps}> | ||
{children} | ||
</Component> | ||
) | ||
} | ||
|
||
type LinkProps = Props & { viewProps?: ViewProps } | ||
|
||
function Link({ viewProps, ...props }: LinkProps) { | ||
return ( | ||
<LinkCore | ||
{...props} | ||
Component={Platform.select({ | ||
web: View, | ||
default: Pressable as any, | ||
})} | ||
componentProps={viewProps} | ||
/> | ||
) | ||
} | ||
|
||
type TextLinkProps = Props & { textProps?: TextProps } | ||
|
||
function TextLink({ textProps, ...props }: TextLinkProps) { | ||
return <LinkCore {...props} Component={Text} componentProps={textProps} /> | ||
} | ||
|
||
export { Link, TextLink, LinkCore } |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { NextRouter } from 'next/router' | ||
|
||
const parseNextPath = (from: Parameters<NextRouter['push']>[0]) => { | ||
let path = (typeof from == 'string' ? from : from.pathname) || '' | ||
|
||
// replace each instance of [key] with the corresponding value from query[key] | ||
// this ensures we're navigating to the correct URL | ||
// it currently ignores [...param] | ||
// but I can't see why you would use this with RN + Next.js | ||
if (typeof from == 'object' && from.query && typeof from.query == 'object') { | ||
const query = { ...from.query } | ||
for (const key in query) { | ||
if (path.includes(`[${key}]`)) { | ||
path = path.replace(`[${key}]`, `${query[key] ?? ''}`) | ||
delete query[key] | ||
} | ||
} | ||
if (Object.keys(query).length) { | ||
path += '?' | ||
for (const key in query) { | ||
if (query[key] != null) { | ||
path += `${key}=${query[key]}&` | ||
} | ||
} | ||
if (path.endsWith('&')) { | ||
path = path.slice(0, -1) | ||
} | ||
} | ||
} | ||
|
||
return path | ||
} | ||
|
||
export { parseNextPath } |
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