-
Notifications
You must be signed in to change notification settings - Fork 460
chore(astro): Add unstyled components #3656
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
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
65ab498
feat(astro): Introduce Astro SDK
panteliselef 52bb715
chore(astro): Cleanup
panteliselef f6ae5ae
chore(astro): Drop deprecations
panteliselef 0421dd0
chore(astro): Make hotloading the default
panteliselef 3f6b158
chore(astro): add react unstyled components
wobsoriano 2e0aa4f
chore(astro): add astro unstyled components
wobsoriano 82226fd
chore(astro): fix typescript error in signout click handler
wobsoriano 243f023
chore(astro): update polymorphic element name for clarity
wobsoriano a596fd2
chore(astro): spread element props in dynamic tag
wobsoriano f247208
chore(astro): make sure options dont merge with element props
wobsoriano 9809f1d
chore(astro): add missing redirectUrl prop type
wobsoriano 2a79d56
chore(astro): add default slot content
wobsoriano 5b5272d
chore(astro): add changeset
wobsoriano 62b269a
chore(astro): add comments regarding duplicate functions
wobsoriano d405525
chore(astro): add comments regarding duplicate functions
wobsoriano 45ae288
chore(astro): use data attribute in referencing unstyled components
wobsoriano fb964c8
Merge branch 'main' into rob/eco-2-export-unstyled-components
wobsoriano 0754f04
chore(astro): Set default Prop value for Polymorphic types
wobsoriano 209e67f
fix(astro): Add build step as dependency for e2e tests (#3677)
panteliselef f4a1500
fix(elements): Correct TS types for verification machine (#3671)
LekoArts 6c609c7
chore(astro): Optimize stream modification (#3673)
wobsoriano 380c329
chore(astro): Add session id prop to sign out button
wobsoriano ec8a1d4
Merge branch 'main' into rob/eco-2-export-unstyled-components
wobsoriano 61db931
chore(astro): Remove wrapped unstyled react components
wobsoriano 7e92620
chore(astro): Fix children prop type
wobsoriano 0d9ccf9
Merge branch 'main' into rob/eco-2-export-unstyled-components
wobsoriano 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,5 @@ | ||
| --- | ||
| "@clerk/astro": patch | ||
| --- | ||
|
|
||
| Add unstyled authentication button components for Astro and React integration |
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
48 changes: 48 additions & 0 deletions
48
packages/astro/src/astro-components/unstyled/SignInButton.astro
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,48 @@ | ||
| --- | ||
| import type { HTMLTag, Polymorphic } from 'astro/types' | ||
| import type { SignInProps } from "@clerk/types"; | ||
| type Props<Tag extends HTMLTag = HTMLTag> = Polymorphic<SignInProps & { as: Tag; mode?: 'redirect' | 'modal' }> | ||
|
|
||
| import { customAlphabet, urlAlphabet } from "nanoid"; | ||
|
|
||
| const safeId = customAlphabet(urlAlphabet, 10)(); | ||
|
|
||
| const { | ||
| as: Tag = 'button', | ||
| forceRedirectUrl, | ||
| fallbackRedirectUrl, | ||
| signUpFallbackRedirectUrl, | ||
| signUpForceRedirectUrl, | ||
| mode, | ||
| ...elementProps | ||
| } = Astro.props | ||
|
|
||
| const signInOptions = { | ||
| forceRedirectUrl, | ||
| fallbackRedirectUrl, | ||
| signUpFallbackRedirectUrl, | ||
| signUpForceRedirectUrl, | ||
| }; | ||
| --- | ||
|
|
||
| <Tag {...elementProps} data-clerk-unstyled-id={safeId}> | ||
| <slot>Sign in</slot> | ||
| </Tag > | ||
|
|
||
| <script is:inline define:vars={{ signInOptions, mode, safeId }}> | ||
| const btn = document.querySelector(`[data-clerk-unstyled-id="${safeId}"]`); | ||
|
|
||
| btn.addEventListener("click", () => { | ||
| const clerk = window.Clerk | ||
|
|
||
| if (mode === 'modal') { | ||
| return clerk.openSignIn(signInOptions); | ||
| } | ||
|
|
||
| return clerk.redirectToSignIn({ | ||
| ...signInOptions, | ||
| signInFallbackRedirectUrl: signInOptions.fallbackRedirectUrl, | ||
| signInForceRedirectUrl: signInOptions.forceRedirectUrl, | ||
| }); | ||
| }); | ||
| </script> | ||
28 changes: 28 additions & 0 deletions
28
packages/astro/src/astro-components/unstyled/SignOutButton.astro
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,28 @@ | ||
| --- | ||
| import type { HTMLTag, Polymorphic } from 'astro/types' | ||
| import type { SignOutOptions } from '@clerk/types'; | ||
| type Props<Tag extends HTMLTag = HTMLTag> = Polymorphic<{ as: Tag; } & SignOutOptions> | ||
|
|
||
| import { customAlphabet, urlAlphabet } from "nanoid"; | ||
|
|
||
| const safeId = customAlphabet(urlAlphabet, 10)(); | ||
|
|
||
| const { | ||
| as: Tag = 'button', | ||
| redirectUrl = '/', | ||
| sessionId, | ||
| ...elementProps | ||
| } = Astro.props | ||
| --- | ||
|
|
||
| <Tag {...elementProps} data-clerk-unstyled-id={safeId}> | ||
| <slot>Sign out</slot> | ||
| </Tag > | ||
|
|
||
| <script is:inline define:vars={{ redirectUrl, sessionId, safeId }}> | ||
| const btn = document.querySelector(`[data-clerk-unstyled-id="${safeId}"]`); | ||
|
|
||
| btn.addEventListener("click", () => { | ||
| window.Clerk.signOut({ redirectUrl, sessionId }) | ||
| }); | ||
| </script> |
50 changes: 50 additions & 0 deletions
50
packages/astro/src/astro-components/unstyled/SignUpButton.astro
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,50 @@ | ||
| --- | ||
| import type { HTMLTag, Polymorphic } from 'astro/types' | ||
| import type { SignUpProps } from "@clerk/types"; | ||
| type Props<Tag extends HTMLTag = HTMLTag> = Polymorphic<SignUpProps & { as: Tag; mode?: 'redirect' | 'modal' }> | ||
|
|
||
| import { customAlphabet, urlAlphabet } from "nanoid"; | ||
|
|
||
| const safeId = customAlphabet(urlAlphabet, 10)(); | ||
|
|
||
| const { | ||
| as: Tag = 'button', | ||
| fallbackRedirectUrl, | ||
| forceRedirectUrl, | ||
| signInFallbackRedirectUrl, | ||
| signInForceRedirectUrl, | ||
| mode, | ||
| unsafeMetadata, | ||
| ...elementProps | ||
| } = Astro.props | ||
|
|
||
| const signUpOptions = { | ||
| fallbackRedirectUrl, | ||
| forceRedirectUrl, | ||
| signInFallbackRedirectUrl, | ||
| signInForceRedirectUrl, | ||
| unsafeMetadata, | ||
| } | ||
| --- | ||
|
|
||
| <Tag {...elementProps} data-clerk-unstyled-id={safeId}> | ||
| <slot>Sign up</slot> | ||
| </Tag > | ||
|
|
||
| <script is:inline define:vars={{ signUpOptions, mode, safeId }}> | ||
| const btn = document.querySelector(`[data-clerk-unstyled-id="${safeId}"]`); | ||
|
|
||
| btn.addEventListener("click", () => { | ||
| const clerk = window.Clerk | ||
|
|
||
| if (mode === 'modal') { | ||
| return clerk.openSignUp(signUpOptions); | ||
| } | ||
|
|
||
| return clerk.redirectToSignUp({ | ||
| ...signUpOptions, | ||
| signUpFallbackRedirectUrl: signUpOptions.fallbackRedirectUrl, | ||
| signUpForceRedirectUrl: signUpOptions.forceRedirectUrl, | ||
| }); | ||
| }); | ||
| </script> |
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 @@ | ||
| // The `ts-ignore` comments here are necessary because we're importing this file inside the `astro:components` | ||
| // virtual module's types, which means that `tsc` will try to resolve these imports. Don't mind the editor errors. | ||
| // @ts-ignore | ||
| export { default as SignInButton } from './SignInButton.astro'; | ||
| // @ts-ignore | ||
| export { default as SignUpButton } from './SignUpButton.astro'; | ||
| // @ts-ignore | ||
| export { default as SignOutButton } from './SignOutButton.astro'; |
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,46 @@ | ||
| import type { SignInProps } from '@clerk/types'; | ||
| import React from 'react'; | ||
|
|
||
| import type { SignInButtonProps } from './types'; | ||
| import { assertSingleChild, normalizeWithDefaultValue, safeExecute, withClerk, type WithClerkProp } from './utils'; | ||
|
|
||
| export type { SignInButtonProps }; | ||
|
|
||
| export const SignInButton = withClerk(({ clerk, children, ...props }: WithClerkProp<SignInButtonProps>) => { | ||
| const { signUpFallbackRedirectUrl, forceRedirectUrl, fallbackRedirectUrl, signUpForceRedirectUrl, mode, ...rest } = | ||
| props; | ||
| children = normalizeWithDefaultValue(children, 'Sign in'); | ||
| const child = assertSingleChild(children)('SignInButton'); | ||
|
|
||
| const clickHandler = () => { | ||
| const opts: SignInProps = { | ||
| forceRedirectUrl, | ||
| fallbackRedirectUrl, | ||
| signUpFallbackRedirectUrl, | ||
| signUpForceRedirectUrl, | ||
| }; | ||
|
|
||
| if (!clerk) { | ||
| return; | ||
| } | ||
|
|
||
| if (mode === 'modal') { | ||
| return clerk.openSignIn(opts); | ||
| } | ||
| return clerk.redirectToSignIn({ | ||
| ...opts, | ||
| signInFallbackRedirectUrl: fallbackRedirectUrl, | ||
| signInForceRedirectUrl: forceRedirectUrl, | ||
| }); | ||
| }; | ||
|
|
||
| const wrappedChildClickHandler: React.MouseEventHandler = async e => { | ||
| if (child && typeof child === 'object' && 'props' in child) { | ||
| await safeExecute(child.props.onClick)(e); | ||
| } | ||
| return clickHandler(); | ||
| }; | ||
|
|
||
| const childProps = { ...rest, onClick: wrappedChildClickHandler }; | ||
| return React.cloneElement(child as React.ReactElement<unknown>, childProps); | ||
| }, 'SignInButton'); |
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,28 @@ | ||
| import type { SignOutOptions } from '@clerk/types'; | ||
| import React from 'react'; | ||
|
|
||
| import type { WithClerkProp } from './utils'; | ||
| import { assertSingleChild, normalizeWithDefaultValue, safeExecute, withClerk } from './utils'; | ||
|
|
||
| export type SignOutButtonProps = SignOutOptions & { | ||
| children?: React.ReactNode; | ||
| }; | ||
|
|
||
| export const SignOutButton = withClerk( | ||
| ({ clerk, children, ...props }: React.PropsWithChildren<WithClerkProp<SignOutButtonProps>>) => { | ||
| const { redirectUrl = '/', sessionId, ...rest } = props; | ||
|
|
||
| children = normalizeWithDefaultValue(children, 'Sign out'); | ||
| const child = assertSingleChild(children)('SignOutButton'); | ||
|
|
||
| const clickHandler = () => clerk?.signOut({ redirectUrl, sessionId }); | ||
| const wrappedChildClickHandler: React.MouseEventHandler = async e => { | ||
| await safeExecute((child as any).props.onClick)(e); | ||
| return clickHandler(); | ||
| }; | ||
|
|
||
| const childProps = { ...rest, onClick: wrappedChildClickHandler }; | ||
| return React.cloneElement(child as React.ReactElement<unknown>, childProps); | ||
| }, | ||
| 'SignOutButton', | ||
| ); |
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,56 @@ | ||
| import type { SignUpProps } from '@clerk/types'; | ||
| import React from 'react'; | ||
|
|
||
| import type { SignUpButtonProps } from './types'; | ||
| import { assertSingleChild, normalizeWithDefaultValue, safeExecute, withClerk, type WithClerkProp } from './utils'; | ||
|
|
||
| export type { SignUpButtonProps }; | ||
|
|
||
| export const SignUpButton = withClerk(({ clerk, children, ...props }: WithClerkProp<SignUpButtonProps>) => { | ||
| const { | ||
| fallbackRedirectUrl, | ||
| forceRedirectUrl, | ||
| signInFallbackRedirectUrl, | ||
| signInForceRedirectUrl, | ||
| mode, | ||
| unsafeMetadata, | ||
| ...rest | ||
| } = props; | ||
|
|
||
| children = normalizeWithDefaultValue(children, 'Sign up'); | ||
| const child = assertSingleChild(children)('SignUpButton'); | ||
|
|
||
| const clickHandler = () => { | ||
| const opts: SignUpProps = { | ||
| fallbackRedirectUrl, | ||
| forceRedirectUrl, | ||
| signInFallbackRedirectUrl, | ||
| signInForceRedirectUrl, | ||
| unsafeMetadata, | ||
| }; | ||
|
|
||
| if (!clerk) { | ||
| return; | ||
| } | ||
|
|
||
| if (mode === 'modal') { | ||
| return clerk.openSignUp(opts); | ||
| } | ||
|
|
||
| return clerk.redirectToSignUp({ | ||
| ...opts, | ||
| signUpFallbackRedirectUrl: fallbackRedirectUrl, | ||
| signUpForceRedirectUrl: forceRedirectUrl, | ||
| }); | ||
| }; | ||
|
|
||
| const wrappedChildClickHandler: React.MouseEventHandler = async e => { | ||
| if (child && typeof child === 'object' && 'props' in child) { | ||
| await safeExecute(child.props.onClick)(e); | ||
| } | ||
| return clickHandler(); | ||
| }; | ||
|
|
||
| const childProps = { ...rest, onClick: wrappedChildClickHandler }; | ||
| return React.cloneElement(child as React.ReactElement<unknown>, childProps); | ||
| }, 'SignUpButton'); |
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 |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| import { SignInButton, type SignInButtonProps } from './SignInButton'; | ||
| import { SignOutButton, type SignOutButtonProps } from './SignOutButton'; | ||
| import { SignUpButton, type SignUpButtonProps } from './SignUpButton'; | ||
|
|
||
| export * from './uiComponents'; | ||
| export * from './controlComponents'; | ||
| export * from './hooks'; | ||
| export { SignInButton, SignOutButton, SignUpButton }; | ||
|
|
||
| export type { SignInButtonProps, SignOutButtonProps, SignUpButtonProps }; |
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,23 @@ | ||
| import type { SignInProps, SignUpProps } from '@clerk/types'; | ||
|
|
||
| // TODO-SHARED: Duplicate from @clerk/clerk-react | ||
| type ButtonProps = { | ||
| mode?: 'redirect' | 'modal'; | ||
| children?: React.ReactNode; | ||
| }; | ||
|
|
||
| // TODO-SHARED: Duplicate from @clerk/clerk-react | ||
| export type SignInButtonProps = ButtonProps & | ||
| Pick< | ||
| SignInProps, | ||
| 'fallbackRedirectUrl' | 'forceRedirectUrl' | 'signUpForceRedirectUrl' | 'signUpFallbackRedirectUrl' | ||
| >; | ||
|
|
||
| // TODO-SHARED: Duplicate from @clerk/clerk-react | ||
| export type SignUpButtonProps = { | ||
| unsafeMetadata?: SignUpUnsafeMetadata; | ||
| } & ButtonProps & | ||
| Pick< | ||
| SignUpProps, | ||
| 'fallbackRedirectUrl' | 'forceRedirectUrl' | 'signInForceRedirectUrl' | 'signInFallbackRedirectUrl' | ||
| >; |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Starting to notice we're repeating this piece of logic in all our Astro components. I'll make a ticket that will address this