-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(elements): Add
<Link />
component (#4456)
Co-authored-by: Laura Beatris <48022589+LauraBeatris@users.noreply.github.com>
- Loading branch information
1 parent
0800fc3
commit 1a0c8fe
Showing
8 changed files
with
102 additions
and
0 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,20 @@ | ||
--- | ||
'@clerk/elements': patch | ||
--- | ||
|
||
Add Elements `<Link />` component. | ||
|
||
```tsx | ||
import * as Clerk from '@clerk/elements/common'; | ||
import NextLink from 'next/link'; | ||
|
||
function SignInPage() { | ||
return ( | ||
<> | ||
<Clerk.Link navigate='sign-up'>Sign up</Clerk.Link> | ||
|
||
<Clerk.Link navigate='sign-up'>{url => <NextLink href={url}>Sign up</NextLink>}</Clerk.Link> | ||
</> | ||
); | ||
} | ||
``` |
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 @@ | ||
--- | ||
'@clerk/clerk-js': patch | ||
'@clerk/shared': patch | ||
'@clerk/clerk-react': patch | ||
'@clerk/types': patch | ||
--- | ||
|
||
Expose internal `__internal_getOption` method from Clerk. |
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,60 @@ | ||
import { useClerk } from '@clerk/shared/react'; | ||
import { useClerkRouter } from '@clerk/shared/router'; | ||
import type { ClerkOptions } from '@clerk/types'; | ||
import React from 'react'; | ||
|
||
type Destination = 'sign-in' | 'sign-up'; | ||
export interface LinkProps extends Omit<React.HTMLAttributes<HTMLAnchorElement>, 'children'> { | ||
navigate: Destination; | ||
children: React.ReactNode | ((props: { url: string }) => React.ReactNode); | ||
} | ||
|
||
const paths: Record<Destination, keyof Pick<ClerkOptions, 'signInUrl' | 'signUpUrl'>> = { | ||
'sign-in': 'signInUrl', | ||
'sign-up': 'signUpUrl', | ||
}; | ||
|
||
/** | ||
* The `<Link>` component is used to navigate between sign-in and sign-up flows. | ||
* | ||
* @param {Destination} navigate - The destination to navigate to. | ||
* | ||
* @example | ||
* ```tsx | ||
* <Link navigate="sign-in">Sign in</Link> | ||
* ``` | ||
* @example | ||
* ```tsx | ||
* <Link navigate="sign-in"> | ||
* {({ url }) => ( | ||
* <NextLink href={url}>Sign in</NextLink> | ||
* )} | ||
* </Link> | ||
*/ | ||
|
||
export function Link({ navigate, children, ...rest }: LinkProps) { | ||
const router = useClerkRouter(); | ||
const clerk = useClerk(); | ||
const destinationUrl = router.makeDestinationUrlWithPreservedQueryParameters( | ||
clerk.__internal_getOption(paths[navigate])!, | ||
); | ||
|
||
if (typeof children === 'function') { | ||
return children({ url: destinationUrl }); | ||
} | ||
|
||
return ( | ||
<a | ||
onClick={e => { | ||
if (router) { | ||
e.preventDefault(); | ||
router.push(destinationUrl); | ||
} | ||
}} | ||
href={destinationUrl} | ||
{...rest} | ||
> | ||
{children} | ||
</a> | ||
); | ||
} |
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