-
Notifications
You must be signed in to change notification settings - Fork 404
feat(elements): Add <Link /> component
#4456
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
Merged
alexcarpenter
merged 14 commits into
main
from
alexcarpenter/elements-expose-link-component
Nov 4, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
433b611
feat(elements): Add `<Link />` component
alexcarpenter 5d1dd72
allow reading child href
alexcarpenter 70034b3
improve
alexcarpenter 2a56635
add changeset
alexcarpenter b168b08
Update short-mails-brush.md
alexcarpenter 933434e
add changeset
alexcarpenter 8e66536
Merge branch 'alexcarpenter/elements-expose-link-component' of github…
alexcarpenter 5ada8c8
mark as internal
alexcarpenter 3c41f08
Update weak-hornets-hunt.md
alexcarpenter e60ce32
Update packages/elements/src/react/common/link.tsx
alexcarpenter 7c25856
fix variable
alexcarpenter cbb6952
update type
alexcarpenter 409c69b
doc block comment
alexcarpenter aba7c9f
Merge branch 'main' into alexcarpenter/elements-expose-link-component
alexcarpenter 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,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 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,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 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
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,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 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
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
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.
Uh oh!
There was an error while loading. Please reload this page.